mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
refactor(Form): input events (#99)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -4,6 +4,9 @@ import Input, { type InputProps, type InputSlots } from '../../src/runtime/compo
|
||||
import ComponentRender from '../component-render'
|
||||
import theme from '#build/ui/input'
|
||||
|
||||
import { renderForm } from '../utils/form'
|
||||
import type { FormInputEvents } from '~/src/module'
|
||||
|
||||
describe('Input', () => {
|
||||
const sizes = Object.keys(theme.variants.size) as any
|
||||
const colors = Object.keys(theme.variants.color) as any
|
||||
@@ -67,4 +70,82 @@ describe('Input', () => {
|
||||
await input.trigger('change')
|
||||
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [['']] })
|
||||
})
|
||||
|
||||
describe('emits', () => {
|
||||
test('update:modelValue event', async () => {
|
||||
const wrapper = mount(Input)
|
||||
const input = wrapper.find('input')
|
||||
await input.setValue('bob@dylan.com')
|
||||
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [['bob@dylan.com']] })
|
||||
})
|
||||
|
||||
test('change event', async () => {
|
||||
const wrapper = mount(Input)
|
||||
const input = wrapper.find('input')
|
||||
await input.setValue('bob@dylan.com')
|
||||
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
|
||||
})
|
||||
|
||||
test('blur event', async () => {
|
||||
const wrapper = mount(Input)
|
||||
const input = wrapper.find('input')
|
||||
await input.trigger('blur')
|
||||
expect(wrapper.emitted()).toMatchObject({ blur: [[{ type: 'blur' }]] })
|
||||
})
|
||||
})
|
||||
|
||||
describe('form integration', async () => {
|
||||
async function createForm(validateOn?: FormInputEvents[]) {
|
||||
const wrapper = await renderForm({
|
||||
props: {
|
||||
validateOn,
|
||||
validateOnInputDelay: 0,
|
||||
async validate(state: any) {
|
||||
if (state.value !== 'valid')
|
||||
return [{ name: 'value', message: 'Error message' }]
|
||||
return []
|
||||
}
|
||||
},
|
||||
slotTemplate: `
|
||||
<UFormField name="value">
|
||||
<UInput id="input" v-model="state.value" />
|
||||
</UFormField>
|
||||
`
|
||||
})
|
||||
const input = wrapper.find('#input')
|
||||
return {
|
||||
wrapper,
|
||||
input
|
||||
}
|
||||
}
|
||||
|
||||
test('validate on blur works', async () => {
|
||||
const { input, wrapper } = await createForm(['blur'])
|
||||
await input.trigger('blur')
|
||||
expect(wrapper.text()).toContain('Error message')
|
||||
|
||||
await input.setValue('valid')
|
||||
await input.trigger('blur')
|
||||
expect(wrapper.text()).not.toContain('Error message')
|
||||
})
|
||||
|
||||
test('validate on change works', async () => {
|
||||
const { input, wrapper } = await createForm(['change'])
|
||||
await input.trigger('change')
|
||||
expect(wrapper.text()).toContain('Error message')
|
||||
|
||||
input.setValue('valid')
|
||||
await input.trigger('change')
|
||||
expect(wrapper.text()).not.toContain('Error message')
|
||||
})
|
||||
|
||||
test('validate on input works', async () => {
|
||||
const { input, wrapper } = await createForm(['input'])
|
||||
await input.setValue('value')
|
||||
expect(wrapper.text()).toContain('Error message')
|
||||
|
||||
await input.setValue('valid')
|
||||
expect(wrapper.text()).not.toContain('Error message')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user