From ec6c5af24445f4946c0a19a4a2698af219366da6 Mon Sep 17 00:00:00 2001 From: dlimyy Date: Mon, 25 Sep 2023 13:08:54 +0800 Subject: [PATCH 1/2] Add unit tests to datepickerformatter --- .../datepicker/datepicker-formatter.spec.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/web/app/components/datepicker/datepicker-formatter.spec.ts diff --git a/src/web/app/components/datepicker/datepicker-formatter.spec.ts b/src/web/app/components/datepicker/datepicker-formatter.spec.ts new file mode 100644 index 00000000000..70a9c50dc8b --- /dev/null +++ b/src/web/app/components/datepicker/datepicker-formatter.spec.ts @@ -0,0 +1,44 @@ +import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; +import { DatePickerFormatter } from './datepicker-formatter'; + +describe('DatepickerFormatter', () => { + let formatter : DatePickerFormatter; + + beforeEach(() => { + formatter = new DatePickerFormatter(); + }); + + it('should create an instance', () => { + expect(formatter).toBeTruthy(); + }); + + it('should return an empty string if date is null', () => { + // I used any as the type here as it is the only way to pass in null since + // null is not compatible with NgbDateStruct + const date: any = null; + const formattedDate: String = formatter.format(date); + expect(formattedDate).toEqual(''); + }); + + it('should return a properly formatted date string', () => { + const date: NgbDateStruct = { year: 2023, month: 12, day: 12 }; + const formattedDate = formatter.format(date); + expect(formattedDate).toEqual('Tue, 12 Dec, 2023'); + }); + + it('should parse the valid date string correctly', () => { + const date : string = 'Tue, 12 Dec, 2023'; + const parsedDate : NgbDateStruct = formatter.parse(date); + expect(parsedDate.day).toEqual(12); + expect(parsedDate.month).toEqual(12); + expect(parsedDate.year).toEqual(2023); + }); + + it('should return NaN for all the fields if invalid date string format is parsed', () => { + const date : string = '12th December 2023'; + const parsedDate : NgbDateStruct = formatter.parse(date); + expect(parsedDate.day).toEqual(NaN); + expect(parsedDate.month).toEqual(NaN); + expect(parsedDate.year).toEqual(NaN); + }); +}); From 23e6118f7700ed871a6b669d39e7996541a2a03d Mon Sep 17 00:00:00 2001 From: dlimyy Date: Mon, 25 Sep 2023 23:11:08 +0800 Subject: [PATCH 2/2] Update wording for comment regarding date --- src/web/app/components/datepicker/datepicker-formatter.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/web/app/components/datepicker/datepicker-formatter.spec.ts b/src/web/app/components/datepicker/datepicker-formatter.spec.ts index 70a9c50dc8b..63f55cfe29c 100644 --- a/src/web/app/components/datepicker/datepicker-formatter.spec.ts +++ b/src/web/app/components/datepicker/datepicker-formatter.spec.ts @@ -13,8 +13,7 @@ describe('DatepickerFormatter', () => { }); it('should return an empty string if date is null', () => { - // I used any as the type here as it is the only way to pass in null since - // null is not compatible with NgbDateStruct + // Any is used as the type for date here as it is the only way to pass in null to NgbDateStruct const date: any = null; const formattedDate: String = formatter.format(date); expect(formattedDate).toEqual('');