@@ -27,10 +27,61 @@ describe('firestore().doc().onSnapshot()', function () {
2727 describe ( 'modular' , function ( ) {
2828 it ( 'onSnapshotsInSync() returns an unsubscribe function' , function ( ) {
2929 const firestore = firebase . firestore ( ) ;
30- const unsubscribe = firestore . onSnapshotsInSync ( function ( ) { } ) ;
30+ const unsubscribe = onSnapshotsInSync ( firestore ) ;
3131
3232 expect ( unsubscribe ) . to . be . a ( 'function' ) ;
3333 expect ( unsubscribe ( ) ) . to . equal ( undefined ) ;
3434 } ) ;
3535 } ) ;
36+
37+ it ( 'onSnapshotsInSync fires after listeners are in sync' , ( ) => {
38+ const testDocs = {
39+ a : { foo : 1 }
40+ } ;
41+ return withTestCollection ( persistence , testDocs , async ( coll , db ) => {
42+ let events = [ ] ;
43+ const gotInitialSnapshot = ( ( ) => {
44+ let resolve , reject ;
45+ const promise = new Promise ( ( res , rej ) => {
46+ resolve = res ;
47+ reject = rej ;
48+ } ) ;
49+ return { promise, resolve, reject } ;
50+ } ) ( ) ;
51+ const docA = doc ( coll , 'a' ) ;
52+
53+ onSnapshot ( docA , snap => {
54+ events . push ( 'doc' ) ;
55+ gotInitialSnapshot . resolve ( ) ;
56+ } ) ;
57+ await gotInitialSnapshot . promise ;
58+ events = [ ] ;
59+
60+ const done = ( ( ) => {
61+ let resolve , reject ;
62+ const promise = new Promise ( ( res , rej ) => {
63+ resolve = res ;
64+ reject = rej ;
65+ } ) ;
66+ return { promise, resolve, reject } ;
67+ } ) ( ) ;
68+ onSnapshotsInSync ( db , ( ) => {
69+ events . push ( 'snapshots-in-sync' ) ;
70+ if ( events . length === 3 ) {
71+ // We should have an initial snapshots-in-sync event, then a snapshot
72+ // event for set(), then another event to indicate we're in sync
73+ // again.
74+ expect ( events ) . to . deep . equal ( [
75+ 'snapshots-in-sync' ,
76+ 'doc' ,
77+ 'snapshots-in-sync'
78+ ] ) ;
79+ done . resolve ( ) ;
80+ }
81+ } ) ;
82+
83+ await setDoc ( docA , { foo : 3 } ) ;
84+ await done . promise ;
85+ } ) ;
86+ } ) ;
3687} ) ;
0 commit comments