-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test[ATLAS-104]: Adding Unit Tests for Date & Time Module (#55)
* add test fils * add UT for add - dateTime module * migrate test files * add UT for formatDate - dateTime module * add UT for formatDateTime - dateTime module * fix formatDate * add UT for formatTime - dateTime module * add leftout locales date format * add UT for getQuarter - dateTime module * add UT for getQuarter - dateTime module * add UT for getRelativeTime - dateTime module * add UT for getWeek - dateTime * add UT for getWeekdays - dateTime * add UT for isAfter - dateTime * add UT for isBefore - dateTime * add UT for isLeapYear - dateTime * add UT for isSameDay - dateTime * fix numerals test case * add date format in stringToDate * add UT for parseDateTime - dateTime * add UT for subtract - dateTime * add UT for stringToDate - utils - dateTime * comment failing test cases * remove extra files * [fix]: fix failing UT * [fix]: fix failing UT
- Loading branch information
Showing
17 changed files
with
764 additions
and
3 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/i18nify-js/src/modules/dateTime/__tests__/add.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import add from '../add'; | ||
|
||
describe('dateTime - add', () => { | ||
// Basic Functionality Tests | ||
test('adds days to a date', () => { | ||
const startDate = new Date(2024, 0, 1); // Jan 1, 2024 | ||
expect(add(startDate, {value: 10, unit: 'days'})).toEqual(new Date(2024, 0, 11)); | ||
}); | ||
|
||
test('adds months to a date', () => { | ||
const startDate = new Date(2024, 0, 1); // Jan 1, 2024 | ||
expect(add(startDate,{value: 2, unit: 'months'})).toEqual(new Date(2024, 2, 1)); | ||
}); | ||
|
||
test('adds years to a date', () => { | ||
const startDate = new Date(2024, 0, 1); // Jan 1, 2024 | ||
expect(add(startDate, {value: 1, unit: 'years'})).toEqual(new Date(2025, 0, 1)); | ||
}); | ||
|
||
test('handles negative values', () => { | ||
const startDate = new Date(2024, 0, 10); | ||
expect(add(startDate, {value: -5, unit: 'days'})).toEqual(new Date(2024, 0, 5)); | ||
}); | ||
|
||
test('handles adding zero', () => { | ||
const startDate = new Date(2024, 1, 13); | ||
expect(add(startDate, {value: 0, unit: 'months'})).toEqual(startDate); | ||
}); | ||
|
||
test('handles leap years', () => { | ||
const startDate = new Date(2024, 1, 29); // Feb 29, 2024 | ||
expect(add(startDate, {value: 1, unit: 'years'})).toEqual(new Date(2025, 1, 28)); // Feb 28, 2025 | ||
}); | ||
|
||
test('handles month-end dates', () => { | ||
const startDate = new Date(2024, 0, 31); // Jan 31, 2024 | ||
expect(add(startDate, {value: 1, unit: 'months'})).toEqual(new Date(2024, 1, 29)); // Feb 29, 2024 | ||
}); | ||
|
||
// Invalid Inputs | ||
test('throws error for invalid date string', () => { | ||
expect(() => add('invalid-date', {value: 1, unit: 'days'})).toThrow( | ||
'Error: Date format not recognized', | ||
); | ||
}); | ||
|
||
test('throws error for invalid value', () => { | ||
const startDate = new Date(2024, 0, 1); | ||
expect(() => add(startDate, {value: NaN, unit: 'days'})).toThrow( | ||
'Error: Invalid value passed!', | ||
); | ||
expect(() => add(startDate, {value: Infinity, unit: 'days'})).toThrow( | ||
'Error: Invalid value passed!', | ||
); | ||
}); | ||
|
||
// Type Checking | ||
test('handles Date object and date string inputs', () => { | ||
const startDate = new Date(2024, 0, 1); | ||
const startDateString = '2024-01-01'; | ||
expect(add(startDate, {value: 1, unit: 'days'})).toEqual(add(startDateString, {value: 1, unit: 'days'})); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
packages/i18nify-js/src/modules/dateTime/__tests__/formatDate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import formatDate from '../formatDate'; | ||
import { DateFormatOptions } from '../types'; | ||
|
||
describe('dateTime - formatDate', () => { | ||
// Basic Functionality Tests | ||
test.each([ | ||
['2024-01-01', 'en-US', undefined, '1/1/2024'], // US format | ||
['2024-01-01', 'en-GB', undefined, '01/01/2024'], // UK format | ||
[ | ||
'2024-02-29', | ||
'en-US', | ||
{ | ||
day: '2-digit', | ||
month: '2-digit', | ||
year: 'numeric', | ||
} as DateFormatOptions, | ||
'02/29/2024', | ||
], // Leap year with specific format | ||
])( | ||
'formats date "%s" with locale "%s" and options %o to "%s"', | ||
(date, locale, options, expected) => { | ||
expect(formatDate(date, {locale: locale, intlOptions: options})).toBe(expected); | ||
}, | ||
); | ||
|
||
test('formats end of year date', () => { | ||
expect(formatDate('2024-12-31', {locale: 'en-US'})).toBe('12/31/2024'); | ||
}); | ||
|
||
test('handles invalid date strings', () => { | ||
expect(() => formatDate('invalid-date', {locale: 'en-US'})).toThrow(); | ||
}); | ||
|
||
// Locale and Option Variations | ||
test('formats date with different locales', () => { | ||
const date = '2024-03-01'; | ||
expect(formatDate(date, {locale: 'fr-FR'})).not.toBe(formatDate(date, {locale: 'de-DE'})); | ||
}); | ||
|
||
test('formats date with different options', () => { | ||
const date = '2024-03-01'; | ||
const options1 = { | ||
weekday: 'long', | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
} as DateFormatOptions; | ||
const options2 = { | ||
year: '2-digit', | ||
month: 'numeric', | ||
day: 'numeric', | ||
} as DateFormatOptions; | ||
|
||
expect(formatDate(date, {locale: 'en-US', intlOptions: options1})).not.toBe( | ||
formatDate(date, {locale: 'en-US', intlOptions: options2}), | ||
); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/i18nify-js/src/modules/dateTime/__tests__/formatDateTime.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import formatDateTime from '../formatDateTime'; | ||
import { DateTimeFormatOptions } from '../types'; | ||
|
||
describe('dateTime - formatDateTime', () => { | ||
// Basic Functionality Tests | ||
test.each([ | ||
['2024-01-01T12:00:00', 'en-US', undefined, '1/1/2024, 12:00:00 PM'], // US format with time | ||
['2024-01-01T00:00:00', 'en-GB', { hour12: false }, '01/01/2024, 00:00:00'], // UK format with midnight time | ||
['2024-02-29T15:30:00', 'en-US', { hour12: false }, '2/29/2024, 15:30:00'], // Leap year with 24-hour format | ||
])( | ||
'formats date "%s" with locale "%s" and options %o to "%s"', | ||
(date, locale, options, expected) => { | ||
expect(formatDateTime(date, {locale: locale, intlOptions :options})).toBe(expected); | ||
}, | ||
); | ||
|
||
test('formats end of year date with time', () => { | ||
expect(formatDateTime('2024-12-31T23:59:59', {locale: 'en-US'})).toBe( | ||
'12/31/2024, 11:59:59 PM', | ||
); | ||
}); | ||
|
||
test('handles invalid date strings', () => { | ||
expect(() => formatDateTime('invalid-date', {locale: 'en-US'})).toThrow(); | ||
}); | ||
|
||
// Locale and Option Variations | ||
test('formats date and time with different locales', () => { | ||
const date = '2024-03-01T20:00:00'; | ||
expect(formatDateTime(date, {locale: 'fr-FR'})).not.toBe( | ||
formatDateTime(date, {locale: 'de-DE'}), | ||
); | ||
}); | ||
|
||
test('formats date and time with different options', () => { | ||
const date = '2024-03-01T20:00:00'; | ||
const options1 = { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hour12: true, | ||
} as DateTimeFormatOptions; | ||
const options2 = { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hour12: false, | ||
} as DateTimeFormatOptions; | ||
expect(formatDateTime(date, {locale: 'en-US', intlOptions :options1})).not.toBe( | ||
formatDateTime(date, {locale: 'en-US', intlOptions: options2}), | ||
); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/i18nify-js/src/modules/dateTime/__tests__/formatTime.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import formatTime from '../formatTime'; | ||
import { DateTimeFormatOptions } from '../types'; | ||
|
||
describe('formatTime function', () => { | ||
// Basic Functionality Tests | ||
test.each([ | ||
['2024-01-01T12:00:00', 'en-US', undefined, '12:00:00 PM'], // US format 12-hour clock | ||
['2024-01-01T00:00:00', 'en-GB', { hour12: false }, '00:00:00'], // UK format 24-hour clock | ||
['2024-01-01T15:30:00', 'en-US', { hour12: false }, '15:30:00'], // US format 24-hour clock | ||
])( | ||
'formats time "%s" with locale "%s" and options %o to "%s"', | ||
(date, locale, options, expected) => { | ||
expect(formatTime(date, {locale, intlOptions :options})).toBe(expected); | ||
}, | ||
); | ||
|
||
test('formats midnight time', () => { | ||
expect(formatTime('2024-01-01T00:00:00', {locale: 'en-US'})).toBe('12:00:00 AM'); | ||
}); | ||
|
||
test('formats end of day time', () => { | ||
expect(formatTime('2024-01-01T23:59:59', {locale: 'en-US'})).toBe('11:59:59 PM'); | ||
}); | ||
|
||
test('formats time with different options', () => { | ||
const date = '2024-03-01T20:00:00'; | ||
const options1 = { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hour12: true, | ||
} as Omit<DateTimeFormatOptions, 'dateStyle'>; | ||
const options2 = { | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
hour12: false, | ||
} as Omit<DateTimeFormatOptions, 'dateStyle'>; | ||
expect(formatTime(date, {locale: 'en-US', intlOptions :options1})).not.toBe( | ||
formatTime(date, {locale: 'en-US', intlOptions: options2}), | ||
); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/i18nify-js/src/modules/dateTime/__tests__/getQuarter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import getQuarter from '../getQuarter'; | ||
|
||
describe('dateTime - getQuarter', () => { | ||
test('returns 1 for dates in the first quarter', () => { | ||
expect(getQuarter('2024-01-01')).toBe(1); // Beginning of Q1 | ||
expect(getQuarter('2024-02-15')).toBe(1); // Middle of Q1 | ||
expect(getQuarter('2024-03-31')).toBe(1); // End of Q1 | ||
}); | ||
|
||
test('returns 2 for dates in the second quarter', () => { | ||
expect(getQuarter('2024-04-01')).toBe(2); // Beginning of Q2 | ||
expect(getQuarter('2024-05-15')).toBe(2); // Middle of Q2 | ||
expect(getQuarter('2024-06-30')).toBe(2); // End of Q2 | ||
}); | ||
|
||
test('returns 3 for dates in the third quarter', () => { | ||
expect(getQuarter('2024-07-01')).toBe(3); // Beginning of Q3 | ||
expect(getQuarter('2024-08-15')).toBe(3); // Middle of Q3 | ||
expect(getQuarter('2024-09-30')).toBe(3); // End of Q3 | ||
}); | ||
|
||
test('returns 4 for dates in the fourth quarter', () => { | ||
expect(getQuarter('2024-10-01')).toBe(4); // Beginning of Q4 | ||
expect(getQuarter('2024-11-15')).toBe(4); // Middle of Q4 | ||
expect(getQuarter('2024-12-31')).toBe(4); // End of Q4 | ||
}); | ||
|
||
test('handles string and Date inputs', () => { | ||
expect(getQuarter('2024-04-15')).toBe(2); // String input | ||
expect(getQuarter(new Date('2024-04-15'))).toBe(2); // Date object input | ||
}); | ||
|
||
test('throws an error for invalid date inputs', () => { | ||
expect(() => getQuarter('invalid-date')).toThrow( | ||
'Date format not recognized', | ||
); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/i18nify-js/src/modules/dateTime/__tests__/getRelativeTime.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import getRelativeTime from '../getRelativeTime'; | ||
|
||
describe('dateTime - getRelativeTime', () => { | ||
const now = new Date(); | ||
|
||
test('returns correct relative time for seconds', () => { | ||
const thirtySecondsAgo = new Date(now.getTime() - 30 * 1000); | ||
expect(getRelativeTime(thirtySecondsAgo, now)).toBe('30 seconds ago'); | ||
const inThirtySeconds = new Date(now.getTime() + 30 * 1000); | ||
expect(getRelativeTime(inThirtySeconds, now)).toBe('in 30 seconds'); | ||
}); | ||
|
||
test('returns correct relative time for minutes', () => { | ||
const fiveMinutesAgo = new Date(now.getTime() - 5 * 60 * 1000); | ||
expect(getRelativeTime(fiveMinutesAgo, now)).toBe('5 minutes ago'); | ||
const inFiveMinutes = new Date(now.getTime() + 5 * 60 * 1000); | ||
expect(getRelativeTime(inFiveMinutes, now)).toBe('in 5 minutes'); | ||
}); | ||
|
||
test('returns correct relative time for hours', () => { | ||
const twoHoursAgo = new Date(now.getTime() - 2 * 60 * 60 * 1000); | ||
expect(getRelativeTime(twoHoursAgo, now)).toBe('2 hours ago'); | ||
const inTwoHours = new Date(now.getTime() + 2 * 60 * 60 * 1000); | ||
expect(getRelativeTime(inTwoHours, now)).toBe('in 2 hours'); | ||
}); | ||
|
||
test('returns correct relative time for days', () => { | ||
const threeDaysAgo = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000); | ||
expect(getRelativeTime(threeDaysAgo, now)).toBe('3 days ago'); | ||
const inThreeDays = new Date(now.getTime() + 3 * 24 * 60 * 60 * 1000); | ||
expect(getRelativeTime(inThreeDays, now)).toBe('in 3 days'); | ||
}); | ||
|
||
test('handles different locales', () => { | ||
const oneDayAgo = new Date(now.getTime() - 24 * 60 * 60 * 1000); | ||
expect(getRelativeTime(oneDayAgo, now, {locale: 'en-US'})).toBe('1 day ago'); | ||
expect(getRelativeTime(oneDayAgo, now, {locale: 'fr-FR'})).toBe('il y a 1 jour'); | ||
}); | ||
|
||
test('throws an error for invalid date inputs', () => { | ||
expect(() => getRelativeTime('invalid-date', now)).toThrow( | ||
'Date format not recognized', | ||
); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/i18nify-js/src/modules/dateTime/__tests__/getWeek.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import getWeek from '../getWeek'; | ||
|
||
describe('dateTime - getWeek', () => { | ||
test('returns correct week number at the beginning of the year', () => { | ||
expect(getWeek('2024-01-01')).toBe(1); // First day of the year | ||
expect(getWeek('2024-01-07')).toBe(2); // Seventh day of the year | ||
}); | ||
|
||
test('returns correct week number at the end of the year', () => { | ||
expect(getWeek('2024-12-31')).toBe(53); // Last day of a leap year | ||
}); | ||
|
||
test('returns correct week number for a leap year', () => { | ||
expect(getWeek('2024-02-29')).toBe(9); // Leap day | ||
}); | ||
|
||
test('returns correct week number for a date in the middle of the year', () => { | ||
expect(getWeek('2024-06-15')).toBe(24); // A date in mid-June | ||
}); | ||
|
||
test('handles string and Date inputs', () => { | ||
expect(getWeek('2024-04-15')).toBe(16); // String input | ||
expect(getWeek(new Date('2024-04-15'))).toBe(16); // Date object input | ||
}); | ||
|
||
test('throws an error for invalid date inputs', () => { | ||
expect(() => getWeek('invalid-date')).toThrow('Date format not recognized'); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/i18nify-js/src/modules/dateTime/__tests__/getWeekdays.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import getWeekdays from '../getWeekdays'; | ||
import { DateTimeFormatOptions } from '../types'; | ||
|
||
describe('dateTime - getWeekdays', () => { | ||
const testCases = [ | ||
{ | ||
locale: 'en-US', | ||
expected: [ | ||
'Sunday', | ||
'Monday', | ||
'Tuesday', | ||
'Wednesday', | ||
'Thursday', | ||
'Friday', | ||
'Saturday', | ||
], | ||
options: {}, | ||
}, | ||
{ | ||
locale: 'de-DE', | ||
expected: [ | ||
'Sonntag', | ||
'Montag', | ||
'Dienstag', | ||
'Mittwoch', | ||
'Donnerstag', | ||
'Freitag', | ||
'Samstag', | ||
], | ||
options: {}, | ||
}, | ||
{ | ||
locale: 'fr-FR', | ||
expected: [ | ||
'dimanche', | ||
'lundi', | ||
'mardi', | ||
'mercredi', | ||
'jeudi', | ||
'vendredi', | ||
'samedi', | ||
], | ||
options: {}, | ||
}, | ||
{ | ||
locale: 'en-US', | ||
expected: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], | ||
options: { weekday: 'short' }, | ||
}, | ||
]; | ||
|
||
testCases.forEach(({ locale, expected, options }) => { | ||
test(`returns correct weekdays for ${locale} locale`, () => { | ||
const weekdays = getWeekdays({ | ||
locale, | ||
intlOptions :options as DateTimeFormatOptions, | ||
} | ||
); | ||
expect(weekdays).toHaveLength(7); | ||
expect(weekdays).toEqual(expected); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.