Skip to content

Fix DocumentChange.newIndex #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}