Skip to content

Commit

Permalink
Test formatDistanceToNow
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Jun 20, 2023
1 parent 07c42d3 commit 1de5ce9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/568.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test formatDistanceToNow
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formatDistanceToNow should format future dates 1`] = `
[
"1 min",
"1 min",
"20 mins",
"1 hr",
"1 days",
"1 mos",
"1 yr",
"8 mos",
]
`;

exports[`formatDistanceToNow should format past dates 1`] = `
[
"1 min",
"1 min",
"20 mins",
"1 hr",
"1 days",
"1 mos",
"1 yr",
"8 mos",
]
`;
38 changes: 38 additions & 0 deletions src/app/utils/__tests__/formatDistanceToNow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { formatDistanceToNow } from '../dateFormatter'

describe('formatDistanceToNow', () => {
beforeEach(() => {
jest.useFakeTimers()
jest.setSystemTime(new Date('2023-01-01T01:01:01Z'))
})

afterEach(() => {
jest.useRealTimers()
})

it('should format future dates', () => {
expect([
formatDistanceToNow(new Date('2023-01-01T01:01:01Z')),
formatDistanceToNow(new Date('2023-01-01T01:01:21Z')),
formatDistanceToNow(new Date('2023-01-01T01:21:21Z')),
formatDistanceToNow(new Date('2023-01-01T02:01:01Z')),
formatDistanceToNow(new Date('2023-01-02T01:01:01Z')),
formatDistanceToNow(new Date('2023-02-01T01:01:01Z')),
formatDistanceToNow(new Date('2024-01-01T01:01:01Z')),
formatDistanceToNow(new Date('2023-08-23T12:20:56Z')),
]).toMatchSnapshot()
})

it('should format past dates', () => {
expect([
formatDistanceToNow(new Date('2023-01-01T01:01:01Z')),
formatDistanceToNow(new Date('2023-01-01T01:00:41Z')),
formatDistanceToNow(new Date('2023-01-01T00:41:01Z')),
formatDistanceToNow(new Date('2023-01-01T00:01:01Z')),
formatDistanceToNow(new Date('2022-12-31T01:01:01Z')),
formatDistanceToNow(new Date('2022-12-01T01:01:01Z')),
formatDistanceToNow(new Date('2022-01-01T01:01:01Z')),
formatDistanceToNow(new Date('2022-04-23T12:20:56Z')),
]).toMatchSnapshot()
})
})

0 comments on commit 1de5ce9

Please sign in to comment.