diff --git a/backend/src/utils/date.ts b/backend/src/utils/date.ts new file mode 100644 index 0000000..792bb93 --- /dev/null +++ b/backend/src/utils/date.ts @@ -0,0 +1,27 @@ +// Extending the built-in Date interface to give a precise return type to toISOString +declare global { + interface Date { + toISOString(): TDateISO; + } +} + +// Type aliases for different parts of a date/time string +type TYear = `${number}${number}${number}${number}`; +type TMonth = `${number}${number}`; +type TDay = `${number}${number}`; +type THours = `${number}${number}`; +type TMinutes = `${number}${number}`; +type TSeconds = `${number}${number}`; +type TMilliseconds = `${number}${number}${number}`; + +// Represent a string like `2021-01-08` (date only) +type TDateISODate = `${TYear}-${TMonth}-${TDay}`; + +// Represent a string like `14:42:34.678` (time part) +type TDateISOTime = `${THours}:${TMinutes}:${TSeconds}.${TMilliseconds}`; + +// Represent a string like `2021-01-08T14:42:34.678Z` (full ISO 8601 format) +type TDateISOFull = `${TDateISODate}T${TDateISOTime}Z`; + +// Expects either the full date-time format or date-only format +export type TDateISO = TDateISOFull | TDateISODate; diff --git a/backend/tests/date.test.ts b/backend/tests/date.test.ts new file mode 100644 index 0000000..5522c6c --- /dev/null +++ b/backend/tests/date.test.ts @@ -0,0 +1,34 @@ +import { describe, it, expect } from 'vitest'; +import { TDateISO } from '../src/utils/date'; // Import the type + +// Helper function to check if a string matches the YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ format +const isDateISO = (date: string): boolean => { + // Regex for the format YYYY-MM-DD or YYYY-MM-DDTHH:mm:ss.sssZ + const regex = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z)$/; + return regex.test(date); +}; + +describe('Date ISO Format', () => { + it('should match the expected date format (YYYY-MM-DD)', () => { + const date: TDateISO = '2025-01-28'; // YYYY-MM-DD + expect(isDateISO(date)).toBe(true); + }); + + it('should match the expected date-time format (YYYY-MM-DDTHH:mm:ss.sssZ)', () => { + const date: TDateISO = '2025-01-28T14:42:34.678Z'; // YYYY-MM-DDTHH:mm:ss.sssZ + expect(isDateISO(date)).toBe(true); + }); + + it('should match the correct format for `toISOString()` method', () => { + const date = new Date(); + const isoString: TDateISO = date.toISOString(); + expect(isDateISO(isoString)).toBe(true); + }); + + it('should return a string in the correct format from `toISOString()`', () => { + const date = new Date(); + const isoString = date.toISOString(); + // Check if the ISO string matches either of the two formats + expect(isoString).toMatch(/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}\.\d{3}Z)?$/); + }); +}); diff --git a/frontend/tests/Mock.test.tsx b/frontend/tests/Mock.test.tsx index 554183e..9d0a152 100644 --- a/frontend/tests/Mock.test.tsx +++ b/frontend/tests/Mock.test.tsx @@ -48,7 +48,7 @@ describe("Account component", () => { // Act 1 render( - +