@@ -1291,7 +1291,7 @@ apiDescribe('Queries', (persistence: boolean) => {
1291
1291
1292
1292
// eslint-disable-next-line no-restricted-properties
1293
1293
// Reproduces https://github.com/firebase/firebase-js-sdk/issues/5873
1294
- ( persistence ? describe . only : describe . skip ) ( 'Caching empty results ' , ( ) => {
1294
+ ( persistence ? describe : describe . skip ) ( 'Caching empty results ' , ( ) => {
1295
1295
it ( 'can cache empty query results' , ( ) => {
1296
1296
return withTestCollection ( persistence , { } , async coll => {
1297
1297
const snapshot1 = await getDocs ( coll ) ; // Populate the cache
@@ -1321,39 +1321,58 @@ apiDescribe('Queries', (persistence: boolean) => {
1321
1321
1322
1322
// Add a snapshot listener whose first event should be raised from cache.
1323
1323
const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1324
- onSnapshot (
1325
- coll ,
1326
- { includeMetadataChanges : true } ,
1327
- storeEvent . storeEvent
1328
- ) ;
1324
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1329
1325
const snapshot2 = await storeEvent . awaitEvent ( ) ;
1330
1326
expect ( snapshot2 . metadata . fromCache ) . to . be . true ;
1331
1327
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
1328
} ) ;
1338
1329
} ) ;
1339
1330
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' } ) ;
1331
+ it ( 'can raise snapshot from a cached collection which was emptied offline' , ( ) => {
1332
+ const testDocs = {
1333
+ a : { key : 'a' }
1334
+ } ;
1335
+ return withTestCollection (
1336
+ persistence ,
1337
+ testDocs ,
1338
+ async ( coll , firestore ) => {
1339
+ await getDocs ( coll ) ; // Populate the cache
1340
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1341
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1342
+ await storeEvent . awaitEvent ( ) ;
1343
+
1344
+ await disableNetwork ( firestore ) ;
1345
+ deleteDoc ( doc ( coll , 'a' ) ) ;
1346
+ await enableNetwork ( firestore ) ;
1347
+
1348
+ const snapshot = await storeEvent . awaitEvent ( ) ;
1349
+ expect ( snapshot . metadata . fromCache ) . to . be . true ;
1350
+ expect ( toDataArray ( snapshot ) ) . to . deep . equal ( [ ] ) ;
1351
+ }
1352
+ ) ;
1353
+ } ) ;
1351
1354
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
- } ) ;
1355
+ it ( 'can register a listener and empty cache offline, and raise snaoshot from it when came back online' , ( ) => {
1356
+ const testDocs = {
1357
+ a : { key : 'a' }
1358
+ } ;
1359
+ return withTestCollection (
1360
+ persistence ,
1361
+ testDocs ,
1362
+ async ( coll , firestore ) => {
1363
+ await getDocs ( coll ) ; // Populate the cache
1364
+ await disableNetwork ( firestore ) ;
1365
+ const storeEvent = new EventsAccumulator < QuerySnapshot > ( ) ;
1366
+ onSnapshot ( coll , storeEvent . storeEvent ) ;
1367
+ await storeEvent . awaitEvent ( ) ;
1368
+ deleteDoc ( doc ( coll , 'a' ) ) ;
1369
+ await enableNetwork ( firestore ) ;
1370
+
1371
+ const snapshot = await storeEvent . awaitEvent ( ) ;
1372
+ expect ( snapshot . metadata . fromCache ) . to . be . true ;
1373
+ expect ( toDataArray ( snapshot ) ) . to . deep . equal ( [ ] ) ;
1374
+ }
1375
+ ) ;
1357
1376
} ) ;
1358
1377
} ) ;
1359
1378
} ) ;
0 commit comments