1
0
mirror of synced 2024-12-01 17:46:07 +03:00
vue-formulario/test/unit/FormularioInput.test.js

279 lines
9.7 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
2020-05-22 14:22:16 +03:00
import flushPromises from 'flush-promises'
import { mount } from '@vue/test-utils'
2020-10-09 22:58:28 +03:00
import Formulario from '@/index.ts'
2020-05-22 14:22:16 +03:00
import FormularioForm from '@/FormularioForm.vue'
import FormularioInput from '@/FormularioInput.vue'
const globalRule = jest.fn(() => { return false })
2020-05-22 14:22:16 +03:00
Vue.use(Formulario, {
validationRules: { globalRule },
validationMessages: {
required: () => 'required',
'in': () => 'in',
min: () => 'min',
globalRule: () => 'globalRule',
},
2020-05-22 14:22:16 +03:00
})
describe('FormularioInput', () => {
it('Allows custom field-rule level validation strings', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
value: 'other value',
2020-05-22 14:22:16 +03:00
validation: 'required|in:abcdef',
validationMessages: { in: 'the value was different than expected' },
validationBehavior: 'live',
2020-05-22 14:22:16 +03:00
},
scopedSlots: {
default: `<div><span v-for="violation in props.context.violations">{{ violation.message }}</span></div>`
2020-10-09 22:58:28 +03:00
},
2020-05-22 14:22:16 +03:00
})
await flushPromises()
expect(wrapper.find('span').text()).toBe('the value was different than expected')
})
it('No validation on created when validationBehavior is not live', async () => {
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|in:abcdef',
validationMessages: {in: 'the value was different than expected'},
value: 'other value'
},
scopedSlots: {
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
}
})
await flushPromises()
expect(wrapper.find('span').exists()).toBe(false)
})
it('No validation on value change when validationBehavior is "submit"', async () => {
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|in:abcdef',
validationMessages: {in: 'the value was different than expected'},
validationBehavior: 'submit',
value: 'Initial'
},
scopedSlots: {
default: `<div>
<input type="text" v-model="props.context.model">
<span v-for="error in props.context.violations">{{ error.message }}</span>
</div>`
}
})
await flushPromises()
expect(wrapper.find('span').exists()).toBe(false)
wrapper.find('input[type="text"]').element['value'] = 'Test'
wrapper.find('input[type="text"]').trigger('change')
await flushPromises()
expect(wrapper.find('input[type="text"]').element['value']).toBe('Test')
expect(wrapper.find('span').exists()).toBe(false)
})
it('Allows custom field-rule level validation functions', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|in:abcdef',
validationMessages: { in: ({ value }) => `The string ${value} is not correct.` },
validationBehavior: 'live',
2020-05-22 14:22:16 +03:00
value: 'other value'
},
scopedSlots: {
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
2020-05-22 14:22:16 +03:00
}
})
await flushPromises()
expect(wrapper.find('span').text()).toBe('The string other value is not correct.')
})
it('No validation on created when validationBehavior is default', async () => {
2020-10-18 20:45:18 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|in:abcdef',
validationMessages: { in: 'the value was different than expected' },
2020-10-18 20:45:18 +03:00
value: 'other value'
},
scopedSlots: {
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
2020-10-18 20:45:18 +03:00
}
})
await flushPromises()
expect(wrapper.find('span').exists()).toBe(false)
})
it('Uses custom async validation rules on defined on the field', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|foobar',
validationRules: { foobar: async ({ value }) => value === 'foo' },
validationMessages: { foobar: 'failed the foobar check' },
validationBehavior: 'live',
2020-05-22 14:22:16 +03:00
value: 'bar'
},
scopedSlots: {
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
2020-05-22 14:22:16 +03:00
}
})
await flushPromises()
expect(wrapper.find('span').text()).toBe('failed the foobar check')
})
it('Uses custom sync validation rules on defined on the field', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
value: 'bar',
2020-05-22 14:22:16 +03:00
validation: 'required|foobar',
validationRules: { foobar: ({ value }) => value === 'foo' },
validationMessages: { foobar: 'failed the foobar check' },
validationBehavior: 'live',
2020-05-22 14:22:16 +03:00
},
scopedSlots: {
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
2020-05-22 14:22:16 +03:00
}
})
await flushPromises()
expect(wrapper.find('span').text()).toBe('failed the foobar check')
})
it('Uses global custom validation rules', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
value: 'bar',
2020-05-22 14:22:16 +03:00
validation: 'required|globalRule',
validationBehavior: 'live',
2020-05-22 14:22:16 +03:00
}
})
await flushPromises()
expect(globalRule.mock.calls.length).toBe(1)
})
it('Emits correct validation event', async () => {
2020-10-09 22:58:28 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'fieldName',
value: '',
validation: 'required',
validationBehavior: 'live',
2020-10-09 22:58:28 +03:00
}
})
2020-05-22 14:22:16 +03:00
await flushPromises()
expect(wrapper.emitted('validation')).toBeTruthy()
expect(wrapper.emitted('validation')[0][0]).toEqual({
name: 'fieldName',
violations: [{
rule: expect.stringContaining('required'),
args: expect.any(Array),
context: expect.any(Object),
message: expect.any(String),
}],
2020-05-22 14:22:16 +03:00
})
})
it('Can bail on validation when encountering the bail rule', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'bail|required|in:xyz',
validationBehavior: 'live',
},
2020-05-22 14:22:16 +03:00
})
await flushPromises();
expect(wrapper.vm.context.violations.length).toBe(1);
2020-05-22 14:22:16 +03:00
})
it('Can show multiple validation errors if they occur before the bail rule', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|in:xyz|bail',
validationBehavior: 'live',
},
2020-05-22 14:22:16 +03:00
})
await flushPromises();
expect(wrapper.vm.context.violations.length).toBe(2);
2020-05-22 14:22:16 +03:00
})
it('Can avoid bail behavior by using modifier', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
value: '123',
validation: '^required|in:xyz|min:10,length',
validationBehavior: 'live',
},
2020-05-22 14:22:16 +03:00
})
await flushPromises();
expect(wrapper.vm.context.violations.length).toBe(2);
2020-05-22 14:22:16 +03:00
})
it('Prevents later error messages when modified rule fails', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: '^required|in:xyz|min:10,length',
validationBehavior: 'live',
},
2020-05-22 14:22:16 +03:00
})
await flushPromises();
expect(wrapper.vm.context.violations.length).toBe(1);
2020-05-22 14:22:16 +03:00
})
it('can bail in the middle of the rule set with a modifier', async () => {
const wrapper = mount(FormularioInput, {
propsData: {
name: 'test',
validation: 'required|^in:xyz|min:10,length',
validationBehavior: 'live',
},
2020-05-22 14:22:16 +03:00
})
await flushPromises();
expect(wrapper.vm.context.violations.length).toBe(2);
2020-05-22 14:22:16 +03:00
})
it('Displays errors when validation-behavior is submit and form is submitted', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioForm, {
propsData: { name: 'test' },
2020-05-22 14:22:16 +03:00
slots: {
default: `
<FormularioInput
v-slot="{ context }"
name="testinput"
validation="required"
validation-behavior="submit"
>
<span v-for="error in context.violations">{{ error.message }}</span>
2020-05-22 14:22:16 +03:00
</FormularioInput>
`
}
})
2020-10-18 20:45:18 +03:00
await flushPromises()
expect(wrapper.find('span').exists()).toBe(false)
2020-05-22 14:22:16 +03:00
wrapper.trigger('submit')
2020-10-18 20:45:18 +03:00
await flushPromises()
2020-05-22 14:22:16 +03:00
expect(wrapper.find('span').exists()).toBe(true)
})
})