Skip to content

Commit

Permalink
fix(afs): Don't filter empty changes (allow for null set)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored and davideast committed Oct 5, 2017
1 parent c804664 commit eb71edc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/firestore/collection/changes.ts
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
32 changes: 30 additions & 2 deletions src/firestore/collection/collection.spec.ts
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,34 @@ 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()
}).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, make sure the array is still the same
// length but the updated item is now modified
if(count === 2) {
expect(data.length).toEqual(0);
sub.unsubscribe();
deleteThemAll(names, ref).then(done).catch(done.fail);
}
});
});

});

describe('snapshotChanges()', () => {
Expand All @@ -83,7 +112,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 +161,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
1 change: 1 addition & 0 deletions tools/build.js
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

0 comments on commit eb71edc

Please sign in to comment.