1
1
import "fake-indexeddb/auto" ;
2
2
3
- import { describe , expect , test } from "vitest" ;
3
+ import { describe , expect , test , vitest } from "vitest" ;
4
4
import { act , renderHook } from "@testing-library/react" ;
5
5
import useDb , { type UseDbOptions } from "./index.js" ;
6
+ import { DbStorage } from "local-db-storage" ;
6
7
7
8
describe ( "use-db" , ( ) => {
8
9
describe ( "optimistic" , ( ) => {
@@ -65,7 +66,7 @@ describe("use-db", () => {
65
66
expect ( todos ) . toStrictEqual ( [ "first" , "second" , "third" , "forth" ] ) ;
66
67
} ) ;
67
68
68
- test ( "removes item from state" , ( ) => {
69
+ test ( "removes item from state" , async ( ) => {
69
70
const key = crypto . randomUUID ( ) ;
70
71
const { result } = renderHook ( ( ) =>
71
72
useDb ( key , {
@@ -74,9 +75,9 @@ describe("use-db", () => {
74
75
) ;
75
76
76
77
{
77
- act ( ( ) => {
78
+ await act ( ( ) => {
78
79
const setTodos = result . current [ 1 ] ;
79
- setTodos ( [ "third" , "forth" ] ) ;
80
+ return setTodos ( [ "third" , "forth" ] ) ;
80
81
} ) ;
81
82
const [ todos ] = result . current ;
82
83
expect ( todos ) . toStrictEqual ( [ "third" , "forth" ] ) ;
@@ -166,6 +167,66 @@ describe("use-db", () => {
166
167
const { unmount } = renderHook ( ( ) => useDb ( key ) ) ;
167
168
unmount ( ) ;
168
169
} ) ;
170
+
171
+ test ( "set state throws an error" , ( ) => {
172
+ const key = crypto . randomUUID ( ) ;
173
+ const { result } = renderHook ( ( ) => useDb ( key ) ) ;
174
+
175
+ vitest
176
+ . spyOn ( DbStorage . prototype , "setItem" )
177
+ . mockReturnValue ( Promise . reject ( "QuotaExceededError" ) ) ;
178
+
179
+ act ( ( ) => {
180
+ const setState = result . current [ 1 ] ;
181
+ setState ( "defined" ) ;
182
+ } ) ;
183
+ } ) ;
184
+
185
+ test ( "set state throws an error and reverts to previous state" , async ( ) => {
186
+ const key = crypto . randomUUID ( ) ;
187
+ const { result } = renderHook ( ( ) => useDb ( key ) ) ;
188
+
189
+ await act ( ( ) => {
190
+ const setState = result . current [ 1 ] ;
191
+ return setState ( 1 ) ;
192
+ } ) ;
193
+
194
+ vitest
195
+ . spyOn ( DbStorage . prototype , "setItem" )
196
+ . mockReturnValue ( Promise . reject ( "QuotaExceededError" ) ) ;
197
+
198
+ await act ( ( ) => {
199
+ const setState = result . current [ 1 ] ;
200
+ return setState ( 2 ) ;
201
+ } ) ;
202
+
203
+ const [ number ] = result . current ;
204
+ expect ( number ) . toBe ( 1 ) ;
205
+ } ) ;
206
+
207
+ test ( "remove item throws an error and reverts to previous state" , async ( ) => {
208
+ const key = crypto . randomUUID ( ) ;
209
+ const { result } = renderHook ( ( ) =>
210
+ useDb ( key , { defaultValue : 1 } ) ,
211
+ ) ;
212
+
213
+ await act ( ( ) => {
214
+ const [ , setNumber ] = result . current ;
215
+ return setNumber ( 2 ) ;
216
+ } ) ;
217
+
218
+ vitest
219
+ . spyOn ( DbStorage . prototype , "removeItem" )
220
+ . mockReturnValue ( Promise . reject ( "QuotaExceededError" ) ) ;
221
+
222
+ await act ( ( ) => {
223
+ const removeItem = result . current [ 2 ] ;
224
+ return removeItem ( ) ;
225
+ } ) ;
226
+
227
+ const [ number ] = result . current ;
228
+ expect ( number ) . toBe ( 2 ) ;
229
+ } ) ;
169
230
} ) ;
170
231
171
232
describe ( "non-optimistic" , ( ) => {
@@ -199,5 +260,33 @@ describe("use-db", () => {
199
260
const [ todos ] = result . current ;
200
261
expect ( todos ) . toStrictEqual ( [ "third" , "forth" ] ) ;
201
262
} ) ;
263
+
264
+ test ( "removes item from state" , async ( ) => {
265
+ const key = crypto . randomUUID ( ) ;
266
+ const { result } = renderHook ( ( ) =>
267
+ useDb ( key , {
268
+ optimistic : false ,
269
+ defaultValue : [ "first" , "second" ] ,
270
+ } ) ,
271
+ ) ;
272
+
273
+ {
274
+ await act ( ( ) => {
275
+ const setTodos = result . current [ 1 ] ;
276
+ return setTodos ( [ "third" , "forth" ] ) ;
277
+ } ) ;
278
+ const [ todos ] = result . current ;
279
+ expect ( todos ) . toStrictEqual ( [ "third" , "forth" ] ) ;
280
+ }
281
+
282
+ {
283
+ await act ( ( ) => {
284
+ const removeItem = result . current [ 2 ] ;
285
+ return removeItem ( ) ;
286
+ } ) ;
287
+ const [ todos ] = result . current ;
288
+ expect ( todos ) . toStrictEqual ( [ "first" , "second" ] ) ;
289
+ }
290
+ } ) ;
202
291
} ) ;
203
292
} ) ;
0 commit comments