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

test[ATLAS-104]: Adding Unit Tests for Date & Time Module #55

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
68b7e1b
add test fils
RgnDunes Jan 18, 2024
70c7b6e
Merge remote-tracking branch 'origin/ATLAS-104-date-module-and-ap-is-…
RgnDunes Jan 23, 2024
3302bd6
add UT for add - dateTime module
RgnDunes Jan 23, 2024
391572f
migrate test files
RgnDunes Jan 23, 2024
c9980a5
add UT for formatDate - dateTime module
RgnDunes Jan 23, 2024
a5b5788
add UT for formatDateTime - dateTime module
RgnDunes Jan 23, 2024
9437770
fix formatDate
RgnDunes Jan 23, 2024
f07ba81
add UT for formatTime - dateTime module
RgnDunes Jan 23, 2024
ca73e4e
add leftout locales date format
RgnDunes Jan 23, 2024
47ecef2
add UT for getQuarter - dateTime module
RgnDunes Jan 23, 2024
98c481a
add UT for getQuarter - dateTime module
RgnDunes Jan 23, 2024
64e439b
add UT for getRelativeTime - dateTime module
RgnDunes Jan 23, 2024
1242856
add UT for getWeek - dateTime
RgnDunes Jan 24, 2024
2496c40
add UT for getWeekdays - dateTime
RgnDunes Jan 24, 2024
1e84ec3
add UT for isAfter - dateTime
RgnDunes Jan 24, 2024
4da2c66
add UT for isBefore - dateTime
RgnDunes Jan 24, 2024
87cce04
add UT for isLeapYear - dateTime
RgnDunes Jan 24, 2024
4eae537
add UT for isSameDay - dateTime
RgnDunes Jan 24, 2024
091b44e
fix numerals test case
RgnDunes Jan 24, 2024
267b774
add date format in stringToDate
RgnDunes Jan 24, 2024
c775b91
add UT for parseDateTime - dateTime
RgnDunes Jan 24, 2024
25feda1
add UT for subtract - dateTime
RgnDunes Jan 24, 2024
cd2ba13
add UT for stringToDate - utils - dateTime
RgnDunes Jan 24, 2024
dd74a28
Merge remote-tracking branch 'origin/ATLAS-104-date-module-and-ap-is-…
RgnDunes Jan 24, 2024
a34962c
comment failing test cases
RgnDunes Jan 24, 2024
9cf9293
remove extra files
RgnDunes Jan 29, 2024
3e4c127
Merge remote-tracking branch 'origin/ATLAS-104-date-module-and-ap-is-…
RgnDunes Jan 29, 2024
5d6cdaa
Merge remote-tracking branch 'origin/ATLAS-104-date-module-and-ap-is-…
RgnDunes Feb 14, 2024
34de1e9
[fix]: fix failing UT
RgnDunes Feb 14, 2024
a37fdd3
[fix]: fix failing UT
RgnDunes Feb 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/i18nify-js/src/modules/dateTime/__tests__/add.test.ts
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'}));
});
});
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}),
);
});
});
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}),
);
});
});
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}),
);
});
});
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',
);
});
});
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 packages/i18nify-js/src/modules/dateTime/__tests__/getWeek.test.ts
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');
});
});
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);
});
});
});
Loading
Loading