Skip to content

Commit 65a72b7

Browse files
committed
test: refactor rtdb options
1 parent 7af2c6e commit 65a72b7

File tree

1 file changed

+49
-68
lines changed

1 file changed

+49
-68
lines changed

tests/database/options.spec.ts

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,88 +8,69 @@ import {
88
} from '../../src'
99
import { setupDatabaseRefs } from '../utils'
1010

11-
const component = defineComponent({ template: 'no' })
11+
const component = defineComponent({
12+
template: 'no',
13+
data: () => ({ items: [], item: null }),
14+
})
1215

1316
describe('RTDB: plugin options', () => {
1417
const { databaseRef, push } = setupDatabaseRefs()
1518

16-
it('allows customizing $rtdbBind', () => {
17-
const wrapper = mount(component, {
18-
global: {
19-
plugins: [
20-
[
21-
databasePlugin,
22-
{
23-
bindName: '$myBind',
24-
unbindName: '$myUnbind',
25-
},
26-
],
27-
],
28-
},
29-
})
30-
expect(typeof (wrapper.vm as any).$myBind).toBe('function')
31-
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function')
32-
})
33-
34-
it('calls custom serialize function with a ref', async () => {
35-
const pluginOptions: DatabasePluginOptions = {
36-
serialize: vi.fn(() => ({ id: '2', foo: 'bar' })),
37-
}
38-
const { vm } = mount(
39-
{
40-
template: 'no',
41-
data: () => ({ items: [] }),
42-
},
43-
{
19+
describe('$rtdbBind', () => {
20+
function factory(pluginOptions?: DatabasePluginOptions) {
21+
return mount(component, {
4422
global: {
4523
plugins: [[databasePlugin, pluginOptions]],
4624
},
47-
}
48-
)
25+
})
26+
}
4927

50-
const itemListRef = databaseRef()
28+
it('allows customizing $rtdbBind', () => {
29+
const wrapper = factory({
30+
bindName: '$myBind',
31+
unbindName: '$myUnbind',
32+
})
33+
expect(typeof (wrapper.vm as any).$myBind).toBe('function')
34+
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function')
35+
})
5136

52-
const p = vm.$rtdbBind('items', itemListRef)
53-
await push(itemListRef, { text: 'foo' })
37+
it('calls custom serialize function with a ref', async () => {
38+
const serialize = vi.fn(() => ({ id: '2', foo: 'bar' }))
39+
const { vm } = factory({ serialize })
5440

55-
expect(pluginOptions.serialize).toHaveBeenCalledTimes(1)
56-
expect(pluginOptions.serialize).toHaveBeenCalledWith(
57-
expect.objectContaining({ val: expect.any(Function) })
58-
)
59-
expect(vm.items).toEqual([{ foo: 'bar', id: '2' }])
60-
})
41+
const itemListRef = databaseRef()
6142

62-
it('can override serialize with local option', async () => {
63-
const pluginOptions: DatabasePluginOptions = {
64-
serialize: vi.fn(() => ({ id: '2', foo: 'bar' })),
65-
}
43+
const p = vm.$rtdbBind('items', itemListRef)
44+
await push(itemListRef, { text: 'foo' })
6645

67-
const items = databaseRef()
68-
const { vm } = mount(
69-
{
70-
template: 'no',
71-
data: () => ({ items: [] }),
72-
},
73-
{
74-
global: {
75-
plugins: [[databasePlugin, pluginOptions]],
76-
},
77-
}
78-
)
46+
expect(serialize).toHaveBeenCalledTimes(1)
47+
expect(serialize).toHaveBeenCalledWith(
48+
expect.objectContaining({ val: expect.any(Function) })
49+
)
50+
expect(vm.items).toEqual([{ foo: 'bar', id: '2' }])
51+
})
52+
53+
it('can override serialize with local option', async () => {
54+
const serialize = vi.fn(() => ({ id: '2', foo: 'bar' }))
55+
const items = databaseRef()
56+
const { vm } = factory({ serialize })
7957

80-
const spy: DatabaseSnapshotSerializer = vi.fn(() => ({
81-
id: '3',
82-
bar: 'bar',
83-
}))
58+
const spy: DatabaseSnapshotSerializer = vi.fn(() => ({
59+
id: '3',
60+
bar: 'bar',
61+
}))
8462

85-
vm.$rtdbBind('items', items, { serialize: spy })
86-
await push(items, { text: 'foo' })
63+
vm.$rtdbBind('items', items, { serialize: spy })
64+
await push(items, { text: 'foo' })
8765

88-
expect(pluginOptions.serialize).not.toHaveBeenCalled()
89-
expect(spy).toHaveBeenCalledTimes(1)
90-
expect(spy).toHaveBeenCalledWith(
91-
expect.objectContaining({ val: expect.any(Function) })
92-
)
93-
expect(vm.items).toEqual([{ bar: 'bar', id: '3' }])
66+
expect(serialize).not.toHaveBeenCalled()
67+
expect(spy).toHaveBeenCalledTimes(1)
68+
expect(spy).toHaveBeenCalledWith(
69+
expect.objectContaining({ val: expect.any(Function) })
70+
)
71+
expect(vm.items).toEqual([{ bar: 'bar', id: '3' }])
72+
})
9473
})
74+
75+
// describe('firebase option', () => {})
9576
})

0 commit comments

Comments
 (0)