Skip to content

Commit

Permalink
Merge pull request #568 from oasisprotocol/lw/test-date
Browse files Browse the repository at this point in the history
Test formatDistanceToNow
  • Loading branch information
lukaw3d authored Jun 21, 2023
2 parents 2b4168f + 04e451b commit e7a1e3a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 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
Expand Up @@ -6,7 +6,7 @@ import { suggestedParsedBlock } from '../../../utils/test-fixtures'
describe('BlockDetailView', () => {
beforeEach(() => {
jest.useFakeTimers()
jest.setSystemTime(new Date(2023, 1, 1))
jest.setSystemTime(new Date('2023-01-01T01:01:01Z'))
})

afterEach(() => {
Expand All @@ -16,7 +16,7 @@ describe('BlockDetailView', () => {
it('should display formatted values', () => {
renderWithProviders(<BlockDetailView isLoading={false} block={suggestedParsedBlock} />)
expect(screen.getByText('1,396,255')).toBeInTheDocument()
expect(screen.getByText('May 13, 2022 at 6:39 AM UTC (9 months ago)')).toBeInTheDocument()
expect(screen.getByText('May 13, 2022 at 6:39 AM UTC (8 months ago)')).toBeInTheDocument()
expect(screen.getByText('4,214 bytes')).toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SearchResultsList } from '../SearchResultsList'
describe('SearchResultsView', () => {
beforeEach(() => {
jest.useFakeTimers()
jest.setSystemTime(new Date(2023, 1, 1))
jest.setSystemTime(new Date('2023-01-01T01:01:01Z'))
})

afterEach(() => {
Expand Down
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 e7a1e3a

Please sign in to comment.