-
-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { defineComponent } from 'vue' | ||
import { mount } from '@vue/test-utils' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { rtdbPlugin, DatabasePluginOptions } from '../../src' | ||
import { setupDatabaseRefs } from '../utils' | ||
import { push } from 'firebase/database' | ||
|
||
const component = defineComponent({ template: 'no' }) | ||
|
||
describe('RTDB: plugin options', () => { | ||
const { databaseRef } = setupDatabaseRefs() | ||
|
||
it('allows customizing $rtdbBind', () => { | ||
const wrapper = mount(component, { | ||
global: { | ||
plugins: [ | ||
[ | ||
rtdbPlugin, | ||
{ | ||
bindName: '$myBind', | ||
unbindName: '$myUnbind', | ||
}, | ||
], | ||
], | ||
}, | ||
}) | ||
expect(typeof (wrapper.vm as any).$myBind).toBe('function') | ||
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function') | ||
}) | ||
|
||
it('calls custom serialize function with a ref', async () => { | ||
const pluginOptions: DatabasePluginOptions = { | ||
serialize: vi.fn(() => ({ foo: 'bar' })), | ||
} | ||
const { vm } = mount( | ||
{ | ||
template: 'no', | ||
data: () => ({ items: [] }), | ||
}, | ||
{ | ||
global: { | ||
plugins: [[rtdbPlugin, pluginOptions]], | ||
}, | ||
} | ||
) | ||
|
||
const itemListRef = databaseRef() | ||
|
||
const p = vm.$rtdbBind('items', itemListRef) | ||
push(itemListRef, { text: 'foo' }) | ||
|
||
await p | ||
|
||
expect(pluginOptions.serialize).toHaveBeenCalledTimes(1) | ||
expect(pluginOptions.serialize).toHaveBeenCalledWith( | ||
expect.objectContaining({ val: expect.any(Function) }) | ||
) | ||
expect(vm.items).toEqual([{ foo: 'bar' }]) | ||
}) | ||
|
||
it('can override serialize with local option', async () => { | ||
const pluginOptions = { | ||
serialize: vi.fn(() => ({ foo: 'bar' })), | ||
} | ||
|
||
const items = databaseRef() | ||
const { vm } = mount( | ||
{ | ||
template: 'no', | ||
data: () => ({ items: [] }), | ||
}, | ||
{ | ||
global: { | ||
plugins: [[rtdbPlugin, pluginOptions]], | ||
}, | ||
} | ||
) | ||
|
||
const spy = vi.fn(() => ({ bar: 'bar' })) | ||
|
||
vm.$rtdbBind('items', items, { serialize: spy }) | ||
await push(items, { text: 'foo' }) | ||
|
||
expect(pluginOptions.serialize).not.toHaveBeenCalled() | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
expect(spy).toHaveBeenCalledWith( | ||
expect.objectContaining({ val: expect.any(Function) }) | ||
) | ||
expect(vm.items).toEqual([{ bar: 'bar' }]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters