forked from heyxyz/hey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add cases for date time utils (heyxyz#4282)
- Loading branch information
Showing
10 changed files
with
219 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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 { describe, expect, test } from 'vitest'; | ||
|
||
import formatDate from './formatDate'; | ||
|
||
describe('formatDate', () => { | ||
test('should correctly format a given date in default format', () => { | ||
const exampleDate = new Date('2023-12-01'); | ||
const result = formatDate(exampleDate); | ||
const expectedResult = 'December 1, 2023'; | ||
expect(result).toBe(expectedResult); | ||
}); | ||
|
||
test('should correctly format a given date in a custom format', () => { | ||
const exampleDate = new Date('2023-12-01'); | ||
const result = formatDate(exampleDate, 'YYYY-MM-DD'); | ||
const expectedResult = '2023-12-01'; | ||
expect(result).toBe(expectedResult); | ||
}); | ||
|
||
test('should correctly format the current date in default format', () => { | ||
const result = formatDate(new Date()); | ||
const expectedResult = new Date().toLocaleDateString('en-US', { | ||
day: 'numeric', | ||
month: 'long', | ||
year: 'numeric' | ||
}); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
}); |
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
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,49 @@ | ||
import dayjs from 'dayjs'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import formatRelativeOrAbsolute from './formatRelativeOrAbsolute'; | ||
|
||
describe('formatRelativeOrAbsolute', () => { | ||
test('should format a date a few seconds old as seconds', () => { | ||
const now = new Date(); | ||
const fewSecondsAgo = new Date(now.getTime() - 10 * 1000); | ||
const result = formatRelativeOrAbsolute(fewSecondsAgo); | ||
expect(result).toBe('10s'); | ||
}); | ||
|
||
test('should format a date a few minutes old as minutes', () => { | ||
const now = new Date(); | ||
const fewMinutesAgo = new Date(now.getTime() - 10 * 60 * 1000); | ||
const result = formatRelativeOrAbsolute(fewMinutesAgo); | ||
expect(result).toBe('10m'); | ||
}); | ||
|
||
test('should format a date a few hours old as hours', () => { | ||
const now = new Date(); | ||
const fewHoursAgo = new Date(now.getTime() - 3 * 60 * 60 * 1000); | ||
const result = formatRelativeOrAbsolute(fewHoursAgo); | ||
expect(result).toBe('3h'); | ||
}); | ||
|
||
test('should format a date more than a day but less than a week old as days', () => { | ||
const now = new Date(); | ||
const fourDaysAgo = new Date(now.getTime() - 4 * 24 * 60 * 60 * 1000); | ||
const result = formatRelativeOrAbsolute(fourDaysAgo); | ||
expect(result).toBe('4d'); | ||
}); | ||
|
||
test('should format a date older than a week but within the same year as MMM D', () => { | ||
const now = new Date(); | ||
const lastYear = new Date(now.getFullYear() - 1, 11, 25); | ||
const result = formatRelativeOrAbsolute(lastYear); | ||
const expected = dayjs(lastYear).format('MMM D, YYYY'); | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('should format a date older than a week and in a different year as MMM D, YYYY', () => { | ||
const olderDate = new Date(2020, 0, 1); | ||
const result = formatRelativeOrAbsolute(olderDate); | ||
const expected = dayjs(olderDate).format('MMM D, YYYY'); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
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 dayjs from 'dayjs'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import getNumberOfDaysFromDate from './getNumberOfDaysFromDate'; | ||
|
||
describe('getNumberOfDaysFromDate', () => { | ||
test('should return 0 for the current date', () => { | ||
const currentDate = new Date(); | ||
const result = getNumberOfDaysFromDate(currentDate); | ||
expect(result).toBe(0); | ||
}); | ||
|
||
test('should return 1 for tomorrow', () => { | ||
const tomorrow = dayjs().add(1, 'day').toDate(); | ||
const result = getNumberOfDaysFromDate(tomorrow); | ||
expect(result).toBe(1); | ||
}); | ||
|
||
test('should return -1 for yesterday', () => { | ||
const yesterday = dayjs().subtract(1, 'day').toDate(); | ||
const result = getNumberOfDaysFromDate(yesterday); | ||
expect(result).toBe(-1); | ||
}); | ||
|
||
test('should return correct number of days for a future date', () => { | ||
const futureDate = dayjs().add(5, 'day').toDate(); | ||
const result = getNumberOfDaysFromDate(futureDate); | ||
expect(result).toBe(5); | ||
}); | ||
|
||
test('should return correct number of days for a past date', () => { | ||
const pastDate = dayjs().subtract(3, 'day').toDate(); | ||
const result = getNumberOfDaysFromDate(pastDate); | ||
expect(result).toBe(-3); | ||
}); | ||
|
||
test('should return correct number of days for a date in a different month/year', () => { | ||
const differentMonthYear = new Date(2024, 0, 1); // January 1, 2024 | ||
const daysDifference = dayjs(differentMonthYear) | ||
.startOf('day') | ||
.diff(dayjs().startOf('day'), 'day'); | ||
const result = getNumberOfDaysFromDate(differentMonthYear); | ||
expect(result).toBe(daysDifference); | ||
}); | ||
}); |
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,42 @@ | ||
import dayjs from 'dayjs'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import getTimeAddedNDay from './getTimeAddedNDay'; | ||
|
||
describe('getTimeAddedNDay', () => { | ||
test('should add 0 days to the current date', () => { | ||
const currentDateUTC = dayjs().utc().format(); | ||
const result = getTimeAddedNDay(0); | ||
expect(result).toBe(currentDateUTC); | ||
}); | ||
|
||
test('should add 1 day to the current date', () => { | ||
const oneDayAddedUTC = dayjs().add(1, 'day').utc().format(); | ||
const result = getTimeAddedNDay(1); | ||
expect(result).toBe(oneDayAddedUTC); | ||
}); | ||
|
||
test('should add 5 days to the current date', () => { | ||
const fiveDaysAddedUTC = dayjs().add(5, 'day').utc().format(); | ||
const result = getTimeAddedNDay(5); | ||
expect(result).toBe(fiveDaysAddedUTC); | ||
}); | ||
|
||
test('should subtract 1 day from the current date', () => { | ||
const oneDaySubtractedUTC = dayjs().subtract(1, 'day').utc().format(); | ||
const result = getTimeAddedNDay(-1); | ||
expect(result).toBe(oneDaySubtractedUTC); | ||
}); | ||
|
||
test('should subtract 10 days from the current date', () => { | ||
const tenDaysSubtractedUTC = dayjs().subtract(10, 'day').utc().format(); | ||
const result = getTimeAddedNDay(-10); | ||
expect(result).toBe(tenDaysSubtractedUTC); | ||
}); | ||
|
||
test('should add a large number of days to test leap years and month changes', () => { | ||
const largeDaysAddedUTC = dayjs().add(400, 'day').utc().format(); | ||
const result = getTimeAddedNDay(400); | ||
expect(result).toBe(largeDaysAddedUTC); | ||
}); | ||
}); |
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,48 @@ | ||
import dayjs from 'dayjs'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
import getTimetoNow from './getTimetoNow'; | ||
|
||
describe('getTimetoNow', () => { | ||
test('should format a date a few seconds in the past', () => { | ||
const fewSecondsAgo = dayjs().subtract(10, 'seconds').toDate(); | ||
const result = getTimetoNow(fewSecondsAgo); | ||
expect(result).toBe('a few seconds'); | ||
}); | ||
|
||
test('should format a date a few minutes in the past', () => { | ||
const fewMinutesAgo = dayjs().subtract(2, 'minutes').toDate(); | ||
const result = getTimetoNow(fewMinutesAgo); | ||
expect(result).toBe('2 minutes'); | ||
}); | ||
|
||
test('should format a date a few hours in the past', () => { | ||
const fewHoursAgo = dayjs().subtract(3, 'hours').toDate(); | ||
const result = getTimetoNow(fewHoursAgo); | ||
expect(result).toBe('3 hours'); | ||
}); | ||
|
||
test('should format a date a few days in the past', () => { | ||
const fewDaysAgo = dayjs().subtract(4, 'days').toDate(); | ||
const result = getTimetoNow(fewDaysAgo); | ||
expect(result).toBe('4 days'); | ||
}); | ||
|
||
test('should format a date a few weeks in the past', () => { | ||
const fewWeeksAgo = dayjs().subtract(3, 'weeks').toDate(); | ||
const result = getTimetoNow(fewWeeksAgo); | ||
expect(result).toMatch(/days/); // The exact text may vary | ||
}); | ||
|
||
test('should format a date a few months in the past', () => { | ||
const fewMonthsAgo = dayjs().subtract(5, 'months').toDate(); | ||
const result = getTimetoNow(fewMonthsAgo); | ||
expect(result).toMatch(/months/); // The exact text may vary | ||
}); | ||
|
||
test('should format a date a few years in the past', () => { | ||
const fewYearsAgo = dayjs().subtract(2, 'years').toDate(); | ||
const result = getTimetoNow(fewYearsAgo); | ||
expect(result).toMatch(/years/); // The exact text may vary | ||
}); | ||
}); |