Skip to content

Commit

Permalink
test: date utils in the most extreme timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
LinHuiqing committed May 16, 2023
1 parent 9894d97 commit 5eaa50a
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions frontend/src/utils/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import timezoneMock from 'timezone-mock'

import * as DateUtils from './date'

describe('date', () => {
Expand Down Expand Up @@ -76,25 +78,52 @@ describe('date', () => {
})

describe('normalizeDateToUtc', () => {
beforeEach(() => {
timezoneMock.unregister()
})
it('should convert local dates to UTC', () => {
// We can only test using this function in different system times by
// manually changing system time and rerunning this test

const dateString = '2023-04-23T00:00:00'
const localDate = new Date(Date.parse(dateString))
const utcDate = new Date(Date.parse(`${dateString}+00:00`))

const result = DateUtils.normalizeDateToUtc(localDate)

expect(result).toStrictEqual(utcDate)
})
it('should convert negative UTC (local) dates to UTC', () => {
const dateString = '2023-04-23T00:00:00'

// First, create Date value for UTC
timezoneMock.register('UTC')
const utcDate = new Date(Date.parse(dateString))

// Simulate 'local' timezone when users are in UTC-12
timezoneMock.register('Etc/GMT+12')
const negativeUtcDate = new Date(Date.parse(dateString))

const result = DateUtils.normalizeDateToUtc(negativeUtcDate)

expect(result).toStrictEqual(utcDate)
})
it('should convert positive UTC (local) dates to UTC', () => {
const dateString = '2023-04-23T00:00:00'

// First, create Date value for UTC
timezoneMock.register('UTC')
const utcDate = new Date(Date.parse(dateString))

// Simulate 'local' timezone when users are in UTC+14
timezoneMock.register('Etc/GMT-14')
const positiveUtcDate = new Date(Date.parse(dateString))

const result = DateUtils.normalizeDateToUtc(positiveUtcDate)

expect(result).toStrictEqual(utcDate)
})
})

describe('loadDateFromNormalizedDate', () => {
it('should convert normalised (UTC) dates to local date', () => {
// We can only test using this function in different system times by
// manually changing system time and rerunning this test

const dateString = '2023-04-23T00:00:00'
const utcDate = new Date(Date.parse(`${dateString}+00:00`))
const localDate = new Date(Date.parse(dateString))
Expand All @@ -103,5 +132,35 @@ describe('date', () => {

expect(result).toStrictEqual(localDate)
})
it('should convert normalised (UTC) dates to negative UTC (local) date', () => {
const dateString = '2023-04-23T00:00:00'

// First, create Date value for UTC
timezoneMock.register('UTC')
const utcDate = new Date(Date.parse(dateString))

// Simulate 'local' timezone when users are in UTC-12
timezoneMock.register('Etc/GMT+12')
const negativeUtcDate = new Date(Date.parse(dateString))

const result = DateUtils.loadDateFromNormalizedDate(utcDate)

expect(result).toStrictEqual(negativeUtcDate)
})
it('should convert normalised (UTC) dates to positive UTC (local) date', () => {
const dateString = '2023-04-23T00:00:00'

// First, create Date value for UTC
timezoneMock.register('UTC')
const utcDate = new Date(Date.parse(dateString))

// Simulate 'local' timezone when users are in UTC+14
timezoneMock.register('Etc/GMT-14')
const positiveUtcDate = new Date(Date.parse(dateString))

const result = DateUtils.loadDateFromNormalizedDate(utcDate)

expect(result).toStrictEqual(positiveUtcDate)
})
})
})

0 comments on commit 5eaa50a

Please sign in to comment.