Skip to content

Commit

Permalink
shuffle helpers around
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed Aug 13, 2024
1 parent 754d1fd commit da0ccca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/react/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/react/useLiveQuery.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down
16 changes: 15 additions & 1 deletion tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down Expand Up @@ -52,4 +55,15 @@ export async function populateDatabase(db: Database, texts: string[]) {
const ok = await db.put<Todo>({ text, date: Date.now(), completed: false });
expect(ok.id).toBeDefined();
}
}
}

type UseFireproofProps = [Partial<string | Database>, Partial<ConfigOpts>];
export function getUseFireproofHook(db: Database): RenderHookResult<UseFireproof, UseFireproofProps> {
return renderHook(() => useFireproof(db));
}

type UseLiveQueryHook = RenderHookResult<LiveQueryResult<Todo, string>, UseLiveQueryProps>;
type UseLiveQueryProps = [string | MapFn<Todo>, QueryOpts<string>, IndexRow<string, Todo, DocFragment>[]];
export function getUseLiveQueryHook(useLiveQuery: UseLiveQuery): UseLiveQueryHook {
return renderHook(() => useLiveQuery("date", { limit: 100, descending: true }));
}
61 changes: 28 additions & 33 deletions tests/react/useLiveQuery.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof useFireproof>;
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<Todo>("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();
Expand Down Expand Up @@ -120,7 +116,6 @@ describe("HOOK: useLiveQuery", () => {
state++;
});
// result.rerender()
// });
*/

});

0 comments on commit da0ccca

Please sign in to comment.