|
| 1 | +import {Direction, Directionality} from '@angular/cdk/bidi'; |
| 2 | +import {MatDatepickerModule} from './datepicker-module'; |
| 3 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 4 | +import {MatDatepickerIntl} from './datepicker-intl'; |
| 5 | +import {DEC, FEB, JAN, MatNativeDateModule} from '@angular/material/core'; |
| 6 | +import {Component} from '@angular/core'; |
| 7 | +import {MatCalendar} from './calendar'; |
| 8 | +import {By} from '@angular/platform-browser'; |
| 9 | +import {yearsPerPage} from './multi-year-view'; |
| 10 | + |
| 11 | +describe('MatCalendarHeader', () => { |
| 12 | + let dir: { value: Direction }; |
| 13 | + |
| 14 | + beforeEach(async(() => { |
| 15 | + TestBed.configureTestingModule({ |
| 16 | + imports: [ |
| 17 | + MatNativeDateModule, |
| 18 | + MatDatepickerModule, |
| 19 | + ], |
| 20 | + declarations: [ |
| 21 | + // Test components. |
| 22 | + StandardCalendar, |
| 23 | + ], |
| 24 | + providers: [ |
| 25 | + MatDatepickerIntl, |
| 26 | + {provide: Directionality, useFactory: () => dir = {value: 'ltr'}} |
| 27 | + ], |
| 28 | + }); |
| 29 | + |
| 30 | + TestBed.compileComponents(); |
| 31 | + })); |
| 32 | + |
| 33 | + describe('standard calendar', () => { |
| 34 | + let fixture: ComponentFixture<StandardCalendar>; |
| 35 | + let testComponent: StandardCalendar; |
| 36 | + let calendarElement: HTMLElement; |
| 37 | + let periodButton: HTMLElement; |
| 38 | + let prevButton: HTMLElement; |
| 39 | + let nextButton: HTMLElement; |
| 40 | + let calendarInstance: MatCalendar<Date>; |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + fixture = TestBed.createComponent(StandardCalendar); |
| 44 | + fixture.detectChanges(); |
| 45 | + |
| 46 | + let calendarDebugElement = fixture.debugElement.query(By.directive(MatCalendar)); |
| 47 | + calendarElement = calendarDebugElement.nativeElement; |
| 48 | + periodButton = calendarElement.querySelector('.mat-calendar-period-button') as HTMLElement; |
| 49 | + prevButton = calendarElement.querySelector('.mat-calendar-previous-button') as HTMLElement; |
| 50 | + nextButton = calendarElement.querySelector('.mat-calendar-next-button') as HTMLElement; |
| 51 | + |
| 52 | + calendarInstance = calendarDebugElement.componentInstance; |
| 53 | + testComponent = fixture.componentInstance; |
| 54 | + }); |
| 55 | + |
| 56 | + it('should be in month view with specified month active', () => { |
| 57 | + expect(calendarInstance.currentView).toBe('month'); |
| 58 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should toggle view when period clicked', () => { |
| 62 | + expect(calendarInstance.currentView).toBe('month'); |
| 63 | + |
| 64 | + periodButton.click(); |
| 65 | + fixture.detectChanges(); |
| 66 | + |
| 67 | + expect(calendarInstance.currentView).toBe('multi-year'); |
| 68 | + |
| 69 | + periodButton.click(); |
| 70 | + fixture.detectChanges(); |
| 71 | + |
| 72 | + expect(calendarInstance.currentView).toBe('month'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should go to next and previous month', () => { |
| 76 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 77 | + |
| 78 | + nextButton.click(); |
| 79 | + fixture.detectChanges(); |
| 80 | + |
| 81 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, FEB, 28)); |
| 82 | + |
| 83 | + prevButton.click(); |
| 84 | + fixture.detectChanges(); |
| 85 | + |
| 86 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 28)); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should go to previous and next year', () => { |
| 90 | + periodButton.click(); |
| 91 | + fixture.detectChanges(); |
| 92 | + |
| 93 | + expect(calendarInstance.currentView).toBe('multi-year'); |
| 94 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 95 | + |
| 96 | + (calendarElement.querySelector('.mat-calendar-body-active') as HTMLElement).click(); |
| 97 | + fixture.detectChanges(); |
| 98 | + |
| 99 | + expect(calendarInstance.currentView).toBe('year'); |
| 100 | + |
| 101 | + nextButton.click(); |
| 102 | + fixture.detectChanges(); |
| 103 | + |
| 104 | + expect(calendarInstance.activeDate).toEqual(new Date(2018, JAN, 31)); |
| 105 | + |
| 106 | + prevButton.click(); |
| 107 | + fixture.detectChanges(); |
| 108 | + |
| 109 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should go to previous and next multi-year range', () => { |
| 113 | + periodButton.click(); |
| 114 | + fixture.detectChanges(); |
| 115 | + |
| 116 | + expect(calendarInstance.currentView).toBe('multi-year'); |
| 117 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 118 | + |
| 119 | + nextButton.click(); |
| 120 | + fixture.detectChanges(); |
| 121 | + |
| 122 | + expect(calendarInstance.activeDate).toEqual(new Date(2017 + yearsPerPage, JAN, 31)); |
| 123 | + |
| 124 | + prevButton.click(); |
| 125 | + fixture.detectChanges(); |
| 126 | + |
| 127 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should go back to month view after selecting year and month', () => { |
| 131 | + periodButton.click(); |
| 132 | + fixture.detectChanges(); |
| 133 | + |
| 134 | + expect(calendarInstance.currentView).toBe('multi-year'); |
| 135 | + expect(calendarInstance.activeDate).toEqual(new Date(2017, JAN, 31)); |
| 136 | + |
| 137 | + let yearCells = calendarElement.querySelectorAll('.mat-calendar-body-cell'); |
| 138 | + (yearCells[0] as HTMLElement).click(); |
| 139 | + fixture.detectChanges(); |
| 140 | + |
| 141 | + expect(calendarInstance.currentView).toBe('year'); |
| 142 | + expect(calendarInstance.activeDate).toEqual(new Date(2016, JAN, 31)); |
| 143 | + |
| 144 | + let monthCells = calendarElement.querySelectorAll('.mat-calendar-body-cell'); |
| 145 | + (monthCells[monthCells.length - 1] as HTMLElement).click(); |
| 146 | + fixture.detectChanges(); |
| 147 | + |
| 148 | + expect(calendarInstance.currentView).toBe('month'); |
| 149 | + expect(calendarInstance.activeDate).toEqual(new Date(2016, DEC, 31)); |
| 150 | + expect(testComponent.selected).toBeFalsy('no date should be selected yet'); |
| 151 | + }); |
| 152 | + |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +@Component({ |
| 157 | + template: ` |
| 158 | + <mat-calendar |
| 159 | + [startAt]="startDate" |
| 160 | + [(selected)]="selected" |
| 161 | + (yearSelected)="selectedYear=$event" |
| 162 | + (monthSelected)="selectedMonth=$event"> |
| 163 | + </mat-calendar>` |
| 164 | +}) |
| 165 | +class StandardCalendar { |
| 166 | + selected: Date; |
| 167 | + selectedYear: Date; |
| 168 | + selectedMonth: Date; |
| 169 | + startDate = new Date(2017, JAN, 31); |
| 170 | +} |
0 commit comments