@@ -119,6 +119,56 @@ describe('QueryCache', () => {
119
119
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
120
120
} ) ;
121
121
122
+ it ( 'should store count-only queries' , async ( ) => {
123
+ const map = new Map ( ) ;
124
+
125
+ const cache = new QueryCache ( ctx , {
126
+ stores : [ new MemoryStore ( { persistentMap : map } ) ] ,
127
+ fresh : 1000 ,
128
+ stale : 2000 ,
129
+ } ) ;
130
+
131
+ const query = client
132
+ . from ( 'contact' )
133
+ . select ( 'id,username' , { count : 'exact' , head : true } )
134
+ . ilike ( 'username' , `${ testRunPrefix } %` ) ;
135
+
136
+ const spy = vi . spyOn ( query , 'then' ) ;
137
+
138
+ const res = await cache . query ( query ) ;
139
+
140
+ const res2 = await cache . query ( query ) ;
141
+
142
+ expect ( res . count ) . toEqual ( 4 ) ;
143
+ expect ( res2 . count ) . toEqual ( 4 ) ;
144
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
145
+ } ) ;
146
+
147
+ it ( 'should not store empty results' , async ( ) => {
148
+ const map = new Map ( ) ;
149
+
150
+ const cache = new QueryCache ( ctx , {
151
+ stores : [ new MemoryStore ( { persistentMap : map } ) ] ,
152
+ fresh : 1000 ,
153
+ stale : 2000 ,
154
+ } ) ;
155
+
156
+ const query = client
157
+ . from ( 'contact' )
158
+ . select ( 'id,username' )
159
+ . eq ( 'username' , 'unknown' )
160
+ . maybeSingle ( ) ;
161
+
162
+ const spy = vi . spyOn ( query , 'then' ) ;
163
+
164
+ const res = await cache . query ( query ) ;
165
+ const res2 = await cache . query ( query ) ;
166
+
167
+ expect ( res . data ) . toBeFalsy ( ) ;
168
+ expect ( res2 . data ) . toBeFalsy ( ) ;
169
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
170
+ } ) ;
171
+
122
172
it ( 'should not store result if store() returns false' , async ( ) => {
123
173
const map = new Map ( ) ;
124
174
@@ -249,6 +299,55 @@ describe('QueryCache', () => {
249
299
expect ( res2 . count ) . toEqual ( 4 ) ;
250
300
expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
251
301
} ) ;
302
+
303
+ it ( 'should store count-only queries' , async ( ) => {
304
+ const map = new Map ( ) ;
305
+
306
+ const cache = new QueryCache ( ctx , {
307
+ stores : [ new MemoryStore ( { persistentMap : map } ) ] ,
308
+ fresh : 1000 ,
309
+ stale : 2000 ,
310
+ } ) ;
311
+
312
+ const query = client
313
+ . from ( 'contact' )
314
+ . select ( 'id,username' , { count : 'exact' , head : true } )
315
+ . ilike ( 'username' , `${ testRunPrefix } %` ) ;
316
+
317
+ const spy = vi . spyOn ( query , 'then' ) ;
318
+
319
+ const res = await cache . swr ( query ) ;
320
+
321
+ const res2 = await cache . swr ( query ) ;
322
+
323
+ expect ( res . count ) . toEqual ( 4 ) ;
324
+ expect ( res2 . count ) . toEqual ( 4 ) ;
325
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
326
+ } ) ;
327
+
328
+ it ( 'should not store empty results' , async ( ) => {
329
+ const map = new Map ( ) ;
330
+
331
+ const cache = new QueryCache ( ctx , {
332
+ stores : [ new MemoryStore ( { persistentMap : map } ) ] ,
333
+ fresh : 1000 ,
334
+ stale : 2000 ,
335
+ } ) ;
336
+
337
+ const query = client
338
+ . from ( 'contact' )
339
+ . select ( 'id,username' )
340
+ . ilike ( 'username' , 'unknown' ) ;
341
+
342
+ const spy = vi . spyOn ( query , 'then' ) ;
343
+
344
+ const res = await cache . swr ( query ) ;
345
+ const res2 = await cache . swr ( query ) ;
346
+
347
+ expect ( res . data ) . toEqual ( [ ] ) ;
348
+ expect ( res2 . data ) . toEqual ( [ ] ) ;
349
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
350
+ } ) ;
252
351
} ) ;
253
352
254
353
it ( 'should dedupe' , async ( ) => {
0 commit comments