Skip to content

Commit f94e262

Browse files
committed
test: collections firestore
1 parent 1ccbf0d commit f94e262

File tree

1 file changed

+63
-58
lines changed

1 file changed

+63
-58
lines changed

__tests__/core/firestore/collection.spec.ts

Lines changed: 63 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { bindCollection } from '../../../src/core'
22
import { db, createOps, spyUnbind } from '../../src'
33
import { firestore } from 'firebase'
44
import { OperationsType } from '../../../src/core'
5+
import { ref, Ref } from 'vue'
56

67
describe('collections', () => {
78
let collection: firestore.CollectionReference,
8-
vm: Record<string, any>,
9+
target: Ref<Record<string, any>>,
910
resolve: (data: any) => void,
1011
reject: (error: any) => void,
1112
ops: OperationsType,
@@ -14,14 +15,13 @@ describe('collections', () => {
1415
beforeEach(async () => {
1516
// @ts-ignore
1617
collection = db.collection()
17-
vm = {}
18+
target = ref({})
1819
ops = createOps()
1920
await new Promise((res, rej) => {
2021
resolve = jest.fn(res)
2122
reject = jest.fn(rej)
2223
unbind = bindCollection({
23-
vm,
24-
key: 'items',
24+
target,
2525
collection,
2626
resolve,
2727
reject,
@@ -32,30 +32,34 @@ describe('collections', () => {
3232

3333
it('initialise the array', () => {
3434
expect(ops.set).toHaveBeenCalledTimes(1)
35-
expect(ops.set).toHaveBeenCalledWith(vm, 'items', [])
35+
expect(ops.set).toHaveBeenCalledWith(target, 'value', [])
3636
})
3737

3838
it('add elements', async () => {
3939
await collection.add({ text: 'foo' })
4040
expect(ops.add).toHaveBeenCalledTimes(1)
41-
expect(ops.add).toHaveBeenLastCalledWith(vm.items, 0, { text: 'foo' })
41+
expect(ops.add).toHaveBeenLastCalledWith(target.value, 0, {
42+
text: 'foo',
43+
})
4244
await collection.add({ text: 'bar' })
4345
expect(ops.add).toHaveBeenCalledTimes(2)
44-
expect(ops.add).toHaveBeenLastCalledWith(vm.items, 1, { text: 'bar' })
46+
expect(ops.add).toHaveBeenLastCalledWith(target.value, 1, {
47+
text: 'bar',
48+
})
4549
})
4650

4751
it('deletes items', async () => {
4852
await collection.add({ text: 'foo' })
49-
await collection.doc(vm.items[0].id).delete()
53+
await collection.doc(target.value[0].id).delete()
5054
expect(ops.remove).toHaveBeenCalledTimes(1)
51-
expect(ops.remove).toHaveBeenLastCalledWith(vm.items, 0)
55+
expect(ops.remove).toHaveBeenLastCalledWith(target.value, 0)
5256
})
5357

5458
it('update items', async () => {
5559
const doc = await collection.add({ text: 'foo', more: true })
5660
await doc.update({ text: 'bar' })
5761
expect(ops.set).toHaveBeenCalledTimes(1)
58-
expect(ops.set).toHaveBeenLastCalledWith(vm, 'items', [
62+
expect(ops.set).toHaveBeenLastCalledWith(target, 'value', [
5963
{ more: true, text: 'bar' },
6064
])
6165
})
@@ -64,32 +68,32 @@ describe('collections', () => {
6468
const doc = await collection.add({ text: 'foo' })
6569
await doc.update({ other: 'bar' })
6670
expect(ops.set).toHaveBeenCalledTimes(1)
67-
expect(ops.set).toHaveBeenLastCalledWith(vm, 'items', [
71+
expect(ops.set).toHaveBeenLastCalledWith(target, 'value', [
6872
{ other: 'bar', text: 'foo' },
6973
])
7074
})
7175

7276
it('can bind arrays with null', async () => {
7377
await collection.add({ array: [2, null] })
7478
expect(ops.set).toHaveBeenCalledTimes(1)
75-
expect(ops.set).toHaveBeenLastCalledWith(vm, 'items', [
79+
expect(ops.set).toHaveBeenLastCalledWith(target, 'value', [
7680
{ array: [2, null] },
7781
])
7882
})
7983

8084
// TODO move to vuefire
8185
it.skip('unbinds when the instance is destroyed', async () => {
82-
expect(vm._firestoreUnbinds).toBeTruthy()
83-
expect(vm.items).toEqual([])
84-
const spy = jest.spyOn(vm._firestoreUnbinds, 'items')
86+
expect(target.value._firestoreUnbinds).toBeTruthy()
87+
expect(target.value.items).toEqual([])
88+
const spy = jest.spyOn(target.value._firestoreUnbinds, 'items')
8589
expect(() => {
86-
vm.$destroy()
90+
target.value.$destroy()
8791
}).not.toThrow()
8892
expect(spy).toHaveBeenCalled()
89-
expect(vm._firestoreUnbinds).toBe(null)
93+
expect(target.value._firestoreUnbinds).toBe(null)
9094
await expect(async () => {
9195
await collection.add({ text: 'foo' })
92-
expect(vm.items).toEqual([])
96+
expect(target.value.items).toEqual([])
9397
}).not.toThrow()
9498
})
9599

@@ -98,8 +102,8 @@ describe('collections', () => {
98102
const b = await collection.doc('u1')
99103
await a.update({})
100104
await b.update({})
101-
expect(vm.items.length).toBe(2)
102-
vm.items.forEach((item: Record<string, any>, i: number) => {
105+
expect(target.value.length).toBe(2)
106+
target.value.forEach((item: Record<string, any>, i: number) => {
103107
expect(Object.getOwnPropertyDescriptor(item, 'id')).toEqual({
104108
configurable: false,
105109
enumerable: false,
@@ -119,25 +123,24 @@ describe('collections', () => {
119123
}
120124
await new Promise((resolve, reject) => {
121125
unbind = bindCollection({
122-
vm,
126+
target,
123127
collection,
124-
key: 'items',
125128
resolve,
126129
reject,
127130
ops,
128131
})
129132
})
130133

131134
expect(unbindSpy).not.toHaveBeenCalled()
132-
expect(vm.items).toEqual([{ text: 'foo' }])
135+
expect(target.value).toEqual([{ text: 'foo' }])
133136
unbind()
134137
expect(unbindSpy).toHaveBeenCalled()
135138

136139
// reset data manually
137-
const expected = vm.items
140+
const expected = target.value
138141
await collection.add({ text: 'bar' })
139142
// still old version
140-
expect(vm.items).toEqual(expected)
143+
expect(target.value).toEqual(expected)
141144
unbindSpy.mockRestore()
142145
})
143146

@@ -151,7 +154,7 @@ describe('collections', () => {
151154
collection.onSnapshot = jest.fn(fakeOnSnapshot)
152155
await expect(
153156
new Promise((resolve, reject) => {
154-
bindCollection({ vm, collection, key: 'items', resolve, reject, ops })
157+
bindCollection({ target, collection, resolve, reject, ops })
155158
})
156159
).rejects.toThrow()
157160
// @ts-ignore
@@ -162,10 +165,16 @@ describe('collections', () => {
162165
await collection.add({ foo: 'foo' })
163166
await collection.add({ foo: 'foo' })
164167
const promise = new Promise((resolve, reject) => {
165-
bindCollection({ vm, collection, key: 'items', resolve, reject, ops })
168+
bindCollection({
169+
target,
170+
collection,
171+
resolve,
172+
reject,
173+
ops,
174+
})
166175
})
167176
await promise
168-
expect(vm.items).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
177+
expect(target.value).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
169178
})
170179

171180
it('resets the value when unbinding', async () => {
@@ -175,18 +184,17 @@ describe('collections', () => {
175184
}
176185
const promise = new Promise((resolve, reject) => {
177186
unbind = bindCollection({
178-
vm,
187+
target,
179188
collection,
180-
key: 'items',
181189
resolve,
182190
reject,
183191
ops,
184192
})
185193
})
186194
await promise
187-
expect(vm.items).toEqual([{ foo: 'foo' }])
195+
expect(target.value).toEqual([{ foo: 'foo' }])
188196
unbind()
189-
expect(vm.items).toEqual([])
197+
expect(target.value).toEqual([])
190198
})
191199

192200
it('can be left as is with reset: false', async () => {
@@ -196,18 +204,17 @@ describe('collections', () => {
196204
}
197205
const promise = new Promise((resolve, reject) => {
198206
unbind = bindCollection({
199-
vm,
207+
target,
200208
collection,
201-
key: 'items',
202209
resolve,
203210
reject,
204211
ops,
205212
})
206213
})
207214
await promise
208-
expect(vm.items).toEqual([{ foo: 'foo' }])
215+
expect(target.value).toEqual([{ foo: 'foo' }])
209216
unbind(false)
210-
expect(vm.items).toEqual([{ foo: 'foo' }])
217+
expect(target.value).toEqual([{ foo: 'foo' }])
211218
})
212219

213220
it('can be reset to a specific value', async () => {
@@ -217,18 +224,17 @@ describe('collections', () => {
217224
}
218225
const promise = new Promise((resolve, reject) => {
219226
unbind = bindCollection({
220-
vm,
227+
target,
221228
collection,
222-
key: 'items',
223229
resolve,
224230
reject,
225231
ops,
226232
})
227233
})
228234
await promise
229-
expect(vm.items).toEqual([{ foo: 'foo' }])
235+
expect(target.value).toEqual([{ foo: 'foo' }])
230236
unbind(() => [{ bar: 'bar' }, { baz: 'baz' }])
231-
expect(vm.items).toEqual([{ bar: 'bar' }, { baz: 'baz' }])
237+
expect(target.value).toEqual([{ bar: 'bar' }, { baz: 'baz' }])
232238
})
233239

234240
it('ignores reset option in bind when calling unbind', async () => {
@@ -239,19 +245,19 @@ describe('collections', () => {
239245

240246
await new Promise((resolve, reject) => {
241247
unbind = bindCollection(
242-
{ vm, collection: other, key: 'items', resolve, reject, ops },
248+
{ target, collection: other, resolve, reject, ops },
243249
{ reset: false }
244250
)
245251
})
246-
expect(vm.items).toEqual([{ a: 0 }, { b: 1 }])
252+
expect(target.value).toEqual([{ a: 0 }, { b: 1 }])
247253
unbind()
248-
expect(vm.items).toEqual([])
254+
expect(target.value).toEqual([])
249255
})
250256

251257
it('can wait until ready', async () => {
252258
await collection.add({ foo: 'foo' })
253259
await collection.add({ foo: 'foo' })
254-
expect(vm.items).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
260+
expect(target.value).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
255261

256262
// @ts-ignore
257263
const other: firestore.CollectionReference = db.collection()
@@ -260,40 +266,39 @@ describe('collections', () => {
260266
unbind(false)
261267
const promise = new Promise((resolve, reject) => {
262268
bindCollection(
263-
{ vm, collection: other, key: 'items', resolve, reject, ops },
269+
{ target, collection: other, resolve, reject, ops },
264270
{ wait: true }
265271
)
266272
})
267-
expect(vm.items).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
273+
expect(target.value).toEqual([{ foo: 'foo' }, { foo: 'foo' }])
268274
await promise
269-
expect(vm.items).toEqual([])
275+
expect(target.value).toEqual([])
270276
// we can add other stuff
271277
await other.add({ a: 0 })
272278
await other.add({ b: 1 })
273-
expect(vm.items).toEqual([{ a: 0 }, { b: 1 }])
279+
expect(target.value).toEqual([{ a: 0 }, { b: 1 }])
274280
})
275281

276282
it('sets the value to an empty array even with no documents', async () => {
277-
vm.items = 'foo'
283+
// @ts-ignore
284+
target.value = 'foo'
278285
await new Promise((resolve, reject) => {
279286
bindCollection(
280287
{
281-
vm,
282-
// @ts-ignore
283-
collection: db.collection(),
284-
key: 'items',
288+
target,
289+
collection: db.collection() as any,
285290
resolve,
286291
reject,
287292
ops,
288293
},
289294
{ wait: true }
290295
)
291296
})
292-
expect(vm.items).toEqual([])
297+
expect(target.value).toEqual([])
293298
})
294299

295300
it('can wait until ready with empty arrays', async () => {
296-
expect(vm.items).toEqual([])
301+
expect(target.value).toEqual([])
297302
expect(resolve).toHaveBeenCalledWith([])
298303

299304
// @ts-ignore
@@ -305,12 +310,12 @@ describe('collections', () => {
305310
unbind(false)
306311
const promise = new Promise((resolve, reject) => {
307312
bindCollection(
308-
{ vm, collection: other, key: 'items', resolve, reject, ops },
313+
{ target, collection: other, resolve, reject, ops },
309314
{ wait: true }
310315
)
311316
})
312-
expect(vm.items).toEqual([])
317+
expect(target.value).toEqual([])
313318
await promise
314-
expect(vm.items).toEqual([{ a: 0 }, { b: 1 }])
319+
expect(target.value).toEqual([{ a: 0 }, { b: 1 }])
315320
})
316321
})

0 commit comments

Comments
 (0)