Skip to content

Commit

Permalink
Add unit tests for datepicker.component (#12592)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Qiu <[email protected]>
  • Loading branch information
dlimyy and jasonqiu212 authored Sep 26, 2023
1 parent a1e2ca5 commit 8bc5ed6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/web/app/components/datepicker/datepicker.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});

0 comments on commit 8bc5ed6

Please sign in to comment.