1
0
mirror of synced 2024-11-25 23:06:02 +03:00

Merge branch 'master' of github.com:wearebraid/vue-formulate

This commit is contained in:
Justin Schroeder 2020-04-13 12:00:36 -04:00
commit 26117ed5b3
9 changed files with 97 additions and 7 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -231,7 +231,7 @@ class Formulate {
* @param {error} * @param {error}
*/ */
handle (err, formName, skip = false) { handle (err, formName, skip = false) {
const e = skip ? err : this.options.errorHandler(err) const e = skip ? err : this.options.errorHandler(err, formName)
if (formName && this.registry.has(formName)) { if (formName && this.registry.has(formName)) {
this.registry.get(formName).applyErrors({ this.registry.get(formName).applyErrors({
formErrors: arrayify(e.formErrors), formErrors: arrayify(e.formErrors),

View File

@ -177,8 +177,6 @@ function createOptionList (options) {
optionList.push({ value, label: options[value], id: `${that.elementAttributes.id}_${value}` }) optionList.push({ value, label: options[value], id: `${that.elementAttributes.id}_${value}` })
} }
return optionList return optionList
} else if (Array.isArray(options) && !options.length) {
return [{ value: this.value, label: (this.label || this.name), id: this.context.id || nanoid(9) }]
} }
return options return options
} }

View File

@ -230,6 +230,47 @@ describe('FormulateForm', () => {
expect(wrapper.vm.$formulate.registry.get('login')).toBe(wrapper.vm) expect(wrapper.vm.$formulate.registry.get('login')).toBe(wrapper.vm)
}) })
it('calls custom error handler with error and name', async () => {
const mockHandler = jest.fn((err, name) => err);
const wrapper = mount({
template: `
<div>
<FormulateForm
name="login"
/>
<FormulateForm
name="register"
/>
</div>
`
})
wrapper.vm.$formulate.extend({ errorHandler: mockHandler })
wrapper.vm.$formulate.handle({ formErrors: ['This is an error message'] }, 'login')
expect(mockHandler.mock.calls.length).toBe(1);
expect(mockHandler.mock.calls[0]).toEqual([{ formErrors: ['This is an error message'] }, 'login']);
})
it('errors are displayed on correctly named components', async () => {
const wrapper = mount({
template: `
<div>
<FormulateForm
name="login"
/>
<FormulateForm
name="register"
/>
</div>
`
})
expect(wrapper.vm.$formulate.registry.has('login') && wrapper.vm.$formulate.registry.has('register')).toBe(true)
wrapper.vm.$formulate.handle({ formErrors: ['This is an error message'] }, 'login')
await flushPromises()
expect(wrapper.findAll('.formulate-form').length).toBe(2)
expect(wrapper.find('.formulate-form--login .formulate-form-errors').exists()).toBe(true)
expect(wrapper.find('.formulate-form--register .formulate-form-errors').exists()).toBe(false)
})
it('errors are displayed on correctly named components', async () => { it('errors are displayed on correctly named components', async () => {
const wrapper = mount({ const wrapper = mount({
template: ` template: `

View File

@ -201,4 +201,10 @@ describe('FormulateInputBox', () => {
await flushPromises() await flushPromises()
expect(wrapper.find('.formulate-input-error').exists()).toBe(true) expect(wrapper.find('.formulate-input-error').exists()).toBe(true)
}) })
it('renders no boxes when options array is empty', async () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'checkbox', options: [] } })
expect(wrapper.contains(FormulateInputGroup)).toBe(true)
expect(wrapper.find('input[type="checkbox"]').exists()).toBe(false)
})
}) })

View File

@ -1,6 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import { mount } from '@vue/test-utils' import { mount } from '@vue/test-utils'
import Formulate from '../../src/Formulate.js' import Formulate from '@/Formulate.js'
import FormulateInput from '@/FormulateInput.vue' import FormulateInput from '@/FormulateInput.vue'
import FormulateInputButton from '@/inputs/FormulateInputButton.vue' import FormulateInputButton from '@/inputs/FormulateInputButton.vue'

View File

@ -0,0 +1,45 @@
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import Formulate from '@/Formulate.js'
import FormulateInput from '@/FormulateInput.vue'
import FormulateInputSelect from '@/inputs/FormulateInputSelect.vue'
Vue.use(Formulate)
describe('FormulateInputSelect', () => {
it('renders select input when type is "select"', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'select' } })
expect(wrapper.contains(FormulateInputSelect)).toBe(true)
})
it('renders select options when options object is passed', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'select', options: { first: 'First', second: 'Second' } } })
const option = wrapper.find('option[value="second"]')
expect(option.exists()).toBe(true)
expect(option.text()).toBe('Second')
})
it('renders select options when options array is passed', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'select', options: [
{ value: 13, label: 'Jane' },
{ value: 22, label: 'Jon' }
]} })
const option = wrapper.find('option[value="22"]')
expect(option.exists()).toBe(true)
expect(option.text()).toBe('Jon')
})
it('renders select list with no options when empty array is passed.', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'select', options: []} })
const option = wrapper.find('option')
expect(option.exists()).toBe(false)
})
it('renders select list placeholder option.', () => {
const wrapper = mount(FormulateInput, { propsData: { type: 'select', placeholder: 'Select this', options: []} })
const options = wrapper.findAll('option')
expect(options.length).toBe(1)
expect(options.at(0).attributes('disabled')).toBeTruthy()
})
})