1
1
import { rtdbBindAsArray } from '../../../src/core'
2
2
import { MockFirebase , createOps , MockedReference } from '../../src'
3
3
import { ResetOption } from '../../../src/shared'
4
+ import { ref , Ref } from 'vue'
4
5
5
6
describe ( 'RTDB collection' , ( ) => {
6
7
let collection : MockedReference ,
7
- vm : Record < string , any > ,
8
+ target : Ref < Record < string , any > > ,
8
9
resolve : ( data : any ) => void ,
9
10
reject : ( error : any ) => void ,
10
11
unbind : ReturnType < typeof rtdbBindAsArray >
11
12
const ops = createOps ( )
12
13
13
14
beforeEach ( async ( ) => {
14
15
collection = new MockFirebase ( ) . child ( 'data' )
15
- vm = { }
16
+ target = ref ( [ ] )
16
17
await new Promise ( ( res , rej ) => {
17
18
resolve = jest . fn ( res )
18
19
reject = jest . fn ( rej )
19
20
unbind = rtdbBindAsArray ( {
20
- vm,
21
- key : 'items' ,
21
+ target,
22
22
collection,
23
23
resolve,
24
24
reject,
@@ -33,7 +33,7 @@ describe('RTDB collection', () => {
33
33
collection . push ( { name : 'two' } )
34
34
collection . push ( { name : 'three' } )
35
35
collection . flush ( )
36
- expect ( vm . items ) . toEqual ( [
36
+ expect ( target . value ) . toEqual ( [
37
37
{ name : 'one' } ,
38
38
{ name : 'two' } ,
39
39
{ name : 'three' } ,
@@ -45,9 +45,9 @@ describe('RTDB collection', () => {
45
45
collection . push ( { name : 'two' } )
46
46
collection . push ( { name : 'three' } )
47
47
collection . flush ( )
48
- collection . child ( vm . items [ 1 ] [ '.key' ] ) . remove ( )
48
+ collection . child ( target . value [ 1 ] [ '.key' ] ) . remove ( )
49
49
collection . flush ( )
50
- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'three' } ] )
50
+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'three' } ] )
51
51
} )
52
52
53
53
it ( 'stops listening to events when unbound' , async ( ) => {
@@ -64,8 +64,7 @@ describe('RTDB collection', () => {
64
64
unbind ( )
65
65
await new Promise ( ( resolve , reject ) => {
66
66
rtdbBindAsArray ( {
67
- vm,
68
- key : 'items' ,
67
+ target,
69
68
collection : items ,
70
69
resolve,
71
70
reject,
@@ -74,7 +73,7 @@ describe('RTDB collection', () => {
74
73
items . flush ( )
75
74
} )
76
75
77
- expect ( vm . items ) . toEqual ( [
76
+ expect ( target . value ) . toEqual ( [
78
77
{ other : 'one' } ,
79
78
{ other : 'two' } ,
80
79
{ other : 'three' } ,
@@ -105,8 +104,7 @@ describe('RTDB collection', () => {
105
104
resolve = jest . fn ( res )
106
105
reject = jest . fn ( rej )
107
106
rtdbBindAsArray ( {
108
- vm,
109
- key : 'items' ,
107
+ target,
110
108
collection,
111
109
resolve,
112
110
reject,
@@ -115,44 +113,44 @@ describe('RTDB collection', () => {
115
113
collection . flush ( )
116
114
} )
117
115
118
- expect ( vm . items ) . toEqual ( [ { value : 3 } , { value : 1 } , { value : 2 } ] )
116
+ expect ( target . value ) . toEqual ( [ { value : 3 } , { value : 1 } , { value : 2 } ] )
119
117
120
118
childChangedCb (
121
119
{
122
- key : vm . items [ 0 ] [ '.key' ] ,
120
+ key : target . value [ 0 ] [ '.key' ] ,
123
121
} ,
124
- vm . items [ 2 ] [ '.key' ]
122
+ target . value [ 2 ] [ '.key' ]
125
123
)
126
124
127
- expect ( vm . items ) . toEqual ( [ { value : 1 } , { value : 2 } , { value : 3 } ] )
125
+ expect ( target . value ) . toEqual ( [ { value : 1 } , { value : 2 } , { value : 3 } ] )
128
126
129
127
// move to beginning
130
128
childChangedCb (
131
129
{
132
- key : vm . items [ 1 ] [ '.key' ] ,
130
+ key : target . value [ 1 ] [ '.key' ] ,
133
131
} ,
134
132
null
135
133
)
136
134
137
- expect ( vm . items ) . toEqual ( [ { value : 2 } , { value : 1 } , { value : 3 } ] )
135
+ expect ( target . value ) . toEqual ( [ { value : 2 } , { value : 1 } , { value : 3 } ] )
138
136
139
137
mock . mockClear ( )
140
138
} )
141
139
142
140
it ( 'updates an item' , ( ) => {
143
141
collection . push ( { name : 'foo' } )
144
142
collection . flush ( )
145
- collection . child ( vm . items [ 0 ] [ '.key' ] ) . set ( { name : 'bar' } )
143
+ collection . child ( target . value [ 0 ] [ '.key' ] ) . set ( { name : 'bar' } )
146
144
collection . flush ( )
147
- expect ( vm . items ) . toEqual ( [ { name : 'bar' } ] )
145
+ expect ( target . value ) . toEqual ( [ { name : 'bar' } ] )
148
146
} )
149
147
150
148
it ( 'resets the value when unbinding' , ( ) => {
151
149
collection . push ( { name : 'foo' } )
152
150
collection . flush ( )
153
- expect ( vm . items ) . toEqual ( [ { name : 'foo' } ] )
151
+ expect ( target . value ) . toEqual ( [ { name : 'foo' } ] )
154
152
unbind ( )
155
- expect ( vm . items ) . toEqual ( [ ] )
153
+ expect ( target . value ) . toEqual ( [ ] )
156
154
} )
157
155
158
156
it ( 'can be left as is reset: false' , async ( ) => {
@@ -161,9 +159,8 @@ describe('RTDB collection', () => {
161
159
}
162
160
const promise = new Promise ( ( resolve , reject ) => {
163
161
unbind = rtdbBindAsArray ( {
164
- vm ,
162
+ target ,
165
163
collection,
166
- key : 'itemsReset' ,
167
164
resolve,
168
165
reject,
169
166
ops,
@@ -173,9 +170,9 @@ describe('RTDB collection', () => {
173
170
await promise
174
171
collection . push ( { foo : 'foo' } )
175
172
collection . flush ( )
176
- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
173
+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
177
174
unbind ( false )
178
- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
175
+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
179
176
} )
180
177
181
178
it ( 'can be reset to a specific value' , async ( ) => {
@@ -184,9 +181,8 @@ describe('RTDB collection', () => {
184
181
}
185
182
const promise = new Promise ( ( resolve , reject ) => {
186
183
unbind = rtdbBindAsArray ( {
187
- vm ,
184
+ target ,
188
185
collection,
189
- key : 'itemsReset' ,
190
186
resolve,
191
187
reject,
192
188
ops,
@@ -196,9 +192,9 @@ describe('RTDB collection', () => {
196
192
await promise
197
193
collection . push ( { foo : 'foo' } )
198
194
collection . flush ( )
199
- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
195
+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
200
196
unbind ( ( ) => [ { bar : 'bar' } ] )
201
- expect ( vm . itemsReset ) . toEqual ( [ { bar : 'bar' } ] )
197
+ expect ( target . value ) . toEqual ( [ { bar : 'bar' } ] )
202
198
} )
203
199
204
200
it ( 'ignores reset option in bind when calling unbind' , async ( ) => {
@@ -207,7 +203,7 @@ describe('RTDB collection', () => {
207
203
}
208
204
const promise = new Promise ( ( resolve , reject ) => {
209
205
unbind = rtdbBindAsArray (
210
- { vm , collection, key : 'itemsReset' , resolve, reject, ops } ,
206
+ { target , collection, resolve, reject, ops } ,
211
207
// will have no effect when unbinding
212
208
{ reset : ( ) => [ 'Foo' ] }
213
209
)
@@ -217,7 +213,7 @@ describe('RTDB collection', () => {
217
213
collection . push ( { foo : 'foo' } )
218
214
collection . flush ( )
219
215
unbind ( )
220
- expect ( vm . itemsReset ) . toEqual ( [ ] )
216
+ expect ( target . value ) . toEqual ( [ ] )
221
217
} )
222
218
223
219
it ( 'can wait until ready' , async ( ) => {
@@ -227,15 +223,14 @@ describe('RTDB collection', () => {
227
223
228
224
const other = new MockFirebase ( ) . child ( 'other' )
229
225
230
- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
226
+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
231
227
232
228
// force the unbind without resetting the value
233
229
unbind ( false )
234
230
const promise = new Promise ( ( resolve , reject ) => {
235
231
rtdbBindAsArray (
236
232
{
237
- vm,
238
- key : 'items' ,
233
+ target,
239
234
collection : other ,
240
235
resolve,
241
236
reject,
@@ -245,20 +240,20 @@ describe('RTDB collection', () => {
245
240
)
246
241
} )
247
242
248
- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
243
+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
249
244
other . flush ( )
250
245
await promise
251
- expect ( vm . items ) . toEqual ( [ ] )
246
+ expect ( target . value ) . toEqual ( [ ] )
252
247
253
248
other . push ( { other : 'one' } )
254
249
other . push ( { other : 'two' } )
255
250
other . flush ( )
256
251
257
- expect ( vm . items ) . toEqual ( [ { other : 'one' } , { other : 'two' } ] )
252
+ expect ( target . value ) . toEqual ( [ { other : 'one' } , { other : 'two' } ] )
258
253
} )
259
254
260
255
it ( 'can wait until ready with empty arrays' , async ( ) => {
261
- expect ( vm . items ) . toEqual ( [ ] )
256
+ expect ( target . value ) . toEqual ( [ ] )
262
257
263
258
const other = new MockFirebase ( ) . child ( 'other' )
264
259
other . push ( { a : 0 } )
@@ -269,8 +264,7 @@ describe('RTDB collection', () => {
269
264
const promise = new Promise ( ( resolve , reject ) => {
270
265
rtdbBindAsArray (
271
266
{
272
- vm,
273
- key : 'items' ,
267
+ target,
274
268
collection : other ,
275
269
resolve,
276
270
reject,
@@ -280,9 +274,28 @@ describe('RTDB collection', () => {
280
274
)
281
275
} )
282
276
283
- expect ( vm . items ) . toEqual ( [ ] )
277
+ expect ( target . value ) . toEqual ( [ ] )
284
278
other . flush ( )
285
279
await promise
286
- expect ( vm . items ) . toEqual ( [ { a : 0 } , { b : 1 } ] )
280
+ expect ( target . value ) . toEqual ( [ { a : 0 } , { b : 1 } ] )
281
+ } )
282
+
283
+ it ( 'rejects when errors' , async ( ) => {
284
+ const error = new Error ( )
285
+ const collection = new MockFirebase ( ) . child ( 'data' )
286
+ collection . failNext ( 'once' , error )
287
+ const target = ref ( [ ] )
288
+ await expect (
289
+ new Promise ( ( resolve , reject ) => {
290
+ unbind = rtdbBindAsArray ( {
291
+ target,
292
+ collection,
293
+ resolve,
294
+ reject,
295
+ ops,
296
+ } )
297
+ collection . flush ( )
298
+ } )
299
+ ) . rejects . toBe ( error )
287
300
} )
288
301
} )
0 commit comments