test(01-01): add failing tests for IndexedDB wrapper
- Tests for openDB with 5 stores, progress CRUD, settings CRUD - Tests for null return on empty DB - RED phase: implementation not yet created Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
|||||||
|
import "fake-indexeddb/auto";
|
||||||
|
import { describe, expect, it, beforeEach } from "vitest";
|
||||||
|
import { openDB, getProgress, saveProgress, getSettings, saveSettings } from "./db";
|
||||||
|
import type { Progress, Settings } from "../types";
|
||||||
|
|
||||||
|
describe("IndexedDB wrapper", () => {
|
||||||
|
let db: IDBDatabase;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
// Each test gets a fresh database by using a unique name
|
||||||
|
// fake-indexeddb/auto resets between tests in vitest
|
||||||
|
db = await openDB();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("openDB resolves to an IDBDatabase with 5 object store names", () => {
|
||||||
|
const storeNames = Array.from(db.objectStoreNames);
|
||||||
|
expect(storeNames).toHaveLength(5);
|
||||||
|
expect(storeNames).toContain("progress");
|
||||||
|
expect(storeNames).toContain("companionAssets");
|
||||||
|
expect(storeNames).toContain("styleReference");
|
||||||
|
expect(storeNames).toContain("forestElements");
|
||||||
|
expect(storeNames).toContain("settings");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saveProgress then getProgress returns same data", async () => {
|
||||||
|
const progress: Progress = {
|
||||||
|
id: 1,
|
||||||
|
currentLevel: 1,
|
||||||
|
completedLevels: [],
|
||||||
|
totalSessions: 0,
|
||||||
|
sessionDates: [],
|
||||||
|
selectedCharacter: "fee",
|
||||||
|
selectedLayout: "de",
|
||||||
|
};
|
||||||
|
await saveProgress(db, progress);
|
||||||
|
const result = await getProgress(db);
|
||||||
|
expect(result).toEqual(progress);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saveSettings then getSettings returns same data", async () => {
|
||||||
|
const settings: Settings = {
|
||||||
|
id: 1,
|
||||||
|
audioEnabled: true,
|
||||||
|
apiKey: "test-key",
|
||||||
|
};
|
||||||
|
await saveSettings(db, settings);
|
||||||
|
const result = await getSettings(db);
|
||||||
|
expect(result).toEqual(settings);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getProgress on empty DB returns null", async () => {
|
||||||
|
const result = await getProgress(db);
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getSettings on empty DB returns null", async () => {
|
||||||
|
const result = await getSettings(db);
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user