2020-10-10 22:45:28 +03:00
|
|
|
import Vue from 'vue'
|
2020-05-22 14:22:16 +03:00
|
|
|
import flushPromises from 'flush-promises'
|
2020-10-10 22:45:28 +03:00
|
|
|
import { mount } from '@vue/test-utils'
|
2020-10-09 22:58:28 +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'
|
|
|
|
import FormularioInput from '@/FormularioInput.vue'
|
|
|
|
|
2020-10-10 22:45:28 +03:00
|
|
|
const globalRule = jest.fn(() => { return false })
|
2020-05-22 14:22:16 +03:00
|
|
|
|
2020-10-10 22:45:28 +03:00
|
|
|
Vue.use(Formulario, {
|
2020-10-22 10:47:53 +03:00
|
|
|
rules: { globalRule },
|
2020-10-25 22:39:38 +03:00
|
|
|
messages: {
|
2020-10-22 10:47:53 +03:00
|
|
|
required: () => 'required',
|
|
|
|
'in': () => 'in',
|
|
|
|
min: () => 'min',
|
|
|
|
globalRule: () => 'globalRule',
|
|
|
|
}
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('FormularioInput', () => {
|
2020-10-25 14:28:14 +03:00
|
|
|
it('Allows custom field-rule level validation strings', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
2020-10-25 14:28:14 +03:00
|
|
|
validationMessages: { in: 'the value was different than expected' },
|
2020-05-22 14:22:16 +03:00
|
|
|
errorBehavior: 'live',
|
2020-10-25 14:28:14 +03:00
|
|
|
value: 'other value',
|
2020-05-22 14:22:16 +03:00
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-10-25 14:28:14 +03:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2020-10-16 10:52:54 +03:00
|
|
|
it('no validation on created when errorBehavior 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.allErrors">{{ error.message }}</span></div>`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
2020-10-25 14:28:14 +03:00
|
|
|
it('No validation on value change when errorBehavior is not live', async () => {
|
2020-10-20 18:26:41 +03:00
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: {in: 'the value was different than expected'},
|
|
|
|
errorBehavior: 'submit',
|
2020-10-25 14:28:14 +03:00
|
|
|
value: 'Initial'
|
2020-10-20 18:26:41 +03:00
|
|
|
},
|
|
|
|
scopedSlots: {
|
|
|
|
default: `<div>
|
|
|
|
<input type="text" v-model="props.context.model">
|
2020-10-25 14:28:14 +03:00
|
|
|
<span v-for="error in props.context.violations">{{ error.message }}</span>
|
2020-10-20 18:26:41 +03:00
|
|
|
</div>`
|
|
|
|
}
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
|
2020-10-20 18:26:41 +03:00
|
|
|
await flushPromises()
|
2020-10-25 14:28:14 +03:00
|
|
|
|
2020-10-20 18:26:41 +03:00
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
|
2020-10-25 14:28:14 +03:00
|
|
|
wrapper.find('input[type="text"]').element['value'] = 'Test'
|
|
|
|
wrapper.find('input[type="text"]').trigger('change')
|
|
|
|
|
2020-10-20 18:26:41 +03:00
|
|
|
await flushPromises()
|
2020-10-25 14:28:14 +03:00
|
|
|
|
|
|
|
expect(wrapper.find('input[type="text"]').element['value']).toBe('Test')
|
2020-10-20 18:26:41 +03:00
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
it('allows custom field-rule level validation functions', async () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: { in: ({ value }) => `The string ${value} is not correct.` },
|
|
|
|
errorBehavior: 'live',
|
|
|
|
value: 'other value'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-05-25 12:49:49 +03:00
|
|
|
default: `<div><span v-for="error in props.context.allErrors">{{ 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.')
|
|
|
|
})
|
|
|
|
|
2020-10-18 20:45:18 +03:00
|
|
|
it('no validation on created when errorBehavior 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.allErrors">{{ error.message }}</span></div>`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
it('uses custom async validation rules on defined on the field', async () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|foobar',
|
|
|
|
validationMessages: {
|
|
|
|
foobar: 'failed the foobar check'
|
|
|
|
},
|
|
|
|
validationRules: {
|
|
|
|
foobar: async ({ value }) => value === 'foo'
|
|
|
|
},
|
|
|
|
errorBehavior: 'live',
|
|
|
|
value: 'bar'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-05-25 12:49:49 +03:00
|
|
|
default: `<div><span v-for="error in props.context.allErrors">{{ 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 () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|foobar',
|
|
|
|
validationMessages: {
|
|
|
|
foobar: 'failed the foobar check'
|
|
|
|
},
|
|
|
|
validationRules: {
|
|
|
|
foobar: ({ value }) => value === 'foo'
|
|
|
|
},
|
|
|
|
errorBehavior: 'live',
|
|
|
|
value: 'bar'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-05-25 12:49:49 +03:00
|
|
|
default: `<div><span v-for="error in props.context.allErrors">{{ error.message }}</span></div>`
|
2020-05-22 14:22:16 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').text()).toBe('failed the foobar check')
|
|
|
|
})
|
|
|
|
|
2020-10-25 14:28:14 +03:00
|
|
|
it('Uses global custom validation rules', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|globalRule',
|
|
|
|
errorBehavior: 'live',
|
|
|
|
value: 'bar'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(globalRule.mock.calls.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2020-10-25 14:28:14 +03:00
|
|
|
it('Emits correct validation event', async () => {
|
2020-10-09 22:58:28 +03:00
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
validation: 'required',
|
|
|
|
errorBehavior: 'live',
|
|
|
|
value: '',
|
2020-10-25 22:17:01 +03:00
|
|
|
name: 'fieldName',
|
2020-10-09 22:58:28 +03:00
|
|
|
}
|
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
await flushPromises()
|
2020-10-25 22:17:01 +03:00
|
|
|
|
|
|
|
expect(wrapper.emitted('validation')).toBeTruthy()
|
|
|
|
expect(wrapper.emitted('validation')[0][0]).toEqual({
|
|
|
|
name: 'fieldName',
|
|
|
|
violations: [{
|
2020-10-22 16:37:57 +03:00
|
|
|
rule: expect.stringContaining('required'),
|
2020-10-25 22:17:01 +03:00
|
|
|
args: expect.any(Array),
|
2020-10-22 16:37:57 +03:00
|
|
|
context: expect.any(Object),
|
|
|
|
message: expect.any(String),
|
|
|
|
}],
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-22 16:37:57 +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', errorBehavior: 'live' }
|
|
|
|
})
|
|
|
|
await flushPromises();
|
2020-10-25 14:28:14 +03:00
|
|
|
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 () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: { name: 'test', validation: 'required|in:xyz|bail', errorBehavior: 'live' }
|
|
|
|
})
|
|
|
|
await flushPromises();
|
2020-10-25 14:28:14 +03:00
|
|
|
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 () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: { name: 'test', validation: '^required|in:xyz|min:10,length', errorBehavior: 'live', value: '123' }
|
|
|
|
})
|
|
|
|
await flushPromises();
|
2020-10-25 14:28:14 +03:00
|
|
|
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 () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: { name: 'test', validation: '^required|in:xyz|min:10,length', errorBehavior: 'live' }
|
|
|
|
})
|
|
|
|
await flushPromises();
|
2020-10-25 14:28:14 +03:00
|
|
|
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', errorBehavior: 'live' }
|
|
|
|
})
|
|
|
|
await flushPromises();
|
2020-10-25 14:28:14 +03:00
|
|
|
expect(wrapper.vm.context.violations.length).toBe(2);
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not show errors on blur when set error-behavior is submit', async () => {
|
|
|
|
const wrapper = mount(FormularioInput, {
|
|
|
|
propsData: {
|
|
|
|
validation: 'required',
|
|
|
|
errorBehavior: 'submit',
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
|
|
|
default: `
|
|
|
|
<div>
|
2020-10-25 22:45:58 +03:00
|
|
|
<input v-model="props.context.model" @blur="props.context.runValidation()">
|
2020-05-25 12:49:49 +03:00
|
|
|
<span v-if="props.context.formShouldShowErrors" v-for="error in props.context.allErrors">{{ error.message }}</span>
|
2020-05-22 14:22:16 +03:00
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
wrapper.find('input').trigger('input')
|
|
|
|
wrapper.find('input').trigger('blur')
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('displays errors when error-behavior is submit and form is submitted', async () => {
|
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-10 22:45:28 +03:00
|
|
|
propsData: { name: 'test' },
|
2020-05-22 14:22:16 +03:00
|
|
|
slots: {
|
|
|
|
default: `
|
|
|
|
<FormularioInput v-slot="inputProps" validation="required" name="testinput" error-behavior="submit">
|
2020-05-25 12:49:49 +03:00
|
|
|
<span v-for="error in inputProps.context.allErrors">{{ error.message }}</span>
|
2020-05-22 14:22:16 +03:00
|
|
|
</FormularioInput>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
2020-10-18 20:45:18 +03:00
|
|
|
|
2020-10-16 10:52:54 +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)
|
|
|
|
})
|
|
|
|
})
|