Skip to content

Commit

Permalink
refactor(test): manually bind refs on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Dec 28, 2017
1 parent 3e252a0 commit 5dd96c7
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions test/refs-documents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ beforeEach(async () => {
b = db.collection().doc()
c = collection.doc()
d = collection.doc()
await a.update({ isA: true })
await c.update({ c: true })
await d.update({ ref: c })

Expand All @@ -31,8 +32,6 @@ beforeEach(async () => {
}),

firestore: {
a,
b,
c,
d
}
Expand All @@ -42,17 +41,17 @@ beforeEach(async () => {
await delay(5)
})

// NOTE(1) need to wait because we updated with a ref

test('binds refs on documents', async () => {
// create an empty doc and update using the ref instead of plain data
const c = collection.doc()
await c.update({ foo: 'foo' })
await a.update({ ref: c })

// NOTE(1) need to wait because we updated with a ref
await delay(5)
await c.update({ isC: true })
await b.update({ ref: c })
await vm.$bind('b', b)

expect(vm.a).toEqual({
ref: { foo: 'foo' }
expect(vm.b).toEqual({
ref: { isC: true }
})
})

Expand Down Expand Up @@ -111,7 +110,7 @@ test('update inner ref', async () => {
})

test('is null if ref does not exist', async () => {
await d.update({ ref: a })
await d.update({ ref: b })

// NOTE see #1
await delay(5)
Expand All @@ -138,7 +137,7 @@ test('unbinds previously bound document when overwriting a bound', async () => {
})
// we call update twice to make sure our mock works
expect(spy).toHaveBeenCalledTimes(2)
await d.update({ ref: a })
await d.update({ ref: b })
// NOTE see #1
await delay(5)

Expand Down Expand Up @@ -172,33 +171,34 @@ test('does not rebind if it is the same ref', async () => {
})

test('resolves the promise when refs are resolved in a document', async () => {
await a.update({ a: true })
await b.update({ ref: a })

await vm.$bind('item', b)
expect(vm.item).toEqual({ ref: { a: true }})
expect(vm.item).toEqual({ ref: { isA: true }})
})

test('resolves the promise when nested refs are resolved in a document', async () => {
await a.update({ a: b })
await b.update({ b: true })
await d.update({ ref: a })
const item = db.collection().doc()
await item.update({ ref: b })
await b.update({ isB: true })
await d.update({ ref: item })

await vm.$bind('item', d)
expect(vm.item).toEqual({ ref: { a: { b: true }}})
expect(vm.item).toEqual({ ref: { ref: { isB: true }}})
})

test('resolves the promise when nested non-existant refs are resolved in a document', async () => {
await a.update({ a: b })
await d.update({ ref: a })
const item = db.collection().doc()
await item.update({ ref: b })
await d.update({ ref: item })

await vm.$bind('item', d)
expect(vm.item).toEqual({ ref: { a: null }})
expect(vm.item).toEqual({ ref: { ref: null }})
})

test('resolves the promise when the document does not exist', async () => {
expect(vm.item).toEqual(null)
await vm.$bind('item', a)
await vm.$bind('item', b)
expect(vm.item).toBe(null)
})

Expand Down Expand Up @@ -228,15 +228,14 @@ test('unbinds nested refs when the document is unbound', async () => {
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)

await a.update({ a: true })
await c.update({ ref: a })
await d.update({ ref: c })

await vm.$bind('d', d)
expect(vm.d).toEqual({
ref: {
ref: {
a: true
isA: true
}
}
})
Expand All @@ -258,14 +257,13 @@ test('unbinds multiple refs when the document is unbound', async () => {
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)

await a.update({ a: true })
await c.update({ c: true })
await c.update({ isC: true })
await d.update({ c, a })

await vm.$bind('d', d)
expect(vm.d).toEqual({
a: { a: true },
c: { c: true }
a: { isA: true },
c: { isC: true }
})
vm.$unbind('d')

Expand All @@ -283,8 +281,6 @@ test('unbinds when a ref is replaced', async () => {
const cSpy = spyUnbind(c)
const dSpy = spyUnbind(d)

await a.update({ a: true })

await vm.$bind('d', d)
expect(vm.d).toEqual({
ref: {
Expand All @@ -297,7 +293,7 @@ test('unbinds when a ref is replaced', async () => {
await delay(5)
expect(vm.d).toEqual({
ref: {
a: true
isA: true
}
})

Expand Down Expand Up @@ -404,3 +400,7 @@ test('properly updates a documen with refs', async () => {
a: { isA: true }
})
})

test('updates refs in arrays', async () => {
const item = db.collection().doc()
})

0 comments on commit 5dd96c7

Please sign in to comment.