FitTrackee/fittrackee_client/tests/unit/utils/dates.spec.ts

413 lines
11 KiB
TypeScript
Raw Permalink Normal View History

2023-11-11 17:12:20 +01:00
import { describe, it, expect } from 'vitest'
import {
getCalendarStartAndEnd,
incrementDate,
2021-10-23 15:44:25 +02:00
getStartDate,
formatWorkoutDate,
formatDate,
availableDateFormatOptions,
getDateFormat,
} from '@/utils/dates'
describe('startDate (week starting Sunday)', () => {
const testsParams = [
{
description: "returns start day for 'month' duration",
inputDuration: 'month',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 1, 2021 00:00:00',
},
{
description: "returns start day for 'week' duration",
inputDuration: 'week',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 15, 2021 00:00:00',
},
{
description: "returns start day for 'year' duration",
inputDuration: 'year',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'January 1, 2021 00:00:00',
},
]
testsParams.map((testParams) =>
it(testParams.description, () => {
const day: Date = new Date(testParams.inputDate)
const expected: Date = new Date(testParams.expectedDate)
2023-11-11 17:12:20 +01:00
expect(getStartDate(testParams.inputDuration, day, false)).toStrictEqual(
expected
)
})
)
})
describe('startDate (week starting Monday)', () => {
const testsParams = [
{
description: "returns start day for 'month' duration",
inputDuration: 'month',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 1, 2021 00:00:00',
},
{
description: 'returns start day for week duration',
inputDuration: 'week',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 16, 2021 00:00:00',
},
{
description: "returns start day for 'year' duration",
inputDuration: 'year',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'January 1, 2021 00:00:00',
},
]
testsParams.map((testParams) =>
it(testParams.description, () => {
const day: Date = new Date(testParams.inputDate)
const expected: Date = new Date(testParams.expectedDate)
2023-11-11 17:12:20 +01:00
expect(getStartDate(testParams.inputDuration, day, true)).toStrictEqual(
2021-10-23 15:44:25 +02:00
expected
)
})
)
})
describe('startDate (week starting Monday)', () => {
it('it throws an exception if duration is invalid', () => {
const day: Date = new Date('August 21, 2021 20:00:00')
2021-10-23 15:44:25 +02:00
expect(() => getStartDate('invalid duration', day, true)).to.throw(
'Invalid duration, expected: "week", "month", "year", got: "invalid duration"'
)
})
})
describe('dateIncrement', () => {
const testsParams = [
{
description: "returns incremented day for 'month' duration",
inputDuration: 'month',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'September 21, 2021 20:00:00',
},
{
description: 'returns incremented day for week duration',
inputDuration: 'week',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 28, 2021 20:00:00',
},
{
description:
'returns incremented day for week duration (February w/ 28 days)',
inputDuration: 'week',
inputDate: 'February 22, 2021 20:00:00',
expectedDate: 'March 1, 2021 20:00:00',
},
{
description:
'returns incremented day for week duration (February w/ 29 days)',
inputDuration: 'week',
inputDate: 'February 22, 2020 20:00:00',
expectedDate: 'February 29, 2020 20:00:00',
},
{
description: "returns incremented day for 'year' duration",
inputDuration: 'year',
inputDate: 'August 21, 2021 20:00:00',
expectedDate: 'August 21, 2022 20:00:00',
},
{
description: "returns incremented day for 'year' duration (February, 29)",
inputDuration: 'year',
inputDate: 'February 29, 2020 20:00:00',
expectedDate: 'February 28, 2021 20:00:00',
},
]
testsParams.map((testParams) =>
it(testParams.description, () => {
const day: Date = new Date(testParams.inputDate)
const expected: Date = new Date(testParams.expectedDate)
2023-11-11 17:12:20 +01:00
expect(incrementDate(testParams.inputDuration, day)).toStrictEqual(
expected
)
})
)
})
describe('dateIncrement', () => {
it('it throws an exception if duration is invalid', () => {
const day: Date = new Date('August 21, 2021 20:00:00')
expect(() => incrementDate('invalid duration', day)).to.throw(
'Invalid duration, expected: "week", "month", "year", got: "invalid duration"'
)
})
})
2021-09-05 17:43:14 +02:00
describe('getCalendarStartAndEnd', () => {
const testsParams = [
{
description: 'returns start and end dates for calendar',
2021-09-05 17:43:14 +02:00
inputDate: 'September 5, 2021 20:00:00',
inputWeekStartingMonday: false,
2021-09-05 17:43:14 +02:00
expectedStartDate: 'August 29, 2021 00:00:00',
expectedEndDate: 'October 2, 2021 23:59:59.999',
},
{
description:
'returns start and end dates for calendar when week starts on Monday',
inputDate: 'April 1, 2021 20:00:00',
inputWeekStartingMonday: true,
expectedStartDate: 'March 29, 2021 00:00:00',
expectedEndDate: 'May 2, 2021 23:59:59.999',
},
2021-09-05 17:43:14 +02:00
]
testsParams.map((testParams) =>
it(testParams.description, () => {
const date: Date = new Date(testParams.inputDate)
const results = getCalendarStartAndEnd(
date,
testParams.inputWeekStartingMonday
)
2023-11-11 17:12:20 +01:00
expect(results.start).toStrictEqual(
new Date(testParams.expectedStartDate)
)
expect(results.end).toStrictEqual(new Date(testParams.expectedEndDate))
2021-09-05 17:43:14 +02:00
})
)
})
describe('formatWorkoutDate', () => {
const testsParams = [
{
description: 'returns date and time with default format',
inputParams: {
date: new Date('August 21, 2021 20:00:00'),
dateFormat: null,
timeFormat: null,
},
expected: {
workout_date: '2021/08/21',
workout_time: '20:00',
},
},
{
description: 'returns date and time with provided date format',
inputParams: {
date: new Date('August 21, 2021 20:00:00'),
dateFormat: 'dd MM yyyy',
timeFormat: null,
},
expected: {
workout_date: '21 08 2021',
workout_time: '20:00',
},
},
{
description: 'returns date and time with provided time format',
inputParams: {
date: new Date('August 21, 2021 20:00:00'),
dateFormat: null,
timeFormat: 'HH:mm:ss',
},
expected: {
workout_date: '2021/08/21',
workout_time: '20:00:00',
},
},
{
description: 'returns date and time with provided date and time formats',
inputParams: {
date: new Date('August 21, 2021 20:00:00'),
dateFormat: 'dd-MM-yyyy',
timeFormat: 'HH:mm:ss',
},
expected: {
workout_date: '21-08-2021',
workout_time: '20:00:00',
},
},
]
testsParams.map((testParams) => {
it(testParams.description, () => {
2023-11-11 17:12:20 +01:00
expect(
formatWorkoutDate(
testParams.inputParams.date,
testParams.inputParams.dateFormat,
testParams.inputParams.timeFormat
2023-11-11 17:12:20 +01:00
)
).toStrictEqual(testParams.expected)
})
})
})
describe('formatDate', () => {
const dateString = 'Tue, 01 Nov 2022 00:00:00 GMT'
const testsParams = [
{
description:
'format date for "Europe/Paris" timezone and "dd/MM/yyyy" format (with time)',
inputParams: {
timezone: 'Europe/Paris',
dateFormat: 'dd/MM/yyyy',
withTime: true,
},
expectedDate: '01/11/2022 01:00',
},
{
description:
'format date for "America/New_York" timezone and "MM/dd/yyyy" format (w/o time)',
inputParams: {
timezone: 'America/New_York',
dateFormat: 'MM/dd/yyyy',
withTime: false,
},
expectedDate: '10/31/2022',
},
]
testsParams.map((testParams) => {
it(testParams.description, () => {
2023-11-11 17:12:20 +01:00
expect(
formatDate(
dateString,
testParams.inputParams.timezone,
testParams.inputParams.dateFormat,
testParams.inputParams.withTime
2023-11-11 17:12:20 +01:00
)
).toStrictEqual(testParams.expectedDate)
})
})
})
describe('formatDate (w/ default value)', () => {
it('format date for "Europe/Paris" timezone and "dd/MM/yyyy" format', () => {
2023-11-11 17:12:20 +01:00
expect(
formatDate('Tue, 01 Nov 2022 00:00:00 GMT', 'Europe/Paris', 'yyyy-MM-dd')
).toBe('2022-11-01 01:00')
})
})
describe('formatDate with_seconds', () => {
it('format date for "Europe/Paris" timezone and "dd/MM/yyyy" format and seconds', () => {
2023-11-11 17:12:20 +01:00
expect(
formatDate(
'Tue, 01 Nov 2022 00:00:00 GMT',
'Europe/Paris',
'yyyy-MM-dd',
true,
null,
true
2023-11-11 17:12:20 +01:00
)
).toBe('2022-11-01 01:00:00')
})
})
describe('getDateFormat', () => {
const testsParams = [
{
inputParams: {
dateFormat: 'dd/MM/yyyy',
language: 'en',
},
expectedFormat: 'dd/MM/yyyy',
},
{
inputParams: {
dateFormat: 'MM/dd/yyyy',
language: 'en',
},
expectedFormat: 'MM/dd/yyyy',
},
{
inputParams: {
dateFormat: 'yyyy-MM-dd',
language: 'en',
},
expectedFormat: 'yyyy-MM-dd',
},
{
inputParams: {
dateFormat: 'date_string',
language: 'en',
},
expectedFormat: 'MMM. do, yyyy',
},
{
inputParams: {
dateFormat: 'date_string',
language: 'fr',
},
expectedFormat: 'd MMM yyyy',
},
{
inputParams: {
dateFormat: 'date_string',
language: 'de',
},
2022-11-01 12:17:20 +01:00
expectedFormat: 'do MMM yyyy',
},
]
testsParams.map((testParams) => {
it(`get date format for "${testParams.inputParams.language}" and "${testParams.inputParams.dateFormat}" `, () => {
2023-11-11 17:12:20 +01:00
expect(
getDateFormat(
testParams.inputParams.dateFormat,
testParams.inputParams.language
2023-11-11 17:12:20 +01:00
)
).toBe(testParams.expectedFormat)
})
})
})
describe('availableDateFormatOptions', () => {
const inputDate = `Sun, 9 Oct 2022 18:18:41 GMT`
const inputTimezone = `Europe/Paris`
const testsParams = [
{
inputLanguage: 'en',
expectedOptions: [
{ label: 'MM/dd/yyyy - 10/09/2022', value: 'MM/dd/yyyy' },
{ label: 'dd/MM/yyyy - 09/10/2022', value: 'dd/MM/yyyy' },
{ label: 'yyyy-MM-dd - 2022-10-09', value: 'yyyy-MM-dd' },
{ label: 'MMM. do, yyyy - Oct. 9th, 2022', value: 'date_string' },
],
},
{
inputLanguage: 'fr',
expectedOptions: [
{ label: 'MM/dd/yyyy - 10/09/2022', value: 'MM/dd/yyyy' },
{ label: 'dd/MM/yyyy - 09/10/2022', value: 'dd/MM/yyyy' },
{ label: 'yyyy-MM-dd - 2022-10-09', value: 'yyyy-MM-dd' },
{ label: 'd MMM yyyy - 9 oct. 2022', value: 'date_string' },
],
},
{
inputLanguage: 'de',
expectedOptions: [
{ label: 'MM/dd/yyyy - 10/09/2022', value: 'MM/dd/yyyy' },
{ label: 'dd/MM/yyyy - 09/10/2022', value: 'dd/MM/yyyy' },
{ label: 'yyyy-MM-dd - 2022-10-09', value: 'yyyy-MM-dd' },
2022-11-01 12:17:20 +01:00
{ label: 'do MMM yyyy - 9. Okt. 2022', value: 'date_string' },
],
},
]
testsParams.map((testParams) => {
it(`returns available options for ${testParams.inputLanguage} locale`, () => {
2023-11-11 17:12:20 +01:00
expect(
availableDateFormatOptions(
inputDate,
inputTimezone,
testParams.inputLanguage
2023-11-11 17:12:20 +01:00
)
).toStrictEqual(testParams.expectedOptions)
})
})
})