2020-05-22 14:22:16 +03:00
|
|
|
import Vue from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import flushPromises from 'flush-promises'
|
2020-10-11 11:41:32 +03:00
|
|
|
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', () => {
|
2020-10-14 08:43:17 +03:00
|
|
|
it('Grouped fields to be set', async () => {
|
2020-05-22 14:22:16 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
slots: {
|
|
|
|
default: `
|
2020-10-14 08:43:17 +03:00
|
|
|
<FormularioGrouping name="group">
|
2020-10-11 00:52:18 +03:00
|
|
|
<FormularioInput name="text" v-slot="{ context }">
|
|
|
|
<input type="text" v-model="context.model">
|
2020-05-22 14:22:16 +03:00
|
|
|
</FormularioInput>
|
|
|
|
</FormularioGrouping>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
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()
|
|
|
|
expect(emitted['submit'].length).toBe(1)
|
2020-10-22 16:37:57 +03:00
|
|
|
expect(emitted['submit'][0]).toEqual([{ group: { text: 'test' } }])
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2020-10-14 08:43:17 +03:00
|
|
|
it('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: {
|
|
|
|
formularioValue: {
|
|
|
|
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: `
|
2020-10-14 08:43:17 +03:00
|
|
|
<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>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
2020-10-14 08:43:17 +03:00
|
|
|
expect(wrapper.find('input[type="text"]').element.value).toBe('Group text')
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
|
2020-10-14 08:43:17 +03:00
|
|
|
it('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: `
|
2020-10-14 08:43:17 +03:00
|
|
|
<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')
|
|
|
|
})
|
|
|
|
|
2020-10-14 08:43:17 +03:00
|
|
|
it('Errors are set for grouped fields', async () => {
|
2020-10-22 16:37:57 +03:00
|
|
|
const wrapper = mount(FormularioForm, {
|
|
|
|
propsData: {
|
|
|
|
formularioValue: {},
|
|
|
|
errors: { 'group.text': 'Test error' },
|
|
|
|
},
|
|
|
|
slots: {
|
|
|
|
default: `
|
2020-10-14 08:43:17 +03:00
|
|
|
<FormularioGrouping name="group">
|
2020-10-22 16:37:57 +03:00
|
|
|
<FormularioInput ref="input" name="text" v-slot="{ context }">
|
|
|
|
<span v-for="error in context.allErrors">{{ error }}</span>
|
2020-05-22 14:22:16 +03:00
|
|
|
</FormularioInput>
|
|
|
|
</FormularioGrouping>
|
2020-10-22 16:37:57 +03:00
|
|
|
`,
|
|
|
|
},
|
2020-05-22 14:22:16 +03:00
|
|
|
})
|
|
|
|
expect(wrapper.findAll('span').length).toBe(1)
|
|
|
|
})
|
|
|
|
})
|