Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12588] Add unit tests to DatePickerFormatter #12593

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/web/app/components/datepicker/datepicker-formatter.spec.ts
Original file line number Diff line number Diff line change
@@ -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
jasonqiu212 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});