Skip to content

Commit 6576780

Browse files
committed
fix(bind): do not extract refs on geopoints
Fix #179
1 parent 515b68e commit 6576780

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/utils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ export function extractRefs (doc, oldDoc, path = '', result = [{}, {}]) {
2727
// TODO handle array
2828
tot[0][key] = Array(ref.length).fill(null)
2929
extractRefs(ref, oldDoc[key], path + key + '.', [tot[0][key], tot[1]])
30-
} else if (ref instanceof Date) {
30+
} else if (
31+
ref instanceof Date ||
32+
(ref.longitude && ref.latitude) // GeoPoint
33+
) {
3134
tot[0][key] = ref
3235
} else if (isObject(ref)) {
3336
tot[0][key] = {}

test/helpers/mock.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
export class GeoPoint {
2+
constructor (lat, long) {
3+
this._lat = lat
4+
this._long = long
5+
}
6+
7+
get latitude () {
8+
return this._lat
9+
}
10+
get longitude () {
11+
return this._long
12+
}
13+
}
114
export class DocumentSnapshot {
215
constructor (firestore, key, document, exists) {
316
this._firestore = firestore

test/utils.spec.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import {
2-
createSnapshot,
3-
extractRefs
4-
} from '../src/utils'
5-
import {
6-
Key,
7-
db,
8-
_id,
9-
DocumentReference,
10-
DocumentSnapshot
11-
} from './helpers'
1+
import { createSnapshot, extractRefs } from '../src/utils'
2+
import { Key, db, _id, DocumentReference, GeoPoint, DocumentSnapshot } from './helpers'
123

134
let id, doc, snapshot, collection, docRef
145
beforeEach(() => {
@@ -60,8 +51,19 @@ test('leave Date objects alone when extracting refs', () => {
6051
foo: 1,
6152
bar: d
6253
})
63-
expect(doc.foo).toEqual(1)
64-
expect(doc.bar).toEqual(d)
54+
expect(doc.foo).toBe(1)
55+
expect(doc.bar).toBe(d)
56+
expect(refs).toEqual({})
57+
})
58+
59+
test('leave GeoPoint objects alone when extracting refs', () => {
60+
const d = new GeoPoint(2, 48)
61+
const [doc, refs] = extractRefs({
62+
foo: 1,
63+
bar: d
64+
})
65+
expect(doc.foo).toBe(1)
66+
expect(doc.bar).toBe(d)
6567
expect(refs).toEqual({})
6668
})
6769

@@ -99,11 +101,7 @@ test('extracts refs from array', async () => {
99101
index: 0
100102
})
101103
const [noRefsDoc, refs] = extractRefs({
102-
arr: [
103-
docRef,
104-
docRef2,
105-
docRef
106-
]
104+
arr: [docRef, docRef2, docRef]
107105
})
108106
expect(noRefsDoc.arr[0]).toEqual(docRef.path)
109107
expect(noRefsDoc.arr[1]).toEqual(docRef2.path)

0 commit comments

Comments
 (0)