From 8bc5ed63f00c8f795179facf9df5d4ef4272820f Mon Sep 17 00:00:00 2001 From: Douglas Lim <97420966+dlimyy@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:46:02 +0800 Subject: [PATCH] Add unit tests for datepicker.component (#12592) Co-authored-by: Jason Qiu --- .../datepicker/datepicker.component.spec.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/web/app/components/datepicker/datepicker.component.spec.ts b/src/web/app/components/datepicker/datepicker.component.spec.ts index 10eed4d17c9..f40abeb8421 100644 --- a/src/web/app/components/datepicker/datepicker.component.spec.ts +++ b/src/web/app/components/datepicker/datepicker.component.spec.ts @@ -1,6 +1,8 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { FormsModule } from '@angular/forms'; -import { NgbDatepickerModule } from '@ng-bootstrap/ng-bootstrap'; +import { By } from '@angular/platform-browser'; +import { NgbInputDatepicker, NgbDatepickerModule } from '@ng-bootstrap/ng-bootstrap'; +import { DateFormat } from 'src/web/types/datetime-const'; import { DatepickerComponent } from './datepicker.component'; describe('DatepickerComponent', () => { @@ -27,4 +29,21 @@ describe('DatepickerComponent', () => { it('should create', () => { expect(component).toBeTruthy(); }); + + it('should emit the date for changeDate', () => { + const changeDateSpy = jest.spyOn(component.dateChangeCallback, 'emit'); + const date : DateFormat = { year: 2023, month: 10, day: 12 }; + component.changeDate(date); + expect(changeDateSpy).toHaveBeenCalledWith(date); + }); + + it('the datepicker should navigate to today\'s date for selectTodayDate', () => { + const datepicker = fixture.debugElement.query(By.directive(NgbInputDatepicker)).injector.get(NgbInputDatepicker); + const todayDate = component.calendar.getToday(); + const selectTodayDateSpy = jest.spyOn(component.dateChangeCallback, 'emit'); + const datePickerNavigateSpy = jest.spyOn(datepicker, 'navigateTo'); + component.selectTodayDate(datepicker); + expect(selectTodayDateSpy).toHaveBeenCalledWith(todayDate); + expect(datePickerNavigateSpy).toHaveBeenCalledWith(todayDate); + }); });