Improve unit test.

This commit is contained in:
HarelM
2026-07-12 11:08:54 +03:00
parent 83c099bef4
commit 89be954485
+10 -11
View File
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach, vi } from "vitest"; 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 { class LocalStorageMock {
private store: Record<string, string> = {}; private store: Record<string, string> = {};
get length() { get length() {
@@ -22,15 +22,14 @@ class LocalStorageMock {
this.store = {}; this.store = {};
} }
} }
vi.stubGlobal("window", { localStorage: new LocalStorageMock() }); vi.stubGlobal("window", { localStorage: new LocalStorageMock() });
// loadDefaultStyle fetches the default style over the network; serve it locally.
// Avoid network in loadDefaultStyle. vi.stubGlobal(
vi.mock("../urlopen", () => ({ "fetch",
loadStyleUrl: vi.fn(async () => ({ version: 8, id: "default", sources: {}, layers: [] })), vi.fn(async () => ({
})); json: async () => ({ version: 8, id: "default", sources: {}, layers: [] }),
}))
import { StyleStore } from "./stylestore"; );
const style = (id: string) => ({ version: 8, id, sources: {}, layers: [] }) as any; 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 () => { it("saves a style and reads it back as the latest", async () => {
const store = new StyleStore(); const store = new StyleStore();
await store.save(style("abc")); store.save(style("abc"));
// A fresh store discovers the persisted style ids. // A fresh store discovers the persisted style ids.
const reopened = new StyleStore(); const reopened = new StyleStore();
const latest = await reopened.getLatestStyle(); const latest = await reopened.getLatestStyle();
@@ -56,7 +55,7 @@ describe("StyleStore", () => {
it("purge removes all maputnik keys", async () => { it("purge removes all maputnik keys", async () => {
const store = new StyleStore(); const store = new StyleStore();
await store.save(style("abc")); store.save(style("abc"));
window.localStorage.setItem("unrelated", "keep"); window.localStorage.setItem("unrelated", "keep");
store.purge(); store.purge();
expect(window.localStorage.getItem("maputnik:style:abc")).toBeNull(); expect(window.localStorage.getItem("maputnik:style:abc")).toBeNull();