Skip to content

Commit

Permalink
test(GeoJoinerOnSnapshot): increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jan 2, 2019
1 parent 7872c29 commit 27803b3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 8
- 10
sudo: required
install:
- npm install
Expand Down
6 changes: 3 additions & 3 deletions src/GeoJoinerOnSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class GeoJoinerOnSnapshot {
this._subscriptions.push(subscription);
});

this._interval = setInterval(() => this._emit(), 1000);
this._interval = setInterval(() => this._emit(), 100);
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ export class GeoJoinerOnSnapshot {
private _emit(): void {
if (this._error) {
if (this._onError) this._onError(this._error);
this.unsubscribe();
this.unsubscribe()();
} else if (this._newValues && this._firstRoundResolved) {
this._newValues = false;
this._next();
Expand All @@ -104,7 +104,7 @@ export class GeoJoinerOnSnapshot {
private _processSnapshot(snapshot: GeoFirestoreTypes.web.QuerySnapshot, index: number): void {
if (!this._firstRoundResolved) this._queriesResolved[index] = 1;
snapshot.docChanges().forEach((change) => {
const distance = calculateDistance(this._near.center, change.doc.data().l);
const distance = change.doc.data().l ? calculateDistance(this._near.center, change.doc.data().l) : null;
const id = change.doc.id;
const fromMap = this._docs.get(id);
const doc: any = {
Expand Down
11 changes: 4 additions & 7 deletions test/GeoDocumentSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,10 @@ describe('GeoDocumentSnapshot Tests:', () => {

describe('isEqual:', () => {
it('isEqual() returns true when given corresponding DocumentSnapshot', (done) => {
let geoQuerySnapshot;
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => geoQuerySnapshot = snapshot)
.then(() => collection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(geoQuerySnapshot.isEqual(snapshot)).to.equal(true);
expect(snapshot.isEqual(snapshot['_snapshot'])).to.equal(true);
})
.then(done);
});
Expand All @@ -183,13 +180,13 @@ describe('GeoDocumentSnapshot Tests:', () => {
});

it('isEqual() returns false when given non-corresponding DocumentSnapshot', (done) => {
let geoQuerySnapshot;
let snapshotDoc0;
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => geoQuerySnapshot = snapshot)
.then((snapshot) => snapshotDoc0 = snapshot)
.then(() => collection.doc(dummyData[1].key).get())
.then((snapshot) => {
expect(geoQuerySnapshot.isEqual(snapshot)).to.equal(false);
expect(snapshotDoc0.isEqual(snapshot)).to.equal(false);
})
.then(done);
});
Expand Down
82 changes: 82 additions & 0 deletions test/GeoJoinerOnSnapshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as chai from 'chai';
import * as firebase from 'firebase/app';

import { GeoJoinerOnSnapshot } from '../src/GeoJoinerOnSnapshot';
import {
afterEachHelper, beforeEachHelper, collection,
stubDatabase, invalidQueryCriterias, validQueryCriterias, geocollection
} from './common';

const expect = chai.expect;

describe('GeoJoinerOnSnapshot Tests:', () => {
// Reset the Firestore before each test
beforeEach((done) => {
beforeEachHelper(done);
});

afterEach((done) => {
afterEachHelper(done);
});

describe('Constructor:', () => {
it('Constructor throws errors given invalid Query Criteria', (done) => {
stubDatabase()
.then(() => {
invalidQueryCriterias.forEach((criteria) => {
// @ts-ignore
expect(() => new GeoJoinerOnSnapshot([collection], criteria, () => { })).to.throw();
});
}).then(done);
});

it('Constructor does not throw errors given valid arguments', (done) => {
stubDatabase()
.then(() => {
validQueryCriterias.forEach((criteria) => {
if (criteria.center) {
expect(() => {
const joiner = new GeoJoinerOnSnapshot([collection], criteria, () => { }, () => { });
joiner.unsubscribe()();
}).not.to.throw();
}
});
}).then(done);
});
});

describe('unsubscribe():', () => {
it('unsubscribe() stops all updates to function', (done) => {
stubDatabase()
.then(() => {
return new Promise((resolve) => {
let count = 0;
let timer;
const joiner = new GeoJoinerOnSnapshot([collection], validQueryCriterias[0], (da) => {
if (!timer) {
joiner.unsubscribe()();
geocollection.add({ coordinates: validQueryCriterias[0].center });
timer = setTimeout(() => {
expect(count).to.equal(1);
resolve();
}, 5000);
}
count++;
});
});
}).then(done);
});
});

describe('Other:', () => {
it('GeoJoinerOnSnapshot handles error', (done) => {
stubDatabase()
.then(() => {
return new Promise((resolve) => {
const query = geocollection.where('error', '==', true).near({ center: new firebase.firestore.GeoPoint(0, 0), radius: 1 });
query.onSnapshot(() => { }, () => resolve());
});
}).then(done);
});
});
});

0 comments on commit 27803b3

Please sign in to comment.