stratoflights-web/tests/unit/settings.test.ts
2026-08-14 00:01:49 +09:00

43 lines
1.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { getPath, setPath, DEFAULT_SETTINGS } from '$features/settings/store';
import { formatAltitude, formatRate, formatCoords, toDms, formatTime } from '$domain';
describe('getPath / setPath', () => {
it('reads a nested value', () => {
expect(getPath(DEFAULT_SETTINGS, 'format.units')).toBe('metric');
expect(getPath(DEFAULT_SETTINGS, 'map.baseLayer')).toBe('osm');
});
it('returns undefined for a missing path', () => {
expect(getPath(DEFAULT_SETTINGS, 'nope.missing')).toBeUndefined();
});
it('sets immutably, leaving the original untouched', () => {
const next = setPath(DEFAULT_SETTINGS, 'format.units', 'imperial');
expect(getPath(next, 'format.units')).toBe('imperial');
expect(DEFAULT_SETTINGS.format.units).toBe('metric');
// Sibling branches are preserved.
expect(getPath(next, 'map.baseLayer')).toBe('osm');
});
});
describe('display formatting', () => {
it('converts altitude and rate for imperial display only', () => {
expect(formatAltitude(1000, 'metric')).toBe('1000 m');
expect(formatAltitude(1000, 'imperial')).toBe('3281 ft');
expect(formatRate(5, 'metric')).toBe('5.0 m/s');
expect(formatRate(5, 'imperial')).toBe('984 ft/min');
});
it('formats coordinates as DD or DMS with hemispheres', () => {
expect(formatCoords(62.5, 129.25, 'dd')).toBe('62.5000, 129.2500');
expect(toDms(62.5, 'lat')).toBe('62°30\'00.00"N');
expect(toDms(-0.1278, 'lon')).toContain('W');
});
it('labels UTC and leaves local unlabelled', () => {
const d = new Date('2026-01-01T07:08:09Z');
expect(formatTime(d, 'utc')).toBe('07:08:09 UTC');
expect(formatTime(d, 'local')).toMatch(/^\d{2}:\d{2}:\d{2}$/);
});
});