diff --git a/src/firestore/collection/changes.ts b/src/firestore/collection/changes.ts index 7a9c26f8d..345ac0fda 100644 --- a/src/firestore/collection/changes.ts +++ b/src/firestore/collection/changes.ts @@ -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 }))); } /** @@ -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) { diff --git a/src/firestore/collection/collection.spec.ts b/src/firestore/collection/collection.spec.ts index 3c8ff3192..40e1d923f 100644 --- a/src/firestore/collection/collection.spec.ts +++ b/src/firestore/collection/collection.spec.ts @@ -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'; @@ -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(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() + }).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()', () => { @@ -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) { @@ -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); @@ -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); diff --git a/tools/build.js b/tools/build.js index 6492a1c65..ad0cabc1d 100644 --- a/tools/build.js +++ b/tools/build.js @@ -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',