|
| 1 | +import { renderHook } from '@testing-library/react-hooks' |
| 2 | +import React, { ReactNode } from 'react' |
| 3 | +import { ConfigProvider, useTimeZoneConversion } from '../index' |
| 4 | + |
| 5 | +const defaultConfig = { baseUrl: '/', apiVersion: 40 } |
| 6 | +const defaultSystemInfo = { |
| 7 | + version: '40', |
| 8 | + contextPath: '', |
| 9 | + serverTimeZoneId: 'UTC', |
| 10 | +} |
| 11 | + |
| 12 | +// tests are set to run at UTC when running yarn test |
| 13 | + |
| 14 | +describe('useTimeZoneConversion', () => { |
| 15 | + it('Hook returns a fromClientDate and fromServerDate function', () => { |
| 16 | + const config = { baseUrl: '/', apiVersion: 30 } |
| 17 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 18 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 19 | + ) |
| 20 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 21 | + wrapper, |
| 22 | + }) |
| 23 | + |
| 24 | + expect(result.current).toHaveProperty('fromClientDate') |
| 25 | + expect(typeof result.current.fromClientDate).toBe('function') |
| 26 | + expect(result.current).toHaveProperty('fromServerDate') |
| 27 | + expect(typeof result.current.fromServerDate).toBe('function') |
| 28 | + }) |
| 29 | + |
| 30 | + it('returns fromServerDate that corrects for server time zone', () => { |
| 31 | + const systemInfo = { |
| 32 | + ...defaultSystemInfo, |
| 33 | + serverTimeZoneId: 'Europe/Oslo', |
| 34 | + } |
| 35 | + const config = { ...defaultConfig, systemInfo } |
| 36 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 37 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 38 | + ) |
| 39 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 40 | + wrapper, |
| 41 | + }) |
| 42 | + |
| 43 | + const serverDate = result.current.fromServerDate('2010-01-01') |
| 44 | + const expectedDateString = '2009-12-31T23:00:00.000' |
| 45 | + expect(serverDate.getClientZonedISOString()).toBe(expectedDateString) |
| 46 | + }) |
| 47 | + |
| 48 | + // fromServerDate accepts number, valid date string, or date object |
| 49 | + it('returns fromServerDate which accepts number, valid date string, or date object', () => { |
| 50 | + const config = { ...defaultConfig, systemInfo: defaultSystemInfo } |
| 51 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 52 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 53 | + ) |
| 54 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 55 | + wrapper, |
| 56 | + }) |
| 57 | + |
| 58 | + const dateString = '2010-01-01' |
| 59 | + const dateFromString = new Date('2010-01-01') |
| 60 | + const millisecondsAfterUTC = dateFromString.getTime() |
| 61 | + |
| 62 | + const serverDateFromString = result.current.fromServerDate(dateString) |
| 63 | + const serverDateFromDate = result.current.fromServerDate(dateFromString) |
| 64 | + const serverDateFromNumber = |
| 65 | + result.current.fromServerDate(millisecondsAfterUTC) |
| 66 | + |
| 67 | + expect(serverDateFromString).toEqual(serverDateFromDate) |
| 68 | + expect(serverDateFromString).toEqual(serverDateFromNumber) |
| 69 | + }) |
| 70 | + |
| 71 | + // returns current (client) date if no argument is provided |
| 72 | + it('returns fromServerDate which returns current timestamp if no argument is passed', () => { |
| 73 | + const config = { ...defaultConfig, systemInfo: defaultSystemInfo } |
| 74 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 75 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 76 | + ) |
| 77 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 78 | + wrapper, |
| 79 | + }) |
| 80 | + |
| 81 | + // if no date-like is passed to fromSeverDate, Date.now() is used to initialize date |
| 82 | + jest.spyOn(global.Date, 'now').mockImplementation(() => |
| 83 | + new Date('2020-10-15T12:00:00.000Z').valueOf() |
| 84 | + ) |
| 85 | + |
| 86 | + const timeFromHook = result.current.fromServerDate() |
| 87 | + |
| 88 | + expect(timeFromHook).toEqual(new Date('2020-10-15T12:00:00.000Z')) |
| 89 | + }) |
| 90 | + |
| 91 | + // fromServerDate defaults to client time zone if invalid server time zone provided |
| 92 | + it('returns fromServerDate that assumes no time zone difference if provided time zone is invalid', () => { |
| 93 | + const systemInfo = { |
| 94 | + ...defaultSystemInfo, |
| 95 | + serverTimeZoneId: 'Asia/Oslo', |
| 96 | + } |
| 97 | + const config = { ...defaultConfig, systemInfo } |
| 98 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 99 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 100 | + ) |
| 101 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 102 | + wrapper, |
| 103 | + }) |
| 104 | + |
| 105 | + const serverDate = result.current.fromServerDate('2010-01-01') |
| 106 | + const expectedDateString = '2010-01-01T00:00:00.000' |
| 107 | + expect(serverDate.getClientZonedISOString()).toBe(expectedDateString) |
| 108 | + }) |
| 109 | + |
| 110 | + it('returns fromServerDate with server date that matches passed time regardless of timezone', () => { |
| 111 | + const systemInfo = { |
| 112 | + ...defaultSystemInfo, |
| 113 | + serverTimeZoneId: 'Asia/Jakarta', |
| 114 | + } |
| 115 | + const config = { ...defaultConfig, systemInfo } |
| 116 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 117 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 118 | + ) |
| 119 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 120 | + wrapper, |
| 121 | + }) |
| 122 | + |
| 123 | + const serverDate = result.current.fromServerDate('2015-03-03T12:00:00') |
| 124 | + const expectedDateString = '2015-03-03T12:00:00.000' |
| 125 | + expect(serverDate.getServerZonedISOString()).toBe(expectedDateString) |
| 126 | + }) |
| 127 | + |
| 128 | + it('returns fromClientDate that reflects client time but makes server time string accessible', () => { |
| 129 | + const systemInfo = { |
| 130 | + ...defaultSystemInfo, |
| 131 | + serverTimeZoneId: 'America/Guatemala', |
| 132 | + } |
| 133 | + const config = { ...defaultConfig, systemInfo } |
| 134 | + const wrapper = ({ children }: { children?: ReactNode }) => ( |
| 135 | + <ConfigProvider config={config}>{children}</ConfigProvider> |
| 136 | + ) |
| 137 | + const { result } = renderHook(() => useTimeZoneConversion(), { |
| 138 | + wrapper, |
| 139 | + }) |
| 140 | + |
| 141 | + const serverDate = result.current.fromClientDate('2018-08-15T12:00:00') |
| 142 | + const expectedClientDateString = '2018-08-15T12:00:00.000' |
| 143 | + const expectedServerDateString = '2018-08-15T06:00:00.000' |
| 144 | + const javascriptDate = new Date('2018-08-15T12:00:00') |
| 145 | + expect(serverDate.getClientZonedISOString()).toBe( |
| 146 | + expectedClientDateString |
| 147 | + ) |
| 148 | + expect(serverDate.getServerZonedISOString()).toBe( |
| 149 | + expectedServerDateString |
| 150 | + ) |
| 151 | + expect(serverDate.getTime()).toEqual(javascriptDate.getTime()) |
| 152 | + }) |
| 153 | +}) |
0 commit comments