1
0
mirror of synced 2024-11-28 16:16:09 +03:00
vue-formulario/test/unit/FormularioGrouping.test.js

95 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-05-22 14:22:16 +03:00
import Vue from 'vue'
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import Formulario from '@/index.ts'
2020-05-22 14:22:16 +03:00
import FormularioForm from '@/FormularioForm.vue'
import FormularioGrouping from '@/FormularioGrouping.vue'
Vue.use(Formulario)
describe('FormularioGrouping', () => {
it('Grouped fields to be set', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioForm, {
propsData: { name: 'form' },
2020-05-22 14:22:16 +03:00
slots: {
default: `
<FormularioGrouping name="group">
<FormularioInput name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
2020-05-22 14:22:16 +03:00
</FormularioInput>
</FormularioGrouping>
`
}
})
expect(wrapper.findAll('input[type="text"]').length).toBe(1)
wrapper.find('input[type="text"]').setValue('test')
const submission = await wrapper.vm.formSubmitted()
expect(submission).toEqual({ group: { text: 'test' } })
2020-05-22 14:22:16 +03:00
})
it('Grouped fields to be got', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioForm, {
propsData: {
name: 'form',
formularioValue: {
group: { text: 'Group text' },
text: 'Text',
}
},
2020-05-22 14:22:16 +03:00
slots: {
default: `
<FormularioGrouping name="group">
<FormularioInput name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
2020-05-22 14:22:16 +03:00
</FormularioInput>
</FormularioGrouping>
`
}
})
expect(wrapper.find('input[type="text"]').element.value).toBe('Group text')
2020-05-22 14:22:16 +03:00
})
it('Data reactive with grouped fields', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount({
data: () => ({ values: {} }),
2020-05-22 14:22:16 +03:00
template: `
<FormularioForm name="form" v-model="values">
<FormularioGrouping name="group">
<FormularioInput name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
<span>{{ values.group.text }}</span>
2020-05-22 14:22:16 +03:00
</FormularioInput>
</FormularioGrouping>
</FormularioForm>
`
})
expect(wrapper.find('span').text()).toBe('')
wrapper.find('input[type="text"]').setValue('test')
await flushPromises()
expect(wrapper.find('span').text()).toBe('test')
})
it('Errors are set for grouped fields', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount({
data: () => ({ values: {} }),
2020-05-22 14:22:16 +03:00
template: `
<FormularioForm
v-model="values"
:errors="{'group.text': 'Test error'}"
name="form"
>
<FormularioGrouping name="group">
<FormularioInput name="text" v-slot="{ context }">
<span v-for="error in context.allErrors">
{{ error }}
</span>
2020-05-22 14:22:16 +03:00
</FormularioInput>
</FormularioGrouping>
</FormularioForm>
`
})
expect(wrapper.findAll('span').length).toBe(1)
})
})