-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(GeoTransaction): bring coverage to 100%
- Loading branch information
1 parent
23f59ce
commit b9a6374
Showing
3 changed files
with
258 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |