@@ -1281,13 +1281,52 @@ apiDescribe('Queries', (persistence: boolean) => {
1281
1281
} ;
1282
1282
1283
1283
return withTestCollection ( persistence , testDocs , async coll => {
1284
- await getDocs ( query ( coll ) ) ; // Populate the cache
1284
+ await getDocs ( query ( coll ) ) ; // Populate the cache.
1285
1285
const snapshot = await getDocs (
1286
1286
query ( coll , where ( 'map.nested' , '==' , 'foo' ) )
1287
1287
) ;
1288
1288
expect ( toDataArray ( snapshot ) ) . to . deep . equal ( [ { map : { nested : 'foo' } } ] ) ;
1289
1289
} ) ;
1290
1290
} ) ;
1291
+
1292
+ // Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
1293
+ // eslint-disable-next-line no-restricted-properties
1294
+ ( persistence ? describe : describe . skip ) ( 'Caching empty results' , ( ) => {
1295
+ it ( 'can raise initial snapshot from cache, even if it is empty' , ( ) => {
1296
+ return withTestCollection ( persistence , { } , async coll => {
1297
+ const snapshot1 = await getDocs ( coll ) ; // Populate the cache.
1298
+ expect ( snapshot1 . metadata . fromCache ) . to . be . false ;
1299
+ expect ( toDataArray ( snapshot1 ) ) . to . deep . equal ( [ ] ) ; // Precondition check.
1300
+
1301
+ // Add a snapshot listener whose first event should be raised from cache.
1302
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1303
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1304
+ const snapshot2 = await storeEvent . awaitEvent ( ) ;
1305
+ expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1306
+ expect ( toDataArray ( snapshot2 ) ) . to . deep . equal ( [ ] ) ;
1307
+ } ) ;
1308
+ } ) ;
1309
+
1310
+ it ( 'can raise initial snapshot from cache, even if it has become empty' , ( ) => {
1311
+ const testDocs = {
1312
+ a : { key : 'a' }
1313
+ } ;
1314
+ return withTestCollection ( persistence , testDocs , async coll => {
1315
+ // Populate the cache.
1316
+ const snapshot1 = await getDocs ( coll ) ;
1317
+ expect ( snapshot1 . metadata . fromCache ) . to . be . false ;
1318
+ expect ( toDataArray ( snapshot1 ) ) . to . deep . equal ( [ { key : 'a' } ] ) ;
1319
+ // Empty the collection.
1320
+ void deleteDoc ( doc ( coll , 'a' ) ) ;
1321
+
1322
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1323
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1324
+ const snapshot2 = await storeEvent . awaitEvent ( ) ;
1325
+ expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1326
+ expect ( toDataArray ( snapshot2 ) ) . to . deep . equal ( [ ] ) ;
1327
+ } ) ;
1328
+ } ) ;
1329
+ } ) ;
1291
1330
} ) ;
1292
1331
1293
1332
function verifyDocumentChange < T > (
0 commit comments