Skip to content

Commit

Permalink
test: update tests for new data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSolati committed Jun 14, 2020
1 parent 3e0cc7b commit 5a2e669
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 445 deletions.
2 changes: 1 addition & 1 deletion src/GeoDocumentReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class GeoDocumentReference {
onError?: (error: Error) => void
) => () => void {
return (
onNext?: (snapshot: GeoDocumentSnapshot) => void,
onNext: (snapshot: GeoDocumentSnapshot) => void,
onError: (error: Error) => void = () => {}
) => {
return (this
Expand Down
4 changes: 2 additions & 2 deletions src/GeoFirestoreTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import '@types/node';

export namespace GeoFirestoreTypes {
export interface GeoDocumentData extends DocumentData {
g?: {
geopoint: web.GeoPoint | cloud.GeoPoint;
g: {
geohash: string;
geopoint: web.GeoPoint | cloud.GeoPoint;
};
}
export interface DocumentData {
Expand Down
14 changes: 11 additions & 3 deletions src/GeoJoinerOnSnapshot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {GeoFirestoreTypes} from './GeoFirestoreTypes';
import {GeoQuerySnapshot} from './GeoQuerySnapshot';
import {validateQueryCriteria, calculateDistance} from './utils';
import {
validateQueryCriteria,
calculateDistance,
validateGeoDocument,
} from './utils';

interface DocMap {
change: GeoFirestoreTypes.web.DocumentChange;
Expand Down Expand Up @@ -174,8 +178,12 @@ export class GeoJoinerOnSnapshot {
if (docChanges.length) {
// Snapshot has data, key during first snapshot
docChanges.forEach(change => {
const distance = change.doc.data().l
? calculateDistance(this._queryCriteria.center, change.doc.data().l)
const docData = change.doc.data() as GeoFirestoreTypes.GeoDocumentData;
const geopoint = validateGeoDocument(docData, true)
? docData.g.geopoint
: null;
const distance = geopoint
? calculateDistance(this._queryCriteria.center, geopoint)
: null;
const id = change.doc.id;
const fromMap = this._docs.get(id);
Expand Down
2 changes: 1 addition & 1 deletion src/GeoQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class GeoQuery {
value: any
): GeoQuery {
return new GeoQuery(
this._query.where(fieldPath ? 'd.' + fieldPath : fieldPath, opStr, value),
this._query.where(fieldPath, opStr, value),
this._queryCriteria
);
}
Expand Down
18 changes: 9 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export function encodeGeoDocument(
geopoint,
geohash,
};
return document;
return document as GeoFirestoreTypes.GeoDocumentData;
}

/**
Expand Down Expand Up @@ -279,8 +279,8 @@ export function encodeAddDocument(
export function encodeSetDocument(
documentData: GeoFirestoreTypes.DocumentData,
options?: GeoFirestoreTypes.SetOptions
): GeoFirestoreTypes.GeoDocumentData {
if (Object.prototype.toString.call(documentData) === '[object Object]') {
): GeoFirestoreTypes.GeoDocumentData | GeoFirestoreTypes.DocumentData {
if (Object.prototype.toString.call(documentData) !== '[object Object]') {
throw new Error('document must be an object');
}
const customKey = options ? options.customKey : null;
Expand All @@ -299,25 +299,25 @@ export function encodeSetDocument(
/**
* Encodes a Document used by GeoWriteBatch.update as a GeoDocument.
*
* @param data The document being updated.
* @param documentData The document being updated.
* @param customKey The key of the document to use as the location. Otherwise we default to `coordinates`.
* @return The document encoded as GeoDocument object.
*/
export function encodeUpdateDocument(
data: GeoFirestoreTypes.UpdateData,
documentData: GeoFirestoreTypes.UpdateData,
customKey?: string
): GeoFirestoreTypes.UpdateData {
if (Object.prototype.toString.call(data) === '[object Object]') {
if (Object.prototype.toString.call(documentData) !== '[object Object]') {
throw new Error('document must be an object');
}
const geopoint = findGeoPoint(data, customKey, true);
const geopoint = findGeoPoint(documentData, customKey, true);
if (geopoint) {
(data as GeoFirestoreTypes.GeoDocumentData).g = {
(documentData as GeoFirestoreTypes.GeoDocumentData).g = {
geopoint,
geohash: encodeGeohash(geopoint),
};
}
return data;
return documentData;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions test/GeoCollectionReference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
invalidFirestores,
invalidObjects,
testCollectionName,
validGeoFirestoreDocuments,
validDocumentData,
wait,
invalidGeoFirestoreDocuments,
} from './common';

const expect = chai.expect;
Expand Down Expand Up @@ -88,13 +89,13 @@ describe('GeoCollectionReference Tests:', () => {

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();
validDocumentData.forEach(doc => {
expect(() => geocollection.add(doc)).to.not.throw();
});
});

it('add() does throw an error when given an ivalid object', () => {
validGeoFirestoreDocuments.forEach(doc => {
it('add() does throw an error when given an invalid object', () => {
invalidGeoFirestoreDocuments.forEach(doc => {
expect(() => geocollection.add(doc)).to.throw();
});
});
Expand Down
76 changes: 43 additions & 33 deletions test/GeoDocumentReference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import {
afterEachHelper,
beforeEachHelper,
collection,
dummyData,
validDocumentData,
geocollection,
geofirestore,
invalidFirestores,
invalidObjects,
sortObject,
stubDatabase,
wait,
} from './common';
Expand Down Expand Up @@ -104,11 +103,11 @@ describe('GeoDocumentReference Tests:', () => {

describe('onSnapshot:', () => {
it('onSnapshot returns data', done => {
const documentReference = geocollection.doc('loc1');
const documentReference = geocollection.doc('loc0');
documentReference
.set({coordinates: new firebase.firestore.GeoPoint(0, 0)})
.then(() => {
const sub: () => void = documentReference.onSnapshot(snapshot => {
const sub = documentReference.onSnapshot(snapshot => {
sub();
expect(snapshot.exists).to.equal(true);
done();
Expand All @@ -117,27 +116,20 @@ describe('GeoDocumentReference Tests:', () => {
});

it('onSnapshot detects change in data', done => {
const documentReference = geocollection.doc('loc1');
const documentReference = geocollection.doc('loc0');
documentReference
.set({key: 1, coordinates: new firebase.firestore.GeoPoint(0, 0)})
.then(() => {
let called = false;
documentReference.onSnapshot(snapshot => {
const sub = documentReference.onSnapshot(snapshot => {
if (snapshot.exists) {
if (snapshot.data()['key'] === 1) {
documentReference
.set({
key: 2,
coordinates: new firebase.firestore.GeoPoint(1, 1),
})
.then();
} else if (snapshot.data()['key'] === 2) {
expect(snapshot.data()).to.deep.equal({
key: 2,
coordinates: new firebase.firestore.GeoPoint(1, 1),
});
if (snapshot.get('key') === 1) {
documentReference.set({key: 2}, {merge: true});
} else if (snapshot.get('key') === 2) {
expect(snapshot.get('key')).to.equal(2);
if (!called) {
called = true;
sub();
done();
}
}
Expand Down Expand Up @@ -220,17 +212,21 @@ describe('GeoDocumentReference Tests:', () => {

describe('set():', () => {
it('set() does not throw an error when given a valid object', () => {
dummyData.forEach(doc => {
expect(() => geocollection.doc(doc.key).set(doc)).to.not.throw();
validDocumentData.forEach((doc, index) => {
expect(() => geocollection.doc(`loc${index}`).set(doc)).to.not.throw();
});
});

it('set() does throw an error when given an invalid object', () => {
dummyData.forEach(doc => {
expect(() => geocollection.doc(doc.key).set(null)).to.throw();
expect(() => geocollection.doc(doc.key).set({key: doc.key})).to.throw();
expect(() => geocollection.doc(doc.key).set(1 as any)).to.throw();
expect(() => geocollection.doc(doc.key).set(false as any)).to.throw();
validDocumentData.forEach((_, index) => {
expect(() => geocollection.doc(`loc${index}`).set(null)).to.throw();
expect(() =>
geocollection.doc(`loc${index}`).set({key: 'key'})
).to.throw();
expect(() => geocollection.doc(`loc${index}`).set(1 as any)).to.throw();
expect(() =>
geocollection.doc(`loc${index}`).set(false as any)
).to.throw();
});
});

Expand Down Expand Up @@ -330,12 +326,14 @@ describe('GeoDocumentReference Tests:', () => {
.then(() => documentReference.update({key: 1}))
.then(() => documentReference.get())
.then(doc => {
expect(sortObject(doc.data())).to.deep.equal(
sortObject({
coordinates: new firebase.firestore.GeoPoint(0, 0),
key: 1,
})
);
expect(doc.data()).to.deep.include({
g: {
geohash: '7zzzzzzzzz',
geopoint: new firebase.firestore.GeoPoint(0, 0),
},
coordinates: new firebase.firestore.GeoPoint(0, 0),
key: 1,
});
})
.then(done);
});
Expand Down Expand Up @@ -386,21 +384,29 @@ describe('GeoDocumentReference Tests:', () => {
.then(doc => {
expect(doc.exists).to.equal(true);
expect(doc.data()).to.deep.equal({
g: {
geohash: '7zzzzzzzzz',
geopoint: new firebase.firestore.GeoPoint(0, 0),
},
coordinates: new firebase.firestore.GeoPoint(0, 0),
});
})
.then(done);
});

it('get() returns a document, when not on web', done => {
const documentReference = geocollection.doc('loc1');
const documentReference = geocollection.doc('loc0');
documentReference['_isWeb'] = false;
documentReference
.set({coordinates: new firebase.firestore.GeoPoint(0, 0)})
.then(() => documentReference.get())
.then(doc => {
expect(doc.exists).to.equal(true);
expect(doc.data()).to.deep.equal({
g: {
geohash: '7zzzzzzzzz',
geopoint: new firebase.firestore.GeoPoint(0, 0),
},
coordinates: new firebase.firestore.GeoPoint(0, 0),
});
})
Expand All @@ -415,6 +421,10 @@ describe('GeoDocumentReference Tests:', () => {
.then(doc => {
expect(doc.exists).to.equal(true);
expect(doc.data()).to.deep.equal({
g: {
geohash: '7zzzzzzzzz',
geopoint: new firebase.firestore.GeoPoint(0, 0),
},
coordinates: new firebase.firestore.GeoPoint(0, 0),
});
})
Expand All @@ -427,7 +437,7 @@ describe('GeoDocumentReference Tests:', () => {
.get()
.then(doc => {
expect(doc.exists).to.equal(false);
expect(doc.data()).to.deep.equal(null);
expect(doc.data()).to.deep.equal(undefined);
})
.then(done);
});
Expand Down
Loading

0 comments on commit 5a2e669

Please sign in to comment.