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', () => {
|
2020-10-25 14:28:14 +03:00
|
|
|
it('Allows custom field-rule level validation strings', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-05-22 14:22:16 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
2020-10-26 00:36:03 +03:00
|
|
|
value: 'other value',
|
2020-05-22 14:22:16 +03:00
|
|
|
validation: 'required|in:abcdef',
|
2020-10-25 14:28:14 +03:00
|
|
|
validationMessages: { in: 'the value was different than expected' },
|
2020-10-26 00:36:03 +03:00
|
|
|
validationBehavior: 'live',
|
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-26 00:36:03 +03:00
|
|
|
it('No validation on created when validationBehavior is not live', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-10-16 10:52:54 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: {in: 'the value was different than expected'},
|
|
|
|
value: 'other value'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-10-26 00:36:03 +03:00
|
|
|
default: `<div><span v-for="error in props.context.violations">{{ error.message }}</span></div>`
|
2020-10-16 10:52:54 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').exists()).toBe(false)
|
|
|
|
})
|
|
|
|
|
2020-10-26 00:36:03 +03:00
|
|
|
it('No validation on value change when validationBehavior is "submit"', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-10-20 18:26:41 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: {in: 'the value was different than expected'},
|
2020-10-26 00:36:03 +03:00
|
|
|
validationBehavior: '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-10-26 00:36:03 +03:00
|
|
|
it('Allows custom field-rule level validation functions', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-05-22 14:22:16 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
|
|
|
validationMessages: { in: ({ value }) => `The string ${value} is not correct.` },
|
2020-10-26 00:36:03 +03:00
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
value: 'other value'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-10-26 00:36:03 +03:00
|
|
|
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.')
|
|
|
|
})
|
|
|
|
|
2020-10-26 00:36:03 +03:00
|
|
|
it('No validation on created when validationBehavior is default', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-10-18 20:45:18 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|in:abcdef',
|
2020-10-26 00:36:03 +03:00
|
|
|
validationMessages: { in: 'the value was different than expected' },
|
2020-10-18 20:45:18 +03:00
|
|
|
value: 'other value'
|
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-10-26 00:51:19 +03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-10-26 00:36:03 +03:00
|
|
|
it('Uses custom async validation rules on defined on the field', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = mount(FormularioField, {
|
2020-05-22 14:22:16 +03:00
|
|
|
propsData: {
|
|
|
|
name: 'test',
|
|
|
|
validation: 'required|foobar',
|
2020-10-26 00:36:03 +03:00
|
|
|
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: {
|
2020-10-26 00:51:19 +03:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2020-10-26 00:36:03 +03:00
|
|
|
it('Uses custom sync validation rules on defined on the field', async () => {
|
2021-05-22 16:50:53 +03:00
|
|
|
const wrapper = 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|foobar',
|
2020-10-26 00:36:03 +03:00
|
|
|
validationRules: { foobar: ({ value }) => value === 'foo' },
|
|
|
|
validationMessages: { foobar: 'failed the foobar check' },
|
|
|
|
validationBehavior: 'live',
|
2020-05-22 14:22:16 +03:00
|
|
|
},
|
|
|
|
scopedSlots: {
|
2020-10-26 00:36:03 +03:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
it('uses global custom validation rules', async () => {
|
|
|
|
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
|
|
|
})
|
|
|
|
await flushPromises()
|
|
|
|
expect(globalRule.mock.calls.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
2021-05-24 20:07:28 +03:00
|
|
|
it('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: {
|
2020-10-25 22:17:01 +03:00
|
|
|
name: 'fieldName',
|
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([[{
|
2020-10-25 22:17:01 +03:00
|
|
|
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),
|
|
|
|
}],
|
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
|
|
|
|
) => {
|
|
|
|
const wrapper = mount({
|
|
|
|
data: () => ({ validation }),
|
|
|
|
template: `
|
|
|
|
<FormularioField
|
|
|
|
name="test"
|
|
|
|
:validation="validation"
|
|
|
|
validation-behavior="live"
|
|
|
|
v-slot="{ context }"
|
|
|
|
>
|
|
|
|
<div v-for="(_, index) in context.violations" :key="index" data-violation />
|
|
|
|
</FormularioField>
|
|
|
|
`
|
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-24 20:07:28 +03:00
|
|
|
it('proceeds validation if passed a rule with bail modifier', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
template: `
|
|
|
|
<FormularioField
|
|
|
|
name="test"
|
|
|
|
value="123"
|
|
|
|
validation="^required|in:xyz|min:10,length"
|
|
|
|
validation-behavior="live"
|
|
|
|
v-slot="{ context }"
|
|
|
|
>
|
|
|
|
<div v-for="(_, index) in context.violations" :key="index" data-violation />
|
|
|
|
</FormularioField>
|
|
|
|
`
|
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
|
|
|
})
|
|
|
|
|
2020-10-26 00:36:03 +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, {
|
2020-10-10 22:45:28 +03:00
|
|
|
propsData: { name: 'test' },
|
2020-05-22 14:22:16 +03:00
|
|
|
slots: {
|
|
|
|
default: `
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioField
|
2020-10-26 00:51:19 +03:00
|
|
|
v-slot="{ context }"
|
2020-10-26 00:36:03 +03:00
|
|
|
name="testinput"
|
|
|
|
validation="required"
|
|
|
|
validation-behavior="submit"
|
|
|
|
>
|
2020-10-26 00:51:19 +03:00
|
|
|
<span v-for="error in context.violations">{{ error.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
|
|
|
|
2020-10-16 10:52:54 +03:00
|
|
|
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-10-26 00:36:03 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
expect(wrapper.find('span').exists()).toBe(true)
|
|
|
|
})
|
2020-11-13 11:55:01 +03:00
|
|
|
|
|
|
|
it('Model getter for input', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
data: () => ({ values: { test: 'abcd' } }),
|
|
|
|
template: `
|
|
|
|
<FormularioForm v-model="values">
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioField v-slot="{ context }" :model-get-converter="onGet" name="test" >
|
2020-11-13 11:55:01 +03:00
|
|
|
<span>{{ context.model }}</span>
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
2020-11-13 11:55:01 +03:00
|
|
|
</FormularioForm>
|
|
|
|
`,
|
|
|
|
methods: {
|
|
|
|
onGet(source) {
|
|
|
|
if (!(source instanceof Date)) {
|
|
|
|
return 'invalid date'
|
|
|
|
}
|
|
|
|
|
|
|
|
return source.getDate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').text()).toBe('invalid date')
|
|
|
|
|
|
|
|
wrapper.vm.values = { test: new Date('1995-12-17') }
|
|
|
|
await flushPromises()
|
|
|
|
expect(wrapper.find('span').text()).toBe('17')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Model setter for input', async () => {
|
|
|
|
const wrapper = mount({
|
2021-05-24 20:07:28 +03:00
|
|
|
data: () => ({ values: { date: 'not a date' } }),
|
2020-11-13 11:55:01 +03:00
|
|
|
template: `
|
|
|
|
<FormularioForm v-model="values">
|
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
|
|
|
|
|
|
|
expect(wrapper.vm.values.date).toBe(undefined)
|
2020-11-13 11:55:01 +03:00
|
|
|
|
|
|
|
wrapper.find('input[type="text"]').element['value'] = '12'
|
|
|
|
wrapper.find('input[type="text"]').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
|
|
|
|
|
|
|
expect(wrapper.vm.values.date.toISOString()).toBe(
|
|
|
|
(new Date('2001-05-12')).toISOString()
|
|
|
|
)
|
2020-11-13 11:55:01 +03:00
|
|
|
})
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|