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

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,7 +1,8 @@
1
- import { renderHook } from "@testing-library/react";
1
+ import { renderHook, waitFor } from "@testing-library/react";
2
2
  import { describe, expect, it } from "vitest";
3
3
 
4
- import { useFireproof } from "use-fireproof";
4
+ import { Database, fireproof, useFireproof } from "use-fireproof";
5
+ import { LiveQueryResult, UseDocumentResult } from "../../src/react/useFireproof.js";
5
6
 
6
7
  describe("HOOK: useFireproof", () => {
7
8
  it("should be defined", () => {
@@ -17,3 +18,328 @@ describe("HOOK: useFireproof", () => {
17
18
  });
18
19
  });
19
20
  });
21
+
22
+ describe("HOOK: useFireproof useLiveQuery has results", () => {
23
+ const dbName = "dbnameuseFP";
24
+ let db: Database,
25
+ query: LiveQueryResult<{ foo: string }, string>,
26
+ database: ReturnType<typeof useFireproof>["database"],
27
+ useLiveQuery: ReturnType<typeof useFireproof>["useLiveQuery"];
28
+
29
+ beforeEach(async () => {
30
+ db = fireproof(dbName);
31
+ await db.put({ foo: "aha" });
32
+ await db.put({ foo: "bar" });
33
+ await db.put({ foo: "caz" });
34
+
35
+ renderHook(() => {
36
+ const result = useFireproof(dbName);
37
+ database = result.database;
38
+ useLiveQuery = result.useLiveQuery;
39
+ query = useLiveQuery<{ foo: string }>("foo");
40
+ });
41
+ });
42
+
43
+ it("should have setup data", async () => {
44
+ const allDocs = await db.allDocs<{ foo: string }>();
45
+ expect(allDocs.rows.length).toBe(3);
46
+ expect(allDocs.rows[0].value.foo).toBe("aha");
47
+ expect(allDocs.rows[1].value.foo).toBe("bar");
48
+ expect(allDocs.rows[2].value.foo).toBe("caz");
49
+ });
50
+
51
+ it("queries correctly", async () => {
52
+ await waitFor(() => {
53
+ expect(query.rows.length).toBe(3);
54
+ expect(query.rows[0].doc?.foo).toBe("aha");
55
+ expect(query.rows[1].doc?.foo).toBe("bar");
56
+ expect(query.rows[2].doc?.foo).toBe("caz");
57
+ });
58
+ });
59
+
60
+ afterEach(async () => {
61
+ await db.close();
62
+ await db.destroy();
63
+ await database.close();
64
+ await database.destroy();
65
+ });
66
+ });
67
+
68
+ describe("HOOK: useFireproof useDocument has results", () => {
69
+ const dbName = "useDocumentHasResults";
70
+ let db: Database,
71
+ docResult: UseDocumentResult<{ input: string }>,
72
+ database: ReturnType<typeof useFireproof>["database"],
73
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
74
+
75
+ beforeEach(async () => {
76
+ db = fireproof(dbName);
77
+
78
+ renderHook(() => {
79
+ const result = useFireproof(dbName);
80
+ database = result.database;
81
+ useDocument = result.useDocument;
82
+ docResult = useDocument<{ input: string }>({ input: "" });
83
+ });
84
+ });
85
+
86
+ it("should have empty setup data", async () => {
87
+ const allDocs = await db.allDocs<{ input: string }>();
88
+ expect(allDocs.rows.length).toBe(0);
89
+ });
90
+
91
+ it("queries correctly", async () => {
92
+ await waitFor(() => {
93
+ expect(docResult.doc.input).toBe("");
94
+ expect(docResult.doc._id).toBeUndefined();
95
+ });
96
+ });
97
+
98
+ it("handles mutations correctly", async () => {
99
+ docResult.merge({ input: "new" });
100
+ await waitFor(() => {
101
+ expect(docResult.doc.input).toBe("new");
102
+ expect(docResult.doc._id).toBeUndefined();
103
+ });
104
+ });
105
+
106
+ it("handles save correctly", async () => {
107
+ docResult.merge({ input: "first" });
108
+ await waitFor(() => {
109
+ expect(docResult.doc.input).toBe("first");
110
+ expect(docResult.doc._id).toBeUndefined();
111
+ });
112
+
113
+ renderHook(() => {
114
+ docResult.save();
115
+ });
116
+
117
+ await waitFor(() => {
118
+ expect(docResult.doc._id).toBeDefined();
119
+ });
120
+ });
121
+
122
+ it("handles reset after save", async () => {
123
+ docResult.merge({ input: "new" });
124
+ await waitFor(() => {
125
+ expect(docResult.doc.input).toBe("new");
126
+ expect(docResult.doc._id).toBeUndefined();
127
+ });
128
+
129
+ renderHook(() => {
130
+ docResult.save();
131
+ });
132
+
133
+ await waitFor(() => {
134
+ expect(docResult.doc._id).toBeDefined();
135
+ });
136
+
137
+ const doc1 = await db.get<{ input: string }>(docResult.doc._id);
138
+ expect(doc1.input).toBe("new");
139
+
140
+ renderHook(() => {
141
+ docResult.reset();
142
+ });
143
+
144
+ await waitFor(() => {
145
+ expect(docResult.doc.input).toBe("");
146
+ expect(docResult.doc._id).toBeUndefined();
147
+ });
148
+
149
+ renderHook(() => {
150
+ docResult.merge({ input: "fresh" });
151
+ });
152
+
153
+ renderHook(() => {
154
+ docResult.save();
155
+ });
156
+
157
+ await waitFor(() => {
158
+ expect(docResult.doc.input).toBe("fresh");
159
+ expect(docResult.doc._id).toBeDefined();
160
+ });
161
+
162
+ const doc2 = await db.get<{ input: string }>(docResult.doc._id);
163
+ expect(doc2.input).toBe("fresh");
164
+ expect(doc2._id).toBe(docResult.doc._id);
165
+ expect(doc1._id).not.toBe(doc2._id);
166
+
167
+ const allDocs = await db.allDocs<{ input: string }>();
168
+ expect(allDocs.rows.length).toBe(3);
169
+ expect(allDocs.rows[0].value.input).toBe("first");
170
+ expect(allDocs.rows[1].value.input).toBe("new");
171
+ expect(allDocs.rows[2].value.input).toBe("fresh");
172
+ });
173
+
174
+ afterEach(async () => {
175
+ await db.close();
176
+ await db.destroy();
177
+ await database.close();
178
+ await database.destroy();
179
+ });
180
+ });
181
+
182
+ describe("HOOK: useFireproof useDocument has results reset sync", () => {
183
+ const dbName = "useDocumentHasResultsSync";
184
+ let db: Database,
185
+ docResult: UseDocumentResult<{ input: string }>,
186
+ database: ReturnType<typeof useFireproof>["database"],
187
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
188
+
189
+ beforeEach(async () => {
190
+ db = fireproof(dbName);
191
+
192
+ renderHook(() => {
193
+ const result = useFireproof(dbName);
194
+ database = result.database;
195
+ useDocument = result.useDocument;
196
+ docResult = useDocument<{ input: string }>({ input: "" });
197
+ });
198
+ });
199
+
200
+ it("should have empty setup data", async () => {
201
+ const allDocs = await db.allDocs<{ input: string }>();
202
+ expect(allDocs.rows.length).toBe(0);
203
+ });
204
+
205
+ it("queries correctly", async () => {
206
+ await waitFor(() => {
207
+ expect(docResult.doc.input).toBe("");
208
+ expect(docResult.doc._id).toBeUndefined();
209
+ });
210
+ });
211
+
212
+ it("handles mutations correctly", async () => {
213
+ docResult.merge({ input: "new" });
214
+ await waitFor(() => {
215
+ expect(docResult.doc.input).toBe("new");
216
+ expect(docResult.doc._id).toBeUndefined();
217
+ });
218
+ });
219
+
220
+ it("handles save correctly", async () => {
221
+ docResult.merge({ input: "first" });
222
+ await waitFor(() => {
223
+ expect(docResult.doc.input).toBe("first");
224
+ expect(docResult.doc._id).toBeUndefined();
225
+ });
226
+
227
+ renderHook(() => {
228
+ docResult.save();
229
+ });
230
+
231
+ await waitFor(() => {
232
+ expect(docResult.doc._id).toBeDefined();
233
+ });
234
+ });
235
+
236
+ it("handles reset after save", async () => {
237
+ docResult.merge({ input: "new" });
238
+ await waitFor(() => {
239
+ expect(docResult.doc.input).toBe("new");
240
+ expect(docResult.doc._id).toBeUndefined();
241
+ });
242
+
243
+ renderHook(() => {
244
+ docResult.save();
245
+ docResult.reset();
246
+ });
247
+
248
+ await waitFor(() => {
249
+ expect(docResult.doc.input).toBe("");
250
+ expect(docResult.doc._id).toBeUndefined();
251
+ });
252
+
253
+ renderHook(() => {
254
+ docResult.merge({ input: "fresh" });
255
+ });
256
+
257
+ renderHook(() => {
258
+ docResult.save();
259
+ });
260
+
261
+ await waitFor(() => {
262
+ expect(docResult.doc.input).toBe("fresh");
263
+ expect(docResult.doc._id).toBeDefined();
264
+ });
265
+
266
+ const doc2 = await db.get<{ input: string }>(docResult.doc._id);
267
+ expect(doc2.input).toBe("fresh");
268
+ expect(doc2._id).toBe(docResult.doc._id);
269
+
270
+ const allDocs = await db.allDocs<{ input: string }>();
271
+ expect(allDocs.rows.length).toBe(3);
272
+ expect(allDocs.rows[0].value.input).toBe("first");
273
+ expect(allDocs.rows[1].value.input).toBe("new");
274
+ expect(allDocs.rows[2].value.input).toBe("fresh");
275
+ });
276
+
277
+ afterEach(async () => {
278
+ await db.close();
279
+ await db.destroy();
280
+ await database.close();
281
+ await database.destroy();
282
+ });
283
+ });
284
+
285
+ describe("HOOK: useFireproof useDocument with existing document has results", () => {
286
+ const dbName = "useDocumentWithExistingDoc";
287
+ let db: Database,
288
+ docResult: UseDocumentResult<{ input: string }>,
289
+ id: string,
290
+ database: ReturnType<typeof useFireproof>["database"],
291
+ useDocument: ReturnType<typeof useFireproof>["useDocument"];
292
+
293
+ beforeEach(async () => {
294
+ db = fireproof(dbName);
295
+ const res = await db.put({ input: "initial" });
296
+ id = res.id;
297
+
298
+ renderHook(() => {
299
+ const result = useFireproof(dbName);
300
+ database = result.database;
301
+ useDocument = result.useDocument;
302
+ docResult = useDocument<{ input: string }>({ _id: id } as { _id: string; input: string });
303
+ });
304
+ });
305
+
306
+ it("should have setup data", async () => {
307
+ const allDocs = await db.allDocs<{ input: string }>();
308
+ expect(allDocs.rows.length).toBe(1);
309
+ expect(allDocs.rows[0].value.input).toBe("initial");
310
+ expect(allDocs.rows[0].key).toBe(id);
311
+ });
312
+
313
+ it("queries correctly", async () => {
314
+ await waitFor(() => {
315
+ expect(docResult.doc.input).toBe("initial");
316
+ expect(docResult.doc._id).toBe(id);
317
+ });
318
+ });
319
+
320
+ it("handles mutations correctly", async () => {
321
+ // First verify initial state
322
+ await waitFor(() => {
323
+ expect(docResult.doc.input).toBe("initial");
324
+ expect(docResult.doc._id).toBe(id);
325
+ });
326
+
327
+ // Run merge in hook context
328
+ renderHook(() => {
329
+ docResult.merge({ input: "new" });
330
+ });
331
+
332
+ // Then verify the mutation took effect
333
+ await waitFor(() => {
334
+ expect(docResult.doc.input).toBe("new");
335
+ expect(docResult.doc._id).toBe(id);
336
+ });
337
+ });
338
+
339
+ afterEach(async () => {
340
+ await db.close();
341
+ await db.destroy();
342
+ await database.close();
343
+ await database.destroy();
344
+ });
345
+ });