Skip to content

Commit e0bf3f7

Browse files
Fix DocumentChange.newIndex (#4080)
* Fix DocumentChange.newIndex * Create curvy-planets-sneeze.md
1 parent 6a154eb commit e0bf3f7

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

.changeset/curvy-planets-sneeze.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fixes a regression introduced in v8.0.2 that returned invalid values for `DocumentChange.newIndex`.

packages/firestore/src/api/database.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ export class DocumentChange<T = PublicDocumentData>
11361136
}
11371137

11381138
get newIndex(): number {
1139-
return this._delegate.oldIndex;
1139+
return this._delegate.newIndex;
11401140
}
11411141
}
11421142

packages/firestore/test/integration/api/query.test.ts

+50
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,43 @@ apiDescribe('Queries', (persistence: boolean) => {
335335
});
336336
});
337337

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+
338375
it('can listen for the same query with different options', () => {
339376
const testDocs = { a: { v: 'a' }, b: { v: 'b' } };
340377
return withTestCollection(persistence, testDocs, coll => {
@@ -1165,3 +1202,16 @@ apiDescribe('Queries', (persistence: boolean) => {
11651202
});
11661203
});
11671204
});
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

Comments
 (0)