From 89be9544859f6e52216e189084bba078a8f64510 Mon Sep 17 00:00:00 2001 From: HarelM Date: Sun, 12 Jul 2026 11:08:54 +0300 Subject: [PATCH] Improve unit test. --- src/libs/store/stylestore.test.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/libs/store/stylestore.test.ts b/src/libs/store/stylestore.test.ts index 5fbc22cf..f2b718ab 100644 --- a/src/libs/store/stylestore.test.ts +++ b/src/libs/store/stylestore.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; +import { StyleStore } from "./stylestore"; -// StyleStore reads/writes window.localStorage; provide a minimal in-memory mock. class LocalStorageMock { private store: Record = {}; get length() { @@ -22,15 +22,14 @@ class LocalStorageMock { this.store = {}; } } - vi.stubGlobal("window", { localStorage: new LocalStorageMock() }); - -// Avoid network in loadDefaultStyle. -vi.mock("../urlopen", () => ({ - loadStyleUrl: vi.fn(async () => ({ version: 8, id: "default", sources: {}, layers: [] })), -})); - -import { StyleStore } from "./stylestore"; +// loadDefaultStyle fetches the default style over the network; serve it locally. +vi.stubGlobal( + "fetch", + vi.fn(async () => ({ + json: async () => ({ version: 8, id: "default", sources: {}, layers: [] }), + })) +); const style = (id: string) => ({ version: 8, id, sources: {}, layers: [] }) as any; @@ -47,7 +46,7 @@ describe("StyleStore", () => { it("saves a style and reads it back as the latest", async () => { const store = new StyleStore(); - await store.save(style("abc")); + store.save(style("abc")); // A fresh store discovers the persisted style ids. const reopened = new StyleStore(); const latest = await reopened.getLatestStyle(); @@ -56,7 +55,7 @@ describe("StyleStore", () => { it("purge removes all maputnik keys", async () => { const store = new StyleStore(); - await store.save(style("abc")); + store.save(style("abc")); window.localStorage.setItem("unrelated", "keep"); store.purge(); expect(window.localStorage.getItem("maputnik:style:abc")).toBeNull();