@@ -335,6 +335,43 @@ apiDescribe('Queries', (persistence: boolean) => {
335
335
} ) ;
336
336
} ) ;
337
337
338
+ it ( 'maintains correct DocumentChange indices' , async ( ) => {
339
+ const testDocs = {
340
+ 'a' : { order : 1 } ,
341
+ 'b' : { order : 2 } ,
342
+ 'c' : { 'order' : 3 }
343
+ } ;
344
+ await withTestCollection ( persistence , testDocs , async coll => {
345
+ const accumulator = new EventsAccumulator < firestore . QuerySnapshot > ( ) ;
346
+ const unlisten = coll . orderBy ( 'order' ) . onSnapshot ( accumulator . storeEvent ) ;
347
+ await accumulator
348
+ . awaitEvent ( )
349
+ . then ( querySnapshot => {
350
+ const changes = querySnapshot . docChanges ( ) ;
351
+ expect ( changes . length ) . to . equal ( 3 ) ;
352
+ verifyDocumentChange ( changes [ 0 ] , 'a' , - 1 , 0 , 'added' ) ;
353
+ verifyDocumentChange ( changes [ 1 ] , 'b' , - 1 , 1 , 'added' ) ;
354
+ verifyDocumentChange ( changes [ 2 ] , 'c' , - 1 , 2 , 'added' ) ;
355
+ } )
356
+ . then ( ( ) => coll . doc ( 'b' ) . set ( { order : 4 } ) )
357
+ . then ( ( ) => accumulator . awaitEvent ( ) )
358
+ . then ( querySnapshot => {
359
+ const changes = querySnapshot . docChanges ( ) ;
360
+ expect ( changes . length ) . to . equal ( 1 ) ;
361
+ verifyDocumentChange ( changes [ 0 ] , 'b' , 1 , 2 , 'modified' ) ;
362
+ } )
363
+ . then ( ( ) => coll . doc ( 'c' ) . delete ( ) )
364
+ . then ( ( ) => accumulator . awaitEvent ( ) )
365
+ . then ( querySnapshot => {
366
+ const changes = querySnapshot . docChanges ( ) ;
367
+ expect ( changes . length ) . to . equal ( 1 ) ;
368
+ verifyDocumentChange ( changes [ 0 ] , 'c' , 1 , - 1 , 'removed' ) ;
369
+ } ) ;
370
+
371
+ unlisten ( ) ;
372
+ } ) ;
373
+ } ) ;
374
+
338
375
it ( 'can listen for the same query with different options' , ( ) => {
339
376
const testDocs = { a : { v : 'a' } , b : { v : 'b' } } ;
340
377
return withTestCollection ( persistence , testDocs , coll => {
@@ -1165,3 +1202,16 @@ apiDescribe('Queries', (persistence: boolean) => {
1165
1202
} ) ;
1166
1203
} ) ;
1167
1204
} ) ;
1205
+
1206
+ function verifyDocumentChange < T > (
1207
+ change : firestore . DocumentChange < T > ,
1208
+ id : string ,
1209
+ oldIndex : number ,
1210
+ newIndex : number ,
1211
+ type : firestore . DocumentChangeType
1212
+ ) : void {
1213
+ expect ( change . doc . id ) . to . equal ( id ) ;
1214
+ expect ( change . type ) . to . equal ( type ) ;
1215
+ expect ( change . oldIndex ) . to . equal ( oldIndex ) ;
1216
+ expect ( change . newIndex ) . to . equal ( newIndex ) ;
1217
+ }
0 commit comments