@fireproof/core 0.19.123 → 0.19.125

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.
@@ -1,19 +1,639 @@
1
- import { renderHook } from "@testing-library/react";
1
+ import { renderHook, waitFor } from "@testing-library/react";
2
2
  import { describe, expect, it } from "vitest";
3
+ import { fireproof, useFireproof } from "use-fireproof";
4
+ import type { Database, LiveQueryResult, UseDocumentResult } from "use-fireproof";
3
5
 
4
- import { useFireproof } from "use-fireproof";
6
+ // Test timeout value for CI
7
+ const TEST_TIMEOUT = 45000;
5
8
 
6
9
  describe("HOOK: useFireproof", () => {
7
- it("should be defined", () => {
8
- expect(useFireproof).toBeDefined();
10
+ it(
11
+ "should be defined",
12
+ () => {
13
+ expect(useFireproof).toBeDefined();
14
+ },
15
+ TEST_TIMEOUT,
16
+ );
17
+
18
+ it(
19
+ "renders the hook correctly and checks types",
20
+ () => {
21
+ renderHook(() => {
22
+ const { database, useLiveQuery, useDocument } = useFireproof("dbname");
23
+ expect(typeof useLiveQuery).toBe("function");
24
+ expect(typeof useDocument).toBe("function");
25
+ expect(database?.constructor.name).toMatch(/^Database/);
26
+ });
27
+ },
28
+ TEST_TIMEOUT,
29
+ );
30
+ });
31
+
32
+ describe("HOOK: useFireproof useLiveQuery has results", () => {
33
+ const dbName = "useLiveQueryHasResults";
34
+ let db: Database,
35
+ query: LiveQueryResult<{ foo: string }, string>,
36
+ database: ReturnType<typeof useFireproof>["database"],
37
+ useLiveQuery: ReturnType<typeof useFireproof>["useLiveQuery"];
38
+
39
+ let cnt = 0;
40
+
41
+ beforeEach(async () => {
42
+ db = fireproof(dbName);
43
+ await db.put({ foo: "aha", cnt });
44
+ await db.put({ foo: "bar", cnt });
45
+ await db.put({ foo: "caz", cnt });
46
+ cnt++;
47
+ });
48
+
49
+ it(
50
+ "should have setup data",
51
+ async () => {
52
+ const allDocs = await db.allDocs<{ foo: string }>();
53
+ const expectedValues = ["aha", "bar", "caz"];
54
+ expect(allDocs.rows.map((row) => row.value.foo)).toEqual(expectedValues);
55
+ },
56
+ TEST_TIMEOUT,
57
+ );
58
+
59
+ it(
60
+ "queries correctly",
61
+ async () => {
62
+ renderHook(() => {
63
+ const result = useFireproof(dbName);
64
+ database = result.database;
65
+ useLiveQuery = result.useLiveQuery;
66
+ query = useLiveQuery<{ foo: string }>("foo");
67
+ });
68
+
69
+ await waitFor(() => {
70
+ expect(query.rows.map((row) => row.doc?.foo)).toEqual(expect.arrayContaining(["aha", "bar", "caz"]));
71
+ });
72
+ },
73
+ TEST_TIMEOUT,
74
+ );
75
+
76
+ afterEach(async () => {
77
+ await db.close();
78
+ await db.destroy();
79
+ await database?.close();
80
+ await database?.destroy();
9
81
  });
82
+ });
83
+
84
+ describe("HOOK: useFireproof useDocument has results", () => {
85
+ const dbName = "useDocumentHasResults";
86
+ let db: Database,
87
+ docResult: UseDocumentResult<{ input: string }>,
88
+ database: ReturnType<typeof useFireproof>["database"],
89
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
90
+
91
+ beforeEach(async () => {
92
+ db = fireproof(dbName);
10
93
 
11
- it("renders the hook correctly and checks types", () => {
12
94
  renderHook(() => {
13
- const { database, useLiveQuery, useDocument } = useFireproof("dbname");
14
- expect(typeof useLiveQuery).toBe("function");
15
- expect(typeof useDocument).toBe("function");
16
- expect(database?.constructor.name).toBe("Database");
95
+ const result = useFireproof(dbName);
96
+ database = result.database;
97
+ useDocument = result.useDocument;
98
+ docResult = useDocument<{ input: string }>({ input: "" });
17
99
  });
18
100
  });
101
+
102
+ it(
103
+ "should have empty setup data",
104
+ async () => {
105
+ const allDocs = await db.allDocs<{ input: string }>();
106
+ expect(allDocs.rows.length).toBe(0);
107
+ },
108
+ TEST_TIMEOUT,
109
+ );
110
+
111
+ it(
112
+ "queries correctly",
113
+ async () => {
114
+ await waitFor(() => {
115
+ expect(docResult.doc.input).toBe("");
116
+ expect(docResult.doc._id).toBeUndefined();
117
+ });
118
+ },
119
+ TEST_TIMEOUT,
120
+ );
121
+
122
+ it(
123
+ "handles mutations correctly",
124
+ async () => {
125
+ docResult.merge({ input: "new" });
126
+ await waitFor(() => {
127
+ expect(docResult.doc.input).toBe("new");
128
+ expect(docResult.doc._id).toBeUndefined();
129
+ });
130
+ },
131
+ TEST_TIMEOUT,
132
+ );
133
+
134
+ it(
135
+ "handles save correctly",
136
+ async () => {
137
+ docResult.merge({ input: "first" });
138
+ await waitFor(() => {
139
+ expect(docResult.doc.input).toBe("first");
140
+ expect(docResult.doc._id).toBeUndefined();
141
+ });
142
+
143
+ renderHook(() => {
144
+ docResult.save();
145
+ });
146
+
147
+ await waitFor(() => {
148
+ expect(docResult.doc._id).toBeDefined();
149
+ });
150
+ },
151
+ TEST_TIMEOUT,
152
+ );
153
+
154
+ it(
155
+ "handles reset after save",
156
+ async () => {
157
+ docResult.merge({ input: "new" });
158
+ await waitFor(() => {
159
+ expect(docResult.doc.input).toBe("new");
160
+ expect(docResult.doc._id).toBeUndefined();
161
+ });
162
+
163
+ renderHook(() => {
164
+ docResult.save();
165
+ });
166
+
167
+ await waitFor(() => {
168
+ expect(docResult.doc._id).toBeDefined();
169
+ });
170
+
171
+ const doc1 = await db.get<{ input: string }>(docResult.doc._id);
172
+ expect(doc1.input).toBe("new");
173
+
174
+ renderHook(() => {
175
+ docResult.reset();
176
+ });
177
+
178
+ await waitFor(() => {
179
+ expect(docResult.doc.input).toBe("");
180
+ expect(docResult.doc._id).toBeUndefined();
181
+ });
182
+
183
+ renderHook(() => {
184
+ docResult.merge({ input: "fresh" });
185
+ });
186
+
187
+ renderHook(() => {
188
+ docResult.save();
189
+ });
190
+
191
+ await waitFor(() => {
192
+ expect(docResult.doc.input).toBe("fresh");
193
+ expect(docResult.doc._id).toBeDefined();
194
+ });
195
+
196
+ const doc2 = await db.get<{ input: string }>(docResult.doc._id);
197
+ expect(doc2.input).toBe("fresh");
198
+ expect(doc2._id).toBe(docResult.doc._id);
199
+ expect(doc1._id).not.toBe(doc2._id);
200
+
201
+ const allDocs = await db.allDocs<{ input: string }>();
202
+ const inputs = allDocs.rows.map((r) => r.value.input);
203
+ expect(inputs).toEqual(expect.arrayContaining(["first", "new", "fresh"]));
204
+ },
205
+ TEST_TIMEOUT,
206
+ );
207
+
208
+ afterEach(async () => {
209
+ await db.close();
210
+ await db.destroy();
211
+ await database.close();
212
+ await database.destroy();
213
+ });
214
+ });
215
+
216
+ describe("HOOK: useFireproof useDocument has results reset sync", () => {
217
+ const dbName = "useDocumentHasResultsSync";
218
+ let db: Database,
219
+ docResult: UseDocumentResult<{ input: string }>,
220
+ database: ReturnType<typeof useFireproof>["database"],
221
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
222
+
223
+ beforeEach(async () => {
224
+ db = fireproof(dbName);
225
+
226
+ renderHook(() => {
227
+ const result = useFireproof(dbName);
228
+ database = result.database;
229
+ useDocument = result.useDocument;
230
+ docResult = useDocument<{ input: string }>({ input: "" });
231
+ });
232
+ });
233
+
234
+ it(
235
+ "should have empty setup data",
236
+ async () => {
237
+ const allDocs = await db.allDocs<{ input: string }>();
238
+ expect(allDocs.rows.length).toBe(0);
239
+ },
240
+ TEST_TIMEOUT,
241
+ );
242
+
243
+ it(
244
+ "queries correctly",
245
+ async () => {
246
+ await waitFor(() => {
247
+ expect(docResult.doc.input).toBe("");
248
+ expect(docResult.doc._id).toBeUndefined();
249
+ });
250
+ },
251
+ TEST_TIMEOUT,
252
+ );
253
+
254
+ it(
255
+ "handles mutations correctly",
256
+ async () => {
257
+ docResult.merge({ input: "new" });
258
+ await waitFor(() => {
259
+ expect(docResult.doc.input).toBe("new");
260
+ expect(docResult.doc._id).toBeUndefined();
261
+ });
262
+ },
263
+ TEST_TIMEOUT,
264
+ );
265
+
266
+ it(
267
+ "handles save correctly",
268
+ async () => {
269
+ docResult.merge({ input: "first" });
270
+ await waitFor(() => {
271
+ expect(docResult.doc.input).toBe("first");
272
+ expect(docResult.doc._id).toBeUndefined();
273
+ });
274
+
275
+ renderHook(() => {
276
+ docResult.save();
277
+ });
278
+
279
+ await waitFor(() => {
280
+ expect(docResult.doc._id).toBeDefined();
281
+ });
282
+ },
283
+ TEST_TIMEOUT,
284
+ );
285
+
286
+ it(
287
+ "handles reset after save",
288
+ async () => {
289
+ docResult.merge({ input: "new" });
290
+ await waitFor(() => {
291
+ expect(docResult.doc.input).toBe("new");
292
+ expect(docResult.doc._id).toBeUndefined();
293
+ });
294
+
295
+ renderHook(() => {
296
+ docResult.save();
297
+ docResult.reset();
298
+ });
299
+
300
+ await waitFor(() => {
301
+ expect(docResult.doc.input).toBe("");
302
+ expect(docResult.doc._id).toBeUndefined();
303
+ });
304
+
305
+ renderHook(() => {
306
+ docResult.merge({ input: "fresh" });
307
+ });
308
+
309
+ renderHook(() => {
310
+ docResult.save();
311
+ });
312
+
313
+ await waitFor(() => {
314
+ expect(docResult.doc.input).toBe("fresh");
315
+ expect(docResult.doc._id).toBeDefined();
316
+ });
317
+
318
+ const doc2 = await db.get<{ input: string }>(docResult.doc._id);
319
+ expect(doc2.input).toBe("fresh");
320
+ expect(doc2._id).toBe(docResult.doc._id);
321
+
322
+ const allDocs = await db.allDocs<{ input: string }>();
323
+ expect(allDocs.rows.length).toBe(3);
324
+ const inputs = allDocs.rows.map((r) => r.value.input);
325
+ expect(inputs).toEqual(expect.arrayContaining(["first", "new", "fresh"]));
326
+ },
327
+ TEST_TIMEOUT,
328
+ );
329
+
330
+ afterEach(async () => {
331
+ await db.close();
332
+ await db.destroy();
333
+ await database.close();
334
+ await database.destroy();
335
+ });
336
+ });
337
+
338
+ describe("HOOK: useFireproof useDocument with existing document has results", () => {
339
+ const dbName = "useDocumentWithExistingDoc";
340
+ let db: Database,
341
+ docResult: UseDocumentResult<{ input: string }>,
342
+ id: string,
343
+ database: ReturnType<typeof useFireproof>["database"],
344
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
345
+
346
+ beforeEach(async () => {
347
+ db = fireproof(dbName);
348
+ const res = await db.put({ input: "initial" });
349
+ id = res.id;
350
+
351
+ renderHook(() => {
352
+ const result = useFireproof(dbName);
353
+ database = result.database;
354
+ useDocument = result.useDocument;
355
+ docResult = useDocument<{ input: string }>({ _id: id } as { _id: string; input: string });
356
+ });
357
+ });
358
+
359
+ it(
360
+ "should have setup data",
361
+ async () => {
362
+ const allDocs = await db.allDocs<{ input: string }>();
363
+ expect(allDocs.rows.length).toBe(1);
364
+ expect(allDocs.rows[0].value.input).toBe("initial");
365
+ expect(allDocs.rows[0].key).toBe(id);
366
+ },
367
+ TEST_TIMEOUT,
368
+ );
369
+
370
+ it(
371
+ "queries correctly",
372
+ async () => {
373
+ await waitFor(() => {
374
+ expect(docResult.doc.input).toBe("initial");
375
+ expect(docResult.doc._id).toBe(id);
376
+ });
377
+ },
378
+ TEST_TIMEOUT,
379
+ );
380
+
381
+ it(
382
+ "handles mutations correctly",
383
+ async () => {
384
+ // First verify initial state
385
+ await waitFor(() => {
386
+ expect(docResult.doc.input).toBe("initial");
387
+ expect(docResult.doc._id).toBe(id);
388
+ });
389
+
390
+ // Run merge in hook context
391
+ renderHook(() => {
392
+ docResult.merge({ input: "new" });
393
+ });
394
+
395
+ // Then verify the mutation took effect
396
+ await waitFor(() => {
397
+ expect(docResult.doc.input).toBe("new");
398
+ expect(docResult.doc._id).toBe(id);
399
+ });
400
+ },
401
+ TEST_TIMEOUT,
402
+ );
403
+
404
+ afterEach(async () => {
405
+ await db.close();
406
+ await db.destroy();
407
+ await database.close();
408
+ await database.destroy();
409
+ });
410
+ });
411
+
412
+ describe("HOOK: useFireproof useDocument with existing document handles external updates", () => {
413
+ const dbName = "useDocumentWithExternalUpdates";
414
+ let db: Database,
415
+ docResult: UseDocumentResult<{ input: string }>,
416
+ id: string,
417
+ database: ReturnType<typeof useFireproof>["database"],
418
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
419
+
420
+ beforeEach(async () => {
421
+ db = fireproof(dbName);
422
+ const res = await db.put({ input: "initial" });
423
+ id = res.id;
424
+
425
+ renderHook(() => {
426
+ const result = useFireproof(dbName);
427
+ database = result.database;
428
+ useDocument = result.useDocument;
429
+ docResult = useDocument<{ input: string }>({ _id: id } as { _id: string; input: string });
430
+ });
431
+ });
432
+
433
+ it(
434
+ "should have setup data",
435
+ async () => {
436
+ const allDocs = await db.allDocs<{ input: string }>();
437
+ expect(allDocs.rows.length).toBe(1);
438
+ expect(allDocs.rows[0].value.input).toBe("initial");
439
+ expect(allDocs.rows[0].key).toBe(id);
440
+ },
441
+ TEST_TIMEOUT,
442
+ );
443
+
444
+ it(
445
+ "queries correctly",
446
+ async () => {
447
+ await waitFor(() => {
448
+ expect(docResult.doc.input).toBe("initial");
449
+ expect(docResult.doc._id).toBe(id);
450
+ });
451
+ },
452
+ TEST_TIMEOUT,
453
+ );
454
+
455
+ it(
456
+ "handles mutations correctly",
457
+ async () => {
458
+ // First verify initial state
459
+ await waitFor(() => {
460
+ expect(docResult.doc.input).toBe("initial");
461
+ expect(docResult.doc._id).toBe(id);
462
+ });
463
+
464
+ // Run merge in hook context
465
+ renderHook(() => {
466
+ // docResult.merge({ input: "new" });
467
+ db.put({ _id: id, input: "external" });
468
+ });
469
+
470
+ // Then verify the mutation took effect
471
+ await waitFor(() => {
472
+ expect(docResult.doc.input).toBe("external");
473
+ expect(docResult.doc._id).toBe(id);
474
+ });
475
+ },
476
+ TEST_TIMEOUT,
477
+ );
478
+
479
+ afterEach(async () => {
480
+ await db.close();
481
+ await db.destroy();
482
+ await database.close();
483
+ await database.destroy();
484
+ });
485
+ });
486
+
487
+ describe("HOOK: useFireproof bug fix: once the ID is set, it can reset", () => {
488
+ const dbName = "bugTestDocReset";
489
+ let db: Database,
490
+ docResult: UseDocumentResult<{ input: string }>,
491
+ database: ReturnType<typeof useFireproof>["database"],
492
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
493
+
494
+ beforeEach(async () => {
495
+ db = fireproof(dbName);
496
+
497
+ renderHook(() => {
498
+ const result = useFireproof(dbName);
499
+ database = result.database;
500
+ useDocument = result.useDocument;
501
+ docResult = useDocument<{ input: string }>({ input: "" });
502
+ });
503
+ });
504
+
505
+ it(
506
+ "ensures save() then reset() yields an ephemeral doc (blank _id)",
507
+ async () => {
508
+ // Merge some changes
509
+ docResult.merge({ input: "temp data" });
510
+ await waitFor(() => {
511
+ expect(docResult.doc.input).toBe("temp data");
512
+ expect(docResult.doc._id).toBeUndefined();
513
+ });
514
+
515
+ // Save
516
+ renderHook(() => {
517
+ docResult.save();
518
+ docResult.reset();
519
+ });
520
+
521
+ await waitFor(() => {
522
+ expect(docResult.doc._id).toBeUndefined();
523
+ expect(docResult.doc.input).toBe("");
524
+ });
525
+
526
+ renderHook(() => {
527
+ docResult.merge({ input: "new temp data" });
528
+ });
529
+
530
+ renderHook(() => {
531
+ docResult.save();
532
+ });
533
+
534
+ await waitFor(() => {
535
+ expect(docResult.doc._id).toBeDefined();
536
+ expect(docResult.doc.input).toBe("new temp data");
537
+ });
538
+
539
+ // Confirm it's actually in the DB
540
+ const allDocs = await db.allDocs<{ input: string }>();
541
+ expect(allDocs.rows.length).toBe(2);
542
+ const docInputs = allDocs.rows.map((row) => row.value.input);
543
+ expect(docInputs).toContain("temp data");
544
+ expect(docInputs).toContain("new temp data");
545
+ },
546
+ TEST_TIMEOUT,
547
+ );
548
+
549
+ afterEach(async () => {
550
+ await db.close();
551
+ await db.destroy();
552
+ await database.close();
553
+ await database.destroy();
554
+ });
555
+ });
556
+
557
+ describe("HOOK: useFireproof race condition: calling save() without await overwrites reset", () => {
558
+ const dbName = "raceConditionDb";
559
+ let db: Database, docResult: UseDocumentResult<{ input: string }>;
560
+
561
+ beforeEach(async () => {
562
+ db = fireproof(dbName);
563
+
564
+ // Render a new hook instance
565
+ renderHook(() => {
566
+ const { useDocument } = useFireproof(dbName);
567
+ docResult = useDocument<{ input: string }>({ input: "" });
568
+ });
569
+ });
570
+
571
+ it(
572
+ "demonstrates that calling docResult.save() and docResult.reset() in the same tick can overwrite reset",
573
+ async () => {
574
+ // Merge some changes into doc
575
+ docResult.merge({ input: "some data" });
576
+
577
+ // Call save() but DO NOT await it, then immediately reset().
578
+ docResult.save();
579
+ docResult.reset();
580
+
581
+ // Let the async subscription produce a new doc in case the doc is reloaded with an _id
582
+ await new Promise((resolve) => setTimeout(resolve, 500));
583
+
584
+ // If the reset worked, doc._id should STILL be undefined.
585
+ // If the subscription wins, doc._id will be defined => test fails.
586
+ await waitFor(() => {
587
+ expect(docResult.doc._id).toBeUndefined();
588
+ expect(docResult.doc.input).toBe("");
589
+ });
590
+ },
591
+ TEST_TIMEOUT,
592
+ );
593
+
594
+ afterEach(async () => {
595
+ await db.close();
596
+ await db.destroy();
597
+ });
598
+ });
599
+
600
+ describe("useFireproo calling submit()", () => {
601
+ const dbName = "submitDb";
602
+ let db: Database, docResult: UseDocumentResult<{ input: string }>;
603
+
604
+ beforeEach(async () => {
605
+ db = fireproof(dbName);
606
+
607
+ // Render a new hook instance
608
+ renderHook(() => {
609
+ const { useDocument } = useFireproof(dbName);
610
+ docResult = useDocument<{ input: string }>({ input: "" });
611
+ });
612
+ });
613
+
614
+ it(
615
+ "demonstrates that calling docResult.save() and docResult.reset() in the same tick can overwrite reset",
616
+ async () => {
617
+ // Merge some changes into doc
618
+ docResult.merge({ input: "some data" });
619
+
620
+ docResult.submit();
621
+
622
+ // Let the async subscription produce a new doc in case the doc is reloaded with an _id
623
+ await new Promise((resolve) => setTimeout(resolve, 500));
624
+
625
+ // If the reset worked, doc._id should STILL be undefined.
626
+ // If the subscription wins, doc._id will be defined => test fails.
627
+ await waitFor(() => {
628
+ expect(docResult.doc._id).toBeUndefined();
629
+ expect(docResult.doc.input).toBe("");
630
+ });
631
+ },
632
+ TEST_TIMEOUT,
633
+ );
634
+
635
+ afterEach(async () => {
636
+ await db.close();
637
+ await db.destroy();
638
+ });
19
639
  });