Skip to content

Commit

Permalink
test(GeoDocumentSnapshot): bring coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jan 1, 2019
1 parent 7beffe6 commit 5caa5b1
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/GeoDocumentSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export class GeoDocumentSnapshot {
fieldPath: string | GeoFirestoreTypes.cloud.FieldPath | GeoFirestoreTypes.web.FieldPath,
options?: GeoFirestoreTypes.SnapshotOptions
): any {
const d = (this._isWeb && options) ?
(this._snapshot as GeoFirestoreTypes.web.DocumentSnapshot).get(fieldPath, options) : this._snapshot.get(fieldPath);
return d;
const path = 'd.' + fieldPath;
return (this._isWeb && options) ?
(this._snapshot as GeoFirestoreTypes.web.DocumentSnapshot).get(path, options) : this._snapshot.get(path);
}

/**
Expand Down
209 changes: 209 additions & 0 deletions test/GeoDocumentSnapshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import * as chai from 'chai';

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

const expect = chai.expect;

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

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

describe('Constructor:', () => {
it('Constructor does not throw errors given valid Firestore DocumentSnapshot', (done) => {
stubDatabase()
.then(() => collection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(() => new GeoDocumentSnapshot(snapshot)).to.not.throw();
})
.then(done);
});

it('Constructor throws errors given invalid Firestore DocumentSnapshot', () => {
invalidFirestores.forEach((invalid) => {
// @ts-ignore
expect(() => new GeoDocumentSnapshot(invalid)).to.throw();
});
});
});

describe('exists:', () => {
it('exists returns true if document exists', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.exists).to.equal(true);
})
.then(done);
});

it('exists returns false if document does not exists', (done) => {
geocollection.doc(dummyData[0].key).get()
.then((snapshot) => {
expect(snapshot.exists).to.equal(false);
})
.then(done);
});
});

describe('id:', () => {
it('id will be a sting', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(typeof snapshot.id === 'string').to.equal(true);
})
.then(done);
});

it('id returned matches id of doc', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.id).to.equal(dummyData[0].key);
})
.then(done);
});
});

describe('ref:', () => {
it('ref returns a GeoDocumentReference', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.ref).to.be.instanceOf(GeoDocumentReference);
})
.then(done);
});

it('ref returns a GeoDocumentReference of the selected document', (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.ref.isEqual(snapshot.ref)).to.equal(true);
})
.then(done);
});
});

describe('data():', () => {
it('data() returns document', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.data()).to.deep.equal(dummyData[0]);
})
.then(done);
});

it('data() returns document when given SnapshotOptions', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.data({ serverTimestamps: 'estimate' })).to.deep.equal(dummyData[0]);
})
.then(done);
});

it('data() does not throw error when given SnapshotOptions', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(() => snapshot.data({ serverTimestamps: 'estimate' })).to.not.throw();
})
.then(done);
});
});

describe('get():', () => {
it('get() returns field of document', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
Object.getOwnPropertyNames(dummyData[0]).forEach((property) => {
expect(snapshot.get(property)).to.deep.equal(dummyData[0][property]);
});
})
.then(done);
});

it('get() returns field of document when given SnapshotOptions', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
Object.getOwnPropertyNames(dummyData[0]).forEach((property) => {
expect(snapshot.get(property, { serverTimestamps: 'estimate' })).to.deep.equal(dummyData[0][property]);
});
})
.then(done);
});

it('get() does not throw error when given SnapshotOptions', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
Object.getOwnPropertyNames(dummyData[0]).forEach((property) => {
expect(() => snapshot.get(property, { serverTimestamps: 'estimate' })).to.not.throw();
});
})
.then(done);
});
});

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);
})
.then(done);
});

it('isEqual() returns true when given same GeoDocumentSnapshot', (done) => {
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => {
expect(snapshot.isEqual(snapshot)).to.equal(true);
})
.then(done);
});

it('isEqual() returns false when given non-corresponding DocumentSnapshot', (done) => {
let geoQuerySnapshot;
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => geoQuerySnapshot = snapshot)
.then(() => collection.doc(dummyData[1].key).get())
.then((snapshot) => {
expect(geoQuerySnapshot.isEqual(snapshot)).to.equal(false);
})
.then(done);
});

it('isEqual() returns false when given different GeoDocumentSnapshot', (done) => {
let snapshotDoc0;
stubDatabase()
.then(() => geocollection.doc(dummyData[0].key).get())
.then((snapshot) => snapshotDoc0 = snapshot)
.then(() => geocollection.doc(dummyData[1].key).get())
.then((snapshot) => {
expect(snapshotDoc0.isEqual(snapshot)).to.equal(false);
})
.then(done);
});
});
});

0 comments on commit 5caa5b1

Please sign in to comment.