Skip to content

Commit

Permalink
test(GeoCollectionReference): bring coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Nov 15, 2018
1 parent 76fa5af commit 47a17f6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
3 changes: 0 additions & 3 deletions src/GeoCollectionReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export class GeoCollectionReference extends GeoQuery {
*/
constructor(private _collection: GeoFirestoreTypes.cloud.CollectionReference | GeoFirestoreTypes.web.CollectionReference) {
super(_collection);
if (Object.prototype.toString.call(_collection) !== '[object Object]') {
throw new Error('CollectionReference must be an instance of a Firestore CollectionReference');
}
}

/** The identifier of the collection. */
Expand Down
113 changes: 113 additions & 0 deletions test/GeoCollectionReference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as chai from 'chai';
import * as firebase from 'firebase/app';
import 'firebase/firestore';

import { GeoCollectionReference } from '../src/GeoCollectionReference';
import { GeoDocumentReference } from '../src/GeoDocumentReference';
import {
afterEachHelper, beforeEachHelper, firestore, geocollection, invalidFirestores,
invalidObjects, testCollectionName, validGeoFirestoreDocuments, wait
} from './common';

const expect = chai.expect;

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

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

describe('Constructor:', () => {
it('Constructor throws errors given invalid Firestore CollectionReference', () => {
invalidFirestores.forEach((invalidFirestore) => {
// @ts-ignore
expect(() => new GeoCollectionReference(invalidFirestore))
.to.throw(null, 'Query must be an instance of a Firestore Query');
});
});

it('Constructor does not throw errors given valid Firestore CollectionReference', () => {
expect(() => new GeoCollectionReference(firestore.collection(testCollectionName))).not.to.throw();
});
});

describe('id:', () => {
it('id will the identifier of a Firestore CollectionReference', () => {
expect(geocollection.id).to.equal(geocollection['_collection'].id);
});

it('id will be a sting', () => {
expect(typeof geocollection.id).to.equal('string');
});
});

describe('parent:', () => {
it('parent will return null if not a subcollection in a document', () => {
expect(geocollection.parent).to.equal(null);
});

it('parent will return a GeoDocumentReference if a subcollection in a document', () => {
return geocollection.add({ coordinates: new firebase.firestore.GeoPoint(0, 0) }).then(doc => {
const subCollection = doc.collection('subcollection');
expect(subCollection.parent).to.be.instanceOf(GeoDocumentReference);
expect(subCollection.parent.isEqual(doc)).to.deep.equal(true);
});
});
});

describe('path:', () => {
it('path will return path relative to the root of the database', () => {
expect(geocollection.path).to.equal(testCollectionName);
});
});

describe('add():', () => {
it('add() does not throw an error when given a valid object', () => {
validGeoFirestoreDocuments.forEach(doc => {
expect(() => geocollection.add(doc.d)).to.not.throw();
});
});

it('add() does throw an error when given an ivalid object', () => {
validGeoFirestoreDocuments.forEach(doc => {
expect(() => geocollection.add(doc)).to.throw();
});
});

it('add() adds a new object to collection', () => {
return geocollection.add({ coordinates: new firebase.firestore.GeoPoint(0, 0) }).then(d1 => {
return wait(100).then(() => {
return geocollection.doc(d1.id).get().then(d2 => {
expect(d2.exists).to.equal(true);
});
});
});
});

it('add() does throw an error when given a non object', () => {
invalidObjects.forEach(invalidObject => {
// @ts-ignore
expect(() => geocollection.add(invalidObject)).to.throw();
});
});
});

describe('doc():', () => {
it('doc() will auto generate an ID if no path is passed', () => {
const ref = geocollection.doc();
// Auto IDs are 20 characters long
expect(ref.id.length).to.equal(20);
});

it('doc() will return a GeoDocumentReference when a path is passed', () => {
return geocollection.add({ coordinates: new firebase.firestore.GeoPoint(0, 0) }).then(doc => {
expect(geocollection.doc(doc.id)).to.be.instanceOf(GeoDocumentReference);
expect(geocollection.doc(doc.id).isEqual(doc)).to.deep.equal(true);
});
});
});
});
10 changes: 0 additions & 10 deletions test/GeoWriteBatch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as chai from 'chai';
import * as firebase from 'firebase/app';

import { GeoFirestore } from '../src/GeoFirestore';
import { GeoWriteBatch } from '../src/GeoWriteBatch';
import {
afterEachHelper, beforeEachHelper, collection, dummyData, failTestOnCaughtError,
Expand Down Expand Up @@ -144,12 +142,4 @@ describe('GeoWriteBatch Tests:', () => {
});
});
});

describe('collection():', () => {
it('collection() returns a new GeoCollectionReference based on a Firestore CollectionReference', () => {
expect(
(new GeoFirestore(firestore)).collection(testCollectionName)['_collection']
).to.deep.equal(firestore.collection(testCollectionName));
});
});
});
2 changes: 1 addition & 1 deletion test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const invalidQueryCriterias = [
false, undefined, NaN, [], 'a', 1, { center: new firebase.firestore.GeoPoint(1, 2), radius: 2, query: false },
{ center: new firebase.firestore.GeoPoint(1, 2), radius: 2, query: 23 }
];
// export const invalidObjects = [false, true, 'pie', 3, null, undefined, NaN];
export const invalidObjects = [false, true, 'pie', 3, null, undefined, NaN];
export const testCollectionName = 'tests';
export const validGeoFirestoreDocuments: GeoFirestoreTypes.Document[] = [
{ d: { coordinates: new firebase.firestore.GeoPoint(-23.5, -46.9) }, g: '6gydkcbqwf', l: new firebase.firestore.GeoPoint(-23.5, -46.9) },
Expand Down

0 comments on commit 47a17f6

Please sign in to comment.