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

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-05-22 14:22:16 +03:00
import Vue from 'vue'
2020-05-22 14:22:16 +03:00
import { mount } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import Formulario from '@/index.ts'
import FormularioFieldGroup from '@/FormularioFieldGroup.vue'
2020-05-22 14:22:16 +03:00
import FormularioForm from '@/FormularioForm.vue'
Vue.use(Formulario)
describe('FormularioFieldGroup', () => {
test('grouped fields to be set', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioForm, {
slots: {
default: `
<FormularioFieldGroup name="group">
<FormularioField name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
</FormularioField>
</FormularioFieldGroup>
2021-05-30 11:56:23 +03:00
`,
},
2020-05-22 14:22:16 +03:00
})
2021-05-30 11:56:23 +03:00
wrapper.find('input').setValue('test')
wrapper.find('form').trigger('submit')
await flushPromises()
2021-05-30 11:56:23 +03:00
expect(wrapper.emitted('submit')).toEqual([
[{ group: { text: 'test' } }],
])
2020-05-22 14:22:16 +03:00
})
test('grouped fields to be got', async () => {
2020-05-22 14:22:16 +03:00
const wrapper = mount(FormularioForm, {
propsData: {
state: {
group: { text: 'Group text' },
text: 'Text',
},
},
2020-05-22 14:22:16 +03:00
slots: {
default: `
<FormularioFieldGroup name="group">
<FormularioField name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
</FormularioField>
</FormularioFieldGroup>
2021-05-30 11:56:23 +03:00
`,
},
2020-05-22 14:22:16 +03:00
})
2021-05-30 11:56:23 +03:00
expect(wrapper.find('input').element['value']).toBe('Group text')
2020-05-22 14:22:16 +03:00
})
test('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 v-model="values">
<FormularioFieldGroup name="group">
<FormularioField name="text" v-slot="{ context }">
<input type="text" v-model="context.model">
<span>{{ values.group.text }}</span>
</FormularioField>
</FormularioFieldGroup>
2020-05-22 14:22:16 +03:00
</FormularioForm>
2021-05-30 11:56:23 +03:00
`,
2020-05-22 14:22:16 +03:00
})
2020-05-22 14:22:16 +03:00
expect(wrapper.find('span').text()).toBe('')
2021-05-30 11:56:23 +03:00
wrapper.find('input').setValue('test')
2020-05-22 14:22:16 +03:00
await flushPromises()
2020-05-22 14:22:16 +03:00
expect(wrapper.find('span').text()).toBe('test')
})
test('errors are set for grouped fields', async () => {
const wrapper = mount(FormularioForm, {
propsData: { fieldsErrors: { 'address.street': ['Test error'] } },
slots: {
default: `
<FormularioFieldGroup name="address">
<FormularioField ref="input" name="street" v-slot="{ context }">
<span v-for="error in context.errors">{{ error }}</span>
</FormularioField>
</FormularioFieldGroup>
`,
},
2020-05-22 14:22:16 +03:00
})
2021-05-30 11:56:23 +03:00
2020-05-22 14:22:16 +03:00
expect(wrapper.findAll('span').length).toBe(1)
})
})