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:
@@ -1,7 +1,10 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { describe, it, expect, test } from 'vitest'
|
||||
import Checkbox, { type CheckboxProps, type CheckboxSlots } from '../../src/runtime/components/Checkbox.vue'
|
||||
import ComponentRender from '../component-render'
|
||||
import theme from '#build/ui/checkbox'
|
||||
import { renderForm } from '../utils/form'
|
||||
import { mount, flushPromises } from '@vue/test-utils'
|
||||
import type { FormInputEvents } from '~/src/module'
|
||||
|
||||
describe('Checkbox', () => {
|
||||
const sizes = Object.keys(theme.variants.size) as any
|
||||
@@ -31,4 +34,70 @@ describe('Checkbox', () => {
|
||||
const html = await ComponentRender(nameOrHtml, options, Checkbox)
|
||||
expect(html).toMatchSnapshot()
|
||||
})
|
||||
|
||||
describe('emits', () => {
|
||||
test('update:modelValue event', async () => {
|
||||
const wrapper = mount(Checkbox)
|
||||
const input = wrapper.findComponent({ name: 'CheckboxRoot' })
|
||||
await input.vm.$emit('update:checked', true)
|
||||
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[true]] })
|
||||
})
|
||||
|
||||
test('change event', async () => {
|
||||
const wrapper = mount(Checkbox)
|
||||
const input = wrapper.findComponent({ name: 'CheckboxRoot' })
|
||||
await input.vm.$emit('update:checked', false)
|
||||
expect(wrapper.emitted()).toMatchObject({ change: [[{ type: 'change' }]] })
|
||||
})
|
||||
})
|
||||
|
||||
describe('form integration', async () => {
|
||||
async function createForm(validateOn?: FormInputEvents[]) {
|
||||
const wrapper = await renderForm({
|
||||
props: {
|
||||
validateOn,
|
||||
validateOnInputDelay: 0,
|
||||
async validate(state: any) {
|
||||
if (!state.value)
|
||||
return [{ name: 'value', message: 'Error message' }]
|
||||
return []
|
||||
}
|
||||
},
|
||||
slotTemplate: `
|
||||
<UFormField name="value">
|
||||
<UCheckbox v-model="state.value" />
|
||||
</UFormField>
|
||||
`
|
||||
})
|
||||
const input = wrapper.findComponent({ name: 'CheckboxRoot' })
|
||||
return {
|
||||
wrapper,
|
||||
input
|
||||
}
|
||||
}
|
||||
|
||||
test('validate on change works', async () => {
|
||||
const { input, wrapper } = await createForm(['change'])
|
||||
await input.vm.$emit('update:checked', false)
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.text()).toContain('Error message')
|
||||
|
||||
await input.vm.$emit('update:checked', true)
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).not.toContain('Error message')
|
||||
})
|
||||
|
||||
test('validate on input works', async () => {
|
||||
const { input, wrapper } = await createForm(['input'])
|
||||
await input.vm.$emit('update:checked', false)
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.text()).toContain('Error message')
|
||||
|
||||
await input.vm.$emit('update:checked', true)
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).not.toContain('Error message')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user