|
1 | 1 | import {HarnessLoader, parallel} from '@angular/cdk/testing';
|
2 | 2 | import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
|
3 | 3 | import {Component} from '@angular/core';
|
4 |
| -import {MatNativeDateModule} from '@angular/material/core'; |
| 4 | +import {DateAdapter, MatNativeDateModule} from '@angular/material/core'; |
5 | 5 | import {ComponentFixture, TestBed} from '@angular/core/testing';
|
6 | 6 | import {FormsModule} from '@angular/forms';
|
7 | 7 | import {MatDatepickerModule} from '@angular/material/datepicker';
|
@@ -86,6 +86,31 @@ export function runDatepickerInputHarnessTests(
|
86 | 86 | expect(await input.getValue()).toBe('1/1/2020');
|
87 | 87 | });
|
88 | 88 |
|
| 89 | + it('should set the input value based on date adapter validation and formatting', async () => { |
| 90 | + const adapter = fixture.debugElement.injector.get(DateAdapter); |
| 91 | + const input = await loader.getHarness(datepickerInputHarness.with({selector: '#basic'})); |
| 92 | + const validValues: any[] = [new Date(0), '', 0, false]; |
| 93 | + const invalidValues: any[] = [null, undefined]; |
| 94 | + spyOn(adapter, 'format').and.returnValue('FORMATTED_VALUE'); |
| 95 | + spyOn(adapter, 'isValid').and.callFake(value => validValues.includes(value)); |
| 96 | + spyOn(adapter, 'deserialize').and.callFake(value => |
| 97 | + validValues.includes(value) ? value : null, |
| 98 | + ); |
| 99 | + spyOn(adapter, 'getValidDateOrNull').and.callFake(value => |
| 100 | + adapter.isValid(value) ? value : null, |
| 101 | + ); |
| 102 | + |
| 103 | + for (let value of validValues) { |
| 104 | + fixture.componentInstance.date = value; |
| 105 | + expect(await input.getValue()).toBe('FORMATTED_VALUE'); |
| 106 | + } |
| 107 | + |
| 108 | + for (let value of invalidValues) { |
| 109 | + fixture.componentInstance.date = value; |
| 110 | + expect(await input.getValue()).toBe(''); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
89 | 114 | it('should get the input placeholder', async () => {
|
90 | 115 | const inputs = await loader.getAllHarnesses(datepickerInputHarness);
|
91 | 116 | expect(
|
|
0 commit comments