weather_app/tests/lib/router.test.js
hermes-explorigin 36d3625d61 Make tabs hash-routable
- New src/lib/router.js: parseHash/ viewToHash for the 5 tab views, with a
  default fallback for empty/unknown hashes.
- App.svelte: initialize viewMode from location.hash, setView() updates both
  state and the URL hash, and a guarded hashchange listener syncs back/forward
  and manual URL edits. All tab onclicks route through setView().
- Tests for parse/viewToHash round-trips and fallback. 76 total pass.
2026-08-27 00:41:05 +00:00

35 lines
1.1 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import { parseHash, viewToHash, VALID_VIEWS, DEFAULT_VIEW } from '../../src/lib/router.js'
describe('parseHash', () => {
it('parses a standard hash route', () => {
expect(parseHash('#/hourly')).toBe('hourly')
expect(parseHash('#/radar')).toBe('radar')
})
it('handles a bare segment without a leading slash', () => {
expect(parseHash('daily')).toBe('daily')
expect(parseHash('#daily')).toBe('daily')
})
it('falls back to the default view for empty or unknown hashes', () => {
expect(parseHash('')).toBe(DEFAULT_VIEW)
expect(parseHash('#')).toBe(DEFAULT_VIEW)
expect(parseHash('#/nope')).toBe(DEFAULT_VIEW)
expect(parseHash('#/current')).toBe('current')
})
it('ignores a trailing path after the view segment', () => {
expect(parseHash('#/detail/sub')).toBe('detail')
})
})
describe('viewToHash', () => {
it('round-trips a valid view', () => {
for (const v of VALID_VIEWS) {
expect(viewToHash(v)).toBe(`#/${v}`)
expect(parseHash(viewToHash(v))).toBe(v)
}
})
})