Skip to content

fix(afs): Don't filter empty changes (allow for null set), closes #1184 #1186

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
Oct 5, 2017
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
4 changes: 1 addition & 3 deletions src/firestore/collection/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export function sortedChanges(query: firebase.firestore.Query, events: firebase.
return fromCollectionRef(query)
.map(changes => changes.payload.docChanges)
.scan((current, changes) => combineChanges(current, changes, events), [])
.map(changes => changes.map(c => ({ type: c.type, payload: c })))
.filter(changes => changes.length > 0);
.map(changes => changes.map(c => ({ type: c.type, payload: c })));
}

/**
Expand Down Expand Up @@ -60,7 +59,6 @@ export function combineChange(combined: firebase.firestore.DocumentChange[], cha
combined.splice(change.newIndex, 0, change);
break;
case 'modified':
debugger;
// When an item changes position we first remove it
// and then add it's new position
if(change.oldIndex !== change.newIndex) {
Expand Down
60 changes: 58 additions & 2 deletions src/firestore/collection/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { QueryFn } from '../interfaces';

import * as firebase from 'firebase/app';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { of } from 'rxjs/observable/of';
import { Subscription } from 'rxjs/Subscription';
import 'rxjs/add/operator/skip';
Expand Down Expand Up @@ -73,6 +74,33 @@ describe('AngularFirestoreCollection', () => {
});

});

it('should handle dynamic queries that return empty sets', async (done) => {
const ITEMS = 10;
let count = 0;
let firstIndex = 0;
let pricefilter$ = new BehaviorSubject<number|null>(null);
const randomCollectionName = randomName(afs.firestore);
const ref = afs.firestore.collection(`${randomCollectionName}`);
let names = await createRandomStocks(afs.firestore, ref, ITEMS);
const sub = pricefilter$.switchMap(price => {
return afs.collection(randomCollectionName, ref => price ? ref.where('price', '==', price) : ref).valueChanges()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to test multiple events: ['added', 'modified']. That way we can make sure we're not breaking anything with removing the .filter() in sortedChanges(). Ideally you would write a test that limits to two events and tests the proper emission of each event.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}).subscribe(data => {
count = count + 1;
// the first time should all be 'added'
if(count === 1) {
expect(data.length).toEqual(ITEMS);
pricefilter$.next(-1);
}
// on the second round, we should have filtered out everything
if(count === 2) {
expect(data.length).toEqual(0);
sub.unsubscribe();
deleteThemAll(names, ref).then(done).catch(done.fail);
}
});
});

});

describe('snapshotChanges()', () => {
Expand All @@ -83,7 +111,6 @@ describe('AngularFirestoreCollection', () => {
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
const sub = stocks.snapshotChanges().subscribe(data => {
const ids = data.map(d => d.payload.doc.id);
debugger;
count = count + 1;
// the first time should all be 'added'
if(count === 1) {
Expand Down Expand Up @@ -133,7 +160,7 @@ describe('AngularFirestoreCollection', () => {
const ITEMS = 10;
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);

const sub = stocks.snapshotChanges(['modified']).subscribe(data => {
const sub = stocks.snapshotChanges(['modified']).skip(1).subscribe(data => {
sub.unsubscribe();
const change = data.filter(x => x.payload.doc.id === names[0])[0];
expect(data.length).toEqual(1);
Expand Down Expand Up @@ -165,6 +192,35 @@ describe('AngularFirestoreCollection', () => {
delayAdd(stocks, nextId, { price: 2 });
});

it('should be able to filter snapshotChanges() types - added/modified', async (done) => {
const ITEMS = 10;
let { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
const nextId = ref.doc('a').id;
let count = 0;

const sub = stocks.snapshotChanges(['added', 'modified']).skip(1).take(2).subscribe(data => {
count += 1;
if (count == 1) {
const change = data.filter(x => x.payload.doc.id === nextId)[0];
expect(data.length).toEqual(ITEMS + 1);
expect(change.payload.doc.data().price).toEqual(2);
expect(change.type).toEqual('added');
delayUpdate(stocks, names[0], { price: 2 });
}
if (count == 2) {
const change = data.filter(x => x.payload.doc.id === names[0])[0];
expect(data.length).toEqual(ITEMS + 1);
expect(change.payload.doc.data().price).toEqual(2);
expect(change.type).toEqual('modified');
}
}).add(() => {
deleteThemAll(names, ref).then(done).catch(done.fail);
});

names = names.concat([nextId]);
delayAdd(stocks, nextId, { price: 2 });
});

it('should be able to filter snapshotChanges() types - removed', async (done) => {
const ITEMS = 10;
const { randomCollectionName, ref, stocks, names } = await collectionHarness(afs, ITEMS);
Expand Down
1 change: 1 addition & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const GLOBALS = {
'rxjs/Subject': 'Rx',
'rxjs/Observer': 'Rx',
'rxjs/Subscription': 'Rx',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/observable/merge': 'Rx.Observable',
'rxjs/operator/share': 'Rx.Observable.prototype',
'rxjs/operator/observeOn': 'Rx.Observable.prototype',
Expand Down