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'
|
2021-05-22 16:50:53 +03:00
|
|
|
import FormularioField from '@/FormularioField.vue'
|
2021-05-24 20:07:28 +03:00
|
|
|
import FormularioForm from '@/FormularioForm.vue'
|
2020-05-22 14:22:16 +03:00
|
|
|
|
2021-05-22 15:29:01 +03:00
|
|
|
const globalRule = jest.fn(() => false)
|
2020-05-22 14:22:16 +03:00
|
|
|
|
2020-10-10 22:45:28 +03:00
|
|
|
Vue.use(Formulario, {
|
2020-10-26 17:03:24 +03:00
|
|
|
validationRules: { globalRule },
|
|
|
|
validationMessages: {
|
2020-10-22 10:47:53 +03:00
|
|
|
required: () => 'required',
|
|
|
|
'in': () => 'in',
|
|
|
|
min: () => 'min',
|
|
|
|
globalRule: () => 'globalRule',
|
2020-10-26 17:03:24 +03:00
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-22 16:50:53 +03:00
|
|
|
describe('FormularioField', () => {
|
2021-05-25 21:37:42 +03:00
|
|
|
const createWrapper = (props = {}) => mount(FormularioField, {
|
|
|
|
propsData: {
|
|
|
|
name: 'field',
|
|
|
|
value: 'initial',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: { in: 'the value was different than expected' },
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
|
|
|
default: `
|
|
|
|
<div>
|
|
|
|
<input type="text" v-model="props.context.model">
|
|
|
|
<span v-for="(violation, index) in props.context.violations" :key="index" data-violation>
|
|
|
|
{{ violation.message }}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
`,
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('allows custom field-rule level validation strings', async () => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
validationBehavior: 'live',
|
2020-10-16 10:52:54 +03:00
|
|
|
})
|
2021-05-25 21:37:42 +03:00
|
|
|
|
2020-10-16 10:52:54 +03:00
|
|
|
await flushPromises()
|
2021-05-25 21:37:42 +03:00
|
|
|
|
|
|
|
expect(wrapper.find('[data-violation]').text()).toBe(
|
|
|
|
'the value was different than expected'
|
|
|
|
)
|
2020-10-16 10:52:54 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test.each([
|
|
|
|
['demand'],
|
|
|
|
['submit'],
|
|
|
|
])('no validation when validationBehavior is not "live"', async validationBehavior => {
|
|
|
|
const wrapper = createWrapper({ validationBehavior })
|
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
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('[data-violation]').exists()).toBe(false)
|
2020-10-20 18:26:41 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
wrapper.find('input').element['value'] = 'updated'
|
|
|
|
wrapper.find('input').trigger('change')
|
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
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('input').element['value']).toBe('updated')
|
|
|
|
expect(wrapper.find('[data-violation]').exists()).toBe(false)
|
2020-10-20 18:26:41 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('allows custom validation rule message', async () => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
value: 'other value',
|
|
|
|
validationMessages: { in: ({ value }) => `the string "${value}" is not correct` },
|
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2020-10-18 20:45:18 +03:00
|
|
|
await flushPromises()
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('[data-violation]').text()).toBe(
|
|
|
|
'the string "other value" is not correct'
|
|
|
|
)
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test.each([
|
|
|
|
['bar', ({ value }) => value === 'foo'],
|
|
|
|
['bar', ({ value }) => Promise.resolve(value === 'foo')],
|
|
|
|
])('uses local custom validation rules', async (value, rule) => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
value,
|
|
|
|
validation: 'required|custom',
|
|
|
|
validationRules: { custom: rule },
|
|
|
|
validationMessages: { custom: 'failed the custom rule check' },
|
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
2021-05-25 21:37:42 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
await flushPromises()
|
2021-05-25 21:37:42 +03:00
|
|
|
|
|
|
|
expect(wrapper.find('[data-violation]').text()).toBe('failed the custom rule check')
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('uses global custom validation rules', async () => {
|
2021-05-24 20:07:28 +03:00
|
|
|
mount(FormularioField, {
|
2020-05-22 14:22:16 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
2020-10-26 00:36:03 +03:00
|
|
|
value: 'bar',
|
2020-05-22 14:22:16 +03:00
|
|
|
validation: 'required|globalRule',
|
2020-10-26 00:36:03 +03:00
|
|
|
validationBehavior: 'live',
|
2021-05-24 20:07:28 +03:00
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
2021-05-25 21:37:42 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
await flushPromises()
|
2021-05-25 21:37:42 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
expect(globalRule.mock.calls.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('emits correct validation event', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-10-09 22:58:28 +03:00
|
|
|
propsData: {
|
2021-05-25 21:37:42 +03:00
|
|
|
name: 'field',
|
2020-10-26 00:36:03 +03:00
|
|
|
value: '',
|
|
|
|
validation: 'required',
|
|
|
|
validationBehavior: 'live',
|
2021-05-24 20:07:28 +03:00
|
|
|
},
|
2020-10-09 22:58:28 +03:00
|
|
|
})
|
2021-05-24 20:07:28 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
await flushPromises()
|
2020-10-25 22:17:01 +03:00
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
expect(wrapper.emitted('validation')).toEqual([[{
|
2021-05-25 21:37:42 +03:00
|
|
|
name: 'field',
|
2020-10-25 22:17:01 +03:00
|
|
|
violations: [{
|
2021-05-25 21:37:42 +03:00
|
|
|
rule: 'required',
|
2020-10-25 22:17:01 +03:00
|
|
|
args: expect.any(Array),
|
2021-05-25 21:37:42 +03:00
|
|
|
context: {
|
|
|
|
name: 'field',
|
|
|
|
value: '',
|
|
|
|
formValues: {},
|
|
|
|
},
|
2020-10-22 16:37:57 +03:00
|
|
|
message: expect.any(String),
|
|
|
|
}],
|
2021-05-24 20:07:28 +03:00
|
|
|
}]])
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
test.each([
|
|
|
|
['bail|required|in:xyz', 1],
|
|
|
|
['^required|in:xyz|min:10,length', 1],
|
|
|
|
['required|^in:xyz|min:10,length', 2],
|
|
|
|
['required|in:xyz|bail', 2],
|
|
|
|
])('prevents further validation if not passed a rule with bail modifier', async (
|
|
|
|
validation,
|
|
|
|
expectedViolationsCount
|
|
|
|
) => {
|
2021-05-25 21:37:42 +03:00
|
|
|
const wrapper = createWrapper({
|
|
|
|
value: '',
|
|
|
|
validation,
|
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
await flushPromises()
|
2020-05-22 14:22:16 +03:00
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
expect(wrapper.findAll('[data-violation]').length).toBe(expectedViolationsCount)
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('proceeds validation if passed a rule with bail modifier', async () => {
|
|
|
|
const wrapper = createWrapper({
|
|
|
|
value: '123',
|
|
|
|
validation: '^required|in:xyz|min:10,length',
|
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
expect(wrapper.findAll('[data-violation]').length).toBe(2)
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('displays errors when validation-behavior is submit and form is submitted', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
slots: {
|
|
|
|
default: `
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioField
|
2020-10-26 00:51:19 +03:00
|
|
|
v-slot="{ context }"
|
2021-05-25 21:37:42 +03:00
|
|
|
name="field"
|
2020-10-26 00:36:03 +03:00
|
|
|
validation="required"
|
|
|
|
validation-behavior="submit"
|
|
|
|
>
|
2021-05-25 21:37:42 +03:00
|
|
|
<span v-for="(violation, index) in context.violations" :key="index" data-violation>
|
|
|
|
{{ violation.message }}
|
|
|
|
</span>
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
2020-05-22 14:22:16 +03:00
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
2020-10-18 20:45:18 +03:00
|
|
|
|
2020-10-16 10:52:54 +03:00
|
|
|
await flushPromises()
|
2020-10-26 00:36:03 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('[data-violation]').exists()).toBe(false)
|
2020-10-16 10:52:54 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
wrapper.trigger('submit')
|
|
|
|
|
2020-10-18 20:45:18 +03:00
|
|
|
await flushPromises()
|
2020-10-26 00:36:03 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('[data-violation]').exists()).toBe(true)
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
2020-11-13 11:55:01 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('model getter for input', async () => {
|
2020-11-13 11:55:01 +03:00
|
|
|
const wrapper = mount({
|
2021-05-25 21:37:42 +03:00
|
|
|
data: () => ({ state: { date: 'not a date' } }),
|
2020-11-13 11:55:01 +03:00
|
|
|
template: `
|
2021-05-25 21:37:42 +03:00
|
|
|
<FormularioForm v-model="state">
|
|
|
|
<FormularioField
|
|
|
|
v-slot="{ context }"
|
|
|
|
:model-get-converter="onGet"
|
|
|
|
name="date"
|
|
|
|
>
|
|
|
|
<span data-output>{{ context.model }}</span>
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
2020-11-13 11:55:01 +03:00
|
|
|
</FormularioForm>
|
|
|
|
`,
|
|
|
|
methods: {
|
2021-05-25 21:37:42 +03:00
|
|
|
onGet (source) {
|
|
|
|
return source instanceof Date ? source.getDate() : 'invalid date'
|
|
|
|
},
|
2020-11-13 11:55:01 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await flushPromises()
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
expect(wrapper.find('[data-output]').text()).toBe('invalid date')
|
|
|
|
|
|
|
|
wrapper.vm.state = { date: new Date('1995-12-17') }
|
|
|
|
|
2020-11-13 11:55:01 +03:00
|
|
|
await flushPromises()
|
2021-05-25 21:37:42 +03:00
|
|
|
|
|
|
|
expect(wrapper.find('[data-output]').text()).toBe('17')
|
2020-11-13 11:55:01 +03:00
|
|
|
})
|
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
test('model setter for input', async () => {
|
2020-11-13 11:55:01 +03:00
|
|
|
const wrapper = mount({
|
2021-05-25 21:37:42 +03:00
|
|
|
data: () => ({ state: { date: 'not a date' } }),
|
2020-11-13 11:55:01 +03:00
|
|
|
template: `
|
2021-05-25 21:37:42 +03:00
|
|
|
<FormularioForm v-model="state">
|
2021-05-24 20:07:28 +03:00
|
|
|
<FormularioField
|
|
|
|
v-slot="{ context }"
|
|
|
|
:model-get-converter="onGet"
|
|
|
|
:model-set-converter="onSet"
|
|
|
|
name="date"
|
|
|
|
>
|
2020-11-13 11:55:01 +03:00
|
|
|
<input type="text" v-model="context.model">
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
2020-11-13 11:55:01 +03:00
|
|
|
</FormularioForm>
|
|
|
|
`,
|
|
|
|
methods: {
|
2021-05-24 20:07:28 +03:00
|
|
|
onGet (source) {
|
|
|
|
return source instanceof Date ? source.getDate() : source
|
2020-11-13 11:55:01 +03:00
|
|
|
},
|
2021-05-24 20:07:28 +03:00
|
|
|
|
|
|
|
onSet (source) {
|
2020-11-13 11:55:01 +03:00
|
|
|
if (source instanceof Date) {
|
|
|
|
return source
|
|
|
|
}
|
2021-05-24 20:07:28 +03:00
|
|
|
|
2020-11-13 11:55:01 +03:00
|
|
|
if (isNaN(source)) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
const result = new Date('2001-05-01')
|
2020-11-13 11:55:01 +03:00
|
|
|
result.setDate(source)
|
|
|
|
|
|
|
|
return result
|
2021-05-24 20:07:28 +03:00
|
|
|
},
|
2020-11-13 11:55:01 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await flushPromises()
|
2021-05-24 20:07:28 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
wrapper.find('input').setValue('12')
|
|
|
|
wrapper.find('input').trigger('input')
|
2021-05-24 20:07:28 +03:00
|
|
|
|
2020-11-13 11:55:01 +03:00
|
|
|
await flushPromises()
|
2021-05-24 20:07:28 +03:00
|
|
|
|
2021-05-25 21:37:42 +03:00
|
|
|
const form = wrapper.findComponent(FormularioForm)
|
|
|
|
|
|
|
|
expect(form.emitted('input')).toEqual([
|
|
|
|
[{}],
|
|
|
|
[{ date: new Date('2001-05-12') }],
|
|
|
|
])
|
2020-11-13 11:55:01 +03:00
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|