Skip to content

Commit

Permalink
test(GeoTransaction): bring coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Feb 3, 2019
1 parent 23f59ce commit b9a6374
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/GeoTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class GeoTransaction {
* @param data An object containing the fields and values with which to update the document. Fields can contain dots to reference nested
* fields within the document.
* @param customKey The key of the document to use as the location. Otherwise we default to `coordinates`.
* @return This `Transaction` instance. Used for chaining method calls.
* @return This `GeoTransaction` instance. Used for chaining method calls.
*/
public update(
documentRef: GeoDocumentReference | GeoFirestoreTypes.cloud.DocumentReference | GeoFirestoreTypes.web.DocumentReference,
Expand Down
8 changes: 1 addition & 7 deletions test/GeoFirestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ describe('GeoFirestore Tests:', () => {

describe('runTransaction():', () => {
it('runTransaction() doesn\'t throw an error when a valid `updateFunction` is passed in', () => {
expect(() => {
return geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
const docRef = geocollection.doc('testdoc');
return geotransaction.get(docRef);
});
}).to.not.throw();
expect(() => geofirestore.runTransaction((transaction) => Promise.resolve(true))).to.not.throw();
});

it('runTransaction() does throw an error when an invalid `updateFunction` is passed in', (done) => {
Expand Down
256 changes: 256 additions & 0 deletions test/GeoTransaction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
import * as chai from 'chai';

import { GeoTransaction } from '../src';
import {
afterEachHelper, beforeEachHelper, collection, dummyData,
geocollection, geofirestore, invalidFirestores, stubDatabase
} from './common';

const expect = chai.expect;

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

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

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

it('Constructor does not throw errors given valid Firestore Transaction reference', () => {
expect(() => geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return Promise.resolve(true);
})).not.to.throw();
});
});

describe('delete():', () => {
it('delete() removes a document from a Firestore collection when given a GeoDocumentReference', () => {
const dummyDoc = dummyData[0];
const docRef = geocollection.doc(dummyDoc.key);
return stubDatabase()
.then(() => {
return geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then(doc => {
expect(doc.exists).to.be.equal(true);
geotransaction.delete(docRef);
return Promise.resolve(true);
});
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(false);
return Promise.resolve(true);
});
});

it('delete() removes a document from a Firestore collection when given a DocumentReference', () => {
const dummyDoc = dummyData[0];
const docRef = collection.doc(dummyDoc.key);
return stubDatabase()
.then(() => {
return geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
geotransaction.delete(docRef);
return Promise.resolve(true);
});
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(false);
return Promise.resolve(true);
});
});
});

describe('get():', () => {
it('get() reads a document from a Firestore collection when given a GeoDocumentReference', () => {
const dummyDoc = dummyData[0];
const docRef = geocollection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.update(docRef, dummyDoc);
return Promise.resolve(true);
});
});
});
});

it('get() reads a document from a Firestore collection when given a DocumentReference', () => {
const dummyDoc = dummyData[0];
const docRef = collection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.update(docRef, dummyDoc);
return Promise.resolve(true);
});
});
});
});
});

describe('set():', () => {
it('set() writes to a document from a Firestore collection when given a GeoDocumentReference', () => {
const dummyDoc = dummyData[0];
const dummyDoc2 = dummyData[1];
const docRef = geocollection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.set(docRef, dummyDoc2);
return Promise.resolve(true);
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc2);
return Promise.resolve(true);
});
});
});

it('set() writes to a document from a Firestore collection when given a DocumentReference', () => {
const dummyDoc = dummyData[0];
const dummyDoc2 = dummyData[1];
const docRef = collection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.set(docRef, dummyDoc2);
return Promise.resolve(true);
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data().d).to.deep.equal(dummyDoc2);
return Promise.resolve(true);
});
});
});

it('set() creates a new document if no document existed before', () => {
const dummyDoc = dummyData[0];
const docRef = geocollection.doc(dummyDoc.key);
return geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(false);
geotransaction.set(docRef, dummyDoc);
return Promise.resolve(true);
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
return Promise.resolve(true);
});
});
});

describe('update():', () => {
it('update() writes to a document from a Firestore collection when given a GeoDocumentReference', () => {
const dummyDoc = dummyData[0];
const dummyDoc2 = dummyData[1];
const docRef = geocollection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.update(docRef, dummyDoc2);
return Promise.resolve(true);
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc2);
return Promise.resolve(true);
});
});
});

it('update() writes to a document from a Firestore collection when given a DocumentReference', () => {
const dummyDoc = dummyData[0];
const dummyDoc2 = dummyData[1];
const docRef = collection.doc(dummyDoc.key);
return stubDatabase().then(() => {
return geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data()).to.deep.equal(dummyDoc);
geotransaction.update(docRef, dummyDoc2);
return Promise.resolve(true);
});
})
.then(() => docRef.get())
.then((doc) => {
expect(doc.exists).to.be.equal(true);
expect(doc.data().d).to.deep.equal(dummyDoc2);
return Promise.resolve(true);
});
});
});

it('update() fails if no document existed before', (done) => {
const dummyDoc = dummyData[0];
const docRef = geocollection.doc(dummyDoc.key);
let isDone = false;
geofirestore
.runTransaction((transaction) => {
const geotransaction = new GeoTransaction(transaction);
return geotransaction.get(docRef).then((doc) => {
expect(doc.exists).to.be.equal(false);
geotransaction.update(docRef, dummyDoc);
return Promise.resolve(true);
}).catch((e) => {
expect(e).to.not.be.equal(null);
expect(e).to.not.be.equal(undefined);
if (!isDone) {
isDone = true;
done();
}
});
});
});
});
});

0 comments on commit b9a6374

Please sign in to comment.