Skip to content

Commit f8b2cb9

Browse files
committed
test: refactor firestore options
1 parent 8a30732 commit f8b2cb9

File tree

1 file changed

+74
-94
lines changed

1 file changed

+74
-94
lines changed

tests/firestore/options.spec.ts

Lines changed: 74 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,96 @@ import { defineComponent } from 'vue'
22
import { mount } from '@vue/test-utils'
33
import { describe, expect, it, vi } from 'vitest'
44
import { firestorePlugin, FirestorePluginOptions } from '../../src'
5-
import { addDoc, DocumentData } from 'firebase/firestore'
5+
import { DocumentData } from 'firebase/firestore'
66
import { setupFirestoreRefs } from '../utils'
77

8-
const component = defineComponent({ template: 'no' })
8+
const component = defineComponent({
9+
template: 'no',
10+
data: () => ({ itemList: [], item: null }),
11+
})
912

1013
describe('Firestore: Options API', () => {
11-
const { collection, doc } = setupFirestoreRefs()
12-
13-
it('allows customizing $rtdbBind', () => {
14-
const wrapper = mount(component, {
15-
global: {
16-
plugins: [
17-
[
18-
firestorePlugin,
19-
{
20-
bindName: '$myBind',
21-
unbindName: '$myUnbind',
22-
},
23-
],
24-
],
25-
},
26-
})
14+
const { collection, doc, addDoc, setDoc } = setupFirestoreRefs()
2715

28-
// @ts-expect-error: haven't extended the types
29-
expect(wrapper.vm.$myBind).toBeTypeOf('function')
30-
// @ts-expect-error: haven't extended the types
31-
expect(wrapper.vm.$myUnbind).toBeTypeOf('function')
32-
})
33-
34-
it('calls custom serialize function with collection', async () => {
35-
const pluginOptions: FirestorePluginOptions = {
36-
converter: {
37-
fromFirestore: vi.fn((snapshot, options?) => ({
38-
foo: 'bar',
39-
})),
40-
toFirestore(data: DocumentData) {
41-
return data
42-
},
43-
},
44-
}
45-
const wrapper = mount(
46-
{
47-
template: 'no',
48-
data: () => ({ items: [] }),
49-
},
50-
{
16+
describe('$firestoreBind', () => {
17+
function factory(pluginOptions?: FirestorePluginOptions) {
18+
return mount(component, {
5119
global: {
5220
plugins: [[firestorePlugin, pluginOptions]],
5321
},
54-
}
55-
)
56-
57-
const itemsRef = collection()
58-
await addDoc(itemsRef, {})
22+
})
23+
}
5924

60-
await wrapper.vm.$firestoreBind('items', itemsRef)
25+
it('allows customizing $rtdbBind', () => {
26+
const wrapper = factory({
27+
bindName: '$myBind',
28+
unbindName: '$myUnbind',
29+
})
6130

62-
expect(pluginOptions.converter?.fromFirestore).toHaveBeenCalledTimes(1)
63-
expect(pluginOptions.converter?.fromFirestore).toHaveBeenCalledWith(
64-
expect.objectContaining({ data: expect.any(Function) }),
65-
expect.anything()
66-
)
67-
expect(wrapper.vm.items).toEqual([{ foo: 'bar' }])
68-
})
31+
// @ts-expect-error: haven't extended the types
32+
expect(wrapper.vm.$myBind).toBeTypeOf('function')
33+
// @ts-expect-error: haven't extended the types
34+
expect(wrapper.vm.$myUnbind).toBeTypeOf('function')
35+
})
6936

70-
it('can be overridden by local option', async () => {
71-
const pluginOptions: FirestorePluginOptions = {
72-
converter: {
73-
fromFirestore: vi.fn((snapshot, options?) => ({
74-
foo: 'bar',
75-
})),
76-
toFirestore(data: DocumentData) {
77-
return data
37+
it('calls custom serialize function with collection', async () => {
38+
const fromFirestore = vi.fn(() => ({
39+
foo: 'bar',
40+
}))
41+
const wrapper = factory({
42+
converter: {
43+
fromFirestore,
44+
toFirestore: (data: DocumentData) => data,
7845
},
79-
},
80-
}
81-
const wrapper = mount(
82-
{
83-
template: 'no',
84-
data: () => ({ items: [] }),
85-
},
86-
{
87-
global: {
88-
plugins: [[firestorePlugin, pluginOptions]],
89-
},
90-
}
91-
)
46+
})
47+
48+
const itemsRef = collection()
49+
await addDoc(itemsRef, {})
9250

93-
const itemsRef = collection()
94-
await addDoc(itemsRef, {})
51+
await wrapper.vm.$firestoreBind('itemList', itemsRef)
9552

96-
const spy = vi.fn(() => ({ bar: 'bar' }))
53+
expect(fromFirestore).toHaveBeenCalledTimes(1)
54+
expect(fromFirestore).toHaveBeenCalledWith(
55+
expect.objectContaining({ data: expect.any(Function) }),
56+
expect.anything()
57+
)
58+
expect(wrapper.vm.itemList).toEqual([{ foo: 'bar' }])
59+
})
9760

98-
await wrapper.vm.$firestoreBind(
99-
'items',
100-
itemsRef.withConverter({
101-
fromFirestore: spy,
102-
toFirestore(data: DocumentData) {
103-
return data
61+
it('can be overridden by local option', async () => {
62+
const fromFirestore = vi.fn(() => ({
63+
foo: 'bar',
64+
}))
65+
const wrapper = factory({
66+
converter: {
67+
fromFirestore,
68+
toFirestore: (data: DocumentData) => data,
10469
},
105-
}),
106-
{}
107-
)
70+
})
71+
72+
const itemsRef = collection()
73+
await addDoc(itemsRef, {})
74+
75+
const spy = vi.fn(() => ({ bar: 'bar' }))
10876

109-
expect(pluginOptions.converter?.fromFirestore).not.toHaveBeenCalled()
110-
expect(spy).toHaveBeenCalledTimes(1)
111-
expect(spy).toHaveBeenCalledWith(
112-
expect.objectContaining({ data: expect.any(Function) }),
113-
expect.anything()
114-
)
115-
expect(wrapper.vm.items).toEqual([{ bar: 'bar' }])
77+
await wrapper.vm.$firestoreBind(
78+
'itemList',
79+
itemsRef.withConverter({
80+
fromFirestore: spy,
81+
toFirestore(data: DocumentData) {
82+
return data
83+
},
84+
}),
85+
{}
86+
)
87+
88+
expect(fromFirestore).not.toHaveBeenCalled()
89+
expect(spy).toHaveBeenCalledTimes(1)
90+
expect(spy).toHaveBeenCalledWith(
91+
expect.objectContaining({ data: expect.any(Function) }),
92+
expect.anything()
93+
)
94+
expect(wrapper.vm.itemList).toEqual([{ bar: 'bar' }])
95+
})
11696
})
11797
})

0 commit comments

Comments
 (0)