@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.
@@ -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
- await db.put({ foo: "aha", cnt });
44
- await db.put({ foo: "bar", cnt });
45
- await db.put({ foo: "caz", cnt });
46
- cnt++;
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
- "should have setup data",
52
+ "queries correctly",
51
53
  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);
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
- "queries correctly",
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
- query = useLiveQuery<{ foo: string }>("foo");
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
- expect(query.rows.map((row) => row.doc?.foo)).toEqual(["aha", "bar", "caz"]);
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,