From 17c158b6c1f060a647b80d94acbbe74b0ae28949 Mon Sep 17 00:00:00 2001 From: Adrian Gruntkowski Date: Thu, 28 Nov 2024 20:59:28 +0100 Subject: [PATCH] Add first tests for store --- priv/static/js/jenot-tests.js | 1 + priv/static/js/synced-store-test.js | 70 +++++++++++++++++++++++++++++ priv/static/js/synced-store.js | 4 +- priv/static/js/test-utils.js | 2 +- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 priv/static/js/synced-store-test.js diff --git a/priv/static/js/jenot-tests.js b/priv/static/js/jenot-tests.js index 95bdf65..0b836ab 100644 --- a/priv/static/js/jenot-tests.js +++ b/priv/static/js/jenot-tests.js @@ -2,6 +2,7 @@ import { runTests } from "./test-utils.js"; // tests to run import "./components-test.js"; +import "./synced-store-test.js"; const URL_PARAMS = new URLSearchParams(window.location.search); const CONCRETE_TEST = URL_PARAMS.get("t"); diff --git a/priv/static/js/synced-store-test.js b/priv/static/js/synced-store-test.js new file mode 100644 index 0000000..681ac74 --- /dev/null +++ b/priv/static/js/synced-store-test.js @@ -0,0 +1,70 @@ +import { test, assert } from "./test-utils.js"; +import { SyncedNoteStore } from "./synced-store.js"; + +test("synced store stores a note", async (_container, idx) => { + const store = new SyncedNoteStore(`jenot-app-test-${idx}`, "notes", "/", { + add: () => null, + }); + + await store.add({ type: "note", content: "Test 123" }); + + const notes = await store.all(); + + await assert(() => notes.length === 1); + await assert(() => notes[0].type === "note"); + await assert(() => notes[0].content === "Test 123"); + await assert(() => notes[0].id != null); + await assert(() => Number.isInteger(notes[0].created)); + await assert(() => Number.isInteger(notes[0].updated)); + await assert(() => notes[0].deleted === null); + + indexedDB.deleteDatabase(`jenot-app-test-${idx}`); +}); + +test("synced store gets a note", async (_container, idx) => { + let addCalled = false; + + const store = new SyncedNoteStore(`jenot-app-test-${idx}`, "notes", "/", { + add: () => (addCalled = true), + }); + + await store.add({ type: "note", content: "Test 123" }); + + const notes = await store.all(); + + const retrievedNote = await store.get(notes[0].id); + + await assert(() => retrievedNote.id === notes[0].id); + await assert(() => addCalled); + + indexedDB.deleteDatabase(`jenot-app-test-${idx}`); +}); + +test("synced store updates a note", async (_container, idx) => { + let addCalled = false, + updateCalled = false; + + const store = new SyncedNoteStore(`jenot-app-test-${idx}`, "notes", "/", { + add: () => (addCalled = true), + update: () => (updateCalled = true), + }); + + await store.add({ type: "note", content: "Test 123" }); + + const [note] = await store.all(); + const addedNote = structuredClone(note); + + note.content = "New content"; + + await store.update(note); + + const [updatedNote] = await store.all(); + + await assert(() => addCalled); + await assert(() => updateCalled); + await assert(() => addedNote.id === updatedNote.id); + await assert(() => updatedNote.content === "New content"); + await assert(() => updatedNote.updated > addedNote.updated); + + indexedDB.deleteDatabase(`jenot-app-test-${idx}`); +}); diff --git a/priv/static/js/synced-store.js b/priv/static/js/synced-store.js index 476d1e5..e538a8b 100644 --- a/priv/static/js/synced-store.js +++ b/priv/static/js/synced-store.js @@ -62,12 +62,12 @@ class WebNoteStore { } export class SyncedNoteStore extends EventTarget { - constructor(dbName, storeName, endpoint) { + constructor(dbName, storeName, endpoint, webStore) { super(); this.dbName = dbName; this.storeName = storeName; this.db = null; - this.webStore = endpoint && new WebNoteStore(endpoint); + this.webStore = webStore || (endpoint && new WebNoteStore(endpoint)); } async all(since, includeDeleted) { diff --git a/priv/static/js/test-utils.js b/priv/static/js/test-utils.js index 9faec0f..e11aafa 100644 --- a/priv/static/js/test-utils.js +++ b/priv/static/js/test-utils.js @@ -66,7 +66,7 @@ export function runTests(globalOptions) { log.appendChild(logRow); try { - await testFun(container); + await testFun(container, idx); const message = `[OK] ${label}`; console.info(message); logRow.textContent = message;