-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathdate-range-input-harness.spec.ts
303 lines (251 loc) · 11.2 KB
/
date-range-input-harness.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {HarnessLoader, parallel} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule} from '@angular/forms';
import {MATERIAL_ANIMATIONS, MatNativeDateModule} from '../../core';
import {
MatDatepickerModule,
MatDateRangeInput,
MatDateRangePicker,
MatEndDate,
MatStartDate,
} from '../../datepicker';
import {MatFormFieldModule} from '../../form-field';
import {MatCalendarHarness} from './calendar-harness';
import {
MatDateRangeInputHarness,
MatEndDateHarness,
MatStartDateHarness,
} from './date-range-input-harness';
describe('matDateRangeInputHarness', () => {
let fixture: ComponentFixture<DateRangeInputHarnessTest>;
let loader: HarnessLoader;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MatNativeDateModule, MatDatepickerModule, FormsModule, DateRangeInputHarnessTest],
providers: [{provide: MATERIAL_ANIMATIONS, useValue: {animationsDisabled: true}}],
});
fixture = TestBed.createComponent(DateRangeInputHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});
it('should load all date range input harnesses', async () => {
const inputs = await loader.getAllHarnesses(MatDateRangeInputHarness);
expect(inputs.length).toBe(3);
});
it('should load date range input with a specific label', async () => {
const inputs = await loader.getAllHarnesses(
MatDateRangeInputHarness.with({label: 'Date range'}),
);
expect(inputs.length).toBe(1);
});
it('should get whether the input is disabled', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
expect(await input.isDisabled()).toBe(false);
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
expect(await input.isDisabled()).toBe(true);
});
it('should get whether the input is required', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
expect(await input.isRequired()).toBe(false);
fixture.componentInstance.required = true;
fixture.changeDetectorRef.markForCheck();
expect(await input.isRequired()).toBe(true);
});
it('should get the input separator', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
expect(await input.getSeparator()).toBe('–');
});
it('should get the combined input value including the separator', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
fixture.componentInstance.startDate = new Date(2020, 0, 1, 12, 0, 0);
fixture.componentInstance.endDate = new Date(2020, 1, 2, 12, 0, 0);
fixture.changeDetectorRef.markForCheck();
expect(await input.getValue()).toBe('1/1/2020 – 2/2/2020');
});
it('should get harnesses for the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(start).toBeInstanceOf(MatStartDateHarness);
expect(end).toBeInstanceOf(MatEndDateHarness);
});
it('should be able to open and close a calendar in popup mode', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
expect(await input.isCalendarOpen()).toBe(false);
await input.openCalendar();
expect(await input.isCalendarOpen()).toBe(true);
await input.closeCalendar();
expect(await input.isCalendarOpen()).toBe(false);
});
it('should be able to open and close a calendar in touch mode', async () => {
fixture.componentInstance.touchUi = true;
fixture.changeDetectorRef.markForCheck();
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
expect(await input.isCalendarOpen()).toBe(false);
await input.openCalendar();
expect(await input.isCalendarOpen()).toBe(true);
await input.closeCalendar();
expect(await input.isCalendarOpen()).toBe(false);
});
it('should be able to get the harness for the associated calendar', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
await input.openCalendar();
expect(await input.getCalendar()).toBeInstanceOf(MatCalendarHarness);
});
it('should get whether the inner inputs are disabled', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.isDisabled(), end.isDisabled()])).toEqual([false, false]);
fixture.componentInstance.subInputsDisabled = true;
fixture.changeDetectorRef.markForCheck();
expect(await parallel(() => [start.isDisabled(), end.isDisabled()])).toEqual([true, true]);
});
it('should get whether the inner inputs are required', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.isRequired(), end.isRequired()])).toEqual([false, false]);
fixture.componentInstance.subInputsRequired = true;
fixture.changeDetectorRef.markForCheck();
expect(await parallel(() => [start.isRequired(), end.isRequired()])).toEqual([true, true]);
});
it('should get the values of the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
fixture.componentInstance.startDate = new Date(2020, 0, 1, 12, 0, 0);
fixture.componentInstance.endDate = new Date(2020, 1, 2, 12, 0, 0);
fixture.changeDetectorRef.markForCheck();
expect(
await parallel(() => {
return [start.getValue(), end.getValue()];
}),
).toEqual(['1/1/2020', '2/2/2020']);
});
it('should set the values of the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.getValue(), end.getValue()])).toEqual(['', '']);
await parallel(() => [start.setValue('1/1/2020'), end.setValue('2/2/2020')]);
expect(
await parallel(() => {
return [start.getValue(), end.getValue()];
}),
).toEqual(['1/1/2020', '2/2/2020']);
});
it('should get the placeholders of the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.getPlaceholder(), end.getPlaceholder()])).toEqual([
'Start date',
'End date',
]);
});
it('should be able to change the inner input focused state', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await start.isFocused()).toBe(false);
await start.focus();
expect(await start.isFocused()).toBe(true);
await start.blur();
expect(await start.isFocused()).toBe(false);
expect(await end.isFocused()).toBe(false);
await end.focus();
expect(await end.isFocused()).toBe(true);
await end.blur();
expect(await end.isFocused()).toBe(false);
});
it('should get the minimum date of the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.getMin(), end.getMin()])).toEqual([null, null]);
fixture.componentInstance.minDate = new Date(2020, 0, 1, 12, 0, 0);
fixture.changeDetectorRef.markForCheck();
expect(
await parallel(() => {
return [start.getMin(), end.getMin()];
}),
).toEqual(['2020-01-01', '2020-01-01']);
});
it('should get the maximum date of the inner inputs', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(await parallel(() => [start.getMax(), end.getMax()])).toEqual([null, null]);
fixture.componentInstance.maxDate = new Date(2020, 0, 1, 12, 0, 0);
fixture.changeDetectorRef.markForCheck();
expect(
await parallel(() => {
return [start.getMax(), end.getMax()];
}),
).toEqual(['2020-01-01', '2020-01-01']);
});
it('should dispatch the dateChange event when the inner input values have changed', async () => {
const input = await loader.getHarness(MatDateRangeInputHarness.with({selector: '[basic]'}));
const [start, end] = await parallel(() => [input.getStartInput(), input.getEndInput()]);
expect(fixture.componentInstance.startDateChangeCount).toBe(0);
expect(fixture.componentInstance.endDateChangeCount).toBe(0);
await parallel(() => [start.setValue('1/1/2020'), end.setValue('2/2/2020')]);
expect(fixture.componentInstance.startDateChangeCount).toBe(1);
expect(fixture.componentInstance.endDateChangeCount).toBe(1);
});
});
@Component({
template: `
<mat-date-range-input
basic
[disabled]="disabled"
[required]="required"
[min]="minDate"
[max]="maxDate"
[rangePicker]="picker">
<input
matStartDate
[(ngModel)]="startDate"
(dateChange)="startDateChangeCount = startDateChangeCount + 1"
[disabled]="subInputsDisabled"
[required]="subInputsRequired"
placeholder="Start date">
<input
matEndDate
[(ngModel)]="endDate"
(dateChange)="endDateChangeCount = endDateChangeCount + 1"
[disabled]="subInputsDisabled"
[required]="subInputsRequired"
placeholder="End date">
</mat-date-range-input>
<mat-date-range-picker #picker [touchUi]="touchUi"></mat-date-range-picker>
<mat-date-range-input no-range-picker>
<input matStartDate>
<input matEndDate>
</mat-date-range-input>
<mat-form-field>
<mat-label>Date range</mat-label>
<mat-date-range-input basic>
<input matStartDate>
<input matEndDate>
</mat-date-range-input>
</mat-form-field>
`,
imports: [
MatNativeDateModule,
MatDateRangeInput,
MatStartDate,
MatEndDate,
MatDateRangePicker,
MatFormFieldModule,
FormsModule,
],
})
class DateRangeInputHarnessTest {
startDate: Date | null = null;
endDate: Date | null = null;
minDate: Date | null = null;
maxDate: Date | null = null;
touchUi = false;
disabled = false;
required = false;
subInputsDisabled = false;
subInputsRequired = false;
startDateChangeCount = 0;
endDateChangeCount = 0;
}