@@ -1290,19 +1290,70 @@ apiDescribe('Queries', (persistence: boolean) => {
1290
1290
} ) ;
1291
1291
1292
1292
// eslint-disable-next-line no-restricted-properties
1293
- ( persistence ? it : it . skip ) ( 'empty query results are cached' , ( ) => {
1294
- // Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
1295
- return withTestCollection ( persistence , { } , async coll => {
1296
- const snapshot1 = await getDocs ( coll ) ; // Populate the cache
1297
- expect ( snapshot1 . metadata . fromCache ) . to . be . false ;
1298
- expect ( toDataArray ( snapshot1 ) ) . to . deep . equal ( [ ] ) ; // Precondition check
1299
-
1300
- // Add a snapshot listener whose first event should be raised from cache.
1301
- const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1302
- onSnapshot ( coll , storeEvent . storeEvent ) ;
1303
- const snapshot2 = await storeEvent . awaitEvent ( ) ;
1304
- expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1305
- expect ( toDataArray ( snapshot2 ) ) . to . deep . equal ( [ ] ) ;
1293
+ // Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
1294
+ ( persistence ? describe . only : describe . skip ) ( 'Caching empty results ' , ( ) => {
1295
+ it ( 'can cache empty query results' , ( ) => {
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 empty cached collection and raise snapshot from it' , ( ) => {
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
+ deleteDoc ( doc ( coll , 'a' ) ) ;
1321
+
1322
+ // Add a snapshot listener whose first event should be raised from cache.
1323
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1324
+ onSnapshot (
1325
+ coll ,
1326
+ { includeMetadataChanges : true } ,
1327
+ storeEvent . storeEvent
1328
+ ) ;
1329
+ const snapshot2 = await storeEvent . awaitEvent ( ) ;
1330
+ expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1331
+ expect ( toDataArray ( snapshot2 ) ) . to . deep . equal ( [ ] ) ;
1332
+
1333
+ // why this if fromCahe:false ????
1334
+ const snapshot3 = await storeEvent . awaitEvent ( ) ;
1335
+ expect ( snapshot3 . metadata . fromCache ) . to . be . false ;
1336
+ expect ( toDataArray ( snapshot3 ) ) . to . deep . equal ( [ ] ) ;
1337
+ } ) ;
1338
+ } ) ;
1339
+
1340
+ it ( 'can add new doc to cached empty query result' , ( ) => {
1341
+ return withTestCollection ( persistence , { } , async coll => {
1342
+ await getDocs ( coll ) ; // Populate the cache
1343
+
1344
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1345
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1346
+ const snapshot1 = await storeEvent . awaitEvent ( ) ;
1347
+ expect ( snapshot1 . metadata . fromCache ) . to . be . true ;
1348
+ expect ( toDataArray ( snapshot1 ) ) . to . deep . equal ( [ ] ) ;
1349
+
1350
+ await addDoc ( coll , { key : 'a' } ) ;
1351
+
1352
+ const snapshot2 = await storeEvent . awaitEvent ( ) ;
1353
+ expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1354
+ expect ( toDataArray ( snapshot2 ) ) . to . deep . equal ( [ { key : 'a' } ] ) ;
1355
+ expect ( snapshot2 . metadata . hasPendingWrites ) . to . equal ( true ) ;
1356
+ } ) ;
1306
1357
} ) ;
1307
1358
} ) ;
1308
1359
} ) ;
0 commit comments