File
Blob: tests/client/stores/workspace-navigation.test.ts
| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { createFreshDb, deleteDb } from "@tests/client/util/idb"; |
| 3 | import type { BlandDatabase } from "@/client/stores/db/bland-db"; |
| 4 | |
| 5 | let db: BlandDatabase; |
| 6 | let navigationCommands: typeof import("@/client/stores/db/workspace-navigation").navigationCommands; |
| 7 | |
| 8 | beforeEach(async () => { |
| 9 | vi.resetModules(); |
| 10 | db = createFreshDb(); |
| 11 | vi.doMock("@/client/stores/db/bland-db", async () => { |
| 12 | const actual = await vi.importActual<typeof import("@/client/stores/db/bland-db")>("@/client/stores/db/bland-db"); |
| 13 | return { ...actual, db }; |
| 14 | }); |
| 15 | navigationCommands = (await import("@/client/stores/db/workspace-navigation")).navigationCommands; |
| 16 | }); |
| 17 | |
| 18 | afterEach(async () => { |
| 19 | await deleteDb(db); |
| 20 | vi.restoreAllMocks(); |
| 21 | }); |
| 22 | |
| 23 | describe("navigationCommands", () => { |
| 24 | it("setLastVisitedWorkspaceId upserts the key/value meta row", async () => { |
| 25 | await navigationCommands.setLastVisitedWorkspaceId("ws-1"); |
| 26 | expect((await db.workspaceMeta.get("lastVisitedWorkspaceId"))?.value).toBe("ws-1"); |
| 27 | await navigationCommands.setLastVisitedWorkspaceId("ws-2"); |
| 28 | expect((await db.workspaceMeta.get("lastVisitedWorkspaceId"))?.value).toBe("ws-2"); |
| 29 | await navigationCommands.setLastVisitedWorkspaceId(null); |
| 30 | expect((await db.workspaceMeta.get("lastVisitedWorkspaceId"))?.value).toBeNull(); |
| 31 | }); |
| 32 | |
| 33 | it("setLastVisitedPage writes one row per workspace", async () => { |
| 34 | await navigationCommands.setLastVisitedPage("ws-1", "p1"); |
| 35 | await navigationCommands.setLastVisitedPage("ws-2", "pA"); |
| 36 | expect((await db.lastVisitedPages.get("ws-1"))?.pageId).toBe("p1"); |
| 37 | expect((await db.lastVisitedPages.get("ws-2"))?.pageId).toBe("pA"); |
| 38 | |
| 39 | await navigationCommands.setLastVisitedPage("ws-1", "p2"); |
| 40 | expect((await db.lastVisitedPages.get("ws-1"))?.pageId).toBe("p2"); |
| 41 | expect((await db.lastVisitedPages.get("ws-2"))?.pageId).toBe("pA"); |
| 42 | }); |
| 43 | |
| 44 | it("clearForWorkspace drops the lastVisitedPages row and clears the meta pointer when it matches", async () => { |
| 45 | await navigationCommands.setLastVisitedWorkspaceId("ws-1"); |
| 46 | await navigationCommands.setLastVisitedPage("ws-1", "p1"); |
| 47 | await navigationCommands.setLastVisitedPage("ws-2", "pA"); |
| 48 | |
| 49 | await navigationCommands.clearForWorkspace("ws-1"); |
| 50 | |
| 51 | expect(await db.lastVisitedPages.get("ws-1")).toBeUndefined(); |
| 52 | expect((await db.lastVisitedPages.get("ws-2"))?.pageId).toBe("pA"); |
| 53 | expect((await db.workspaceMeta.get("lastVisitedWorkspaceId"))?.value).toBeNull(); |
| 54 | }); |
| 55 | |
| 56 | it("clearForWorkspace preserves the meta pointer when it targets a different workspace", async () => { |
| 57 | await navigationCommands.setLastVisitedWorkspaceId("ws-2"); |
| 58 | await navigationCommands.setLastVisitedPage("ws-1", "p1"); |
| 59 | await navigationCommands.clearForWorkspace("ws-1"); |
| 60 | expect((await db.workspaceMeta.get("lastVisitedWorkspaceId"))?.value).toBe("ws-2"); |
| 61 | }); |
| 62 | }); |