Skip to content

Commit

Permalink
test(refs): test maxRefDepth in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 29, 2017
1 parent fbd9a21 commit 12f19e8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
13 changes: 10 additions & 3 deletions test/helpers/mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class DocumentSnapshot {
constructor (firestore, key, document, exists) {
this._firestore = firestore
this._key = key
this._key = new Key(key)
this._document = document
this.exists = exists
}
Expand All @@ -18,7 +18,11 @@ export class DocumentSnapshot {
export let _id = 0
export class Key {
constructor (v) {
this.v = '' + (v != null ? v : _id++)
if (v instanceof Key) {
this.v = v.v
} else {
this.v = '' + (v != null ? v : _id++)
}
}

get path () {
Expand Down Expand Up @@ -56,13 +60,16 @@ export class DocumentReference extends callbacksAndErrors {
constructor ({ collection, id, data, index }) {
super()
this.collection = collection
this.id = id
this.id = new Key(id)
this.data = data
this.index = index
this.exists = false
}

onSnapshot (cb, onError) {
if (typeof this.id === 'string') {
debugger
}
setTimeout(() => {
cb(new DocumentSnapshot(null, this.id, this.data, this.exists))
}, 0)
Expand Down
54 changes: 54 additions & 0 deletions test/refs-collections.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,57 @@ test('keeps old data of refs when modifying an item', async () => {
newThing: true
})
})

test('respects provided maxRefDepth', async () => {
const a = db.collection().doc()
const b = db.collection().doc()
const c = db.collection().doc()
const d = db.collection().doc()
await a.set({ b })
await b.set({ c })
await d.set({ isD: true })
await c.set({ d })
const collection = db.collection()
await collection.add({ a })

await vm.$bind('items', collection, { maxRefDepth: 1 })
expect(vm.items).toEqual([{
a: {
b: b.path
}
}])

await vm.$bind('items', collection, { maxRefDepth: 3 })
expect(vm.items).toEqual([{
a: {
b: {
c: {
d: d.path
}
}
}
}])
})

test('does not fail with cyclic refs', async () => {
const item = db.collection().doc()
await item.set({ item })
const collection = db.collection()
await collection.add({ item })
await vm.$bind('items', collection, { maxRefDepth: 5 })

expect(vm.items).toEqual([{
// it's easy to see we stop at 5 and we have 5 brackets
item: {
item: {
item: {
item: {
item: {
item: item.path
}
}
}
}
}
}])
})

0 comments on commit 12f19e8

Please sign in to comment.