| 1 |
import { describe, expect, it, vi } from "vitest"; |
| 2 |
import { Prefs, DATE_FORMAT, TIME_FORMAT, PREF_DEFAULTS } from "./Prefs"; |
| 3 |
|
| 4 |
// The module instantiates `new Prefs(db())` at import; stub db() so that doesn't touch IndexedDB. |
| 5 |
vi.mock("./db", () => ({ default: () => ({}) })); |
| 6 |
|
| 7 |
// Minimal fake of the Dexie `prefs` table: get() returns a stored {key, value} or undefined. |
| 8 |
const fakeDb = (stored) => ({ |
| 9 |
prefs: { |
| 10 |
get: async (key) => (key in stored ? { key, value: stored[key] } : undefined), |
| 11 |
put: async () => {}, |
| 12 |
}, |
| 13 |
}); |
| 14 |
|
| 15 |
describe("Prefs date/time format validation", () => { |
| 16 |
it("returns the stored value when it is a known format", async () => { |
| 17 |
const prefs = new Prefs(fakeDb({ dateFormat: DATE_FORMAT.ISO8601, timeFormat: TIME_FORMAT.H24 })); |
| 18 |
expect(await prefs.dateFormat()).toBe(DATE_FORMAT.ISO8601); |
| 19 |
expect(await prefs.timeFormat()).toBe(TIME_FORMAT.H24); |
| 20 |
}); |
| 21 |
|
| 22 |
it("falls back to the default for an unknown stored value (e.g. synced from a newer client)", async () => { |
| 23 |
const prefs = new Prefs(fakeDb({ dateFormat: "newer-client-format", timeFormat: "36h" })); |
| 24 |
expect(await prefs.dateFormat()).toBe(PREF_DEFAULTS.dateFormat); |
| 25 |
expect(await prefs.timeFormat()).toBe(PREF_DEFAULTS.timeFormat); |
| 26 |
}); |
| 27 |
|
| 28 |
it("falls back to the default when unset", async () => { |
| 29 |
const prefs = new Prefs(fakeDb({})); |
| 30 |
expect(await prefs.dateFormat()).toBe(PREF_DEFAULTS.dateFormat); |
| 31 |
expect(await prefs.timeFormat()).toBe(PREF_DEFAULTS.timeFormat); |
| 32 |
}); |
| 33 |
}); |