Add first tests for store

This commit is contained in:
Adrian Gruntkowski 2024-11-28 20:59:28 +01:00
parent f944a3a436
commit 17c158b6c1
4 changed files with 74 additions and 3 deletions

View file

@ -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");

View file

@ -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}`);
});

View file

@ -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) {

View file

@ -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;