@fireproof/core 0.20.0-dev-preview-58 → 0.20.0-dev-preview-60
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/deno.json +1 -1
- package/index.cjs +90 -91
- package/index.cjs.map +1 -1
- package/index.js +12 -13
- package/index.js.map +1 -1
- package/metafile-cjs.json +1 -1
- package/metafile-esm.json +1 -1
- package/package.json +2 -2
- package/react/index.cjs +91 -91
- package/react/index.cjs.map +1 -1
- package/react/index.d.cts +13 -41
- package/react/index.d.ts +13 -41
- package/react/index.js +90 -90
- package/react/index.js.map +1 -1
- package/react/metafile-cjs.json +1 -1
- package/react/metafile-esm.json +1 -1
- package/tests/fireproof/attachable.test.ts +1 -2
- package/tests/fireproof/crdt.test.ts +14 -4
- package/tests/react/use-fireproof-db-switch.test.tsx +91 -0
- package/tests/react/{useFireproof.test.tsx → use-fireproof.test.tsx} +92 -13
@@ -2,6 +2,7 @@ import { renderHook, waitFor } from "@testing-library/react";
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
3
3
|
import { fireproof, useFireproof } from "use-fireproof";
|
4
4
|
import type { Database, DocResponse, LiveQueryResult, UseDocumentResult } from "use-fireproof";
|
5
|
+
import { DocWithId } from "@fireproof/core";
|
5
6
|
|
6
7
|
// Test timeout value for CI
|
7
8
|
const TEST_TIMEOUT = 45000;
|
@@ -36,38 +37,116 @@ describe("HOOK: useFireproof useLiveQuery has results", () => {
|
|
36
37
|
database: ReturnType<typeof useFireproof>["database"],
|
37
38
|
useLiveQuery: ReturnType<typeof useFireproof>["useLiveQuery"];
|
38
39
|
|
39
|
-
let cnt = 0;
|
40
|
-
|
41
40
|
beforeEach(async () => {
|
41
|
+
const expectedValues = ["aha", "bar", "caz"];
|
42
42
|
db = fireproof(dbName);
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
for (const value of expectedValues) {
|
44
|
+
await db.put({ foo: value });
|
45
|
+
}
|
46
|
+
|
47
|
+
const allDocs = await db.allDocs<{ foo: string }>();
|
48
|
+
expect(allDocs.rows.map((row) => row.value.foo)).toEqual(expectedValues);
|
47
49
|
});
|
48
50
|
|
49
51
|
it(
|
50
|
-
"
|
52
|
+
"queries correctly",
|
51
53
|
async () => {
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
renderHook(() => {
|
55
|
+
const result = useFireproof(dbName);
|
56
|
+
database = result.database;
|
57
|
+
useLiveQuery = result.useLiveQuery;
|
58
|
+
query = useLiveQuery<{ foo: string }>("foo");
|
59
|
+
});
|
60
|
+
|
61
|
+
await waitFor(() => {
|
62
|
+
expect(query.rows.map((row) => row.doc?.foo)).toEqual(["aha", "bar", "caz"]);
|
63
|
+
});
|
55
64
|
},
|
56
65
|
TEST_TIMEOUT,
|
57
66
|
);
|
58
67
|
|
68
|
+
afterEach(async () => {
|
69
|
+
await db.close();
|
70
|
+
await db.destroy();
|
71
|
+
await database?.close();
|
72
|
+
await database?.destroy();
|
73
|
+
});
|
74
|
+
});
|
75
|
+
|
76
|
+
// Separate describe block for array-like behavior test
|
77
|
+
describe("HOOK: useFireproof useLiveQuery supports array-like behavior", () => {
|
78
|
+
const dbName = "useLiveQueryArrayLike";
|
79
|
+
let db: Database,
|
80
|
+
database: ReturnType<typeof useFireproof>["database"],
|
81
|
+
useLiveQuery: ReturnType<typeof useFireproof>["useLiveQuery"];
|
82
|
+
|
83
|
+
beforeEach(async () => {
|
84
|
+
const expectedValues = ["aha", "bar", "caz"];
|
85
|
+
db = fireproof(dbName);
|
86
|
+
for (const value of expectedValues) {
|
87
|
+
await db.put({ foo: value });
|
88
|
+
}
|
89
|
+
|
90
|
+
const allDocs = await db.allDocs<{ foo: string }>();
|
91
|
+
expect(allDocs.rows.map((row) => row.value.foo)).toEqual(expectedValues);
|
92
|
+
});
|
93
|
+
|
59
94
|
it(
|
60
|
-
"
|
95
|
+
"supports array-like behavior",
|
61
96
|
async () => {
|
97
|
+
let arrayLikeQuery: LiveQueryResult<{ foo: string }, string>;
|
98
|
+
let destructuredDocs: DocWithId<{ foo: string }>[];
|
99
|
+
|
62
100
|
renderHook(() => {
|
63
101
|
const result = useFireproof(dbName);
|
64
102
|
database = result.database;
|
65
103
|
useLiveQuery = result.useLiveQuery;
|
66
|
-
|
104
|
+
arrayLikeQuery = useLiveQuery<{ foo: string }>("foo");
|
105
|
+
|
106
|
+
// Test destructuring the docs property
|
107
|
+
const { docs } = arrayLikeQuery;
|
108
|
+
destructuredDocs = docs;
|
67
109
|
});
|
68
110
|
|
69
111
|
await waitFor(() => {
|
70
|
-
|
112
|
+
// Verify destructured docs
|
113
|
+
expect(destructuredDocs.length).toBe(3);
|
114
|
+
expect(destructuredDocs.map((doc) => doc.foo)).toEqual(["aha", "bar", "caz"]);
|
115
|
+
|
116
|
+
// Verify array-like behavior - need to use type assertions since our interface doesn't include these methods
|
117
|
+
const arrayLike = arrayLikeQuery as unknown as {
|
118
|
+
length: number;
|
119
|
+
[Symbol.iterator](): Iterator<DocWithId<{ foo: string }>>;
|
120
|
+
map<U>(fn: (doc: DocWithId<{ foo: string }>) => U): U[];
|
121
|
+
filter(fn: (doc: DocWithId<{ foo: string }>) => boolean): DocWithId<{ foo: string }>[];
|
122
|
+
forEach(fn: (doc: DocWithId<{ foo: string }>) => void): void;
|
123
|
+
};
|
124
|
+
|
125
|
+
// Test length property
|
126
|
+
expect(arrayLike.length).toBe(3);
|
127
|
+
|
128
|
+
// Test iteration
|
129
|
+
const values: string[] = [];
|
130
|
+
for (const doc of arrayLike) {
|
131
|
+
values.push(doc.foo);
|
132
|
+
}
|
133
|
+
expect(values).toEqual(["aha", "bar", "caz"]);
|
134
|
+
|
135
|
+
// Test map
|
136
|
+
const mappedValues = arrayLike.map((doc) => doc.foo.toUpperCase());
|
137
|
+
expect(mappedValues).toEqual(["AHA", "BAR", "CAZ"]);
|
138
|
+
|
139
|
+
// Test filter
|
140
|
+
const filtered = arrayLike.filter((doc) => doc.foo.includes("a"));
|
141
|
+
expect(filtered.length).toBe(3);
|
142
|
+
expect(filtered.map((doc) => doc.foo)).toEqual(["aha", "bar", "caz"]);
|
143
|
+
|
144
|
+
// Test forEach
|
145
|
+
const forEachValues: string[] = [];
|
146
|
+
arrayLike.forEach((doc) => {
|
147
|
+
forEachValues.push(doc.foo);
|
148
|
+
});
|
149
|
+
expect(forEachValues).toEqual(["aha", "bar", "caz"]);
|
71
150
|
});
|
72
151
|
},
|
73
152
|
TEST_TIMEOUT,
|