timezones.test.ts 2.0 KB

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