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 { StyleStore } from "./stylestore";
// StyleStore reads/writes window.localStorage; provide a minimal in-memory mock.
class LocalStorageMock {
private store: Record<string, string> = {};
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();