-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
multi-year-view.spec.ts
429 lines (339 loc) · 15.2 KB
/
multi-year-view.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import {Direction, Directionality} from '@angular/cdk/bidi';
import {
DOWN_ARROW,
END,
HOME,
LEFT_ARROW,
PAGE_DOWN,
PAGE_UP,
RIGHT_ARROW,
UP_ARROW,
} from '@angular/cdk/keycodes';
import {dispatchFakeEvent, dispatchKeyboardEvent} from '@angular/cdk/testing/private';
import {Component, ViewChild} from '@angular/core';
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
import {MatNativeDateModule} from '@angular/material/core';
import {JAN} from '@angular/material/testing';
import {By} from '@angular/platform-browser';
import {MatCalendarBody} from './calendar-body';
import {MatMultiYearView, yearsPerPage, yearsPerRow} from './multi-year-view';
describe('MatMultiYearView', () => {
let dir: {value: Direction};
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
MatNativeDateModule,
],
declarations: [
MatCalendarBody,
MatMultiYearView,
// Test components.
StandardMultiYearView,
MultiYearViewWithDateFilter,
MultiYearViewWithMinMaxDate,
MultiYearViewWithDateClass,
],
providers: [
{provide: Directionality, useFactory: () => dir = {value: 'ltr'}}
]
});
TestBed.compileComponents();
}));
describe('standard multi-year view', () => {
let fixture: ComponentFixture<StandardMultiYearView>;
let testComponent: StandardMultiYearView;
let multiYearViewNativeElement: Element;
beforeEach(() => {
fixture = TestBed.createComponent(StandardMultiYearView);
fixture.detectChanges();
let multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView))!;
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
testComponent = fixture.componentInstance;
});
it('has correct number of years', () => {
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell')!;
expect(cellEls.length).toBe(yearsPerPage);
});
it('shows selected year if in same range', () => {
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected')!;
expect(selectedEl.innerHTML.trim()).toBe('2020');
});
it('does not show selected year if in different range', () => {
testComponent.selected = new Date(2040, JAN, 10);
fixture.detectChanges();
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected');
expect(selectedEl).toBeNull();
});
it('fires selected change event on cell clicked', () => {
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
(cellEls[cellEls.length - 1] as HTMLElement).click();
fixture.detectChanges();
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected')!;
expect(selectedEl.innerHTML.trim()).toBe('2039');
});
it('should emit the selected year on cell clicked', () => {
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
(cellEls[1] as HTMLElement).click();
fixture.detectChanges();
const normalizedYear: Date = fixture.componentInstance.selectedYear;
expect(normalizedYear.getFullYear()).toEqual(2017);
});
it('should mark active date', () => {
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect((cellEls[1] as HTMLElement).innerText.trim()).toBe('2017');
expect(cellEls[1].classList).toContain('mat-calendar-body-active');
});
describe('a11y', () => {
it('should set the correct role on the internal table node', () => {
const table = multiYearViewNativeElement.querySelector('table')!;
expect(table.getAttribute('role')).toBe('grid');
});
describe('calendar body', () => {
let calendarBodyEl: HTMLElement;
let calendarInstance: StandardMultiYearView;
beforeEach(() => {
calendarInstance = fixture.componentInstance;
calendarBodyEl =
fixture.debugElement.nativeElement.querySelector('.mat-calendar-body') as HTMLElement;
expect(calendarBodyEl).not.toBeNull();
dir.value = 'ltr';
fixture.componentInstance.date = new Date(2017, JAN, 1);
dispatchFakeEvent(calendarBodyEl, 'focus');
fixture.detectChanges();
});
it('should decrement year on left arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2016, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', LEFT_ARROW);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2015, JAN, 1));
});
it('should increment year on right arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2018, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', RIGHT_ARROW);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2019, JAN, 1));
});
it('should go up a row on up arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 - yearsPerRow, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', UP_ARROW);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 - yearsPerRow * 2, JAN, 1));
});
it('should go down a row on down arrow press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', DOWN_ARROW);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 + yearsPerRow, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', DOWN_ARROW);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 + yearsPerRow * 2, JAN, 1));
});
it('should go to first year in current range on home press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', HOME);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2016, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', HOME);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2016, JAN, 1));
});
it('should go to last year in current range on end press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', END);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2039, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', END);
fixture.detectChanges();
expect(calendarInstance.date).toEqual(new Date(2039, JAN, 1));
});
it('should go to same index in previous year range page up press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', PAGE_UP);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 - yearsPerPage, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', PAGE_UP);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 - yearsPerPage * 2, JAN, 1));
});
it('should go to same index in next year range on page down press', () => {
dispatchKeyboardEvent(calendarBodyEl, 'keydown', PAGE_DOWN);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 + yearsPerPage, JAN, 1));
dispatchKeyboardEvent(calendarBodyEl, 'keydown', PAGE_DOWN);
fixture.detectChanges();
expect(calendarInstance.date)
.toEqual(new Date(2017 + yearsPerPage * 2, JAN, 1));
});
});
});
});
describe('multi year view with date filter', () => {
it('should disable years with no enabled days', () => {
const fixture = TestBed.createComponent(MultiYearViewWithDateFilter);
fixture.detectChanges();
const cells = fixture.nativeElement.querySelectorAll('.mat-calendar-body-cell');
expect(cells[0].classList).not.toContain('mat-calendar-body-disabled');
expect(cells[1].classList).toContain('mat-calendar-body-disabled');
});
it('should not call the date filter function if the date is before the min date', () => {
const fixture = TestBed.createComponent(MultiYearViewWithDateFilter);
const activeDate = fixture.componentInstance.activeDate;
const spy = spyOn(fixture.componentInstance, 'dateFilter').and.callThrough();
fixture.componentInstance.minDate =
new Date(activeDate.getFullYear() + 1, activeDate.getMonth(), activeDate.getDate());
fixture.detectChanges();
expect(spy).not.toHaveBeenCalled();
});
it('should not call the date filter function if the date is after the max date', () => {
const fixture = TestBed.createComponent(MultiYearViewWithDateFilter);
const activeDate = fixture.componentInstance.activeDate;
const spy = spyOn(fixture.componentInstance, 'dateFilter').and.callThrough();
fixture.componentInstance.maxDate =
new Date(activeDate.getFullYear() - 1, activeDate.getMonth(), activeDate.getDate());
fixture.detectChanges();
expect(spy).not.toHaveBeenCalled();
});
});
describe('multi year view with minDate only', () => {
let fixture: ComponentFixture<MultiYearViewWithMinMaxDate>;
let testComponent: MultiYearViewWithMinMaxDate;
let multiYearViewNativeElement: Element;
beforeEach(() => {
fixture = TestBed.createComponent(MultiYearViewWithMinMaxDate);
const multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView))!;
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
testComponent = fixture.componentInstance;
});
it('should begin first page with minDate', () => {
testComponent.minDate = new Date(2014, JAN, 1);
testComponent.maxDate = null;
fixture.detectChanges();
const cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect((cells[0] as HTMLElement).innerText.trim()).toBe('2014');
});
});
describe('multi year view with maxDate only', () => {
let fixture: ComponentFixture<MultiYearViewWithMinMaxDate>;
let testComponent: MultiYearViewWithMinMaxDate;
let multiYearViewNativeElement: Element;
beforeEach(() => {
fixture = TestBed.createComponent(MultiYearViewWithMinMaxDate);
const multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView))!;
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
testComponent = fixture.componentInstance;
});
it('should end last page with maxDate', () => {
testComponent.minDate = null;
testComponent.maxDate = new Date(2020, JAN, 1);
fixture.detectChanges();
const cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect((cells[cells.length - 1] as HTMLElement).innerText.trim()).toBe('2020');
});
});
describe('multi year view with minDate and maxDate', () => {
let fixture: ComponentFixture<MultiYearViewWithMinMaxDate>;
let testComponent: MultiYearViewWithMinMaxDate;
let multiYearViewNativeElement: Element;
beforeEach(() => {
fixture = TestBed.createComponent(MultiYearViewWithMinMaxDate);
const multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView))!;
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
testComponent = fixture.componentInstance;
});
it('should end last page with maxDate', () => {
testComponent.minDate = new Date(2006, JAN, 1);
testComponent.maxDate = new Date (2020, JAN, 1);
fixture.detectChanges();
const cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect((cells[cells.length - 1] as HTMLElement).innerText.trim()).toBe('2020');
});
it('should disable dates before minDate', () => {
testComponent.minDate = new Date(2006, JAN, 1);
testComponent.maxDate = new Date (2020, JAN, 1);
fixture.detectChanges();
const cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect(cells[0].classList).toContain('mat-calendar-body-disabled');
expect(cells[8].classList).toContain('mat-calendar-body-disabled');
expect(cells[9].classList).not.toContain('mat-calendar-body-disabled');
});
});
describe('multi-year view with custom date classes', () => {
let fixture: ComponentFixture<MultiYearViewWithDateClass>;
let multiYearViewNativeElement: Element;
let dateClassSpy: jasmine.Spy;
beforeEach(() => {
fixture = TestBed.createComponent(MultiYearViewWithDateClass);
dateClassSpy = spyOn(fixture.componentInstance, 'dateClass').and.callThrough();
fixture.detectChanges();
let multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView))!;
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
});
it('should be able to add a custom class to some dates', () => {
let cells = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
expect(cells[0].classList).toContain('even');
expect(cells[1].classList).not.toContain('even');
});
it('should call dateClass with the correct view name', () => {
expect(dateClassSpy).toHaveBeenCalledWith(jasmine.any(Date), 'multi-year');
});
});
});
@Component({
template: `
<mat-multi-year-view [(activeDate)]="date" [(selected)]="selected"
(yearSelected)="selectedYear=$event"></mat-multi-year-view>`
})
class StandardMultiYearView {
date = new Date(2017, JAN, 1);
selected = new Date(2020, JAN, 1);
selectedYear: Date;
@ViewChild(MatMultiYearView) multiYearView: MatMultiYearView<Date>;
}
@Component({
template: `
<mat-multi-year-view
[(activeDate)]="activeDate"
[dateFilter]="dateFilter"
[minDate]="minDate"
[maxDate]="maxDate"></mat-multi-year-view>
`
})
class MultiYearViewWithDateFilter {
activeDate = new Date(2017, JAN, 1);
minDate: Date | null = null;
maxDate: Date | null = null;
dateFilter(date: Date) {
return date.getFullYear() !== 2017;
}
}
@Component({
template: `
<mat-multi-year-view [(activeDate)]="activeDate" [minDate]="minDate" [maxDate]="maxDate">
</mat-multi-year-view>
`
})
class MultiYearViewWithMinMaxDate {
activeDate = new Date(2019, JAN, 1);
minDate: Date | null;
maxDate: Date | null;
}
@Component({
template: `
<mat-multi-year-view [activeDate]="activeDate" [dateClass]="dateClass"></mat-multi-year-view>
`
})
class MultiYearViewWithDateClass {
activeDate = new Date(2017, JAN, 1);
dateClass(date: Date) {
return date.getFullYear() % 2 == 0 ? 'even' : undefined;
}
}