aboutsummaryrefslogtreecommitdiff
path: root/tests/storage.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/storage.test.ts')
-rw-r--r--tests/storage.test.ts79
1 files changed, 60 insertions, 19 deletions
diff --git a/tests/storage.test.ts b/tests/storage.test.ts
index 7d6ba73..434a67d 100644
--- a/tests/storage.test.ts
+++ b/tests/storage.test.ts
@@ -1,9 +1,10 @@
+
/*** IMPORT ------------------------------------------- ***/
-import { assertEquals } from "jsr:@std/assert@^1.0.0";
+import { beforeAll, expect, test } from "vitest";
/*** UTILITY ------------------------------------------ ***/
@@ -12,34 +13,74 @@ 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 -------------------------------------------- ***/
-Deno.test("memory storage round-trips objects", () => {
+test("memory storage round-trips objects", () => {
const storage = createMemoryStorage();
storage.set("k", { hello: "world" });
- assertEquals(storage.get<{ hello: string }>("k"), { hello: "world" });
+ expect(storage.get<{ hello: string }>("k")).toEqual({ hello: "world" });
});
-Deno.test("memory storage returns null for missing keys", () => {
+test("memory storage returns null for missing keys", () => {
const storage = createMemoryStorage();
- assertEquals(storage.get("missing"), null);
+ expect(storage.get("missing")).toEqual(null);
});
-Deno.test("memory storage remove clears a key", () => {
+test("memory storage remove clears a key", () => {
const storage = createMemoryStorage();
storage.set("k", 42);
storage.remove("k");
- assertEquals(storage.get("k"), null);
+ expect(storage.get("k")).toEqual(null);
});
-Deno.test("memory storage instances are isolated", () => {
+test("memory storage instances are isolated", () => {
const a = createMemoryStorage();
const b = createMemoryStorage();
a.set("shared", 1);
- assertEquals(b.get("shared"), null);
+ expect(b.get("shared")).toEqual(null);
});
-Deno.test("local storage namespaces keys", () => {
+test("local storage namespaces keys", () => {
globalThis.localStorage.clear();
const alpha = createLocalStorage("alpha");
@@ -48,15 +89,15 @@ Deno.test("local storage namespaces keys", () => {
alpha.set("shared", { tag: "a" });
beta.set("shared", { tag: "b" });
- assertEquals(alpha.get<{ tag: string }>("shared"), { tag: "a" });
- assertEquals(beta.get<{ tag: string }>("shared"), { tag: "b" });
- assertEquals(globalThis.localStorage.getItem("alpha:shared"), JSON.stringify({ tag: "a" }));
- assertEquals(globalThis.localStorage.getItem("beta:shared"), JSON.stringify({ tag: "b" }));
+ expect(alpha.get<{ tag: string }>("shared")).toEqual({ tag: "a" });
+ expect(beta.get<{ tag: string }>("shared")).toEqual({ tag: "b" });
+ expect(globalThis.localStorage.getItem("alpha:shared")).toEqual(JSON.stringify({ tag: "a" }));
+ expect(globalThis.localStorage.getItem("beta:shared")).toEqual(JSON.stringify({ tag: "b" }));
globalThis.localStorage.clear();
});
-Deno.test("local storage remove respects the namespace", () => {
+test("local storage remove respects the namespace", () => {
globalThis.localStorage.clear();
const alpha = createLocalStorage("alpha");
@@ -66,18 +107,18 @@ Deno.test("local storage remove respects the namespace", () => {
beta.set("k", 2);
alpha.remove("k");
- assertEquals(alpha.get("k"), null);
- assertEquals(beta.get<number>("k"), 2);
+ expect(alpha.get("k")).toEqual(null);
+ expect(beta.get<number>("k")).toEqual(2);
globalThis.localStorage.clear();
});
-Deno.test("local storage returns null on malformed JSON", () => {
+test("local storage returns null on malformed JSON", () => {
globalThis.localStorage.clear();
globalThis.localStorage.setItem("alpha:bad", "not-json");
const alpha = createLocalStorage("alpha");
- assertEquals(alpha.get("bad"), null);
+ expect(alpha.get("bad")).toEqual(null);
globalThis.localStorage.clear();
});