when-am-i / files / src / utils / nearestCity.test.ts
nearestCity.test.ts
998b11ce 2026-06-21 Pavel Safronov | 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { nearestCity, countryName } from './nearestCity'; | |
| 3 | ||
| 4 | describe('countryName', () => { | |
| 5 | it('resolves ISO codes to English names', () => { | |
| 6 | expect(countryName('US')).toBe('United States'); | |
| 7 | expect(countryName('jp')).toBe('Japan'); // case-insensitive | |
| 8 | }); | |
| 9 | ||
| 10 | it('falls back to the raw input for a malformed code', () => { | |
| 11 | expect(countryName('99')).toBe('99'); // not a valid region code → throws → echoed back | |
| 12 | }); | |
| 13 | }); | |
| 14 | ||
| 15 | describe('nearestCity', () => { | |
| 16 | it('finds the city at a well-known coordinate', () => { | |
| 17 | const boston = nearestCity(42.36, -71.06); | |
| 18 | expect(boston?.name).toBe('Boston'); | |
| 19 | expect(boston?.country).toBe('United States'); | |
| 20 | expect(boston?.distanceKm).toBeLessThan(10); | |
| 21 | }); | |
| 22 | ||
| 23 | it('works across hemispheres', () => { | |
| 24 | expect(nearestCity(35.69, 139.69)?.country).toBe('Japan'); // Tokyo | |
| 25 | expect(nearestCity(51.5, -0.12)?.country).toBe('United Kingdom'); // London | |
| 26 | }); | |
| 27 | ||
| 28 | it('returns the nearest large city (with distance) for a rural point', () => { | |
| 29 | const c = nearestCity(9.0, 40.0); // Ethiopian highlands | |
| 30 | expect(c?.country).toBe('Ethiopia'); | |
| 31 | expect(c?.distanceKm).toBeGreaterThan(0); | |
| 32 | }); | |
| 33 | }); |