Skip to content

Commit 4a3f81c

Browse files
committed
add tests for multi year view
1 parent b77e36d commit 4a3f81c

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import {Component, ViewChild} from '@angular/core';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
3+
import {JAN, MatNativeDateModule} from '@angular/material/core';
4+
import {By} from '@angular/platform-browser';
5+
import {MatCalendarBody} from './calendar-body';
6+
import {MatMultiYearView, yearsPerPage} from './multi-year-view';
7+
import {MatYearView} from './year-view';
8+
9+
describe('MatMultiYearView', () => {
10+
beforeEach(() => {
11+
TestBed.configureTestingModule({
12+
imports: [
13+
MatNativeDateModule,
14+
],
15+
declarations: [
16+
MatCalendarBody,
17+
MatMultiYearView,
18+
19+
// Test components.
20+
StandardMultiYearView,
21+
],
22+
});
23+
24+
TestBed.compileComponents();
25+
});
26+
27+
describe('standard multi-year view', () => {
28+
let fixture: ComponentFixture<StandardMultiYearView>;
29+
let testComponent: StandardMultiYearView;
30+
let multiYearViewNativeElement: Element;
31+
32+
beforeEach(() => {
33+
fixture = TestBed.createComponent(StandardMultiYearView);
34+
fixture.detectChanges();
35+
36+
let multiYearViewDebugElement = fixture.debugElement.query(By.directive(MatMultiYearView));
37+
multiYearViewNativeElement = multiYearViewDebugElement.nativeElement;
38+
testComponent = fixture.componentInstance;
39+
});
40+
41+
it('has correct number of years', () => {
42+
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell')!;
43+
expect(cellEls.length).toBe(yearsPerPage);
44+
});
45+
46+
it('shows selected year if in same range', () => {
47+
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected')!;
48+
expect(selectedEl.innerHTML.trim()).toBe('2020');
49+
});
50+
51+
it('does not show selected year if in different range', () => {
52+
testComponent.selected = new Date(2040, JAN, 10);
53+
fixture.detectChanges();
54+
55+
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected');
56+
expect(selectedEl).toBeNull();
57+
});
58+
59+
it('fires selected change event on cell clicked', () => {
60+
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
61+
(cellEls[cellEls.length - 1] as HTMLElement).click();
62+
fixture.detectChanges();
63+
64+
let selectedEl = multiYearViewNativeElement.querySelector('.mat-calendar-body-selected')!;
65+
expect(selectedEl.innerHTML.trim()).toBe('2039');
66+
});
67+
68+
it('should mark active date', () => {
69+
let cellEls = multiYearViewNativeElement.querySelectorAll('.mat-calendar-body-cell');
70+
expect((cellEls[1] as HTMLElement).innerText.trim()).toBe('2017');
71+
expect(cellEls[1].classList).toContain('mat-calendar-body-active');
72+
});
73+
});
74+
});
75+
76+
77+
@Component({
78+
template: `
79+
<mat-multi-year-view [activeDate]="date" [(selected)]="selected"></mat-multi-year-view>`,
80+
})
81+
class StandardMultiYearView {
82+
date = new Date(2017, JAN, 1);
83+
selected = new Date(2020, JAN, 1);
84+
85+
@ViewChild(MatYearView) yearView: MatYearView<Date>;
86+
}

0 commit comments

Comments
 (0)