summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/components/date-time-picker/date-time-picker.component.spec.ts
blob: 00d09e3b4d3cf549a76956b38c8cdbe33c119f05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { FormControl, FormsModule } from '@angular/forms';

import { NgbDatepickerModule, NgbTimepickerModule } from '@ng-bootstrap/ng-bootstrap';

import { configureTestBed } from '~/testing/unit-test-helper';
import { DateTimePickerComponent } from './date-time-picker.component';

describe('DateTimePickerComponent', () => {
  let component: DateTimePickerComponent;
  let fixture: ComponentFixture<DateTimePickerComponent>;

  configureTestBed({
    declarations: [DateTimePickerComponent],
    imports: [NgbDatepickerModule, NgbTimepickerModule, FormsModule]
  });

  beforeEach(() => {
    spyOn(Date, 'now').and.returnValue(new Date('2022-02-22T00:00:00.00'));
    fixture = TestBed.createComponent(DateTimePickerComponent);
    component = fixture.componentInstance;
  });

  it('should create with correct datetime', fakeAsync(() => {
    component.control = new FormControl('2022-02-26 00:00:00');
    fixture.detectChanges();
    tick();
    expect(component).toBeTruthy();
    expect(component.control.value).toBe('2022-02-26 00:00:00');
  }));

  it('should update control value if datetime is not valid', fakeAsync(() => {
    component.control = new FormControl('not valid');
    fixture.detectChanges();
    tick();
    expect(component.control.value).toBe('2022-02-22 00:00:00');
  }));

  it('should init with only date enabled', () => {
    component.control = new FormControl();
    component.hasTime = false;
    fixture.detectChanges();
    expect(component.format).toBe('YYYY-MM-DD');
  });

  it('should init with time enabled', () => {
    component.control = new FormControl();
    component.hasSeconds = false;
    fixture.detectChanges();
    expect(component.format).toBe('YYYY-MM-DD HH:mm');
  });

  it('should init with seconds enabled', () => {
    component.control = new FormControl();
    fixture.detectChanges();
    expect(component.format).toBe('YYYY-MM-DD HH:mm:ss');
  });
});