@@ -2,7 +2,7 @@ import "fake-indexeddb/auto";
2
2
3
3
import { describe , expect , test , vi } from "vitest" ;
4
4
import { act , renderHook } from "@testing-library/react" ;
5
- import useDb , { type UseDbOptions } from "./index.js" ;
5
+ import useDb from "./index.js" ;
6
6
import { DbStorage } from "local-db-storage" ;
7
7
8
8
describe ( "use-db" , ( ) => {
@@ -31,14 +31,16 @@ describe("use-db", () => {
31
31
expect ( todos ) . toStrictEqual ( [ "first" , "second" ] ) ;
32
32
} ) ;
33
33
34
- test ( "updates state" , ( ) => {
34
+ test ( "updates state" , async ( ) => {
35
35
const key = crypto . randomUUID ( ) ;
36
36
const { result } = renderHook ( ( ) =>
37
37
useDb ( key , {
38
38
defaultValue : [ "first" , "second" ] ,
39
39
} ) ,
40
40
) ;
41
41
42
+ await wait ( 5 ) ;
43
+
42
44
act ( ( ) => {
43
45
const setTodos = result . current [ 1 ] ;
44
46
setTodos ( [ "third" , "forth" ] ) ;
@@ -48,14 +50,16 @@ describe("use-db", () => {
48
50
expect ( todos ) . toStrictEqual ( [ "third" , "forth" ] ) ;
49
51
} ) ;
50
52
51
- test ( "updates state with callback function" , ( ) => {
53
+ test ( "updates state with callback function" , async ( ) => {
52
54
const key = crypto . randomUUID ( ) ;
53
55
const { result } = renderHook ( ( ) =>
54
56
useDb ( key , {
55
57
defaultValue : [ "first" , "second" ] ,
56
58
} ) ,
57
59
) ;
58
60
61
+ await wait ( 5 ) ;
62
+
59
63
act ( ( ) => {
60
64
const setTodos = result . current [ 1 ] ;
61
65
@@ -74,10 +78,12 @@ describe("use-db", () => {
74
78
} ) ,
75
79
) ;
76
80
81
+ await wait ( 5 ) ;
82
+
77
83
{
78
- await act ( ( ) => {
84
+ act ( ( ) => {
79
85
const setTodos = result . current [ 1 ] ;
80
- return setTodos ( [ "third" , "forth" ] ) ;
86
+ setTodos ( [ "third" , "forth" ] ) ;
81
87
} ) ;
82
88
const [ todos ] = result . current ;
83
89
expect ( todos ) . toStrictEqual ( [ "third" , "forth" ] ) ;
@@ -93,14 +99,16 @@ describe("use-db", () => {
93
99
}
94
100
} ) ;
95
101
96
- test ( "persists state across hook re-renders" , ( ) => {
102
+ test ( "persists state across hook re-renders" , async ( ) => {
97
103
const key = crypto . randomUUID ( ) ;
98
104
const { result, rerender } = renderHook ( ( ) =>
99
105
useDb ( key , {
100
106
defaultValue : [ "first" , "second" ] ,
101
107
} ) ,
102
108
) ;
103
109
110
+ await wait ( 5 ) ;
111
+
104
112
act ( ( ) => {
105
113
const setTodos = result . current [ 1 ] ;
106
114
setTodos ( [ "third" , "fourth" ] ) ;
@@ -112,7 +120,7 @@ describe("use-db", () => {
112
120
expect ( todos ) . toStrictEqual ( [ "third" , "fourth" ] ) ;
113
121
} ) ;
114
122
115
- test ( "handles complex objects" , ( ) => {
123
+ test ( "handles complex objects" , async ( ) => {
116
124
const complexObject = {
117
125
nested : { array : [ 1 , 2 , 3 ] , value : "test" } ,
118
126
} ;
@@ -121,6 +129,8 @@ describe("use-db", () => {
121
129
useDb ( key , { defaultValue : complexObject } ) ,
122
130
) ;
123
131
132
+ await wait ( 5 ) ;
133
+
124
134
const [ storedObject ] = result . current ;
125
135
expect ( storedObject ) . toEqual ( complexObject ) ;
126
136
@@ -138,10 +148,12 @@ describe("use-db", () => {
138
148
} ) ;
139
149
} ) ;
140
150
141
- test ( "handles undefined as a valid state" , ( ) => {
151
+ test ( "handles undefined as a valid state" , async ( ) => {
142
152
const key = crypto . randomUUID ( ) ;
143
153
const { result } = renderHook ( ( ) => useDb ( key ) ) ;
144
154
155
+ await wait ( 5 ) ;
156
+
145
157
const [ initialState ] = result . current ;
146
158
expect ( initialState ) . toBeUndefined ( ) ;
147
159
@@ -168,17 +180,21 @@ describe("use-db", () => {
168
180
unmount ( ) ;
169
181
} ) ;
170
182
171
- test ( "set state throws an error" , ( ) => {
183
+ test ( "set state throws an error" , async ( ) => {
172
184
const key = crypto . randomUUID ( ) ;
173
- const { result } = renderHook ( ( ) => useDb ( key ) ) ;
185
+ const hook = renderHook ( ( ) => useDb ( key ) ) ;
186
+
187
+ // no idea why this is needed.
188
+ // otherwise, it throws "unhadled error -- Vitest caught 1 error during the test run."
189
+ await wait ( 5 ) ;
174
190
175
191
vi . spyOn ( DbStorage . prototype , "setItem" ) . mockReturnValue (
176
192
Promise . reject ( "QuotaExceededError" ) ,
177
193
) ;
178
194
179
- act ( ( ) => {
180
- const setState = result . current [ 1 ] ;
181
- setState ( "defined" ) ;
195
+ await act ( ( ) => {
196
+ const [ , setState ] = hook . result . current ;
197
+ return setState ( "defined" ) ;
182
198
} ) ;
183
199
} ) ;
184
200
@@ -227,6 +243,29 @@ describe("use-db", () => {
227
243
const [ number ] = result . current ;
228
244
expect ( number ) . toBe ( 2 ) ;
229
245
} ) ;
246
+
247
+ // https://github.com/astoilkov/use-db/issues/1
248
+ test ( "cannot read state from IndexDB after page refresh" , async ( ) => {
249
+ const key = crypto . randomUUID ( ) ;
250
+
251
+ const dbStorage = new DbStorage ( {
252
+ name : "node_modules/use-db" ,
253
+ } ) ;
254
+ await dbStorage . setItem ( key , [ "first" , "second" ] ) ;
255
+
256
+ const hook = renderHook ( ( ) => useDb ( key ) ) ;
257
+ const todos = await vi . waitUntil (
258
+ ( ) => {
259
+ const [ todos ] = hook . result . current ;
260
+ return todos ;
261
+ } ,
262
+ {
263
+ timeout : 100 ,
264
+ interval : 10 ,
265
+ } ,
266
+ ) ;
267
+ expect ( todos ) . toStrictEqual ( [ "first" , "second" ] ) ;
268
+ } ) ;
230
269
} ) ;
231
270
232
271
describe ( "non-optimistic" , ( ) => {
@@ -290,3 +329,9 @@ describe("use-db", () => {
290
329
} ) ;
291
330
} ) ;
292
331
} ) ;
332
+
333
+ function wait ( ms : number ) : Promise < void > {
334
+ return new Promise ( ( resolve ) => {
335
+ setTimeout ( resolve , ms ) ;
336
+ } ) ;
337
+ }
0 commit comments