1
0
mirror of synced 2024-11-29 08:36:12 +03:00
vue-formulario/test/unit/FormulateInput.test.js
Justin Schroeder 34de4ba6dc
Adds support for @braid/vue-formulate-i18n
* Adds support form FormulateError form errors

* Adds support for form-errors prop

Also includes tests for both named-form-errors as well, form-errors prop, positioning form errors with the <FormulateErrors /> component, and allowing multiple <FormulateErrors />

* Adds form error support, error handling, and supporting tests

* Remove unused util functions

* Adds german localization locales/de.js (#14)

* Create de.js
* Added startsWith and endsWith

* adds build process for localization support, removes dist

* Adds danish localization

* fixes bug that resulted in validation failing if run more than once

Credit to @luan-nk-nguyen for discovering the bug

* Moves locales to vue-formulate-i18n

* Adds dist files for locales

* Adds vue-formulate-i18n 1.0.1

* Uses i18n 1.0.3

* Build files

* Fixes #19

* Update src/locales/README.md

Co-Authored-By: Andrew Boyd <andrew@wearebraid.com>

* changes v-html to v-text for error message output

credit to @skix123 for the bug report!

Co-authored-by: Andrew Boyd <andrew@wearebraid.com>
2020-03-07 09:03:59 -05:00

121 lines
3.6 KiB
JavaScript

import Vue from 'vue'
import flushPromises from 'flush-promises'
import { mount } from '@vue/test-utils'
import Formulate from '@/Formulate.js'
import FormulateInput from '@/FormulateInput.vue'
import FormulateInputBox from '@/inputs/FormulateInputBox.vue'
const globalRule = jest.fn((context) => { return false })
Vue.use(Formulate, {
locales: {
en: {
email: ({ value }) => `Super invalid email: ${value}`
}
},
rules: {
globalRule
},
library: {
special: {
classification: 'box',
component: 'FormulateInputBox'
}
}
})
describe('FormulateInput', () => {
it('allows custom field-rule level validation strings', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|in:abcdef',
validationMessages: {in: 'the value was different than expected'},
errorBehavior: 'live',
value: 'other value'
} })
await flushPromises()
expect(wrapper.find('.formulate-input-error').text()).toBe('the value was different than expected')
})
it('allows custom field-rule level validation functions', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|in:abcdef',
validationMessages: { in: ({ value }) => `The string ${value} is not correct.` },
errorBehavior: 'live',
value: 'other value'
} })
await flushPromises()
expect(wrapper.find('.formulate-input-error').text()).toBe('The string other value is not correct.')
})
it('uses globally overridden validation messages', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|email',
errorBehavior: 'live',
value: 'wrong email'
} })
await flushPromises()
expect(wrapper.find('.formulate-input-error').text()).toBe('Super invalid email: wrong email')
})
it('uses custom async validation rules on defined on the field', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|foobar',
validationMessages: {
foobar: 'failed the foobar check'
},
validationRules: {
foobar: async ({ value }) => value === 'foo'
},
validation: 'required|foobar',
errorBehavior: 'live',
value: 'bar'
} })
await flushPromises()
expect(wrapper.find('.formulate-input-error').text()).toBe('failed the foobar check')
})
it('uses custom sync validation rules on defined on the field', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|foobar',
validationMessages: {
foobar: 'failed the foobar check'
},
validationRules: {
foobar: ({ value }) => value === 'foo'
},
validation: 'required|foobar',
errorBehavior: 'live',
value: 'bar'
} })
await flushPromises()
expect(wrapper.find('.formulate-input-error').text()).toBe('failed the foobar check')
})
it('uses global custom validation rules', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'text',
validation: 'required|globalRule',
errorBehavior: 'live',
value: 'bar'
} })
await flushPromises()
expect(globalRule.mock.calls.length).toBe(1)
})
it('can extend its standard library of inputs', async () => {
const wrapper = mount(FormulateInput, { propsData: {
type: 'special',
validation: 'required',
errorBehavior: 'live',
value: 'bar'
} })
await flushPromises()
expect(wrapper.contains(FormulateInputBox)).toBe(true)
})
})