timezones.test.ts48 lines · main
1import { describe, expect, it } from 'vitest'
2
3import { ALL_TIMEZONES, findTimezoneByIana, TIMEZONES_BY_IANA } from '@/lib/constants/timezones'
4
5describe('TIMEZONES_BY_IANA', () => {
6 it('produces one row per primary IANA name', () => {
7 const ianas = TIMEZONES_BY_IANA.map((entry) => entry.utc[0])
8 expect(new Set(ianas).size).toBe(ianas.length)
9 })
10
11 it('prefers the standard-time row when multiple catalog rows share a primary IANA', () => {
12 // ALL_TIMEZONES has both PDT (isdst: true) and PST (isdst: false) pointing
13 // at America/Los_Angeles. The deduped view should pick the standard one
14 // so the picker label doesn't flip on DST changes.
15 const collisions = ALL_TIMEZONES.filter((entry) => entry.utc[0] === 'America/Los_Angeles')
16 expect(collisions.length).toBeGreaterThan(1)
17
18 const winner = TIMEZONES_BY_IANA.find((entry) => entry.utc[0] === 'America/Los_Angeles')
19 expect(winner).toBeDefined()
20 expect(winner!.isdst).toBe(false)
21 })
22
23 it('preserves entries that have no collision', () => {
24 // Most rows have a unique primary IANA. The UTC catalog row's primary is
25 // 'America/Danmarkshavn' and survives the dedupe pass unchanged.
26 const utcRow = TIMEZONES_BY_IANA.find((entry) => entry.value === 'UTC')
27 expect(utcRow?.text).toContain('Coordinated Universal Time')
28 expect(utcRow?.utc[0]).toBe('America/Danmarkshavn')
29 })
30})
31
32describe('findTimezoneByIana', () => {
33 it('matches the entry by its primary IANA name', () => {
34 const entry = findTimezoneByIana('America/Danmarkshavn')
35 expect(entry?.text).toContain('Coordinated Universal Time')
36 })
37
38 it('matches the entry by any of its secondary IANA names', () => {
39 // 'Asia/Tokyo' is one of the IANA aliases on the JST row whose primary
40 // IANA is 'Asia/Dili'. Lookup must walk the full alias list.
41 const entry = findTimezoneByIana('Asia/Tokyo')
42 expect(entry?.utc).toContain('Asia/Tokyo')
43 })
44
45 it('returns undefined for an unknown IANA name', () => {
46 expect(findTimezoneByIana('Not/A/Real_Zone')).toBeUndefined()
47 })
48})