forked from fingerprintjs/fingerprintjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimezone.test.ts
65 lines (54 loc) · 1.89 KB
/
timezone.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { withMockProperties } from '../../tests/utils'
import getTimezone from './timezone'
describe('Sources', () => {
describe('timezone', () => {
it('returns a tz identifier in all supported browsers', () => {
const result = getTimezone()
// Some devices on BrowserStack return 'UTC', some '+00:00'.
if (result === 'UTC' || /^[+-]\d{2}:\d{2}$/.test(String(result))) {
return
}
// We expect all modern browsers (with default settings) to return a TZ identifier,
// like 'Europe/Berlin' or 'America/Argentina/Buenos_Aires'.
expect(result).toMatch(/^([a-z]\/+)?[a-z]+\/[a-z_]+$/gi)
})
describe('fallbacks', () => {
function withNoTimezone(januaryOffset: number, julyOffset: number, action: () => void) {
class MockDate {
args: unknown[]
constructor(...args: unknown[]) {
this.args = args
}
getFullYear() {
return 2024
}
getTimezoneOffset() {
if (this.args[1] === 0) {
return januaryOffset
}
if (this.args[1] === 6) {
return julyOffset
}
throw TypeError('Unexpected constructor arguments')
}
}
return withMockProperties(window, { Date: { value: MockDate }, Intl: undefined }, action)
}
it('in the northern eastern hemisphere (Berlin)', async () => {
await withNoTimezone(1 * -60, 2 * -60, () => {
expect(getTimezone()).toEqual('UTC+60')
})
})
it('in the southern western hemisphere (Santiago)', async () => {
await withNoTimezone(-3 * -60, -4 * -60, () => {
expect(getTimezone()).toEqual('UTC-240')
})
})
it('in the middle', async () => {
await withNoTimezone(0, 0, () => {
expect(getTimezone()).toEqual('UTC+0')
})
})
})
})
})