diff options
| author | netop://ウィビ <paul@webb.page> | 2026-04-26 20:18:30 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-04-26 20:18:30 -0700 |
| commit | 3c06c95f396b6e911076bc3291d5855ed01b5caa (patch) | |
| tree | 17cd218339c52fbeee93d931303b04a3ff294f8b /tests/storage.test.ts | |
| parent | f059d97ab7f6d74d61139ac698cb871be7cb632e (diff) | |
| download | graphiql-3c06c95f396b6e911076bc3291d5855ed01b5caa.tar.gz graphiql-3c06c95f396b6e911076bc3291d5855ed01b5caa.zip | |
cleanup and ready for launch
Diffstat (limited to 'tests/storage.test.ts')
| -rw-r--r-- | tests/storage.test.ts | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/tests/storage.test.ts b/tests/storage.test.ts index 434a67d..3fbbdd9 100644 --- a/tests/storage.test.ts +++ b/tests/storage.test.ts @@ -13,47 +13,11 @@ import { createMemoryStorage } from "../source/library/state/storage.ts"; -/*** HELPERS ------------------------------------------ ***/ - -function installLocalStorage(): void { - if (typeof globalThis.localStorage !== "undefined") - return; - - const store = new Map<string, string>(); - - const shim: Storage = { - clear(): void { - store.clear(); - }, - getItem(key: string): string | null { - return store.has(key) ? store.get(key) ?? null : null; - }, - key(index: number): string | null { - return Array.from(store.keys())[index] ?? null; - }, - get length(): number { - return store.size; - }, - removeItem(key: string): void { - store.delete(key); - }, - setItem(key: string, value: string): void { - store.set(key, String(value)); - } - }; - - Object.defineProperty(globalThis, "localStorage", { - configurable: true, - value: shim, - writable: true - }); -} - beforeAll(() => { installLocalStorage(); }); -/*** TESTS -------------------------------------------- ***/ +/*** PROGRAM ------------------------------------------ ***/ test("memory storage round-trips objects", () => { const storage = createMemoryStorage(); @@ -68,15 +32,19 @@ test("memory storage returns null for missing keys", () => { test("memory storage remove clears a key", () => { const storage = createMemoryStorage(); + storage.set("k", 42); storage.remove("k"); + expect(storage.get("k")).toEqual(null); }); test("memory storage instances are isolated", () => { const a = createMemoryStorage(); const b = createMemoryStorage(); + a.set("shared", 1); + expect(b.get("shared")).toEqual(null); }); @@ -105,8 +73,8 @@ test("local storage remove respects the namespace", () => { alpha.set("k", 1); beta.set("k", 2); - alpha.remove("k"); + expect(alpha.get("k")).toEqual(null); expect(beta.get<number>("k")).toEqual(2); @@ -122,3 +90,39 @@ test("local storage returns null on malformed JSON", () => { globalThis.localStorage.clear(); }); + +/*** HELPER ------------------------------------------- ***/ + +function installLocalStorage(): void { + if (typeof globalThis.localStorage !== "undefined") + return; + + const store = new Map<string, string>(); + + const shim: Storage = { + clear(): void { + store.clear(); + }, + getItem(key: string): string | null { + return store.has(key) ? store.get(key) ?? null : null; + }, + key(index: number): string | null { + return Array.from(store.keys())[index] ?? null; + }, + get length(): number { + return store.size; + }, + removeItem(key: string): void { + store.delete(key); + }, + setItem(key: string, value: string): void { + store.set(key, String(value)); + } + }; + + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: shim, + writable: true + }); +} |