1
+ import type { Ref } from 'vue'
1
2
import { get , set } from 'idb-keyval'
2
- import { beforeEach , describe , expect , it , vi } from 'vitest'
3
+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
3
4
import { nextTick } from 'vue'
4
5
import { useIDBKeyval } from './index'
5
6
6
- const cache = { } as any
7
+ const cache = new Map < string , unknown > ( )
7
8
8
9
vi . mock ( 'idb-keyval' , ( ) => ( {
9
- get : ( key : string ) => Promise . resolve ( cache [ key ] ) ,
10
+ get : ( key : string ) => Promise . resolve ( cache . get ( key ) ) ,
10
11
set : vi . fn ( ( key : string , value : any ) => new Promise ( ( resolve , reject ) => {
11
12
if ( value === 'error' ) {
12
13
reject ( new Error ( 'set error' ) )
13
14
return
14
15
}
15
16
16
- cache [ key ] = value
17
+ cache . set ( key , value )
17
18
18
19
resolve ( undefined )
19
20
} ) ) ,
@@ -24,30 +25,39 @@ vi.mock('idb-keyval', () => ({
24
25
return
25
26
}
26
27
27
- cache [ key ] = value
28
+ cache . set ( key , value )
28
29
29
30
resolve ( undefined )
30
31
} ) ,
31
32
del : ( key : string ) => {
32
- delete cache [ key ]
33
+ cache . delete ( key )
33
34
} ,
34
35
} ) )
35
36
36
37
const KEY1 = 'vue-use-idb-keyval-1'
37
38
const KEY2 = 'vue-use-idb-keyval-2'
38
39
const KEY3 = 'vue-use-idb-keyval-3'
39
40
const KEY4 = 'vue-use-idb-keyval-4'
41
+
40
42
describe ( 'useIDBKeyval' , ( ) => {
41
- beforeEach ( ( ) => {
42
- vi . clearAllMocks ( )
43
+ let data1 : Ref < { count : number } | null >
44
+ let data2 : Ref < string [ ] | null >
45
+ let data3 : Ref < string | null >
46
+
47
+ beforeEach ( async ( ) => {
43
48
console . error = vi . fn ( )
44
- } )
45
49
46
- set ( KEY3 , 'hello' )
50
+ await set ( KEY3 , 'hello' ) ;
47
51
48
- const { data : data1 } = useIDBKeyval ( KEY1 , { count : 0 } )
49
- const { data : data2 } = useIDBKeyval ( KEY2 , [ 'foo' , 'bar' ] )
50
- const { data : data3 } = useIDBKeyval ( KEY3 , 'world' , { shallow : true } )
52
+ ( { data : data1 } = useIDBKeyval ( KEY1 , { count : 0 } ) ) ;
53
+ ( { data : data2 } = useIDBKeyval ( KEY2 , [ 'foo' , 'bar' ] ) ) ;
54
+ ( { data : data3 } = useIDBKeyval ( KEY3 , 'world' , { shallow : true } ) )
55
+ } )
56
+
57
+ afterEach ( ( ) => {
58
+ vi . clearAllMocks ( )
59
+ cache . clear ( )
60
+ } )
51
61
52
62
it ( 'get/set' , async ( ) => {
53
63
expect ( data1 . value ) . toEqual ( { count : 0 } )
@@ -60,8 +70,8 @@ describe('useIDBKeyval', () => {
60
70
} )
61
71
62
72
it ( 'update' , async ( ) => {
63
- data1 . value . count ++
64
- data2 . value . push ( 'woo' )
73
+ data1 . value ! . count ++
74
+ data2 . value ! . push ( 'woo' )
65
75
data3 . value = 'world'
66
76
67
77
expect ( await get ( KEY1 ) ) . toEqual ( data1 . value )
@@ -109,6 +119,43 @@ describe('useIDBKeyval', () => {
109
119
it ( 'writeDefaults false' , async ( ) => {
110
120
useIDBKeyval ( KEY4 , 'test' , { writeDefaults : false } )
111
121
112
- expect ( set ) . toHaveBeenCalledTimes ( 0 )
122
+ // the initial 3 IDB calls minus this one
123
+ expect ( set ) . toHaveBeenCalledTimes ( 3 )
124
+ } )
125
+ it ( 'get/set with serializer' , async ( ) => {
126
+ const serializer = {
127
+ read ( raw : unknown ) : string {
128
+ return raw === 1 ? 'foo' : 'bar'
129
+ } ,
130
+ write ( value : string ) : unknown {
131
+ return value === 'foo' ? 1 : 0
132
+ } ,
133
+ }
134
+
135
+ const { data } = useIDBKeyval ( KEY4 , 'foo' , { serializer } )
136
+
137
+ await nextTick ( )
138
+
139
+ expect ( data . value ) . toEqual ( 'foo' )
140
+ expect ( await get ( KEY4 ) ) . toEqual ( 1 )
141
+ } )
142
+ it ( 'get/set with serializer and existing value' , async ( ) => {
143
+ const serializer = {
144
+ read ( raw : unknown ) : string {
145
+ return raw === 1 ? 'foo' : 'bar'
146
+ } ,
147
+ write ( value : string ) : unknown {
148
+ return value === 'foo' ? 1 : 0
149
+ } ,
150
+ }
151
+
152
+ await set ( KEY4 , 0 )
153
+
154
+ const { data } = useIDBKeyval ( KEY4 , 'foo' , { serializer } )
155
+
156
+ await nextTick ( )
157
+
158
+ expect ( data . value ) . toEqual ( 'bar' )
159
+ expect ( await get ( KEY4 ) ) . toEqual ( 0 )
113
160
} )
114
161
} )
0 commit comments