11import { rtdbBindAsArray } from '../../../src/core'
22import { MockFirebase , createOps , MockedReference } from '../../src'
33import { ResetOption } from '../../../src/shared'
4+ import { ref , Ref } from 'vue'
45
56describe ( 'RTDB collection' , ( ) => {
67 let collection : MockedReference ,
7- vm : Record < string , any > ,
8+ target : Ref < Record < string , any > > ,
89 resolve : ( data : any ) => void ,
910 reject : ( error : any ) => void ,
1011 unbind : ReturnType < typeof rtdbBindAsArray >
1112 const ops = createOps ( )
1213
1314 beforeEach ( async ( ) => {
1415 collection = new MockFirebase ( ) . child ( 'data' )
15- vm = { }
16+ target = ref ( [ ] )
1617 await new Promise ( ( res , rej ) => {
1718 resolve = jest . fn ( res )
1819 reject = jest . fn ( rej )
1920 unbind = rtdbBindAsArray ( {
20- vm,
21- key : 'items' ,
21+ target,
2222 collection,
2323 resolve,
2424 reject,
@@ -33,7 +33,7 @@ describe('RTDB collection', () => {
3333 collection . push ( { name : 'two' } )
3434 collection . push ( { name : 'three' } )
3535 collection . flush ( )
36- expect ( vm . items ) . toEqual ( [
36+ expect ( target . value ) . toEqual ( [
3737 { name : 'one' } ,
3838 { name : 'two' } ,
3939 { name : 'three' } ,
@@ -45,9 +45,9 @@ describe('RTDB collection', () => {
4545 collection . push ( { name : 'two' } )
4646 collection . push ( { name : 'three' } )
4747 collection . flush ( )
48- collection . child ( vm . items [ 1 ] [ '.key' ] ) . remove ( )
48+ collection . child ( target . value [ 1 ] [ '.key' ] ) . remove ( )
4949 collection . flush ( )
50- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'three' } ] )
50+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'three' } ] )
5151 } )
5252
5353 it ( 'stops listening to events when unbound' , async ( ) => {
@@ -64,8 +64,7 @@ describe('RTDB collection', () => {
6464 unbind ( )
6565 await new Promise ( ( resolve , reject ) => {
6666 rtdbBindAsArray ( {
67- vm,
68- key : 'items' ,
67+ target,
6968 collection : items ,
7069 resolve,
7170 reject,
@@ -74,7 +73,7 @@ describe('RTDB collection', () => {
7473 items . flush ( )
7574 } )
7675
77- expect ( vm . items ) . toEqual ( [
76+ expect ( target . value ) . toEqual ( [
7877 { other : 'one' } ,
7978 { other : 'two' } ,
8079 { other : 'three' } ,
@@ -105,8 +104,7 @@ describe('RTDB collection', () => {
105104 resolve = jest . fn ( res )
106105 reject = jest . fn ( rej )
107106 rtdbBindAsArray ( {
108- vm,
109- key : 'items' ,
107+ target,
110108 collection,
111109 resolve,
112110 reject,
@@ -115,44 +113,44 @@ describe('RTDB collection', () => {
115113 collection . flush ( )
116114 } )
117115
118- expect ( vm . items ) . toEqual ( [ { value : 3 } , { value : 1 } , { value : 2 } ] )
116+ expect ( target . value ) . toEqual ( [ { value : 3 } , { value : 1 } , { value : 2 } ] )
119117
120118 childChangedCb (
121119 {
122- key : vm . items [ 0 ] [ '.key' ] ,
120+ key : target . value [ 0 ] [ '.key' ] ,
123121 } ,
124- vm . items [ 2 ] [ '.key' ]
122+ target . value [ 2 ] [ '.key' ]
125123 )
126124
127- expect ( vm . items ) . toEqual ( [ { value : 1 } , { value : 2 } , { value : 3 } ] )
125+ expect ( target . value ) . toEqual ( [ { value : 1 } , { value : 2 } , { value : 3 } ] )
128126
129127 // move to beginning
130128 childChangedCb (
131129 {
132- key : vm . items [ 1 ] [ '.key' ] ,
130+ key : target . value [ 1 ] [ '.key' ] ,
133131 } ,
134132 null
135133 )
136134
137- expect ( vm . items ) . toEqual ( [ { value : 2 } , { value : 1 } , { value : 3 } ] )
135+ expect ( target . value ) . toEqual ( [ { value : 2 } , { value : 1 } , { value : 3 } ] )
138136
139137 mock . mockClear ( )
140138 } )
141139
142140 it ( 'updates an item' , ( ) => {
143141 collection . push ( { name : 'foo' } )
144142 collection . flush ( )
145- collection . child ( vm . items [ 0 ] [ '.key' ] ) . set ( { name : 'bar' } )
143+ collection . child ( target . value [ 0 ] [ '.key' ] ) . set ( { name : 'bar' } )
146144 collection . flush ( )
147- expect ( vm . items ) . toEqual ( [ { name : 'bar' } ] )
145+ expect ( target . value ) . toEqual ( [ { name : 'bar' } ] )
148146 } )
149147
150148 it ( 'resets the value when unbinding' , ( ) => {
151149 collection . push ( { name : 'foo' } )
152150 collection . flush ( )
153- expect ( vm . items ) . toEqual ( [ { name : 'foo' } ] )
151+ expect ( target . value ) . toEqual ( [ { name : 'foo' } ] )
154152 unbind ( )
155- expect ( vm . items ) . toEqual ( [ ] )
153+ expect ( target . value ) . toEqual ( [ ] )
156154 } )
157155
158156 it ( 'can be left as is reset: false' , async ( ) => {
@@ -161,9 +159,8 @@ describe('RTDB collection', () => {
161159 }
162160 const promise = new Promise ( ( resolve , reject ) => {
163161 unbind = rtdbBindAsArray ( {
164- vm ,
162+ target ,
165163 collection,
166- key : 'itemsReset' ,
167164 resolve,
168165 reject,
169166 ops,
@@ -173,9 +170,9 @@ describe('RTDB collection', () => {
173170 await promise
174171 collection . push ( { foo : 'foo' } )
175172 collection . flush ( )
176- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
173+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
177174 unbind ( false )
178- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
175+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
179176 } )
180177
181178 it ( 'can be reset to a specific value' , async ( ) => {
@@ -184,9 +181,8 @@ describe('RTDB collection', () => {
184181 }
185182 const promise = new Promise ( ( resolve , reject ) => {
186183 unbind = rtdbBindAsArray ( {
187- vm ,
184+ target ,
188185 collection,
189- key : 'itemsReset' ,
190186 resolve,
191187 reject,
192188 ops,
@@ -196,9 +192,9 @@ describe('RTDB collection', () => {
196192 await promise
197193 collection . push ( { foo : 'foo' } )
198194 collection . flush ( )
199- expect ( vm . itemsReset ) . toEqual ( [ { foo : 'foo' } ] )
195+ expect ( target . value ) . toEqual ( [ { foo : 'foo' } ] )
200196 unbind ( ( ) => [ { bar : 'bar' } ] )
201- expect ( vm . itemsReset ) . toEqual ( [ { bar : 'bar' } ] )
197+ expect ( target . value ) . toEqual ( [ { bar : 'bar' } ] )
202198 } )
203199
204200 it ( 'ignores reset option in bind when calling unbind' , async ( ) => {
@@ -207,7 +203,7 @@ describe('RTDB collection', () => {
207203 }
208204 const promise = new Promise ( ( resolve , reject ) => {
209205 unbind = rtdbBindAsArray (
210- { vm , collection, key : 'itemsReset' , resolve, reject, ops } ,
206+ { target , collection, resolve, reject, ops } ,
211207 // will have no effect when unbinding
212208 { reset : ( ) => [ 'Foo' ] }
213209 )
@@ -217,7 +213,7 @@ describe('RTDB collection', () => {
217213 collection . push ( { foo : 'foo' } )
218214 collection . flush ( )
219215 unbind ( )
220- expect ( vm . itemsReset ) . toEqual ( [ ] )
216+ expect ( target . value ) . toEqual ( [ ] )
221217 } )
222218
223219 it ( 'can wait until ready' , async ( ) => {
@@ -227,15 +223,14 @@ describe('RTDB collection', () => {
227223
228224 const other = new MockFirebase ( ) . child ( 'other' )
229225
230- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
226+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
231227
232228 // force the unbind without resetting the value
233229 unbind ( false )
234230 const promise = new Promise ( ( resolve , reject ) => {
235231 rtdbBindAsArray (
236232 {
237- vm,
238- key : 'items' ,
233+ target,
239234 collection : other ,
240235 resolve,
241236 reject,
@@ -245,20 +240,20 @@ describe('RTDB collection', () => {
245240 )
246241 } )
247242
248- expect ( vm . items ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
243+ expect ( target . value ) . toEqual ( [ { name : 'one' } , { name : 'two' } ] )
249244 other . flush ( )
250245 await promise
251- expect ( vm . items ) . toEqual ( [ ] )
246+ expect ( target . value ) . toEqual ( [ ] )
252247
253248 other . push ( { other : 'one' } )
254249 other . push ( { other : 'two' } )
255250 other . flush ( )
256251
257- expect ( vm . items ) . toEqual ( [ { other : 'one' } , { other : 'two' } ] )
252+ expect ( target . value ) . toEqual ( [ { other : 'one' } , { other : 'two' } ] )
258253 } )
259254
260255 it ( 'can wait until ready with empty arrays' , async ( ) => {
261- expect ( vm . items ) . toEqual ( [ ] )
256+ expect ( target . value ) . toEqual ( [ ] )
262257
263258 const other = new MockFirebase ( ) . child ( 'other' )
264259 other . push ( { a : 0 } )
@@ -269,8 +264,7 @@ describe('RTDB collection', () => {
269264 const promise = new Promise ( ( resolve , reject ) => {
270265 rtdbBindAsArray (
271266 {
272- vm,
273- key : 'items' ,
267+ target,
274268 collection : other ,
275269 resolve,
276270 reject,
@@ -280,9 +274,28 @@ describe('RTDB collection', () => {
280274 )
281275 } )
282276
283- expect ( vm . items ) . toEqual ( [ ] )
277+ expect ( target . value ) . toEqual ( [ ] )
284278 other . flush ( )
285279 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 )
287300 } )
288301} )
0 commit comments