2020-05-22 14:22:16 +03:00
|
|
|
|
import Vue from 'vue'
|
2020-10-10 22:45:28 +03:00
|
|
|
|
import { mount } from '@vue/test-utils'
|
2020-05-22 14:22:16 +03:00
|
|
|
|
import flushPromises from 'flush-promises'
|
2020-10-11 11:41:32 +03:00
|
|
|
|
import Formulario from '@/index.ts'
|
2020-05-22 14:22:16 +03:00
|
|
|
|
import FormularioForm from '@/FormularioForm.vue'
|
|
|
|
|
|
2020-05-25 12:49:49 +03:00
|
|
|
|
Vue.use(Formulario, {
|
2020-10-26 17:03:24 +03:00
|
|
|
|
validationMessages: {
|
2020-10-22 10:47:53 +03:00
|
|
|
|
required: () => 'required',
|
|
|
|
|
'in': () => 'in',
|
|
|
|
|
min: () => 'min',
|
|
|
|
|
}
|
2020-05-25 12:49:49 +03:00
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
|
|
|
|
describe('FormularioForm', () => {
|
|
|
|
|
it('render a form DOM element', () => {
|
|
|
|
|
const wrapper = mount(FormularioForm)
|
|
|
|
|
expect(wrapper.find('form').exists()).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('accepts a default slot', () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: {
|
|
|
|
|
default: '<div class="default-slot-item" />'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
expect(wrapper.find('form div.default-slot-item').exists()).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 10:47:53 +03:00
|
|
|
|
it('Intercepts submit event', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: {
|
2020-10-22 10:47:53 +03:00
|
|
|
|
default: '<button type="submit" />'
|
2020-05-22 14:22:16 +03:00
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-22 10:47:53 +03:00
|
|
|
|
const spy = jest.spyOn(wrapper.vm, 'onFormSubmit')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
wrapper.find('form').trigger('submit')
|
|
|
|
|
expect(spy).toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Can set a field’s initial value', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-22 23:24:05 +03:00
|
|
|
|
propsData: { state: { test: 'Has initial value' } },
|
2020-10-22 13:46:35 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" validation="required|in:bar" name="test" >
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="text">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-10-22 13:46:35 +03:00
|
|
|
|
`
|
|
|
|
|
}
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
await flushPromises()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(wrapper.find('input').element['value']).toBe('Has initial value')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Lets individual fields override form initial value', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-22 23:24:05 +03:00
|
|
|
|
propsData: { state: { test: 'has initial value' } },
|
2020-10-22 13:46:35 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="test" value="123">
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="text">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-10-22 13:46:35 +03:00
|
|
|
|
`
|
|
|
|
|
}
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(wrapper.find('input').element['value']).toBe('123')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Lets fields set form initial value with value prop', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount({
|
2020-10-22 13:46:35 +03:00
|
|
|
|
data: () => ({ values: {} }),
|
|
|
|
|
template: `
|
|
|
|
|
<FormularioForm v-model="values">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField name="test" value="123" />
|
2020-10-22 13:46:35 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(wrapper.vm['values']).toEqual({ test: '123' })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Receives updates to form model when individual fields are edited', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount({
|
2020-10-22 13:46:35 +03:00
|
|
|
|
data: () => ({ values: { test: '' } }),
|
2020-05-22 14:22:16 +03:00
|
|
|
|
template: `
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<FormularioForm v-model="values">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="test" >
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="text">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-05-22 14:22:16 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
wrapper.find('input').setValue('Edited value')
|
|
|
|
|
expect(wrapper.vm['values']).toEqual({ test: 'Edited value' })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Field data updates when it is type of date', async () => {
|
2020-10-16 10:52:40 +03:00
|
|
|
|
const wrapper = mount({
|
2020-10-22 13:46:35 +03:00
|
|
|
|
data: () => ({ formValues: { date: new Date(123) } }),
|
2020-10-16 10:52:40 +03:00
|
|
|
|
template: `
|
|
|
|
|
<FormularioForm v-model="formValues" ref="form">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="date" >
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<span v-if="context.model">{{ context.model.getTime() }}</span>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-10-16 10:52:40 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-10-16 10:52:40 +03:00
|
|
|
|
expect(wrapper.find('span').text()).toBe('123')
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
wrapper.setData({ formValues: { date: new Date(234) } })
|
2020-10-16 10:52:40 +03:00
|
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.find('span').text()).toBe('234')
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Updates initial form values when input contains a populated v-model', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount({
|
2020-10-22 13:46:35 +03:00
|
|
|
|
data: () => ({
|
|
|
|
|
formValues: { test: '' },
|
|
|
|
|
fieldValue: '123',
|
|
|
|
|
}),
|
2020-05-22 14:22:16 +03:00
|
|
|
|
template: `
|
|
|
|
|
<FormularioForm v-model="formValues">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField name="test" v-model="fieldValue" />
|
2020-05-22 14:22:16 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
|
|
|
|
})
|
|
|
|
|
await flushPromises()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(wrapper.vm['formValues']).toEqual({ test: '123' })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Replacement test for the above test - not quite as good of a test.
|
2020-10-22 17:23:45 +03:00
|
|
|
|
it('Updates calls setFieldValue on form when a field contains a populated v-model on registration', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
propsData: {
|
2021-05-22 23:24:05 +03:00
|
|
|
|
state: { test: 'Initial' },
|
2020-05-22 14:22:16 +03:00
|
|
|
|
},
|
|
|
|
|
slots: {
|
2021-05-22 22:34:17 +03:00
|
|
|
|
default: '<FormularioField name="test" value="Overrides" />'
|
2020-10-25 14:28:14 +03:00
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
|
|
|
|
const emitted = wrapper.emitted('input')
|
|
|
|
|
|
|
|
|
|
expect(emitted).toBeTruthy()
|
|
|
|
|
expect(emitted[emitted.length - 1]).toEqual([{ test: 'Overrides' }])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('updates an inputs value when the form v-model is modified', async () => {
|
|
|
|
|
const wrapper = mount({
|
2020-10-25 14:28:14 +03:00
|
|
|
|
data: () => ({ values: { test: 'abcd' } }),
|
2020-05-22 14:22:16 +03:00
|
|
|
|
template: `
|
2020-10-25 14:28:14 +03:00
|
|
|
|
<FormularioForm v-model="values">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="test" >
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="text">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-05-22 14:22:16 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
|
|
|
|
wrapper.vm.values = { test: '1234' }
|
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
|
|
|
|
const input = wrapper.find('input[type="text"]')
|
|
|
|
|
|
|
|
|
|
expect(input).toBeTruthy()
|
|
|
|
|
expect(input.element['value']).toBe('1234')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 17:16:49 +03:00
|
|
|
|
it('Resolves hasValidationErrors to true', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-22 22:34:17 +03:00
|
|
|
|
slots: { default: '<FormularioField name="fieldName" validation="required" />' }
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
wrapper.find('form').trigger('submit')
|
|
|
|
|
await flushPromises()
|
2020-10-22 10:47:53 +03:00
|
|
|
|
|
|
|
|
|
const emitted = wrapper.emitted()
|
|
|
|
|
|
|
|
|
|
expect(emitted['error']).toBeTruthy()
|
|
|
|
|
expect(emitted['error'].length).toBe(1)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 10:47:53 +03:00
|
|
|
|
it('Resolves submitted form values to an object', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-22 22:34:17 +03:00
|
|
|
|
slots: { default: '<FormularioField name="fieldName" validation="required" value="Justin" />' }
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
wrapper.find('form').trigger('submit')
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
2020-10-22 10:47:53 +03:00
|
|
|
|
const emitted = wrapper.emitted()
|
|
|
|
|
|
|
|
|
|
expect(emitted['submit']).toBeTruthy()
|
|
|
|
|
expect(emitted['submit'].length).toBe(1)
|
2020-10-22 16:37:57 +03:00
|
|
|
|
expect(emitted['submit'][0]).toEqual([{ fieldName: 'Justin' }])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Receives a form-errors prop and displays it', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-22 13:46:35 +03:00
|
|
|
|
propsData: { formErrors: ['first', 'second'] },
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
await flushPromises()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(wrapper.vm.mergedFormErrors.length).toBe(2)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Aggregates form-errors prop with form-named errors', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-22 13:46:35 +03:00
|
|
|
|
propsData: { formErrors: ['first', 'second'] }
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
wrapper.vm.setErrors({ formErrors: ['third'] })
|
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.mergedFormErrors).length).toBe(3)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('displays field errors on inputs with errors prop', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-10 22:45:28 +03:00
|
|
|
|
propsData: { errors: { fieldWithErrors: ['This field has an error'] }},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="fieldWithErrors">
|
2020-10-26 00:51:19 +03:00
|
|
|
|
<span v-for="error in context.errors">{{ error }}</span>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-05-22 14:22:16 +03:00
|
|
|
|
`
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
await wrapper.vm.$nextTick()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.find('span').exists()).toBe(true)
|
2020-05-25 12:49:49 +03:00
|
|
|
|
expect(wrapper.find('span').text()).toEqual('This field has an error')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Is able to display multiple errors on multiple elements', async () => {
|
2020-10-22 17:23:45 +03:00
|
|
|
|
const errors = { inputA: ['first'], inputB: ['first', 'second']}
|
|
|
|
|
const wrapper = mount(FormularioForm, { propsData: { errors } })
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await wrapper.vm.$nextTick()
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.mergedFieldErrors).length).toBe(2)
|
|
|
|
|
expect(wrapper.vm.mergedFieldErrors.inputA.length).toBe(1)
|
|
|
|
|
expect(wrapper.vm.mergedFieldErrors.inputB.length).toBe(2)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
it('Can set multiple field errors with setErrors()', async () => {
|
2020-10-22 17:23:45 +03:00
|
|
|
|
const wrapper = mount(FormularioForm)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.mergedFieldErrors).length).toBe(0)
|
|
|
|
|
|
|
|
|
|
wrapper.vm.setErrors({
|
|
|
|
|
inputErrors: {
|
|
|
|
|
inputA: ['first'],
|
|
|
|
|
inputB: ['first', 'second'],
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
|
|
|
|
await wrapper.vm.$nextTick()
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
2020-10-22 13:46:35 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.mergedFieldErrors).length).toBe(2)
|
|
|
|
|
expect(wrapper.vm.mergedFieldErrors.inputA.length).toBe(1)
|
|
|
|
|
expect(wrapper.vm.mergedFieldErrors.inputB.length).toBe(2)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-22 15:29:01 +03:00
|
|
|
|
it('emits correct validation event when no errors', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-22 17:16:49 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="foo" validation="required|in:foo">
|
2020-10-25 22:45:58 +03:00
|
|
|
|
<input v-model="context.model" type="text" @blur="context.runValidation()">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
|
|
|
|
<FormularioField name="bar" validation="required" />
|
2020-10-22 17:16:49 +03:00
|
|
|
|
`,
|
|
|
|
|
}
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 17:16:49 +03:00
|
|
|
|
wrapper.find('input[type="text"]').setValue('foo')
|
|
|
|
|
wrapper.find('input[type="text"]').trigger('blur')
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-10-22 17:16:49 +03:00
|
|
|
|
expect(wrapper.emitted('validation')).toBeTruthy()
|
2021-05-22 15:29:01 +03:00
|
|
|
|
expect(wrapper.emitted('validation')).toEqual([[{
|
2020-10-22 13:46:35 +03:00
|
|
|
|
name: 'foo',
|
2020-10-25 22:17:01 +03:00
|
|
|
|
violations: [],
|
2021-05-22 15:29:01 +03:00
|
|
|
|
}]])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2020-10-22 17:16:49 +03:00
|
|
|
|
it('Emits correct validation event on entry', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: { default: `
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField
|
2020-10-28 21:25:20 +03:00
|
|
|
|
v-slot="{ context }"
|
|
|
|
|
name="firstField"
|
|
|
|
|
validation="required|in:foo"
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
v-model="context.model"
|
|
|
|
|
type="text"
|
|
|
|
|
@blur="context.runValidation()"
|
|
|
|
|
>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
|
|
|
|
<FormularioField
|
2020-10-28 21:25:20 +03:00
|
|
|
|
name="secondField"
|
|
|
|
|
validation="required"
|
|
|
|
|
/>
|
2020-05-22 14:22:16 +03:00
|
|
|
|
` }
|
|
|
|
|
})
|
|
|
|
|
wrapper.find('input[type="text"]').setValue('bar')
|
2020-10-22 17:16:49 +03:00
|
|
|
|
wrapper.find('input[type="text"]').trigger('blur')
|
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2020-10-22 17:16:49 +03:00
|
|
|
|
|
|
|
|
|
expect(wrapper.emitted('validation')).toBeTruthy()
|
|
|
|
|
expect(wrapper.emitted('validation').length).toBe(1)
|
|
|
|
|
expect(wrapper.emitted('validation')[0][0]).toEqual({
|
2020-10-28 21:25:20 +03:00
|
|
|
|
name: 'firstField',
|
|
|
|
|
violations: [ {
|
|
|
|
|
rule: expect.any(String),
|
|
|
|
|
args: ['foo'],
|
|
|
|
|
context: {
|
|
|
|
|
value: 'bar',
|
|
|
|
|
formValues: expect.any(Object),
|
|
|
|
|
name: 'firstField',
|
|
|
|
|
},
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
} ],
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-28 21:25:20 +03:00
|
|
|
|
it('Allows resetting a form, wiping validation.', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount({
|
2020-10-22 13:46:35 +03:00
|
|
|
|
data: () => ({ values: {} }),
|
2020-05-22 14:22:16 +03:00
|
|
|
|
template: `
|
|
|
|
|
<FormularioForm
|
2020-10-22 13:46:35 +03:00
|
|
|
|
v-model="values"
|
2020-05-22 14:22:16 +03:00
|
|
|
|
name="login"
|
|
|
|
|
ref="form"
|
|
|
|
|
>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="username" validation="required">
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="text">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
|
|
|
|
<FormularioField v-slot="{ context }" name="password" validation="required|min:4,length">
|
2020-10-22 13:46:35 +03:00
|
|
|
|
<input v-model="context.model" type="password">
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-05-22 14:22:16 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`,
|
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const password = wrapper.find('input[type="password"]')
|
2020-10-28 21:25:20 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
password.setValue('foo')
|
|
|
|
|
password.trigger('blur')
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
wrapper.find('form').trigger('submit')
|
2020-10-22 13:46:35 +03:00
|
|
|
|
wrapper.vm.$refs.form.setErrors({ inputErrors: { username: ['Failed'] } })
|
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
|
|
|
|
// First make sure we caught the errors
|
2020-05-22 14:22:16 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.$refs.form.mergedFieldErrors).length).toBe(1)
|
2020-10-22 13:46:35 +03:00
|
|
|
|
wrapper.vm.$refs.form.resetValidation()
|
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2020-10-28 21:25:20 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
expect(Object.keys(wrapper.vm.$refs.form.mergedFieldErrors).length).toBe(0)
|
|
|
|
|
})
|
2020-11-12 10:05:56 +03:00
|
|
|
|
|
|
|
|
|
it('Local errors resetted when errors prop cleared', async () => {
|
|
|
|
|
const wrapper = mount({
|
|
|
|
|
data: () => ({ values: {}, errors: { input: ['failure'] } }),
|
|
|
|
|
template: `
|
|
|
|
|
<FormularioForm
|
|
|
|
|
v-model="values"
|
|
|
|
|
:errors="errors"
|
|
|
|
|
ref="form"
|
|
|
|
|
>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
<FormularioField
|
2020-11-12 10:05:56 +03:00
|
|
|
|
v-slot="{ context }"
|
|
|
|
|
name="input"
|
|
|
|
|
ref="form"
|
|
|
|
|
>
|
|
|
|
|
<span v-for="error in context.allErrors">{{ error.message }}</span>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2020-11-12 10:05:56 +03:00
|
|
|
|
</FormularioForm>
|
|
|
|
|
`
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await flushPromises()
|
|
|
|
|
expect(wrapper.find('span').exists()).toBe(true)
|
|
|
|
|
|
|
|
|
|
wrapper.vm.errors = {}
|
|
|
|
|
await flushPromises()
|
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|