-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(datepicker): select correct month and year from month picker view… (
#4501) * fix(datepicker): select correct month and year from month picker view when displayMonths > 1 * test(datepicker): select correct year on month selection * test(datepicker): add test for show and hide * fix(tslint): fix tslint errors * feat(common): clean up
- Loading branch information
1 parent
b2bd459
commit 3a17cc3
Showing
2 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Component, ViewChild } from '@angular/core'; | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { BsDatepickerConfig, BsDatepickerDirective, BsDatepickerModule } from '.'; | ||
import { CalendarCellViewModel } from './models'; | ||
import { BsDatepickerContainerComponent } from './themes/bs/bs-datepicker-container.component'; | ||
|
||
@Component({ | ||
selector: 'test-cmp', | ||
template: `<input type="text" bsDatepicker [bsConfig]="bsConfig">` | ||
}) | ||
class TestComponent { | ||
@ViewChild(BsDatepickerDirective) datepicker: BsDatepickerDirective; | ||
bsConfig: Partial<BsDatepickerConfig> = { | ||
displayMonths: 2 | ||
}; | ||
} | ||
|
||
type TestFixture = ComponentFixture<TestComponent>; | ||
|
||
function getDatepickerDirective(fixture: TestFixture): BsDatepickerDirective { | ||
const datepicker: BsDatepickerDirective = fixture.componentInstance.datepicker; | ||
|
||
return datepicker; | ||
} | ||
|
||
function showDatepicker(fixture: TestFixture): BsDatepickerDirective { | ||
const datepicker = getDatepickerDirective(fixture); | ||
datepicker.show(); | ||
fixture.detectChanges(); | ||
|
||
return datepicker; | ||
} | ||
|
||
function hideDatepicker(fixture: TestFixture): BsDatepickerDirective { | ||
const datepicker = getDatepickerDirective(fixture); | ||
datepicker.hide(); | ||
fixture.detectChanges(); | ||
|
||
return datepicker; | ||
} | ||
|
||
function getDatepickerContainer(datepicker: BsDatepickerDirective): BsDatepickerContainerComponent | null { | ||
return datepicker[`_datepickerRef`] ? datepicker[`_datepickerRef`].instance : null; | ||
} | ||
|
||
describe('datepicker:', () => { | ||
let fixture: TestFixture; | ||
beforeEach( | ||
async(() => TestBed.configureTestingModule({ | ||
declarations: [TestComponent], | ||
imports: [BsDatepickerModule.forRoot()] | ||
}).compileComponents() | ||
)); | ||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should display datepicker on show', () => { | ||
const datepicker = showDatepicker(fixture); | ||
expect(getDatepickerContainer(datepicker)).toBeDefined(); | ||
}); | ||
|
||
it('should hide datepicker on hide', () => { | ||
const datepicker = hideDatepicker(fixture); | ||
expect(getDatepickerContainer(datepicker)).toBeNull(); | ||
}); | ||
|
||
it('should select correct year when a month other than selected year is chosen', () => { | ||
const datepicker = showDatepicker(fixture); | ||
const datepickerContainerInstance = getDatepickerContainer(datepicker); | ||
const yearSelection: CalendarCellViewModel = { date: new Date(2017, 1, 1), label: 'label' }; | ||
const monthSelection: CalendarCellViewModel = { date: new Date(2018, 1, 1), label: 'label' }; | ||
datepickerContainerInstance.yearSelectHandler(yearSelection); | ||
datepickerContainerInstance.monthSelectHandler(monthSelection); | ||
fixture.detectChanges(); | ||
datepickerContainerInstance[`_store`] | ||
.select(state => state.view) | ||
.subscribe(view => { | ||
expect(view.date.getFullYear()).toEqual(monthSelection.date.getFullYear()); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters