@@ -1281,13 +1281,52 @@ apiDescribe('Queries', (persistence: boolean) => {
12811281 } ;
12821282
12831283 return withTestCollection ( persistence , testDocs , async coll => {
1284- await getDocs ( query ( coll ) ) ; // Populate the cache
1284+ await getDocs ( query ( coll ) ) ; // Populate the cache.
12851285 const snapshot = await getDocs (
12861286 query ( coll , where ( 'map.nested' , '==' , 'foo' ) )
12871287 ) ;
12881288 expect ( toDataArray ( snapshot ) ) . to . deep . equal ( [ { map : { nested : 'foo' } } ] ) ;
12891289 } ) ;
12901290 } ) ;
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+ } ) ;
12911330} ) ;
12921331
12931332function verifyDocumentChange < T > (
0 commit comments