Skip to content

Commit 12f19e8

Browse files
committed
test(refs): test maxRefDepth in collections
1 parent fbd9a21 commit 12f19e8

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

Diff for: test/helpers/mock.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export class DocumentSnapshot {
22
constructor (firestore, key, document, exists) {
33
this._firestore = firestore
4-
this._key = key
4+
this._key = new Key(key)
55
this._document = document
66
this.exists = exists
77
}
@@ -18,7 +18,11 @@ export class DocumentSnapshot {
1818
export let _id = 0
1919
export class Key {
2020
constructor (v) {
21-
this.v = '' + (v != null ? v : _id++)
21+
if (v instanceof Key) {
22+
this.v = v.v
23+
} else {
24+
this.v = '' + (v != null ? v : _id++)
25+
}
2226
}
2327

2428
get path () {
@@ -56,13 +60,16 @@ export class DocumentReference extends callbacksAndErrors {
5660
constructor ({ collection, id, data, index }) {
5761
super()
5862
this.collection = collection
59-
this.id = id
63+
this.id = new Key(id)
6064
this.data = data
6165
this.index = index
6266
this.exists = false
6367
}
6468

6569
onSnapshot (cb, onError) {
70+
if (typeof this.id === 'string') {
71+
debugger
72+
}
6673
setTimeout(() => {
6774
cb(new DocumentSnapshot(null, this.id, this.data, this.exists))
6875
}, 0)

Diff for: test/refs-collections.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,57 @@ test('keeps old data of refs when modifying an item', async () => {
147147
newThing: true
148148
})
149149
})
150+
151+
test('respects provided maxRefDepth', async () => {
152+
const a = db.collection().doc()
153+
const b = db.collection().doc()
154+
const c = db.collection().doc()
155+
const d = db.collection().doc()
156+
await a.set({ b })
157+
await b.set({ c })
158+
await d.set({ isD: true })
159+
await c.set({ d })
160+
const collection = db.collection()
161+
await collection.add({ a })
162+
163+
await vm.$bind('items', collection, { maxRefDepth: 1 })
164+
expect(vm.items).toEqual([{
165+
a: {
166+
b: b.path
167+
}
168+
}])
169+
170+
await vm.$bind('items', collection, { maxRefDepth: 3 })
171+
expect(vm.items).toEqual([{
172+
a: {
173+
b: {
174+
c: {
175+
d: d.path
176+
}
177+
}
178+
}
179+
}])
180+
})
181+
182+
test('does not fail with cyclic refs', async () => {
183+
const item = db.collection().doc()
184+
await item.set({ item })
185+
const collection = db.collection()
186+
await collection.add({ item })
187+
await vm.$bind('items', collection, { maxRefDepth: 5 })
188+
189+
expect(vm.items).toEqual([{
190+
// it's easy to see we stop at 5 and we have 5 brackets
191+
item: {
192+
item: {
193+
item: {
194+
item: {
195+
item: {
196+
item: item.path
197+
}
198+
}
199+
}
200+
}
201+
}
202+
}])
203+
})

0 commit comments

Comments
 (0)