diff --git a/src/react/index.ts b/src/react/index.ts index b7b86583..e560f4cc 100644 --- a/src/react/index.ts +++ b/src/react/index.ts @@ -1,5 +1,5 @@ export { type TLUseDocument, useDocument } from "./useDocument"; -export { FireproofCtx, type UseFireproof, useFireproof } from "./useFireproof"; +export { FireproofCtx, type UseFireproof, useFireproof, type UseLiveQuery } from "./useFireproof"; export { type TLUseLiveQuery, useLiveQuery, type LiveQueryResult } from "./useLiveQuery"; export { type TLUseAllDocs, useAllDocs } from "./useAllDocs"; export { type TLUseChanges, useChanges } from "./useChanges"; diff --git a/src/react/useLiveQuery.ts b/src/react/useLiveQuery.ts index dfc2a5e8..47539a7c 100644 --- a/src/react/useLiveQuery.ts +++ b/src/react/useLiveQuery.ts @@ -1,6 +1,6 @@ import { Database, DocFragment, DocTypes, IndexKeyType } from "@fireproof/core"; -import { LiveQueryResult, useFireproof, type UseLiveQuery } from "./useFireproof"; +import { type LiveQueryResult, useFireproof, type UseLiveQuery } from "./useFireproof"; export type { LiveQueryResult }; diff --git a/tests/helpers.ts b/tests/helpers.ts index d7bb758c..ecded7a9 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -1,5 +1,8 @@ import { toCryptoRuntime } from "@adviser/cement"; import { dataDir, rt, Database } from "@fireproof/core"; +import { renderHook, type RenderHookResult } from "@testing-library/react"; +import { useFireproof } from "use-fireproof"; +import type { UseFireproof, ConfigOpts, LiveQueryResult, QueryOpts, IndexRow, DocFragment, MapFn, UseLiveQuery } from "use-fireproof"; export { dataDir }; @@ -52,4 +55,15 @@ export async function populateDatabase(db: Database, texts: string[]) { const ok = await db.put({ text, date: Date.now(), completed: false }); expect(ok.id).toBeDefined(); } -} \ No newline at end of file +} + +type UseFireproofProps = [Partial, Partial]; +export function getUseFireproofHook(db: Database): RenderHookResult { + return renderHook(() => useFireproof(db)); +} + +type UseLiveQueryHook = RenderHookResult, UseLiveQueryProps>; +type UseLiveQueryProps = [string | MapFn, QueryOpts, IndexRow[]]; +export function getUseLiveQueryHook(useLiveQuery: UseLiveQuery): UseLiveQueryHook { + return renderHook(() => useLiveQuery("date", { limit: 100, descending: true })); +} diff --git a/tests/react/useLiveQuery.test.ts b/tests/react/useLiveQuery.test.ts index 1f0d368d..d9d69540 100644 --- a/tests/react/useLiveQuery.test.ts +++ b/tests/react/useLiveQuery.test.ts @@ -1,58 +1,54 @@ -import { renderHook } from "@testing-library/react"; +import { cleanup } from "@testing-library/react"; import { beforeEach, describe, expect, it } from "vitest"; -import { rt, Database, useFireproof } from "use-fireproof"; -import { defaultTodo, generateTexts, populateDatabase, Todo } from "../helpers"; +import { rt, Database } from "use-fireproof"; +import { generateTexts, getUseFireproofHook, getUseLiveQueryHook, populateDatabase } from "../helpers"; const TEST_DB_NAME = "test-useLiveQuery"; -interface TestContext { - useFireproof: ReturnType; - texts: string[]; -} - describe("HOOK: useLiveQuery", () => { let db: Database; const texts = generateTexts(); afterEach(async () => { + cleanup(); await db.close(); await db.destroy(); }); - beforeEach(async (ctx: TestContext) => { + beforeEach(async () => { await rt.SysContainer.start(); db = new Database(TEST_DB_NAME); - - // populate database with test data - await populateDatabase(db, texts); - const allDocs = await db.allDocs(); - console.log({allDocs}); - - // expect(allDocs.rows.length).toBe(texts.length); - - // render the hook, place in testing context - ctx.useFireproof = renderHook(() => useFireproof(TEST_DB_NAME)).result.current; }); - it("should be defined", async ({ useFireproof }: TestContext) => { - expect(useFireproof).toBeDefined(); + it("should be defined", async () => { + const useFireproofHook = getUseFireproofHook(db); + const { useLiveQuery } = useFireproofHook.result.current; + expect(useLiveQuery).toBeDefined(); }); -/* - it("renders the hook correctly and checks types", ({ useFireproof }: TestContext) => { - const { useLiveQuery } = useFireproof; + it("renders the hook correctly and checks types", () => { + const useFireproofHook = getUseFireproofHook(db); + const { useLiveQuery } = useFireproofHook.result.current; expect(typeof useLiveQuery).toBe("function"); }); - it("reads from the database", async ({ useFireproof }: TestContext) => { - const { useDocument, useLiveQuery } = useFireproof; - // get useLiveQuery hook results - const { result: resUseLiveQuery } = renderHook( - () => useLiveQuery("date", { limit: 100, descending: true }) - ); - const todos = resUseLiveQuery.current; + it("reads from the database", async () => { + // populate database with test data + await populateDatabase(db, texts); + const allDocs = await db.allDocs(); + + // render hook + const useFireproofHook = getUseFireproofHook(db); + const { useLiveQuery } = useFireproofHook.result.current; + + const useLiveQueryHook = getUseLiveQueryHook(useLiveQuery); + const todos = useLiveQueryHook.result.current; + console.log("todos", todos, "\n"); + expect(todos.docs.length).toBe(allDocs.rows.length); + }); +/* // get allDocs function call results const allDocs = await db.allDocs(); @@ -120,7 +116,6 @@ describe("HOOK: useLiveQuery", () => { state++; }); // result.rerender() - // }); - */ + });