aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/history.test.ts27
-rw-r--r--tests/operations.test.ts43
-rw-r--r--tests/storage.test.ts79
3 files changed, 96 insertions, 53 deletions
diff --git a/tests/history.test.ts b/tests/history.test.ts
index ecd7785..a08c014 100644
--- a/tests/history.test.ts
+++ b/tests/history.test.ts
@@ -1,9 +1,10 @@
+
/*** IMPORT ------------------------------------------- ***/
-import { assertEquals } from "jsr:@std/assert@^1.0.0";
+import { expect, test } from "vitest";
/*** UTILITY ------------------------------------------ ***/
@@ -21,12 +22,12 @@ function entry(id: string, timestamp: number, favorite = false): Entry {
/*** TESTS -------------------------------------------- ***/
-Deno.test("evict keeps everything when under cap", () => {
+test("evict keeps everything when under cap", () => {
const entries = [entry("a", 3), entry("b", 2), entry("c", 1)];
- assertEquals(evict(entries, 5), entries);
+ expect(evict(entries, 5)).toEqual(entries);
});
-Deno.test("evict drops the oldest non-favorites above cap", () => {
+test("evict drops the oldest non-favorites above cap", () => {
const entries = [
entry("a", 5),
entry("b", 4),
@@ -35,10 +36,10 @@ Deno.test("evict drops the oldest non-favorites above cap", () => {
entry("e", 1)
];
const kept = evict(entries, 3);
- assertEquals(kept.map((e) => e.id), ["a", "b", "c"]);
+ expect(kept.map((e) => e.id)).toEqual(["a", "b", "c"]);
});
-Deno.test("evict never drops favorites", () => {
+test("evict never drops favorites", () => {
const entries = [
entry("a", 10),
entry("b", 9),
@@ -48,11 +49,11 @@ Deno.test("evict never drops favorites", () => {
];
const kept = evict(entries, 3);
- assertEquals(kept.some((e) => e.id === "fav-old"), true);
- assertEquals(kept.length, 3);
+ expect(kept.some((e) => e.id === "fav-old")).toEqual(true);
+ expect(kept.length).toEqual(3);
});
-Deno.test("evict can exceed cap when favorites alone do so", () => {
+test("evict can exceed cap when favorites alone do so", () => {
const entries = [
entry("fav-1", 5, true),
entry("fav-2", 4, true),
@@ -61,11 +62,11 @@ Deno.test("evict can exceed cap when favorites alone do so", () => {
];
const kept = evict(entries, 2);
- assertEquals(kept.length, 3);
- assertEquals(kept.every((e) => e.favorite), true);
+ expect(kept.length).toEqual(3);
+ expect(kept.every((e) => e.favorite)).toEqual(true);
});
-Deno.test("evict sorts by timestamp descending", () => {
+test("evict sorts by timestamp descending", () => {
const entries = [
entry("c", 1),
entry("a", 3),
@@ -73,5 +74,5 @@ Deno.test("evict sorts by timestamp descending", () => {
entry("d", 0)
];
const kept = evict(entries, 3);
- assertEquals(kept.map((e) => e.id), ["a", "b", "c"]);
+ expect(kept.map((e) => e.id)).toEqual(["a", "b", "c"]);
});
diff --git a/tests/operations.test.ts b/tests/operations.test.ts
index 99357ea..14fe768 100644
--- a/tests/operations.test.ts
+++ b/tests/operations.test.ts
@@ -1,9 +1,10 @@
+
/*** IMPORT ------------------------------------------- ***/
-import { assertEquals } from "jsr:@std/assert@^1.0.0";
+import { expect, test } from "vitest";
/*** UTILITY ------------------------------------------ ***/
@@ -11,54 +12,54 @@ import { deriveTitle, parseOperations } from "../source/library/graphql/operatio
/*** TESTS -------------------------------------------- ***/
-Deno.test("parseOperations returns empty for blank query", () => {
- assertEquals(parseOperations(""), []);
- assertEquals(parseOperations(" "), []);
+test("parseOperations returns empty for blank query", () => {
+ expect(parseOperations("")).toEqual([]);
+ expect(parseOperations(" ")).toEqual([]);
});
-Deno.test("parseOperations returns empty on syntax error", () => {
- assertEquals(parseOperations("query { ..."), []);
+test("parseOperations returns empty on syntax error", () => {
+ expect(parseOperations("query { ...")).toEqual([]);
});
-Deno.test("parseOperations captures a single named query", () => {
+test("parseOperations captures a single named query", () => {
const ops = parseOperations("query Foo { viewer { id } }");
- assertEquals(ops, [{ name: "Foo", type: "query" }]);
+ expect(ops).toEqual([{ name: "Foo", type: "query" }]);
});
-Deno.test("parseOperations returns null name for anonymous ops", () => {
+test("parseOperations returns null name for anonymous ops", () => {
const ops = parseOperations("{ viewer { id } }");
- assertEquals(ops, [{ name: null, type: "query" }]);
+ expect(ops).toEqual([{ name: null, type: "query" }]);
});
-Deno.test("parseOperations captures multiple operations", () => {
+test("parseOperations captures multiple operations", () => {
const ops = parseOperations(`
query Foo { a }
mutation Bar { b }
subscription Baz { c }
`);
- assertEquals(ops, [
+ expect(ops).toEqual([
{ name: "Foo", type: "query" },
{ name: "Bar", type: "mutation" },
{ name: "Baz", type: "subscription" }
]);
});
-Deno.test("deriveTitle prefers the first operation name", () => {
+test("deriveTitle prefers the first operation name", () => {
const ops = parseOperations("query Foo { a }");
- assertEquals(deriveTitle("query Foo { a }", ops), "Foo");
+ expect(deriveTitle("query Foo { a }", ops)).toEqual("Foo");
});
-Deno.test("deriveTitle falls back to operation type", () => {
+test("deriveTitle falls back to operation type", () => {
const ops = parseOperations("mutation { a }");
- assertEquals(deriveTitle("mutation { a }", ops), "mutation");
+ expect(deriveTitle("mutation { a }", ops)).toEqual("mutation");
});
-Deno.test("deriveTitle falls back to the first 20 chars when unparsable", () => {
+test("deriveTitle falls back to the first 20 chars when unparsable", () => {
const query = "this is not valid graphql at all";
- assertEquals(deriveTitle(query, parseOperations(query)), "this is not valid gr");
+ expect(deriveTitle(query, parseOperations(query))).toEqual("this is not valid gr");
});
-Deno.test("deriveTitle returns 'untitled' for empty input", () => {
- assertEquals(deriveTitle("", []), "untitled");
- assertEquals(deriveTitle(" ", []), "untitled");
+test("deriveTitle returns 'untitled' for empty input", () => {
+ expect(deriveTitle("", [])).toEqual("untitled");
+ expect(deriveTitle(" ", [])).toEqual("untitled");
});
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();
});