Skip to content

Commit

Permalink
try more use doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Feb 8, 2025
1 parent 83668a1 commit 53f2632
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/react/useFireproof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ export function useFireproof(name: string | Database = "useFireproof", config: C
}, []);

const reset = useCallback(() => {
// Use originalInitialDoc and explicitly set _id to undefined
setDoc({ ...originalInitialDoc, _id: undefined });
setDoc({ ...originalInitialDoc });
}, [originalInitialDoc]);

// Legacy-compatible updateDoc
Expand Down
27 changes: 18 additions & 9 deletions tests/react/useFireproof.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,24 +436,33 @@ describe("HOOK: useFireproof bug fix: once the ID is set, it can reset", () => {
// Save
renderHook(() => {
docResult.save();
docResult.reset();
});

await waitFor(() => {
expect(docResult.doc._id).toBeDefined();
expect(docResult.doc._id).toBeUndefined();
expect(docResult.doc.input).toBe("");
});

// Confirm it's actually in the DB
const allDocs = await db.allDocs<{ input: string }>();
expect(allDocs.rows.length).toBe(1);
expect(allDocs.rows[0].value.input).toBe("temp data");
renderHook(() => {
docResult.merge({ input: "new temp data" });
});

// Reset
renderHook(() => {
docResult.reset();
docResult.save();
});

await waitFor(() => {
expect(docResult.doc._id).toBeUndefined();
expect(docResult.doc.input).toBe("");
expect(docResult.doc._id).toBeDefined();
expect(docResult.doc.input).toBe("new temp data");
});

// Confirm it's actually in the DB
const allDocs = await db.allDocs<{ input: string }>();
expect(allDocs.rows.length).toBe(2);
const docInputs = allDocs.rows.map((row) => row.value.input);
expect(docInputs).toContain("temp data");
expect(docInputs).toContain("new temp data");
});

afterEach(async () => {
Expand Down

0 comments on commit 53f2632

Please sign in to comment.