2020-05-22 14:22:16 +03:00
|
|
|
|
import Vue from 'vue'
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
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'
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
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', () => {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('renders a form DOM element', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm)
|
|
|
|
|
expect(wrapper.find('form').exists()).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('accepts a default slot', () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
default: '<div data-default />'
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.find('form [data-default]').exists()).toBe(true)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('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
|
|
|
|
})
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
describe('emits input event', () => {
|
|
|
|
|
test('when individual fields contain a populated value', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
propsData: { state: { field: 'initial' } },
|
|
|
|
|
slots: {
|
|
|
|
|
default: '<FormularioField name="field" value="populated" />'
|
|
|
|
|
},
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await Vue.nextTick()
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
const emitted = wrapper.emitted('input')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(emitted).toBeTruthy()
|
|
|
|
|
expect(emitted[emitted.length - 1]).toEqual([{ field: 'populated' }])
|
2020-10-16 10:52:40 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('when individual fields are edited', () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
propsData: { state: { field: 'initial' } },
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<FormularioField v-slot="{ context }" name="field" >
|
|
|
|
|
<input v-model="context.model" type="text">
|
|
|
|
|
</FormularioField>
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
})
|
2020-10-16 10:52:40 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.find('input').setValue('updated')
|
2020-10-16 10:52:40 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
const emitted = wrapper.emitted('input')
|
2020-10-16 10:52:40 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(emitted).toBeTruthy()
|
|
|
|
|
expect(emitted[emitted.length - 1]).toEqual([{ field: 'updated' }])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('updates a field when the form v-model is modified', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
propsData: { state: { field: 'initial' } },
|
2020-05-22 14:22:16 +03:00
|
|
|
|
slots: {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
default: `
|
|
|
|
|
<FormularioField v-slot="{ context }" name="field">
|
|
|
|
|
<input v-model="context.model" type="text">
|
|
|
|
|
</FormularioField>
|
|
|
|
|
`
|
2020-10-25 14:28:14 +03:00
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
const input = wrapper.find('input')
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(input).toBeTruthy()
|
|
|
|
|
expect(input.element['value']).toBe('initial')
|
|
|
|
|
|
|
|
|
|
wrapper.setProps({ state: { field: 'updated' } })
|
|
|
|
|
|
|
|
|
|
await Vue.nextTick()
|
|
|
|
|
|
|
|
|
|
expect(input.element['value']).toBe('updated')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('updates a field when it is an instance of Date', async () => {
|
|
|
|
|
const dateA = new Date('1970-01-01')
|
|
|
|
|
const dateB = new Date()
|
|
|
|
|
|
|
|
|
|
const wrapper = mount(FormularioForm,{
|
|
|
|
|
propsData: { state: { date: dateA } },
|
|
|
|
|
scopedSlots: {
|
|
|
|
|
default: `
|
|
|
|
|
<FormularioField v-slot="{ context }" name="date">
|
|
|
|
|
<span v-if="context.model">{{ context.model.toISOString() }}</span>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2021-05-24 23:12:40 +03:00
|
|
|
|
`,
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.find('span').text()).toBe(dateA.toISOString())
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.setProps({ state: { date: dateB } })
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await Vue.nextTick()
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.find('span').text()).toBe(dateB.toISOString())
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('resolves submitted form values to an object', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
slots: {
|
2021-05-25 01:47:44 +03:00
|
|
|
|
default: '<FormularioField name="name" validation="required" value="Justin" />'
|
2021-05-24 23:12:40 +03:00
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
wrapper.find('form').trigger('submit')
|
2020-10-22 10:47:53 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await flushPromises()
|
2020-10-22 10:47:53 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.emitted('submit')).toEqual([
|
2021-05-25 01:47:44 +03:00
|
|
|
|
[{ name: 'Justin' }],
|
2021-05-24 23:12:40 +03:00
|
|
|
|
])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-25 01:47:44 +03:00
|
|
|
|
test('resolves runValidation', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<div>
|
|
|
|
|
<FormularioField name="address.street" validation="required" />
|
|
|
|
|
<FormularioField name="address.building" validation="required" />
|
|
|
|
|
</div>
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const violations = await wrapper.vm.runValidation()
|
|
|
|
|
const state = {
|
|
|
|
|
address: {
|
|
|
|
|
street: null,
|
2021-06-10 17:01:50 +03:00
|
|
|
|
building: null,
|
2021-05-25 01:47:44 +03:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-30 17:40:33 +03:00
|
|
|
|
expect(violations).toEqual({
|
|
|
|
|
'address.street': [{
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
rule: 'required',
|
|
|
|
|
args: [],
|
|
|
|
|
context: {
|
|
|
|
|
name: 'address.street',
|
2021-06-10 17:01:50 +03:00
|
|
|
|
value: '',
|
2021-05-30 17:40:33 +03:00
|
|
|
|
formValues: state,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
'address.building': [{
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
rule: 'required',
|
|
|
|
|
args: [],
|
|
|
|
|
context: {
|
|
|
|
|
name: 'address.building',
|
|
|
|
|
value: '',
|
|
|
|
|
formValues: state,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('resolves runValidation via $formulario', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
propsData: {
|
|
|
|
|
id: 'address',
|
|
|
|
|
},
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<div>
|
|
|
|
|
<FormularioField name="address.street" validation="required" />
|
|
|
|
|
<FormularioField name="address.building" validation="required" />
|
|
|
|
|
</div>
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const violations = await wrapper.vm.$formulario.runValidation('address')
|
|
|
|
|
const state = {
|
|
|
|
|
address: {
|
|
|
|
|
street: null,
|
2021-06-10 17:01:50 +03:00
|
|
|
|
building: null,
|
2021-05-30 17:40:33 +03:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-25 01:47:44 +03:00
|
|
|
|
expect(violations).toEqual({
|
|
|
|
|
'address.street': [{
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
rule: 'required',
|
|
|
|
|
args: [],
|
|
|
|
|
context: {
|
|
|
|
|
name: 'address.street',
|
2021-06-10 17:01:50 +03:00
|
|
|
|
value: '',
|
2021-05-25 01:47:44 +03:00
|
|
|
|
formValues: state,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
'address.building': [{
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
rule: 'required',
|
|
|
|
|
args: [],
|
|
|
|
|
context: {
|
|
|
|
|
name: 'address.building',
|
|
|
|
|
value: '',
|
|
|
|
|
formValues: state,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('resolves hasValidationErrors to true', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: '<FormularioField name="fieldName" validation="required" />',
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
wrapper.find('form').trigger('submit')
|
2021-05-24 23:12:40 +03:00
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
|
await flushPromises()
|
|
|
|
|
|
2021-05-25 01:47:44 +03:00
|
|
|
|
expect(wrapper.emitted('error')).toBeTruthy()
|
|
|
|
|
expect(wrapper.emitted('error').length).toBe(1)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
describe('allows setting fields errors', () => {
|
|
|
|
|
/**
|
|
|
|
|
* @param props
|
|
|
|
|
* @return {Wrapper<FormularioForm>}
|
|
|
|
|
*/
|
|
|
|
|
const createWrapper = (props = {}) => mount(FormularioForm, {
|
|
|
|
|
propsData: props,
|
|
|
|
|
scopedSlots: {
|
|
|
|
|
default: '<div><div v-for="error in props.errors" data-error /></div>',
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('via prop', async () => {
|
|
|
|
|
const wrapper = createWrapper({ formErrors: ['first', 'second'] })
|
|
|
|
|
|
|
|
|
|
expect(wrapper.findAll('[data-error]').length).toBe(2)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('manually with setErrors()', async () => {
|
|
|
|
|
const wrapper = createWrapper({ formErrors: ['first', 'second'] })
|
|
|
|
|
|
|
|
|
|
wrapper.vm.setErrors({ formErrors: ['third'] })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await wrapper.vm.$nextTick()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.findAll('[data-error]').length).toBe(3)
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('displays field errors on inputs with errors prop', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-24 23:12:40 +03:00
|
|
|
|
propsData: { fieldsErrors: { field: ['This field has an error'] }},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
2021-05-24 23:12:40 +03:00
|
|
|
|
<FormularioField v-slot="{ context }" name="field">
|
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>
|
2021-05-24 23:12:40 +03:00
|
|
|
|
`,
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
describe('allows setting fields errors', () => {
|
|
|
|
|
/**
|
|
|
|
|
* @param props
|
|
|
|
|
* @return {Wrapper<FormularioForm>}
|
|
|
|
|
*/
|
|
|
|
|
const createWrapper = (props = {}) => mount(FormularioForm, {
|
|
|
|
|
propsData: props,
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<div>
|
|
|
|
|
<FormularioField v-slot="{ context }" name="fieldA">
|
|
|
|
|
<div v-for="error in context.errors" data-error-a>{{ error }}</div>
|
|
|
|
|
</FormularioField>
|
|
|
|
|
|
|
|
|
|
<FormularioField v-slot="{ context }" name="fieldB">
|
|
|
|
|
<div v-for="error in context.errors" data-error-b>{{ error }}</div>
|
|
|
|
|
</FormularioField>
|
|
|
|
|
</div>
|
|
|
|
|
`,
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('via prop', async () => {
|
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
|
fieldsErrors: { fieldA: ['first'], fieldB: ['first', 'second']},
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.findAll('[data-error-a]').length).toBe(1)
|
|
|
|
|
expect(wrapper.findAll('[data-error-b]').length).toBe(2)
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('manually with setErrors()', async () => {
|
|
|
|
|
const wrapper = createWrapper()
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.findAll('[data-error-a]').length).toBe(0)
|
|
|
|
|
expect(wrapper.findAll('[data-error-b]').length).toBe(0)
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.vm.setErrors({ fieldsErrors: { fieldA: ['first'], fieldB: ['first', 'second'] } })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await Vue.nextTick()
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.findAll('[data-error-a]').length).toBe(1)
|
|
|
|
|
expect(wrapper.findAll('[data-error-b]').length).toBe(2)
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
describe('emits correct validation event', () => {
|
|
|
|
|
/**
|
|
|
|
|
* @return {Wrapper<FormularioForm>}
|
|
|
|
|
*/
|
|
|
|
|
const createWrapper = () => 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
|
|
|
|
`,
|
2021-05-24 23:12:40 +03:00
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('when no errors', async () => {
|
|
|
|
|
const wrapper = createWrapper()
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.find('input[type="text"]').setValue('foo')
|
|
|
|
|
wrapper.find('input[type="text"]').trigger('blur')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await flushPromises()
|
2020-10-22 17:16:49 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
expect(wrapper.emitted('validation')).toBeTruthy()
|
|
|
|
|
expect(wrapper.emitted('validation')).toEqual([[{
|
2021-06-11 22:43:30 +03:00
|
|
|
|
path: 'foo',
|
2021-05-24 23:12:40 +03:00
|
|
|
|
violations: [],
|
|
|
|
|
}]])
|
|
|
|
|
})
|
2020-10-22 17:16:49 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('on entry', async () => {
|
|
|
|
|
const wrapper = createWrapper()
|
|
|
|
|
|
|
|
|
|
wrapper.find('input[type="text"]').setValue('bar')
|
|
|
|
|
wrapper.find('input[type="text"]').trigger('blur')
|
|
|
|
|
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.emitted('validation')).toBeTruthy()
|
|
|
|
|
expect(wrapper.emitted('validation')).toEqual([[ {
|
2021-06-11 22:43:30 +03:00
|
|
|
|
path: 'foo',
|
2021-05-24 23:12:40 +03:00
|
|
|
|
violations: [ {
|
|
|
|
|
rule: expect.any(String),
|
|
|
|
|
args: ['foo'],
|
|
|
|
|
context: {
|
|
|
|
|
value: 'bar',
|
|
|
|
|
formValues: expect.any(Object),
|
|
|
|
|
name: 'foo',
|
|
|
|
|
},
|
|
|
|
|
message: expect.any(String),
|
|
|
|
|
} ],
|
|
|
|
|
} ]])
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('allows resetting form validation', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<div>
|
|
|
|
|
<FormularioField v-slot="{ context }" name="username" validation="required">
|
|
|
|
|
<input v-model="context.model" type="text">
|
|
|
|
|
<div v-for="error in context.allErrors" data-username-error />
|
|
|
|
|
</FormularioField>
|
|
|
|
|
|
|
|
|
|
<FormularioField v-slot="{ context }" name="password" validation="required|min:4,length">
|
|
|
|
|
<input v-model="context.model" type="password" @blur="context.runValidation()">
|
|
|
|
|
<div v-for="error in context.allErrors" data-password-error />
|
|
|
|
|
</FormularioField>
|
|
|
|
|
</div>
|
|
|
|
|
`,
|
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
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')
|
2021-05-24 23:12:40 +03:00
|
|
|
|
password.trigger('input')
|
2020-05-22 14:22:16 +03:00
|
|
|
|
password.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
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.vm.setErrors({ fieldsErrors: { username: ['required'] } })
|
2020-10-22 13:46:35 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
await Vue.nextTick()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.findAll('[data-username-error]').length).toBe(1)
|
|
|
|
|
expect(wrapper.findAll('[data-password-error]').length).toBe(1)
|
2020-10-28 21:25:20 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.vm.resetValidation()
|
|
|
|
|
|
|
|
|
|
await Vue.nextTick()
|
|
|
|
|
|
|
|
|
|
expect(wrapper.findAll('[data-username-error]').length).toBe(0)
|
|
|
|
|
expect(wrapper.findAll('[data-password-error]').length).toBe(0)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|
2020-11-12 10:05:56 +03:00
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
test('local errors are reset when errors prop cleared', async () => {
|
|
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
|
propsData: { fieldsErrors: { input: ['failure'] } },
|
|
|
|
|
slots: {
|
|
|
|
|
default: `
|
|
|
|
|
<FormularioField v-slot="{ context }" name="input">
|
2020-11-12 10:05:56 +03:00
|
|
|
|
<span v-for="error in context.allErrors">{{ error.message }}</span>
|
2021-05-22 22:34:17 +03:00
|
|
|
|
</FormularioField>
|
2021-05-24 23:12:40 +03:00
|
|
|
|
`,
|
|
|
|
|
},
|
2020-11-12 10:05:56 +03:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(wrapper.find('span').exists()).toBe(true)
|
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
|
wrapper.setProps({ fieldsErrors: {} })
|
|
|
|
|
|
|
|
|
|
await Vue.nextTick()
|
|
|
|
|
|
2020-11-12 10:05:56 +03:00
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
|
})
|