when-am-i / files / src / utils / urlState.test.ts
urlState.test.ts
34c4dd59 2026-06-18 Pavel Safronov | 1 | import { describe, it, expect } from 'vitest'; |
631012ab 2026-06-21 Pavel Safronov | 2 | import { parseInitialState, toSearch } from './urlState'; |
34c4dd59 2026-06-18 Pavel Safronov | 3 | |
| 4 | describe('parseInitialState', () => { | |
| 5 | it('parses a full location + date', () => { | |
| 6 | expect(parseInitialState('?lat=42.36&lng=-71.06&year=1700&month=4&day=1')).toEqual({ | |
| 7 | picked: { lat: 42.36, lng: -71.06 }, | |
| 8 | date: { year: 1700, month: 4, day: 1 }, | |
| 9 | }); | |
| 10 | }); | |
| 11 | ||
| 12 | it('maps era=BCE to astronomical numbering', () => { | |
| 13 | expect(parseInitialState('?year=44&month=3&day=15&era=BCE').date).toEqual({ year: -43, month: 3, day: 15 }); | |
| 14 | }); | |
| 15 | ||
| 16 | it('parses an optional zoom', () => { | |
| 17 | expect(parseInitialState('?lat=1&lng=2&zoom=11').zoom).toBe(11); | |
| 18 | expect(parseInitialState('?lat=1&lng=2').zoom).toBeUndefined(); | |
| 19 | }); | |
| 20 | ||
| 21 | it('ignores a partial location', () => { | |
| 22 | expect(parseInitialState('?lat=42.36').picked).toBeUndefined(); | |
| 23 | }); | |
| 24 | ||
| 25 | it('ignores malformed numbers', () => { | |
| 26 | expect(parseInitialState('?lat=abc&lng=2')).toEqual({}); | |
| 27 | }); | |
| 28 | ||
| 29 | it('returns empty for no params', () => { | |
| 30 | expect(parseInitialState('')).toEqual({}); | |
| 31 | }); | |
| 32 | }); | |
631012ab 2026-06-21 Pavel Safronov | 33 | |
| 34 | describe('toSearch', () => { | |
| 35 | it('serializes pin, zoom, and date into the same params parseInitialState reads', () => { | |
| 36 | const search = toSearch({ lat: 42.36, lng: -71.06 }, { year: 1700, month: 4, day: 1, era: 'CE' }, 6); | |
| 37 | const parsed = parseInitialState(search); | |
| 38 | expect(parsed.picked).toEqual({ lat: 42.36, lng: -71.06 }); | |
| 39 | expect(parsed.zoom).toBe(6); | |
| 40 | expect(parsed.date).toEqual({ year: 1700, month: 4, day: 1 }); | |
| 41 | }); | |
| 42 | ||
| 43 | it('round-trips a BCE date', () => { | |
| 44 | const search = toSearch(null, { year: 44, month: 3, day: 15, era: 'BCE' }, 2); | |
| 45 | expect(search).toContain('era=BCE'); | |
| 46 | expect(parseInitialState(search).date).toEqual({ year: -43, month: 3, day: 15 }); | |
| 47 | }); | |
| 48 | ||
| 49 | it('omits the pin when nothing is picked but still records the date', () => { | |
| 50 | const search = toSearch(null, { year: 2000, month: 1, day: 1, era: 'CE' }, 3); | |
| 51 | expect(search).not.toContain('lat='); | |
| 52 | expect(parseInitialState(search).date).toEqual({ year: 2000, month: 1, day: 1 }); | |
| 53 | }); | |
| 54 | }); |