mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
07eeae8486
An Etc/GMT zone counts westward as positive, so Etc/GMT-4 is four hours ahead of UTC. The clock's title and the zone picker printed the id's own sign, and someone who picked GMT+4 got a clock four hours behind UTC; both now read the real offset, on the desktop and the phone. Closes #3175
140 lines
6.0 KiB
TypeScript
140 lines
6.0 KiB
TypeScript
// FE-COMP-TZHOOK-001 to -009: the world clocks both dashboards share.
|
|
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
|
|
import { useSettingsStore } from '../../store/settingsStore';
|
|
import { shortZone, useDashboardTimezones, zoneOptionLabel } from './useDashboardTimezones';
|
|
|
|
const updateSetting = vi.fn<(key: string, value: unknown) => Promise<void>>();
|
|
const home = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
|
|
function seedSettings(zones: string[] | undefined, isLoaded = false) {
|
|
useSettingsStore.setState((s) => ({
|
|
isLoaded,
|
|
updateSetting: updateSetting as unknown as typeof s.updateSetting,
|
|
settings: { ...s.settings, dashboard_timezones: zones } as typeof s.settings,
|
|
}));
|
|
}
|
|
|
|
beforeEach(() => {
|
|
updateSetting.mockReset().mockResolvedValue(undefined);
|
|
localStorage.clear();
|
|
seedSettings(undefined);
|
|
});
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
localStorage.clear();
|
|
});
|
|
|
|
describe('shortZone', () => {
|
|
it('FE-COMP-TZHOOK-001: keeps the city and spaces out underscores', () => {
|
|
expect(shortZone('America/New_York')).toBe('New York');
|
|
expect(shortZone('UTC')).toBe('UTC');
|
|
});
|
|
});
|
|
|
|
describe('Etc/GMT zones (#3175)', () => {
|
|
it('FE-COMP-TZHOOK-010: names an Etc/GMT zone by its real offset, which is the opposite of the sign in its id', () => {
|
|
expect(shortZone('Etc/GMT-4')).toBe('GMT+4');
|
|
expect(shortZone('Etc/GMT+4')).toBe('GMT-4');
|
|
expect(shortZone('Etc/GMT-14')).toBe('GMT+14');
|
|
expect(shortZone('Etc/GMT+0')).toBe('GMT');
|
|
expect(shortZone('Etc/UTC')).toBe('UTC');
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-011: the picker lists an Etc/GMT zone under its real offset and every other zone by its id', () => {
|
|
expect(zoneOptionLabel('Etc/GMT-4')).toBe('GMT+4');
|
|
expect(zoneOptionLabel('Etc/GMT+4')).toBe('GMT-4');
|
|
expect(zoneOptionLabel('America/New_York')).toBe('America/New York');
|
|
});
|
|
});
|
|
|
|
describe('useDashboardTimezones', () => {
|
|
it('FE-COMP-TZHOOK-002: an unset list falls back to home plus the defaults, a stored one is honoured', () => {
|
|
const fallback = renderHook(() => useDashboardTimezones('en'));
|
|
expect(fallback.result.current.zones).toEqual([home, 'Europe/London', 'Asia/Tokyo']);
|
|
|
|
seedSettings([]);
|
|
const empty = renderHook(() => useDashboardTimezones('en'));
|
|
expect(empty.result.current.zones).toEqual([]);
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-003: adding a new zone writes the list and closes the picker, a known one only closes it', () => {
|
|
seedSettings(['UTC']);
|
|
const { result } = renderHook(() => useDashboardTimezones('en'));
|
|
act(() => result.current.setAdding(true));
|
|
act(() => result.current.addZone('Asia/Tokyo'));
|
|
expect(updateSetting).toHaveBeenCalledWith('dashboard_timezones', ['UTC', 'Asia/Tokyo']);
|
|
expect(result.current.adding).toBe(false);
|
|
|
|
updateSetting.mockClear();
|
|
act(() => result.current.setAdding(true));
|
|
act(() => result.current.addZone('UTC'));
|
|
expect(updateSetting).not.toHaveBeenCalled();
|
|
expect(result.current.adding).toBe(false);
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-004: removing drops the zone from the stored list', () => {
|
|
seedSettings(['UTC', 'Asia/Tokyo']);
|
|
const { result } = renderHook(() => useDashboardTimezones('en'));
|
|
act(() => result.current.removeZone('UTC'));
|
|
expect(updateSetting).toHaveBeenCalledWith('dashboard_timezones', ['Asia/Tokyo']);
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-005: the clocks read the zone time and tick every 30 seconds', () => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date('2026-01-15T12:00:00Z'));
|
|
seedSettings(['UTC']);
|
|
const { result } = renderHook(() => useDashboardTimezones('en-GB'));
|
|
expect(result.current.timeIn('UTC')).toBe('12:00');
|
|
expect(result.current.timeIn('Asia/Tokyo')).toBe('21:00');
|
|
expect(result.current.offsetLabel('UTC')).toBe('UTC');
|
|
act(() => vi.advanceTimersByTime(30000));
|
|
expect(result.current.timeIn('UTC')).toBe('12:00');
|
|
act(() => vi.advanceTimersByTime(30000));
|
|
expect(result.current.timeIn('UTC')).toBe('12:01');
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-006: once settings are loaded, an old localStorage list moves into them', async () => {
|
|
localStorage.setItem('trek_dashboard_tz', JSON.stringify(['Europe/Paris']));
|
|
seedSettings(undefined, true);
|
|
renderHook(() => useDashboardTimezones('en'));
|
|
expect(updateSetting).toHaveBeenCalledWith('dashboard_timezones', ['Europe/Paris']);
|
|
await waitFor(() => expect(localStorage.getItem('trek_dashboard_tz')).toBeNull());
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-007: a malformed localStorage value is dropped without a write', () => {
|
|
localStorage.setItem('trek_dashboard_tz', '{not json');
|
|
seedSettings(undefined, true);
|
|
renderHook(() => useDashboardTimezones('en'));
|
|
expect(localStorage.getItem('trek_dashboard_tz')).toBeNull();
|
|
|
|
localStorage.setItem('trek_dashboard_tz', JSON.stringify({ zone: 'UTC' }));
|
|
renderHook(() => useDashboardTimezones('en'));
|
|
expect(localStorage.getItem('trek_dashboard_tz')).toBeNull();
|
|
expect(updateSetting).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// #2694: Chrome on Windows names no zone for a system zone it cannot map.
|
|
it('FE-COMP-TZHOOK-008: a browser that names no zone still gets the default clocks', () => {
|
|
const original = Intl.DateTimeFormat.prototype.resolvedOptions;
|
|
const spy = vi.spyOn(Intl.DateTimeFormat.prototype, 'resolvedOptions').mockImplementation(function (
|
|
this: Intl.DateTimeFormat
|
|
) {
|
|
return { ...original.call(this), timeZone: undefined as unknown as string };
|
|
});
|
|
try {
|
|
const { result } = renderHook(() => useDashboardTimezones('en'));
|
|
expect(result.current.zones).toEqual(['Europe/London', 'Asia/Tokyo']);
|
|
expect(() => result.current.zones.map((tz) => result.current.timeIn(tz))).not.toThrow();
|
|
} finally {
|
|
spy.mockRestore();
|
|
}
|
|
});
|
|
|
|
it('FE-COMP-TZHOOK-009: a stored zone this browser does not know is left out instead of crashing the clocks', () => {
|
|
seedSettings(['Europe/Berlin', 'Mars/Olympus_Mons', '']);
|
|
const { result } = renderHook(() => useDashboardTimezones('en'));
|
|
expect(result.current.zones).toEqual(['Europe/Berlin']);
|
|
});
|
|
});
|