@fireproof/core 0.20.0-dev-preview-51 → 0.20.0-dev-preview-53
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.
- package/README.md +147 -59
- package/deno.json +2 -2
- package/index.cjs +2 -1
- package/index.cjs.map +1 -1
- package/index.d.cts +1 -1
- package/index.d.ts +1 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/metafile-cjs.json +1 -1
- package/metafile-esm.json +1 -1
- package/package.json +4 -4
- package/react/index.cjs +64 -0
- package/react/index.cjs.map +1 -1
- package/react/index.d.cts +290 -2
- package/react/index.d.ts +290 -2
- package/react/index.js +54 -0
- package/react/index.js.map +1 -1
- package/react/metafile-cjs.json +1 -1
- package/react/metafile-esm.json +1 -1
- package/tests/react/img-file.test.tsx +199 -0
- package/tests/react/useFireproof.test.tsx +418 -334
@@ -1,22 +1,32 @@
|
|
1
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
|
-
|
5
|
-
|
6
|
+
// Test timeout value for CI
|
7
|
+
const TEST_TIMEOUT = 45000;
|
6
8
|
|
7
9
|
describe("HOOK: useFireproof", () => {
|
8
|
-
it(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
+
);
|
20
30
|
});
|
21
31
|
|
22
32
|
describe("HOOK: useFireproof useLiveQuery has results", () => {
|
@@ -36,32 +46,32 @@ describe("HOOK: useFireproof useLiveQuery has results", () => {
|
|
36
46
|
cnt++;
|
37
47
|
});
|
38
48
|
|
39
|
-
it(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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(["aha", "bar", "caz"]);
|
71
|
+
});
|
72
|
+
},
|
73
|
+
TEST_TIMEOUT,
|
74
|
+
);
|
65
75
|
|
66
76
|
afterEach(async () => {
|
67
77
|
await db.close();
|
@@ -89,93 +99,111 @@ describe("HOOK: useFireproof useDocument has results", () => {
|
|
89
99
|
});
|
90
100
|
});
|
91
101
|
|
92
|
-
it(
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
it(
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
docResult.
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
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
|
+
);
|
179
207
|
|
180
208
|
afterEach(async () => {
|
181
209
|
await db.close();
|
@@ -203,81 +231,101 @@ describe("HOOK: useFireproof useDocument has results reset sync", () => {
|
|
203
231
|
});
|
204
232
|
});
|
205
233
|
|
206
|
-
it(
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
it(
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
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
|
+
);
|
281
329
|
|
282
330
|
afterEach(async () => {
|
283
331
|
await db.close();
|
@@ -308,38 +356,50 @@ describe("HOOK: useFireproof useDocument with existing document has results", ()
|
|
308
356
|
});
|
309
357
|
});
|
310
358
|
|
311
|
-
it(
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
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
|
+
);
|
343
403
|
|
344
404
|
afterEach(async () => {
|
345
405
|
await db.close();
|
@@ -370,39 +430,51 @@ describe("HOOK: useFireproof useDocument with existing document handles external
|
|
370
430
|
});
|
371
431
|
});
|
372
432
|
|
373
|
-
it(
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
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
|
+
);
|
406
478
|
|
407
479
|
afterEach(async () => {
|
408
480
|
await db.close();
|
@@ -430,45 +502,49 @@ describe("HOOK: useFireproof bug fix: once the ID is set, it can reset", () => {
|
|
430
502
|
});
|
431
503
|
});
|
432
504
|
|
433
|
-
it(
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
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
|
+
);
|
472
548
|
|
473
549
|
afterEach(async () => {
|
474
550
|
await db.close();
|
@@ -492,24 +568,28 @@ describe("HOOK: useFireproof race condition: calling save() without await overwr
|
|
492
568
|
});
|
493
569
|
});
|
494
570
|
|
495
|
-
it(
|
496
|
-
|
497
|
-
|
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" });
|
498
576
|
|
499
|
-
|
500
|
-
|
501
|
-
|
577
|
+
// Call save() but DO NOT await it, then immediately reset().
|
578
|
+
docResult.save();
|
579
|
+
docResult.reset();
|
502
580
|
|
503
|
-
|
504
|
-
|
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));
|
505
583
|
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
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
|
+
);
|
513
593
|
|
514
594
|
afterEach(async () => {
|
515
595
|
await db.close();
|
@@ -531,22 +611,26 @@ describe("useFireproo calling submit()", () => {
|
|
531
611
|
});
|
532
612
|
});
|
533
613
|
|
534
|
-
it(
|
535
|
-
|
536
|
-
|
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" });
|
537
619
|
|
538
|
-
|
620
|
+
docResult.submit();
|
539
621
|
|
540
|
-
|
541
|
-
|
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));
|
542
624
|
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
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
|
+
);
|
550
634
|
|
551
635
|
afterEach(async () => {
|
552
636
|
await db.close();
|