Skip to content

Commit 7d2f22f

Browse files
committed
test: refs in collections firestore
1 parent f0e207d commit 7d2f22f

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

__tests__/core/firestore/refs-collections.spec.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ import { bindCollection, FirestoreOptions } from '../../../src/core'
22
import { db, delay, spyUnbind, delayUpdate, createOps } from '../../src'
33
import { OperationsType } from '../../../src/shared'
44
import { firestore } from 'firebase'
5+
import { ref } from 'vue'
6+
7+
const buildRefs = () => ({
8+
items: ref(),
9+
a: ref(),
10+
b: ref(),
11+
c: ref(),
12+
})
513

614
describe('refs in collections', () => {
715
let collection: firestore.CollectionReference,
816
a: firestore.DocumentReference,
917
b: firestore.DocumentReference,
10-
vm: Record<string, any>,
18+
target: ReturnType<typeof buildRefs>,
1119
bind: (
12-
key: string,
20+
key: keyof ReturnType<typeof buildRefs>,
1321
collection: firestore.CollectionReference,
1422
options?: FirestoreOptions
1523
) => void,
@@ -18,18 +26,19 @@ describe('refs in collections', () => {
1826
first: Record<string, any>
1927

2028
beforeEach(async () => {
21-
vm = {
22-
items: null,
23-
a: null,
24-
b: null,
25-
c: null,
26-
}
29+
target = buildRefs()
2730
ops = createOps()
2831
bind = (key, collection, options) => {
2932
return new Promise(
3033
(resolve, reject) =>
3134
(unbind = bindCollection(
32-
{ vm, key, collection, resolve, reject, ops },
35+
{
36+
target: target[key],
37+
collection,
38+
resolve,
39+
reject,
40+
ops,
41+
},
3342
options
3443
))
3544
)
@@ -49,7 +58,10 @@ describe('refs in collections', () => {
4958
it('binds refs on collections', async () => {
5059
await bind('items', collection)
5160

52-
expect(vm.items).toEqual([{ ref: { isA: true } }, { ref: { isB: true } }])
61+
expect(target.items.value).toEqual([
62+
{ ref: { isA: true } },
63+
{ ref: { isB: true } },
64+
])
5365
})
5466

5567
it('waits for array to be fully populated', async () => {
@@ -62,8 +74,8 @@ describe('refs in collections', () => {
6274
delayUpdate(c)
6375
const data = await bind('items', collection)
6476

65-
expect(data).toEqual(vm.items)
66-
expect(vm.items).toEqual([
77+
expect(data).toEqual(target.items.value)
78+
expect(target.items.value).toEqual([
6779
{ ref: { isA: true } },
6880
{ ref: { isB: true } },
6981
{ ref: { isC: true } },
@@ -79,7 +91,7 @@ describe('refs in collections', () => {
7991
// wait for refs to update
8092
await delay(5)
8193

82-
expect(vm.items).toEqual([
94+
expect(target.items.value).toEqual([
8395
{ ref: { isA: true } },
8496
{ ref: { isB: true } },
8597
{ ref: { isC: true } },
@@ -127,7 +139,7 @@ describe('refs in collections', () => {
127139
await bind('items', collection)
128140
expect(spyA).toHaveBeenCalledTimes(0)
129141

130-
await collection.doc(vm.items[0].id).delete()
142+
await collection.doc(target.items.value[0].id).delete()
131143
expect(spyA).toHaveBeenCalledTimes(1)
132144

133145
spyA.mockRestore()
@@ -151,7 +163,7 @@ describe('refs in collections', () => {
151163
await first.update({ newThing: true })
152164
await delay(5)
153165

154-
expect(vm.items).toEqual([
166+
expect(target.items.value).toEqual([
155167
{ ref: { isA: true }, newThing: true },
156168
{ ref: { isB: true } },
157169
])
@@ -161,7 +173,7 @@ describe('refs in collections', () => {
161173
await bind('items', collection)
162174
await first.update({ newThing: true })
163175

164-
expect(vm.items[0]).toEqual({
176+
expect(target.items.value[0]).toEqual({
165177
ref: { isA: true },
166178
newThing: true,
167179
})
@@ -173,22 +185,22 @@ describe('refs in collections', () => {
173185
const emptyItem = collection.doc()
174186
const item = await items.add({ o: { ref: emptyItem }, toggle: true })
175187
await bind('items', items)
176-
expect(vm.items).toEqual([
188+
expect(target.items.value).toEqual([
177189
{
178190
o: { ref: null },
179191
toggle: true,
180192
},
181193
])
182194
await items.add({ foo: 'bar' })
183-
expect(vm.items).toEqual([
195+
expect(target.items.value).toEqual([
184196
{
185197
o: { ref: null },
186198
toggle: true,
187199
},
188200
{ foo: 'bar' },
189201
])
190202
await item.update({ toggle: false })
191-
expect(vm.items).toEqual([
203+
expect(target.items.value).toEqual([
192204
{
193205
o: { ref: null },
194206
toggle: false,
@@ -203,22 +215,22 @@ describe('refs in collections', () => {
203215
const emptyItem = collection.doc()
204216
const item = await items.add({ a: [emptyItem], toggle: true })
205217
await bind('items', items)
206-
expect(vm.items).toEqual([
218+
expect(target.items.value).toEqual([
207219
{
208220
a: [null],
209221
toggle: true,
210222
},
211223
])
212224
await items.add({ foo: 'bar' })
213-
expect(vm.items).toEqual([
225+
expect(target.items.value).toEqual([
214226
{
215227
a: [null],
216228
toggle: true,
217229
},
218230
{ foo: 'bar' },
219231
])
220232
await item.update({ toggle: false })
221-
expect(vm.items).toEqual([
233+
expect(target.items.value).toEqual([
222234
{
223235
a: [null],
224236
toggle: false,
@@ -233,22 +245,22 @@ describe('refs in collections', () => {
233245
const c = collection.doc()
234246
const item = await items.add({ a: [a, b, c, { foo: 'bar' }], toggle: true })
235247
await bind('items', items)
236-
expect(vm.items).toEqual([
248+
expect(target.items.value).toEqual([
237249
{
238250
a: [{ isA: true }, { isB: true }, null, { foo: 'bar' }],
239251
toggle: true,
240252
},
241253
])
242254
await items.add({ foo: 'bar' })
243-
expect(vm.items).toEqual([
255+
expect(target.items.value).toEqual([
244256
{
245257
a: [{ isA: true }, { isB: true }, null, { foo: 'bar' }],
246258
toggle: true,
247259
},
248260
{ foo: 'bar' },
249261
])
250262
await item.update({ toggle: false })
251-
expect(vm.items).toEqual([
263+
expect(target.items.value).toEqual([
252264
{
253265
a: [{ isA: true }, { isB: true }, null, { foo: 'bar' }],
254266
toggle: false,
@@ -271,7 +283,7 @@ describe('refs in collections', () => {
271283

272284
// @ts-ignore
273285
await bind('items', collection, { maxRefDepth: 1 })
274-
expect(vm.items).toEqual([
286+
expect(target.items.value).toEqual([
275287
{
276288
a: {
277289
b: b.path,
@@ -281,7 +293,7 @@ describe('refs in collections', () => {
281293

282294
// @ts-ignore
283295
await bind('items', collection, { maxRefDepth: 3 })
284-
expect(vm.items).toEqual([
296+
expect(target.items.value).toEqual([
285297
{
286298
a: {
287299
b: {
@@ -302,7 +314,7 @@ describe('refs in collections', () => {
302314
// @ts-ignore
303315
await bind('items', collection, { maxRefDepth: 5 })
304316

305-
expect(vm.items).toEqual([
317+
expect(target.items.value).toEqual([
306318
{
307319
// it's easy to see we stop at 5 and we have 5 brackets
308320
item: {

0 commit comments

Comments
 (0)