Skip to content

Commit 94aee8f

Browse files
committed
refactor(test): add item and empty docs by default
1 parent 6a54ba1 commit 94aee8f

File tree

1 file changed

+48
-84
lines changed

1 file changed

+48
-84
lines changed

test/refs-documents.spec.js

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ import {
1111

1212
Vue.use(Vuefire)
1313

14-
let vm, collection, a, b, c, d
14+
// a and c existing objects { isA: true }
15+
// item is an empty ready to use object
16+
// empty is an empty object that is left empty
17+
// d has a ref to c
18+
let vm, collection, a, c, d, empty, item, b
1519
beforeEach(async () => {
1620
collection = db.collection()
1721
a = db.collection().doc()
1822
b = db.collection().doc()
23+
empty = db.collection().doc()
24+
item = db.collection().doc()
1925
c = collection.doc()
2026
d = collection.doc()
2127
await a.update({ isA: true })
@@ -41,22 +47,18 @@ beforeEach(async () => {
4147
await delay(5)
4248
})
4349

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

4651
test('binds refs on documents', async () => {
4752
// create an empty doc and update using the ref instead of plain data
48-
const c = collection.doc()
49-
await c.update({ isC: true })
50-
await b.update({ ref: c })
51-
await vm.$bind('b', b)
53+
await item.update({ ref: c })
54+
await vm.$bind('item', item)
5255

53-
expect(vm.b).toEqual({
56+
expect(vm.item).toEqual({
5457
ref: { isC: true }
5558
})
5659
})
5760

5861
test('binds refs nested in documents (objects)', async () => {
59-
const item = collection.doc()
6062
await item.update({
6163
obj: {
6264
ref: c
@@ -72,7 +74,6 @@ test('binds refs nested in documents (objects)', async () => {
7274
})
7375

7476
test('binds refs deeply nested in documents (objects)', async () => {
75-
const item = collection.doc()
7677
await item.update({
7778
obj: {
7879
nested: {
@@ -110,9 +111,9 @@ test('update inner ref', async () => {
110111
})
111112

112113
test('is null if ref does not exist', async () => {
113-
await d.update({ ref: b })
114+
await d.update({ ref: empty })
114115

115-
// NOTE see #1
116+
// NOTE(1) need to wait because we updated with a ref
116117
await delay(5)
117118

118119
expect(vm.d).toEqual({
@@ -121,16 +122,14 @@ test('is null if ref does not exist', async () => {
121122
})
122123

123124
test('unbinds previously bound document when overwriting a bound', async () => {
124-
const c = collection.doc()
125-
126125
// Mock c onSnapshot to spy when the callback is called
127-
const spy = spyOnSnapshotCallback(c)
128-
await c.update({ baz: 'baz' })
129-
await d.update({ ref: c })
126+
const spy = spyOnSnapshotCallback(item)
127+
await item.update({ baz: 'baz' })
128+
await d.update({ ref: item })
130129
// NOTE see #1
131130
await delay(5)
132131
expect(spy).toHaveBeenCalledTimes(1)
133-
await c.update({ baz: 'bar' })
132+
await item.update({ baz: 'bar' })
134133
// make sure things are updating correctly
135134
expect(vm.d).toEqual({
136135
ref: { baz: 'bar' }
@@ -144,7 +143,7 @@ test('unbinds previously bound document when overwriting a bound', async () => {
144143
expect(vm.d).toEqual({
145144
ref: null
146145
})
147-
await c.update({ foo: 'bar' })
146+
await item.update({ foo: 'bar' })
148147

149148
expect(spy).toHaveBeenCalledTimes(2)
150149
expect(vm.d).toEqual({
@@ -154,42 +153,37 @@ test('unbinds previously bound document when overwriting a bound', async () => {
154153
})
155154

156155
test('does not rebind if it is the same ref', async () => {
157-
const c = collection.doc()
158-
159-
const spy = spyOnSnapshot(c)
160-
await c.update({ baz: 'baz' })
161-
await d.update({ ref: c })
156+
const spy = spyOnSnapshot(item)
157+
await item.update({ baz: 'baz' })
158+
await d.update({ ref: item })
162159
// NOTE see #1
163160
await delay(5)
164161
expect(spy).toHaveBeenCalledTimes(1)
165162

166-
await d.update({ ref: c })
163+
await d.update({ ref: item })
167164
await delay(5)
168165

169166
expect(spy).toHaveBeenCalledTimes(1)
170167
spy.mockRestore()
171168
})
172169

173170
test('resolves the promise when refs are resolved in a document', async () => {
174-
await b.update({ ref: a })
171+
await item.update({ ref: a })
175172

176-
await vm.$bind('item', b)
173+
await vm.$bind('item', item)
177174
expect(vm.item).toEqual({ ref: { isA: true }})
178175
})
179176

180177
test('resolves the promise when nested refs are resolved in a document', async () => {
181-
const item = db.collection().doc()
182-
await item.update({ ref: b })
183-
await b.update({ isB: true })
178+
await item.update({ ref: a })
184179
await d.update({ ref: item })
185180

186181
await vm.$bind('item', d)
187-
expect(vm.item).toEqual({ ref: { ref: { isB: true }}})
182+
expect(vm.item).toEqual({ ref: { ref: { isA: true }}})
188183
})
189184

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

195189
await vm.$bind('item', d)
@@ -198,7 +192,7 @@ test('resolves the promise when nested non-existant refs are resolved in a docum
198192

199193
test('resolves the promise when the document does not exist', async () => {
200194
expect(vm.item).toEqual(null)
201-
await vm.$bind('item', b)
195+
await vm.$bind('item', empty)
202196
expect(vm.item).toBe(null)
203197
})
204198

@@ -214,62 +208,46 @@ test('unbinds all refs when the document is unbound', async () => {
214208
})
215209
vm.$unbind('d')
216210

217-
expect(dSpy.mock.calls.length).toBe(1)
218-
expect(cSpy.mock.calls.length).toBe(1)
211+
expect(dSpy).toHaveBeenCalledTimes(1)
212+
expect(cSpy).toHaveBeenCalledTimes(1)
219213

220214
cSpy.mockRestore()
221215
dSpy.mockRestore()
222216
})
223217

224218
test('unbinds nested refs when the document is unbound', async () => {
225-
const c = collection.doc()
226-
const d = collection.doc()
227219
const aSpy = spyUnbind(a)
228-
const cSpy = spyUnbind(c)
229-
const dSpy = spyUnbind(d)
220+
const cSpy = spyUnbind(b)
221+
const dSpy = spyUnbind(item)
230222

231-
await c.update({ ref: a })
232-
await d.update({ ref: c })
223+
await b.update({ ref: a })
224+
await item.update({ ref: b })
233225

234-
await vm.$bind('d', d)
235-
expect(vm.d).toEqual({
236-
ref: {
237-
ref: {
238-
isA: true
239-
}
240-
}
241-
})
242-
vm.$unbind('d')
226+
await vm.$bind('item', item)
227+
vm.$unbind('item')
243228

244-
expect(dSpy.mock.calls.length).toBe(1)
245-
expect(cSpy.mock.calls.length).toBe(1)
246-
expect(aSpy.mock.calls.length).toBe(1)
229+
expect(dSpy).toHaveBeenCalledTimes(1)
230+
expect(cSpy).toHaveBeenCalledTimes(1)
231+
expect(aSpy).toHaveBeenCalledTimes(1)
247232

248233
aSpy.mockRestore()
249234
cSpy.mockRestore()
250235
dSpy.mockRestore()
251236
})
252237

253238
test('unbinds multiple refs when the document is unbound', async () => {
254-
const c = collection.doc()
255-
const d = collection.doc()
256239
const aSpy = spyUnbind(a)
257240
const cSpy = spyUnbind(c)
258-
const dSpy = spyUnbind(d)
241+
const dSpy = spyUnbind(item)
259242

260-
await c.update({ isC: true })
261-
await d.update({ c, a })
243+
await item.update({ c, a })
262244

263-
await vm.$bind('d', d)
264-
expect(vm.d).toEqual({
265-
a: { isA: true },
266-
c: { isC: true }
267-
})
268-
vm.$unbind('d')
245+
await vm.$bind('item', item)
246+
vm.$unbind('item')
269247

270-
expect(dSpy.mock.calls.length).toBe(1)
271-
expect(cSpy.mock.calls.length).toBe(1)
272-
expect(aSpy.mock.calls.length).toBe(1)
248+
expect(dSpy).toHaveBeenCalledTimes(1)
249+
expect(cSpy).toHaveBeenCalledTimes(1)
250+
expect(aSpy).toHaveBeenCalledTimes(1)
273251

274252
aSpy.mockRestore()
275253
cSpy.mockRestore()
@@ -298,8 +276,8 @@ test('unbinds when a ref is replaced', async () => {
298276
})
299277

300278
// expect(dSpy.mock.calls.length).toBe(1)
301-
expect(cSpy.mock.calls.length).toBe(1)
302-
expect(aSpy.mock.calls.length).toBe(0)
279+
expect(cSpy).toHaveBeenCalledTimes(1)
280+
expect(aSpy).toHaveBeenCalledTimes(0)
303281

304282
aSpy.mockRestore()
305283
cSpy.mockRestore()
@@ -308,7 +286,6 @@ test('unbinds when a ref is replaced', async () => {
308286

309287
test('unbinds removed properties', async () => {
310288
const a = db.collection().doc()
311-
const b = db.collection().doc()
312289
const unbindSpy = spyUnbind(a)
313290
const callbackSpy = spyOnSnapshotCallback(a)
314291
const onSnapshotSpy = spyOnSnapshot(a)
@@ -322,11 +299,6 @@ test('unbinds removed properties', async () => {
322299
expect(callbackSpy).toHaveBeenCalledTimes(0)
323300
expect(onSnapshotSpy).toHaveBeenCalledTimes(0)
324301
await vm.$bind('item', item)
325-
expect(vm.item).toEqual({
326-
a: {
327-
isA: true
328-
}
329-
})
330302

331303
expect(unbindSpy).toHaveBeenCalledTimes(0)
332304
expect(callbackSpy).toHaveBeenCalledTimes(1)
@@ -337,12 +309,6 @@ test('unbinds removed properties', async () => {
337309
// NOTE see #1
338310
await delay(5)
339311

340-
expect(vm.item).toEqual({
341-
b: {
342-
isB: true
343-
}
344-
})
345-
346312
expect(unbindSpy).toHaveBeenCalledTimes(1)
347313
expect(callbackSpy).toHaveBeenCalledTimes(1)
348314
expect(onSnapshotSpy).toHaveBeenCalledTimes(1)
@@ -381,9 +347,6 @@ test.skip('binds refs on arrays', async () => {
381347
})
382348

383349
test('properly updates a documen with refs', async () => {
384-
const item = db.collection().doc()
385-
const a = db.collection().doc()
386-
await a.update({ isA: true })
387350
await item.update({ a })
388351
await vm.$bind('item', item)
389352

@@ -393,6 +356,7 @@ test('properly updates a documen with refs', async () => {
393356

394357
await item.update({ newThing: true })
395358

359+
// NOTE see (1)
396360
await delay(5)
397361

398362
expect(vm.item).toEqual({

0 commit comments

Comments
 (0)