Skip to content

Commit

Permalink
Fix DocumentChange.newIndex (#4080)
Browse files Browse the repository at this point in the history
* Fix DocumentChange.newIndex

* Create curvy-planets-sneeze.md
  • Loading branch information
schmidt-sebastian committed Nov 17, 2020
1 parent 6a154eb commit e0bf3f7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/curvy-planets-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixes a regression introduced in v8.0.2 that returned invalid values for `DocumentChange.newIndex`.
2 changes: 1 addition & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ export class DocumentChange<T = PublicDocumentData>
}

get newIndex(): number {
return this._delegate.oldIndex;
return this._delegate.newIndex;
}
}

Expand Down
50 changes: 50 additions & 0 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<firestore.QuerySnapshot>();
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 => {
Expand Down Expand Up @@ -1165,3 +1202,16 @@ apiDescribe('Queries', (persistence: boolean) => {
});
});
});

function verifyDocumentChange<T>(
change: firestore.DocumentChange<T>,
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);
}

0 comments on commit e0bf3f7

Please sign in to comment.