|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | + |
| 4 | +import { BsDatepickerConfig, BsDatepickerDirective, BsDatepickerModule } from '.'; |
| 5 | +import { CalendarCellViewModel } from './models'; |
| 6 | +import { BsDatepickerContainerComponent } from './themes/bs/bs-datepicker-container.component'; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'test-cmp', |
| 10 | + template: `<input type="text" bsDatepicker [bsConfig]="bsConfig">` |
| 11 | +}) |
| 12 | +class TestComponent { |
| 13 | + @ViewChild(BsDatepickerDirective) datepicker: BsDatepickerDirective; |
| 14 | + bsConfig: Partial<BsDatepickerConfig> = { |
| 15 | + displayMonths: 2 |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +type TestFixture = ComponentFixture<TestComponent>; |
| 20 | + |
| 21 | +function getDatepickerDirective(fixture: TestFixture): BsDatepickerDirective { |
| 22 | + const datepicker: BsDatepickerDirective = fixture.componentInstance.datepicker; |
| 23 | + |
| 24 | + return datepicker; |
| 25 | +} |
| 26 | + |
| 27 | +function showDatepicker(fixture: TestFixture): BsDatepickerDirective { |
| 28 | + const datepicker = getDatepickerDirective(fixture); |
| 29 | + datepicker.show(); |
| 30 | + fixture.detectChanges(); |
| 31 | + |
| 32 | + return datepicker; |
| 33 | +} |
| 34 | + |
| 35 | +function hideDatepicker(fixture: TestFixture): BsDatepickerDirective { |
| 36 | + const datepicker = getDatepickerDirective(fixture); |
| 37 | + datepicker.hide(); |
| 38 | + fixture.detectChanges(); |
| 39 | + |
| 40 | + return datepicker; |
| 41 | +} |
| 42 | + |
| 43 | +function getDatepickerContainer(datepicker: BsDatepickerDirective): BsDatepickerContainerComponent | null { |
| 44 | + return datepicker[`_datepickerRef`] ? datepicker[`_datepickerRef`].instance : null; |
| 45 | +} |
| 46 | + |
| 47 | +describe('datepicker:', () => { |
| 48 | + let fixture: TestFixture; |
| 49 | + beforeEach( |
| 50 | + async(() => TestBed.configureTestingModule({ |
| 51 | + declarations: [TestComponent], |
| 52 | + imports: [BsDatepickerModule.forRoot()] |
| 53 | + }).compileComponents() |
| 54 | + )); |
| 55 | + beforeEach(() => { |
| 56 | + fixture = TestBed.createComponent(TestComponent); |
| 57 | + fixture.detectChanges(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should display datepicker on show', () => { |
| 61 | + const datepicker = showDatepicker(fixture); |
| 62 | + expect(getDatepickerContainer(datepicker)).toBeDefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should hide datepicker on hide', () => { |
| 66 | + const datepicker = hideDatepicker(fixture); |
| 67 | + expect(getDatepickerContainer(datepicker)).toBeNull(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should select correct year when a month other than selected year is chosen', () => { |
| 71 | + const datepicker = showDatepicker(fixture); |
| 72 | + const datepickerContainerInstance = getDatepickerContainer(datepicker); |
| 73 | + const yearSelection: CalendarCellViewModel = { date: new Date(2017, 1, 1), label: 'label' }; |
| 74 | + const monthSelection: CalendarCellViewModel = { date: new Date(2018, 1, 1), label: 'label' }; |
| 75 | + datepickerContainerInstance.yearSelectHandler(yearSelection); |
| 76 | + datepickerContainerInstance.monthSelectHandler(monthSelection); |
| 77 | + fixture.detectChanges(); |
| 78 | + datepickerContainerInstance[`_store`] |
| 79 | + .select(state => state.view) |
| 80 | + .subscribe(view => { |
| 81 | + expect(view.date.getFullYear()).toEqual(monthSelection.date.getFullYear()); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments