1 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
2 import { FormControl, FormsModule } from '@angular/forms';
4 import { NgbDatepickerModule, NgbTimepickerModule } from '@ng-bootstrap/ng-bootstrap';
6 import { configureTestBed } from '~/testing/unit-test-helper';
7 import { DateTimePickerComponent } from './date-time-picker.component';
9 describe('DateTimePickerComponent', () => {
10 let component: DateTimePickerComponent;
11 let fixture: ComponentFixture<DateTimePickerComponent>;
14 declarations: [DateTimePickerComponent],
15 imports: [NgbDatepickerModule, NgbTimepickerModule, FormsModule]
19 spyOn(Date, 'now').and.returnValue(new Date('2022-02-22T00:00:00.00'));
20 fixture = TestBed.createComponent(DateTimePickerComponent);
21 component = fixture.componentInstance;
24 it('should create with correct datetime', fakeAsync(() => {
25 component.control = new FormControl('2022-02-26 00:00:00');
26 fixture.detectChanges();
28 expect(component).toBeTruthy();
29 expect(component.control.value).toBe('2022-02-26 00:00:00');
32 it('should update control value if datetime is not valid', fakeAsync(() => {
33 component.control = new FormControl('not valid');
34 fixture.detectChanges();
36 expect(component.control.value).toBe('2022-02-22 00:00:00');
39 it('should init with only date enabled', () => {
40 component.control = new FormControl();
41 component.hasTime = false;
42 fixture.detectChanges();
43 expect(component.format).toBe('YYYY-MM-DD');
46 it('should init with time enabled', () => {
47 component.control = new FormControl();
48 component.hasSeconds = false;
49 fixture.detectChanges();
50 expect(component.format).toBe('YYYY-MM-DD HH:mm');
53 it('should init with seconds enabled', () => {
54 component.control = new FormControl();
55 fixture.detectChanges();
56 expect(component.format).toBe('YYYY-MM-DD HH:mm:ss');