import { Component, Type } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing'; import { NKDatetimeModule } from './ng2-datetime.module'; import { NKDatetime } from './ng2-datetime'; describe('ng2-datetime', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [FormsModule, NKDatetimeModule], declarations: [DatePickerComponent] }); }); it('should instantiate', () => { const component = TestBed.createComponent(NKDatetime).componentInstance; expect(component).not.toBeNull(); expect(component instanceof NKDatetime).toEqual(true); }); describe('datepicker', () => { it('should update ngModel when datepicker value changes', fakeAsync(() => { const fixture = TestBed.createComponent(DatePickerComponent); const component = getComponent(fixture, NKDatetime); (fixture.componentInstance).date = null; tickAndDetect(fixture); component.datepicker.datepicker('setDate', new Date(2011, 2, 5)); tickAndDetect(fixture); expect(fixture.componentInstance.date).toEqual(new Date(2011, 2, 5)); })); }); describe('timepicker', () => { it('should update ngModel when timepicker value changes', fakeAsync(() => { const fixture = TestBed.createComponent(DatePickerComponent); const component = getComponent(fixture, NKDatetime); fixture.componentInstance.date = new Date(2011, 2, 5, 0, 0); tickAndDetect(fixture); tickAndDetect(fixture); component.timepicker.timepicker('setTime', '12:45 AM'); tickAndDetect(fixture); expect(fixture.componentInstance.date).toEqual(new Date(2011, 2, 5, 0, 45)); })); }); }); function getComponent(fixture: ComponentFixture, type: Type): T { // tick for each component in the component tree tickAndDetect(fixture); tickAndDetect(fixture); return fixture.debugElement.query(By.directive(type)).componentInstance; } function tickAndDetect(fixture: ComponentFixture) { tick(); fixture.detectChanges(); } @Component({ template: ` ` }) class DatePickerComponent { date: Date; }