From 985b156abff8698168a24f082f991f6fec089094 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 17 Nov 2020 10:00:39 -0700 Subject: [PATCH 1/2] Fix DocumentChange.newIndex --- packages/firestore/src/api/database.ts | 2 +- .../test/integration/api/query.test.ts | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/firestore/src/api/database.ts b/packages/firestore/src/api/database.ts index dd420f55ae1..af1d08008c5 100644 --- a/packages/firestore/src/api/database.ts +++ b/packages/firestore/src/api/database.ts @@ -1136,7 +1136,7 @@ export class DocumentChange } get newIndex(): number { - return this._delegate.oldIndex; + return this._delegate.newIndex; } } diff --git a/packages/firestore/test/integration/api/query.test.ts b/packages/firestore/test/integration/api/query.test.ts index f8936e1fe32..10bab8cc329 100644 --- a/packages/firestore/test/integration/api/query.test.ts +++ b/packages/firestore/test/integration/api/query.test.ts @@ -335,6 +335,43 @@ apiDescribe('Queries', (persistence: boolean) => { }); }); + it('maintains correct DocumentChange indices', async () => { + const testDocs = { + 'a': { order: 1 }, + 'b': { order: 2 }, + 'c': { 'order': 3 } + }; + await withTestCollection(persistence, testDocs, async coll => { + const accumulator = new EventsAccumulator(); + const unlisten = coll.orderBy('order').onSnapshot(accumulator.storeEvent); + await accumulator + .awaitEvent() + .then(querySnapshot => { + const changes = querySnapshot.docChanges(); + expect(changes.length).to.equal(3); + verifyDocumentChange(changes[0], 'a', -1, 0, 'added'); + verifyDocumentChange(changes[1], 'b', -1, 1, 'added'); + verifyDocumentChange(changes[2], 'c', -1, 2, 'added'); + }) + .then(() => coll.doc('b').set({ order: 4 })) + .then(() => accumulator.awaitEvent()) + .then(querySnapshot => { + const changes = querySnapshot.docChanges(); + expect(changes.length).to.equal(1); + verifyDocumentChange(changes[0], 'b', 1, 2, 'modified'); + }) + .then(() => coll.doc('c').delete()) + .then(() => accumulator.awaitEvent()) + .then(querySnapshot => { + const changes = querySnapshot.docChanges(); + expect(changes.length).to.equal(1); + verifyDocumentChange(changes[0], 'c', 1, -1, 'removed'); + }); + + unlisten(); + }); + }); + it('can listen for the same query with different options', () => { const testDocs = { a: { v: 'a' }, b: { v: 'b' } }; return withTestCollection(persistence, testDocs, coll => { @@ -1165,3 +1202,16 @@ apiDescribe('Queries', (persistence: boolean) => { }); }); }); + +function verifyDocumentChange( + change: firestore.DocumentChange, + id: string, + oldIndex: number, + newIndex: number, + type: firestore.DocumentChangeType +): void { + expect(change.doc.id).to.equal(id); + expect(change.type).to.equal(type); + expect(change.oldIndex).to.equal(oldIndex); + expect(change.newIndex).to.equal(newIndex); +} From 4b94efb4fd1caea701ef7a8b130c180e748b5410 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Tue, 17 Nov 2020 10:02:28 -0700 Subject: [PATCH 2/2] Create curvy-planets-sneeze.md --- .changeset/curvy-planets-sneeze.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curvy-planets-sneeze.md diff --git a/.changeset/curvy-planets-sneeze.md b/.changeset/curvy-planets-sneeze.md new file mode 100644 index 00000000000..415d8f37fcf --- /dev/null +++ b/.changeset/curvy-planets-sneeze.md @@ -0,0 +1,5 @@ +--- +"@firebase/firestore": patch +--- + +Fixes a regression introduced in v8.0.2 that returned invalid values for `DocumentChange.newIndex`.