@@ -11,11 +11,17 @@ import {
11
11
12
12
Vue . use ( Vuefire )
13
13
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
15
19
beforeEach ( async ( ) => {
16
20
collection = db . collection ( )
17
21
a = db . collection ( ) . doc ( )
18
22
b = db . collection ( ) . doc ( )
23
+ empty = db . collection ( ) . doc ( )
24
+ item = db . collection ( ) . doc ( )
19
25
c = collection . doc ( )
20
26
d = collection . doc ( )
21
27
await a . update ( { isA : true } )
@@ -41,22 +47,18 @@ beforeEach(async () => {
41
47
await delay ( 5 )
42
48
} )
43
49
44
- // NOTE(1) need to wait because we updated with a ref
45
50
46
51
test ( 'binds refs on documents' , async ( ) => {
47
52
// 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 )
52
55
53
- expect ( vm . b ) . toEqual ( {
56
+ expect ( vm . item ) . toEqual ( {
54
57
ref : { isC : true }
55
58
} )
56
59
} )
57
60
58
61
test ( 'binds refs nested in documents (objects)' , async ( ) => {
59
- const item = collection . doc ( )
60
62
await item . update ( {
61
63
obj : {
62
64
ref : c
@@ -72,7 +74,6 @@ test('binds refs nested in documents (objects)', async () => {
72
74
} )
73
75
74
76
test ( 'binds refs deeply nested in documents (objects)' , async ( ) => {
75
- const item = collection . doc ( )
76
77
await item . update ( {
77
78
obj : {
78
79
nested : {
@@ -110,9 +111,9 @@ test('update inner ref', async () => {
110
111
} )
111
112
112
113
test ( 'is null if ref does not exist' , async ( ) => {
113
- await d . update ( { ref : b } )
114
+ await d . update ( { ref : empty } )
114
115
115
- // NOTE see #1
116
+ // NOTE(1) need to wait because we updated with a ref
116
117
await delay ( 5 )
117
118
118
119
expect ( vm . d ) . toEqual ( {
@@ -121,16 +122,14 @@ test('is null if ref does not exist', async () => {
121
122
} )
122
123
123
124
test ( 'unbinds previously bound document when overwriting a bound' , async ( ) => {
124
- const c = collection . doc ( )
125
-
126
125
// 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 } )
130
129
// NOTE see #1
131
130
await delay ( 5 )
132
131
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
133
- await c . update ( { baz : 'bar' } )
132
+ await item . update ( { baz : 'bar' } )
134
133
// make sure things are updating correctly
135
134
expect ( vm . d ) . toEqual ( {
136
135
ref : { baz : 'bar' }
@@ -144,7 +143,7 @@ test('unbinds previously bound document when overwriting a bound', async () => {
144
143
expect ( vm . d ) . toEqual ( {
145
144
ref : null
146
145
} )
147
- await c . update ( { foo : 'bar' } )
146
+ await item . update ( { foo : 'bar' } )
148
147
149
148
expect ( spy ) . toHaveBeenCalledTimes ( 2 )
150
149
expect ( vm . d ) . toEqual ( {
@@ -154,42 +153,37 @@ test('unbinds previously bound document when overwriting a bound', async () => {
154
153
} )
155
154
156
155
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 } )
162
159
// NOTE see #1
163
160
await delay ( 5 )
164
161
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
165
162
166
- await d . update ( { ref : c } )
163
+ await d . update ( { ref : item } )
167
164
await delay ( 5 )
168
165
169
166
expect ( spy ) . toHaveBeenCalledTimes ( 1 )
170
167
spy . mockRestore ( )
171
168
} )
172
169
173
170
test ( 'resolves the promise when refs are resolved in a document' , async ( ) => {
174
- await b . update ( { ref : a } )
171
+ await item . update ( { ref : a } )
175
172
176
- await vm . $bind ( 'item' , b )
173
+ await vm . $bind ( 'item' , item )
177
174
expect ( vm . item ) . toEqual ( { ref : { isA : true } } )
178
175
} )
179
176
180
177
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 } )
184
179
await d . update ( { ref : item } )
185
180
186
181
await vm . $bind ( 'item' , d )
187
- expect ( vm . item ) . toEqual ( { ref : { ref : { isB : true } } } )
182
+ expect ( vm . item ) . toEqual ( { ref : { ref : { isA : true } } } )
188
183
} )
189
184
190
185
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 } )
193
187
await d . update ( { ref : item } )
194
188
195
189
await vm . $bind ( 'item' , d )
@@ -198,7 +192,7 @@ test('resolves the promise when nested non-existant refs are resolved in a docum
198
192
199
193
test ( 'resolves the promise when the document does not exist' , async ( ) => {
200
194
expect ( vm . item ) . toEqual ( null )
201
- await vm . $bind ( 'item' , b )
195
+ await vm . $bind ( 'item' , empty )
202
196
expect ( vm . item ) . toBe ( null )
203
197
} )
204
198
@@ -214,62 +208,46 @@ test('unbinds all refs when the document is unbound', async () => {
214
208
} )
215
209
vm . $unbind ( 'd' )
216
210
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 )
219
213
220
214
cSpy . mockRestore ( )
221
215
dSpy . mockRestore ( )
222
216
} )
223
217
224
218
test ( 'unbinds nested refs when the document is unbound' , async ( ) => {
225
- const c = collection . doc ( )
226
- const d = collection . doc ( )
227
219
const aSpy = spyUnbind ( a )
228
- const cSpy = spyUnbind ( c )
229
- const dSpy = spyUnbind ( d )
220
+ const cSpy = spyUnbind ( b )
221
+ const dSpy = spyUnbind ( item )
230
222
231
- await c . update ( { ref : a } )
232
- await d . update ( { ref : c } )
223
+ await b . update ( { ref : a } )
224
+ await item . update ( { ref : b } )
233
225
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' )
243
228
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 )
247
232
248
233
aSpy . mockRestore ( )
249
234
cSpy . mockRestore ( )
250
235
dSpy . mockRestore ( )
251
236
} )
252
237
253
238
test ( 'unbinds multiple refs when the document is unbound' , async ( ) => {
254
- const c = collection . doc ( )
255
- const d = collection . doc ( )
256
239
const aSpy = spyUnbind ( a )
257
240
const cSpy = spyUnbind ( c )
258
- const dSpy = spyUnbind ( d )
241
+ const dSpy = spyUnbind ( item )
259
242
260
- await c . update ( { isC : true } )
261
- await d . update ( { c, a } )
243
+ await item . update ( { c, a } )
262
244
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' )
269
247
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 )
273
251
274
252
aSpy . mockRestore ( )
275
253
cSpy . mockRestore ( )
@@ -298,8 +276,8 @@ test('unbinds when a ref is replaced', async () => {
298
276
} )
299
277
300
278
// 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 )
303
281
304
282
aSpy . mockRestore ( )
305
283
cSpy . mockRestore ( )
@@ -308,7 +286,6 @@ test('unbinds when a ref is replaced', async () => {
308
286
309
287
test ( 'unbinds removed properties' , async ( ) => {
310
288
const a = db . collection ( ) . doc ( )
311
- const b = db . collection ( ) . doc ( )
312
289
const unbindSpy = spyUnbind ( a )
313
290
const callbackSpy = spyOnSnapshotCallback ( a )
314
291
const onSnapshotSpy = spyOnSnapshot ( a )
@@ -322,11 +299,6 @@ test('unbinds removed properties', async () => {
322
299
expect ( callbackSpy ) . toHaveBeenCalledTimes ( 0 )
323
300
expect ( onSnapshotSpy ) . toHaveBeenCalledTimes ( 0 )
324
301
await vm . $bind ( 'item' , item )
325
- expect ( vm . item ) . toEqual ( {
326
- a : {
327
- isA : true
328
- }
329
- } )
330
302
331
303
expect ( unbindSpy ) . toHaveBeenCalledTimes ( 0 )
332
304
expect ( callbackSpy ) . toHaveBeenCalledTimes ( 1 )
@@ -337,12 +309,6 @@ test('unbinds removed properties', async () => {
337
309
// NOTE see #1
338
310
await delay ( 5 )
339
311
340
- expect ( vm . item ) . toEqual ( {
341
- b : {
342
- isB : true
343
- }
344
- } )
345
-
346
312
expect ( unbindSpy ) . toHaveBeenCalledTimes ( 1 )
347
313
expect ( callbackSpy ) . toHaveBeenCalledTimes ( 1 )
348
314
expect ( onSnapshotSpy ) . toHaveBeenCalledTimes ( 1 )
@@ -381,9 +347,6 @@ test.skip('binds refs on arrays', async () => {
381
347
} )
382
348
383
349
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 } )
387
350
await item . update ( { a } )
388
351
await vm . $bind ( 'item' , item )
389
352
@@ -393,6 +356,7 @@ test('properly updates a documen with refs', async () => {
393
356
394
357
await item . update ( { newThing : true } )
395
358
359
+ // NOTE see (1)
396
360
await delay ( 5 )
397
361
398
362
expect ( vm . item ) . toEqual ( {
0 commit comments