2020-05-22 14:22:16 +03:00
|
|
|
import Vue from 'vue'
|
2021-05-22 16:50:53 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import flushPromises from 'flush-promises'
|
2021-05-22 16:50:53 +03:00
|
|
|
|
2020-10-11 11:41:32 +03:00
|
|
|
import Formulario from '@/index.ts'
|
2021-05-22 16:50:53 +03:00
|
|
|
import FormularioFieldGroup from '@/FormularioFieldGroup.vue'
|
2020-05-22 14:22:16 +03:00
|
|
|
import FormularioForm from '@/FormularioForm.vue'
|
|
|
|
|
|
|
|
Vue.use(Formulario)
|
|
|
|
|
2021-05-22 16:50:53 +03:00
|
|
|
describe('FormularioFieldGroup', () => {
|
2021-05-24 23:12:40 +03:00
|
|
|
test('grouped fields to be set', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
slots: {
|
|
|
|
default: `
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioFieldGroup name="group">
|
|
|
|
<FormularioField name="text" v-slot="{ context }">
|
2020-10-11 00:52:18 +03:00
|
|
|
<input type="text" v-model="context.model">
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
|
|
|
</FormularioFieldGroup>
|
2020-05-22 14:22:16 +03:00
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
2020-10-21 15:58:25 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
expect(wrapper.findAll('input[type="text"]').length).toBe(1)
|
2020-10-21 15:58:25 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
wrapper.find('input[type="text"]').setValue('test')
|
2020-10-21 15:58:25 +03:00
|
|
|
wrapper.find('form').trigger('submit')
|
|
|
|
|
|
|
|
await flushPromises()
|
|
|
|
|
|
|
|
const emitted = wrapper.emitted()
|
2020-05-22 14:22:16 +03:00
|
|
|
|
2020-10-21 15:58:25 +03:00
|
|
|
expect(emitted['submit']).toBeTruthy()
|
2021-05-22 16:50:53 +03:00
|
|
|
expect(emitted['submit']).toEqual([[{ group: { text: 'test' } }]])
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
test('grouped fields to be got', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
2020-10-14 08:43:17 +03:00
|
|
|
propsData: {
|
2021-05-22 23:24:05 +03:00
|
|
|
state: {
|
2020-10-14 08:43:17 +03:00
|
|
|
group: { text: 'Group text' },
|
|
|
|
text: 'Text',
|
2020-10-22 16:37:57 +03:00
|
|
|
},
|
2020-10-14 08:43:17 +03:00
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
slots: {
|
|
|
|
default: `
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioFieldGroup name="group">
|
|
|
|
<FormularioField name="text" v-slot="{ context }">
|
2020-10-14 08:43:17 +03:00
|
|
|
<input type="text" v-model="context.model">
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
|
|
|
</FormularioFieldGroup>
|
2020-05-22 14:22:16 +03:00
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
2020-10-25 14:28:14 +03:00
|
|
|
expect(wrapper.find('input[type="text"]').element['value']).toBe('Group text')
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
test('data reactive with grouped fields', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount({
|
2020-10-14 08:43:17 +03:00
|
|
|
data: () => ({ values: {} }),
|
2020-05-22 14:22:16 +03:00
|
|
|
template: `
|
2021-05-24 23:12:40 +03:00
|
|
|
<FormularioForm v-model="values">
|
2021-05-22 16:50:53 +03:00
|
|
|
<FormularioFieldGroup name="group">
|
|
|
|
<FormularioField name="text" v-slot="{ context }">
|
2020-10-14 08:43:17 +03:00
|
|
|
<input type="text" v-model="context.model">
|
|
|
|
<span>{{ values.group.text }}</span>
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
|
|
|
</FormularioFieldGroup>
|
2020-05-22 14:22:16 +03:00
|
|
|
</FormularioForm>
|
|
|
|
`
|
|
|
|
})
|
2021-05-24 23:12:40 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
expect(wrapper.find('span').text()).toBe('')
|
2021-05-24 23:12:40 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
wrapper.find('input[type="text"]').setValue('test')
|
2021-05-24 23:12:40 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
await flushPromises()
|
2021-05-24 23:12:40 +03:00
|
|
|
|
2020-05-22 14:22:16 +03:00
|
|
|
expect(wrapper.find('span').text()).toBe('test')
|
|
|
|
})
|
|
|
|
|
2021-05-24 23:12:40 +03:00
|
|
|
test('errors are set for grouped fields', async () => {
|
2020-10-22 16:37:57 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
2021-05-25 13:59:01 +03:00
|
|
|
propsData: { fieldsErrors: { 'address.street': ['Test error'] } },
|
2020-10-22 16:37:57 +03:00
|
|
|
slots: {
|
|
|
|
default: `
|
2021-05-24 23:12:40 +03:00
|
|
|
<FormularioFieldGroup name="address">
|
|
|
|
<FormularioField ref="input" name="street" v-slot="{ context }">
|
2020-10-26 00:51:19 +03:00
|
|
|
<span v-for="error in context.errors">{{ error }}</span>
|
2021-05-22 16:50:53 +03:00
|
|
|
</FormularioField>
|
2021-05-22 22:34:17 +03:00
|
|
|
</FormularioFieldGroup>
|
2020-10-22 16:37:57 +03:00
|
|
|
`,
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
expect(wrapper.findAll('span').length).toBe(1)
|
|
|
|
})
|
|
|
|
})
|