forked from vuejs/vuefire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.spec.ts
453 lines (378 loc) · 13 KB
/
list.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { mount } from '@vue/test-utils'
import { describe, expect, it, expectTypeOf } from 'vitest'
import {
UseDatabaseRefOptions,
useDatabaseList,
VueDatabaseQueryData,
_RefDatabase,
} from '../../src'
import { expectType, tds, setupDatabaseRefs, database } from '../utils'
import {
computed,
nextTick,
ref,
toValue,
watch,
type Ref,
defineComponent,
MaybeRefOrGetter,
} from 'vue'
import {
DatabaseReference,
orderByChild,
query,
Query,
ref as _databaseRef,
} from 'firebase/database'
import { mockWarn } from '../vitest-mock-warn'
describe('Database lists', () => {
const { databaseRef, push, set, remove, update } = setupDatabaseRefs()
mockWarn()
function factory<T = unknown>({
options,
ref = databaseRef(),
}: {
options?: UseDatabaseRefOptions<T>
ref?: MaybeRefOrGetter<DatabaseReference | Query>
} = {}) {
let data!: _RefDatabase<VueDatabaseQueryData<T>>
const wrapper = mount(
defineComponent({
template: 'no',
setup() {
data = useDatabaseList(ref, options)
const { data: list, pending, error, promise, stop } = data
return { list, pending, error, promise, stop }
},
})
)
return {
wrapper,
// to simplify tests
listRef: toValue(ref) as DatabaseReference,
// non enumerable properties cannot be spread
data: data.data,
pending: data.pending,
error: data.error,
promise: data.promise,
stop: data.stop,
}
}
it('starts the list as an empty array', async () => {
const { wrapper } = factory()
expect(wrapper.vm.list).toEqual([])
})
it('fetches once', async () => {
const listRef = databaseRef()
await push(listRef, { name: 'a' })
await push(listRef, { name: 'b' })
const { wrapper, promise } = factory({
ref: listRef,
options: { once: true },
})
await promise.value
expect(wrapper.vm.list).toHaveLength(2)
await push(listRef, { name: 'c' })
expect(wrapper.vm.list).toHaveLength(2)
})
it('fills the array with $value for primitives', async () => {
const itemRef = databaseRef()
await push(itemRef, 'a')
await push(itemRef, 'b')
await push(itemRef, 'c')
const { wrapper, promise } = factory({ ref: itemRef })
await promise.value
expect(wrapper.vm.list).toMatchObject([
{ $value: 'a', id: expect.any(String) },
{ $value: 'b', id: expect.any(String) },
{ $value: 'c', id: expect.any(String) },
])
})
it('warns if target is the result of useDocument', async () => {
const target = ref()
const { data, listRef } = factory({ options: { target } })
expect(data).toBe(target)
expect(() => useDatabaseList(listRef, { target })).not.toThrow()
expect(/FAIL/).toHaveBeenWarned()
})
it('add items to the list', async () => {
const { wrapper, data, listRef } = factory()
expect(wrapper.vm.list).toEqual([])
await push(listRef, { name: 'a' })
await push(listRef, { name: 'b' })
await push(listRef, { name: 'c' })
expect(wrapper.vm.list).toHaveLength(3)
expect(wrapper.vm.list).toContainEqual({ name: 'a' })
expect(wrapper.vm.list).toContainEqual({ name: 'b' })
expect(wrapper.vm.list).toContainEqual({ name: 'c' })
})
it('delete items from the list', async () => {
const { wrapper, listRef } = factory<{ name: string }>()
const aRef = await push(listRef, { name: 'a' })
const bRef = await push(listRef, { name: 'b' })
const cRef = await push(listRef, { name: 'c' })
await remove(aRef)
expect(wrapper.vm.list).toHaveLength(2)
expect(wrapper.vm.list).toContainEqual({ name: 'b' })
expect(wrapper.vm.list).toContainEqual({ name: 'c' })
await remove(cRef)
expect(wrapper.vm.list).toHaveLength(1)
expect(wrapper.vm.list).toContainEqual({ name: 'b' })
})
it('updates items from the list', async () => {
const { wrapper, listRef } = factory<{ name: string }>()
const aRef = await push(listRef, { name: 'a' })
const bRef = await push(listRef, { name: 'b' })
const cRef = await push(listRef, { name: 'c' })
await update(aRef, { name: 'aa' })
expect(wrapper.vm.list).toHaveLength(3)
expect(wrapper.vm.list).toContainEqual({ name: 'aa' })
expect(wrapper.vm.list).toContainEqual({ name: 'b' })
expect(wrapper.vm.list).toContainEqual({ name: 'c' })
await update(cRef, { name: 'cc' })
expect(wrapper.vm.list).toHaveLength(3)
expect(wrapper.vm.list).toContainEqual({ name: 'aa' })
expect(wrapper.vm.list).toContainEqual({ name: 'b' })
expect(wrapper.vm.list).toContainEqual({ name: 'cc' })
})
it('adds a non-enumerable id property to the items', async () => {
const { wrapper, listRef, data } = factory<{ name: string }>()
const a = await push(listRef, { name: 'a' })
expect(wrapper.vm.list).toHaveLength(1)
expect(data.value[0].id).toBeTypeOf('string')
expect(data.value[0].id).toEqual(a.key)
})
it('unbinds when the component is unmounted', async () => {
const { data, listRef, stop: unbind } = factory()
await push(listRef, { name: 'a' })
const copy = [...data.value]
unbind()
await push(listRef, { name: 'b' })
expect(data.value).toEqual(copy)
})
describe('reset option', () => {
it('resets the value when specified', async () => {
const { wrapper, listRef, data } = factory({
options: { reset: true },
})
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
await wrapper.unmount()
expect(data.value).toHaveLength(0)
})
it('skips resetting by default', async () => {
const { wrapper, listRef, data } = factory()
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
await wrapper.unmount()
expect(data.value).toHaveLength(1)
})
it('can be reset to a specific value', async () => {
const { wrapper, listRef, data } = factory({
options: { reset: () => 'reset' },
})
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
await wrapper.unmount()
expect(data.value).toEqual('reset')
})
it('skips resetting by default when manually reset', async () => {
const { listRef, data, stop } = factory()
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
stop()
expect(data.value).toHaveLength(1)
})
it('resets by default when manually reset', async () => {
const { listRef, data, stop } = factory()
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
stop(true)
expect(data.value).toHaveLength(0)
})
it('can be reset to a specific value when manually reset', async () => {
const { listRef, data, stop } = factory()
await push(listRef, { name: 'a' })
expect(data.value).toHaveLength(1)
stop(() => [1])
expect(data.value).toEqual([1])
})
})
it('awaits before setting the value if wait', async () => {
const { wrapper, listRef, data } = factory({
options: {
wait: true,
target: ref([{ name: 'old' }]),
},
})
const p = push(listRef, { name: 'a' })
expect(data.value).toEqual([{ name: 'old' }])
await p
expect(data.value).toEqual([{ name: 'a' }])
})
it('can be bound to a ref of a query', async () => {
const listA = databaseRef()
const listB = databaseRef()
await push(listA, { text: 'task 1' })
await push(listA, { text: 'task 2' })
await push(listB, { text: 'task 3' })
await push(listA, { text: 'task 4' })
const showFinished = ref(true)
// Using a query failed when using equalTo(true, 'finished')...
const listToDisplay = computed(() => (showFinished.value ? listA : listB))
const { wrapper, data, promise } = factory({
ref: listToDisplay,
})
await promise.value
expect(data.value).toHaveLength(3)
expect(data.value).toContainEqual({ text: 'task 1' })
expect(data.value).toContainEqual({ text: 'task 2' })
expect(data.value).toContainEqual({ text: 'task 4' })
showFinished.value = false
await nextTick()
await promise.value
await nextTick()
expect(data.value).toHaveLength(1)
expect(data.value).toContainEqual({ text: 'task 3' })
})
it('can be bound to a getter', async () => {
const listA = databaseRef()
const listB = databaseRef()
await push(listA, { text: 'task 1' })
await push(listA, { text: 'task 2' })
await push(listB, { text: 'task 3' })
await push(listA, { text: 'task 4' })
const showFinished = ref(true)
const { wrapper, data, promise } = factory({
ref: () => (showFinished.value ? listA : listB),
})
await promise.value
expect(data.value).toHaveLength(3)
expect(data.value).toContainEqual({ text: 'task 1' })
expect(data.value).toContainEqual({ text: 'task 2' })
expect(data.value).toContainEqual({ text: 'task 4' })
showFinished.value = false
await nextTick()
await promise.value
await nextTick()
expect(data.value).toHaveLength(1)
expect(data.value).toContainEqual({ text: 'task 3' })
})
it('reorders items in the array', async () => {
const listRef = databaseRef()
const orderedListRef = query(listRef, orderByChild('n'))
const a = await push(listRef, { name: 'a', n: 0 })
const b = await push(listRef, { name: 'b', n: 10 })
const c = await push(listRef, { name: 'c', n: 20 })
const { wrapper, data } = factory<{ name: string }>({
ref: orderedListRef,
})
await update(a, { n: 15 })
// copy to avoid checking hidden properties
expect([...data.value]).toEqual([
{ name: 'b', n: 10 },
{ name: 'a', n: 15 },
{ name: 'c', n: 20 },
])
// from bottom to top
await update(c, { n: 5 })
expect([...data.value]).toEqual([
{ name: 'c', n: 5 },
{ name: 'b', n: 10 },
{ name: 'a', n: 15 },
])
// from top to bottom
await update(c, { n: 25 })
expect([...data.value]).toEqual([
{ name: 'b', n: 10 },
{ name: 'a', n: 15 },
{ name: 'c', n: 25 },
])
// from middle to top
await update(a, { n: 5 })
expect([...data.value]).toEqual([
{ name: 'a', n: 5 },
{ name: 'b', n: 10 },
{ name: 'c', n: 25 },
])
})
// TODO:
it.todo('rejects on error', async () => {
const { error, promise } = factory({
ref: _databaseRef(database, 'forbidden'),
})
expect(error.value).toBeUndefined()
await expect(toValue(promise)).rejects.toThrow()
expect(error.value).toBeTruthy()
})
it('resolves when the ref is populated', async () => {
const ref = databaseRef()
await push(ref, { name: 'a' })
await push(ref, { name: 'b' })
const { error, promise, data, pending } = factory({ ref })
expect(pending.value).toBe(true)
await expect(toValue(promise)).resolves.toEqual(expect.anything())
expect(pending.value).toBe(false)
expect(data.value).toContainEqual({ name: 'a' })
expect(data.value).toContainEqual({ name: 'b' })
expect(error.value).toBeUndefined()
})
it('can provide a target ref to the composable', async () => {
const dataRef = databaseRef()
await push(dataRef, { name: 'a' })
await push(dataRef, { name: 'b' })
const target = ref([])
let changeCount = 0
watch(
() => target.value,
(newData) => {
changeCount++
},
{ deep: true, flush: 'sync' }
)
const { promise, data } = factory({
ref: dataRef,
options: { target },
})
await promise.value
expect(changeCount).toBe(1)
expect(data.value).toHaveLength(2)
expect(data.value).toContainEqual({ name: 'a' })
expect(data.value).toContainEqual({ name: 'b' })
expect(data.value).toEqual(target.value)
await push(dataRef, { name: 'c' })
expect(data.value).toHaveLength(3)
expect(data.value).toContainEqual({ name: 'a' })
expect(data.value).toContainEqual({ name: 'b' })
expect(data.value).toContainEqual({ name: 'c' })
expect(data.value).toEqual(target.value)
})
tds(() => {
const db = database
const databaseRef = _databaseRef
expectType<Ref<VueDatabaseQueryData>>(
useDatabaseList(databaseRef(db, 'todos'))
)
expectType<string | undefined>(
useDatabaseList(databaseRef(db, 'todos')).value?.[0]?.id
)
expectType<Ref<VueDatabaseQueryData<number>>>(
useDatabaseList<number>(databaseRef(db, 'todos'))
)
expectTypeOf(useDatabaseList(databaseRef(db, 'todos'))).not.toMatchTypeOf<
Ref<VueDatabaseQueryData<{ name: string }>>
>()
expectTypeOf(useDatabaseList(databaseRef(db, 'todos'))).toMatchTypeOf<
Ref<VueDatabaseQueryData<unknown>>
>()
expectTypeOf(
useDatabaseList<{ name: string }>(databaseRef(db, 'todos'))
).toMatchTypeOf<Ref<VueDatabaseQueryData<{ name: string }>>>()
expectTypeOf(
useDatabaseList(databaseRef(db, 'todos'), {
target: ref<{ name: string }>({ name: 'a' }),
})
).toMatchTypeOf<Ref<VueDatabaseQueryData<{ name: string }>>>()
// TODO: tests for id field
})
})