@fireproof/core 0.20.0-dev-preview-36 → 0.20.0-dev-preview-39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -343,3 +343,132 @@ describe("HOOK: useFireproof useDocument with existing document has results", ()
343
343
  await database.destroy();
344
344
  });
345
345
  });
346
+
347
+ describe("HOOK: useFireproof useDocument with existing document handles external updates", () => {
348
+ const dbName = "useDocumentWithExternalUpdates";
349
+ let db: Database,
350
+ docResult: UseDocumentResult<{ input: string }>,
351
+ id: string,
352
+ database: ReturnType<typeof useFireproof>["database"],
353
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
354
+
355
+ beforeEach(async () => {
356
+ db = fireproof(dbName);
357
+ const res = await db.put({ input: "initial" });
358
+ id = res.id;
359
+
360
+ renderHook(() => {
361
+ const result = useFireproof(dbName);
362
+ database = result.database;
363
+ useDocument = result.useDocument;
364
+ docResult = useDocument<{ input: string }>({ _id: id } as { _id: string; input: string });
365
+ });
366
+ });
367
+
368
+ it("should have setup data", async () => {
369
+ const allDocs = await db.allDocs<{ input: string }>();
370
+ expect(allDocs.rows.length).toBe(1);
371
+ expect(allDocs.rows[0].value.input).toBe("initial");
372
+ expect(allDocs.rows[0].key).toBe(id);
373
+ });
374
+
375
+ it("queries correctly", async () => {
376
+ await waitFor(() => {
377
+ expect(docResult.doc.input).toBe("initial");
378
+ expect(docResult.doc._id).toBe(id);
379
+ });
380
+ });
381
+
382
+ it("handles mutations correctly", async () => {
383
+ // First verify initial state
384
+ await waitFor(() => {
385
+ expect(docResult.doc.input).toBe("initial");
386
+ expect(docResult.doc._id).toBe(id);
387
+ });
388
+
389
+ // Run merge in hook context
390
+ renderHook(() => {
391
+ // docResult.merge({ input: "new" });
392
+ db.put({ _id: id, input: "external" });
393
+ });
394
+
395
+ // Then verify the mutation took effect
396
+ await waitFor(() => {
397
+ expect(docResult.doc.input).toBe("external");
398
+ expect(docResult.doc._id).toBe(id);
399
+ });
400
+ });
401
+
402
+ afterEach(async () => {
403
+ await db.close();
404
+ await db.destroy();
405
+ await database.close();
406
+ await database.destroy();
407
+ });
408
+ });
409
+
410
+ describe("HOOK: useFireproof bug fix: once the ID is set, it can reset", () => {
411
+ const dbName = "bugTestDocReset";
412
+ let db: Database,
413
+ docResult: UseDocumentResult<{ input: string }>,
414
+ database: ReturnType<typeof useFireproof>["database"],
415
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
416
+
417
+ beforeEach(async () => {
418
+ db = fireproof(dbName);
419
+
420
+ renderHook(() => {
421
+ const result = useFireproof(dbName);
422
+ database = result.database;
423
+ useDocument = result.useDocument;
424
+ docResult = useDocument<{ input: string }>({ input: "" });
425
+ });
426
+ });
427
+
428
+ it("ensures save() then reset() yields an ephemeral doc (blank _id)", async () => {
429
+ // Merge some changes
430
+ docResult.merge({ input: "temp data" });
431
+ await waitFor(() => {
432
+ expect(docResult.doc.input).toBe("temp data");
433
+ expect(docResult.doc._id).toBeUndefined();
434
+ });
435
+
436
+ // Save
437
+ renderHook(() => {
438
+ docResult.save();
439
+ docResult.reset();
440
+ });
441
+
442
+ await waitFor(() => {
443
+ expect(docResult.doc._id).toBeUndefined();
444
+ expect(docResult.doc.input).toBe("");
445
+ });
446
+
447
+ renderHook(() => {
448
+ docResult.merge({ input: "new temp data" });
449
+ });
450
+
451
+ renderHook(() => {
452
+ docResult.save();
453
+ });
454
+
455
+ await waitFor(() => {
456
+ expect(docResult.doc._id).toBeDefined();
457
+ expect(docResult.doc.input).toBe("new temp data");
458
+ });
459
+
460
+ // Confirm it's actually in the DB
461
+ const allDocs = await db.allDocs<{ input: string }>();
462
+ expect(allDocs.rows.length).toBe(2);
463
+ const docInputs = allDocs.rows.map((row) => row.value.input);
464
+ expect(docInputs).toContain("temp data");
465
+ expect(docInputs).toContain("new temp data");
466
+ });
467
+
468
+ afterEach(async () => {
469
+ await db.close();
470
+ await db.destroy();
471
+ await database.close();
472
+ await database.destroy();
473
+ });
474
+ });