@@ -19,6 +19,7 @@ import { expect } from 'chai';
1919
2020import {
2121 connectFirestoreEmulator ,
22+ loadBundle ,
2223 refEqual ,
2324 snapshotEqual ,
2425 queryEqual
@@ -35,6 +36,15 @@ import {
3536} from '../../util/api_helpers' ;
3637import { keys } from '../../util/helpers' ;
3738
39+ describe ( 'Bundle' , ( ) => {
40+ it ( 'loadBundle silently exits with an empty bundle string)' , async ( ) => {
41+ const db = newTestFirestore ( ) ;
42+ expect ( async ( ) => {
43+ await loadBundle ( db , '' ) ;
44+ } ) . to . not . throw ;
45+ } ) ;
46+ } ) ;
47+
3848describe ( 'CollectionReference' , ( ) => {
3949 it ( 'support equality checking with isEqual()' , ( ) => {
4050 expect ( refEqual ( collectionReference ( 'foo' ) , collectionReference ( 'foo' ) ) ) . to
@@ -107,6 +117,44 @@ describe('DocumentSnapshot', () => {
107117 it ( 'JSON.stringify() does not throw' , ( ) => {
108118 JSON . stringify ( documentSnapshot ( 'foo/bar' , { a : 1 } , true ) ) ;
109119 } ) ;
120+
121+ it ( 'toJSON returns a bundle' , ( ) => {
122+ const json = documentSnapshot (
123+ 'foo/bar' ,
124+ { a : 1 } ,
125+ /*fromCache=*/ true
126+ ) . toJSON ( ) ;
127+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
128+ expect ( ( json as any ) . bundle ) . to . exist ;
129+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130+ expect ( ( json as any ) . bundle . length ) . to . be . greaterThan ( 0 ) ;
131+ } ) ;
132+
133+ it ( 'toJSON returns an empty bundle when there are no documents' , ( ) => {
134+ const json = documentSnapshot (
135+ 'foo/bar' ,
136+ /*data=*/ null ,
137+ /*fromCache=*/ true
138+ ) . toJSON ( ) ;
139+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
140+ expect ( ( json as any ) . bundle ) . to . exist ;
141+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
142+ expect ( ( json as any ) . bundle . length ) . to . equal ( 0 ) ;
143+ } ) ;
144+
145+ it ( 'toJSON throws when there are pending writes' , ( ) => {
146+ expect ( ( ) => {
147+ documentSnapshot (
148+ 'foo/bar' ,
149+ { } ,
150+ /*fromCache=*/ true ,
151+ /*hasPendingWrites=*/ true
152+ ) . toJSON ( ) ;
153+ } ) . to . throw (
154+ `DocumentSnapshot.toJSON() attempted to serialize a document with pending writes. ` +
155+ `Await waitForPendingWrites() before invoking toJSON().`
156+ ) ;
157+ } ) ;
110158} ) ;
111159
112160describe ( 'Query' , ( ) => {
@@ -229,6 +277,45 @@ describe('QuerySnapshot', () => {
229277 querySnapshot ( 'foo' , { } , { a : { a : 1 } } , keys ( ) , false , false )
230278 ) ;
231279 } ) ;
280+
281+ it ( 'toJSON returns a bundle' , ( ) => {
282+ const json = querySnapshot (
283+ 'foo' ,
284+ { } ,
285+ { a : { a : 1 } } ,
286+ keys ( ) ,
287+ false ,
288+ false
289+ ) . toJSON ( ) ;
290+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
291+ expect ( ( json as any ) . bundle ) . to . exist ;
292+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
293+ expect ( ( json as any ) . bundle . length ) . to . be . greaterThan ( 0 ) ;
294+ } ) ;
295+
296+ it ( 'toJSON returns a bundle when there are no documents' , ( ) => {
297+ const json = querySnapshot ( 'foo' , { } , { } , keys ( ) , false , false ) . toJSON ( ) ;
298+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
299+ expect ( ( json as any ) . bundle ) . to . exist ;
300+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
301+ expect ( ( json as any ) . bundle . length ) . to . be . greaterThan ( 0 ) ;
302+ } ) ;
303+
304+ it ( 'toJSON throws when there are pending writes' , ( ) => {
305+ expect ( ( ) =>
306+ querySnapshot (
307+ 'foo' ,
308+ { } ,
309+ { a : { a : 1 } } ,
310+ keys ( 'foo/a' ) ,
311+ true ,
312+ true
313+ ) . toJSON ( )
314+ ) . to . throw (
315+ `QuerySnapshot.toJSON() attempted to serialize a document with pending writes. ` +
316+ `Await waitForPendingWrites() before invoking toJSON().`
317+ ) ;
318+ } ) ;
232319} ) ;
233320
234321describe ( 'SnapshotMetadata' , ( ) => {
0 commit comments