|
| 1 | +import { generatePropsSingleField, mountFormGenerator } from '@test/_resources/utils.js' |
| 2 | +import { expect, it, describe, beforeAll } from 'vitest' |
| 3 | +import { config, mount } from '@vue/test-utils' |
| 4 | + |
| 5 | +import FieldObject from '@/fields/core/FieldObject.vue' |
| 6 | +import FieldNumber from '@/fields/core/FieldNumber.vue' |
| 7 | +import FieldText from '@/fields/core/FieldText.vue' |
| 8 | +import FormGenerator from '@/FormGenerator.vue' |
| 9 | + |
| 10 | +beforeAll(() => { |
| 11 | + config.global.components = { FieldObject, FieldNumber, FieldText } |
| 12 | +}) |
| 13 | + |
| 14 | +const shouldBeOverEighteen = (value) => value && value >= 18 |
| 15 | + |
| 16 | +const form = { |
| 17 | + model: { |
| 18 | + person: { |
| 19 | + name: '', |
| 20 | + age: null |
| 21 | + } |
| 22 | + }, |
| 23 | + schema: { |
| 24 | + fields: [ |
| 25 | + { |
| 26 | + type: 'object', |
| 27 | + model: 'person', |
| 28 | + schema: { |
| 29 | + fields: [ |
| 30 | + { |
| 31 | + type: 'text', |
| 32 | + name: 'name', |
| 33 | + model: 'name', |
| 34 | + label: 'Full name' |
| 35 | + }, |
| 36 | + { |
| 37 | + type: 'number', |
| 38 | + name: 'age', |
| 39 | + model: 'age', |
| 40 | + label: 'Age', |
| 41 | + validator: [ shouldBeOverEighteen ] |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + } |
| 46 | + ] |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const props = generatePropsSingleField(form) |
| 51 | + |
| 52 | +describe('FieldObject', () => { |
| 53 | + |
| 54 | + // For rendering, we'll only need to test if the components are actually present within the Field or FormGenerator. |
| 55 | + // This is because each Field will have their own individual tests for checking if it renders correctly. |
| 56 | + it('Should render properly', async () => { |
| 57 | + const wrapper = mount(FieldObject, { props }) |
| 58 | + // Since the FieldObject basically renders a form inside the form, this component should be there |
| 59 | + expect(wrapper.findComponent(FormGenerator)).toBeTruthy() |
| 60 | + expect(wrapper.findComponent(FieldNumber)).toBeTruthy() |
| 61 | + expect(wrapper.findComponent(FieldText)).toBeTruthy() |
| 62 | + }) |
| 63 | + |
| 64 | + it('Should render properly inside form generator', async () => { |
| 65 | + const wrapper = mountFormGenerator(form.schema, form.model) |
| 66 | + // Since the FieldObject basically renders a form inside the form, this component should be there |
| 67 | + expect(wrapper.findComponent(FormGenerator)).toBeTruthy() |
| 68 | + expect(wrapper.findComponent(FieldNumber)).toBeTruthy() |
| 69 | + expect(wrapper.findComponent(FieldText)).toBeTruthy() |
| 70 | + }) |
| 71 | + |
| 72 | + it('Should properly update model value', async () => { |
| 73 | + const wrapper = mountFormGenerator(form.schema, form.model) |
| 74 | + expect(typeof wrapper.vm.model.person).toBe('object') |
| 75 | + |
| 76 | + const fieldWrapper = wrapper.findComponent(FieldObject) |
| 77 | + fieldWrapper.find('input[type=number]').setValue(21) |
| 78 | + // Both values should match. |
| 79 | + expect(fieldWrapper.vm.model.person.age).toBe(21) |
| 80 | + expect(wrapper.vm.model.person.age).toBe(21) |
| 81 | + |
| 82 | + fieldWrapper.find('input[type=text]').setValue('Test subject') |
| 83 | + // Both values should match. |
| 84 | + expect(fieldWrapper.vm.model.person.name).toBe('Test subject') |
| 85 | + expect(wrapper.vm.model.person.name).toBe('Test subject') |
| 86 | + }) |
| 87 | + |
| 88 | + it('Should properly pass errors to parent form', async () => { |
| 89 | + const wrapper = mountFormGenerator(form.schema, form.model) |
| 90 | + const fieldWrapper = wrapper.findComponent(FieldObject) |
| 91 | + |
| 92 | + const ageInput = fieldWrapper.find('input[type=number]') |
| 93 | + ageInput.setValue(15) |
| 94 | + await ageInput.trigger('blur') |
| 95 | + expect(fieldWrapper.emitted()).toHaveProperty('validated') |
| 96 | + // Default models shouldn't show up in the main form, as the model is an object. |
| 97 | + expect(wrapper.vm.formErrors).not.toHaveProperty('age') |
| 98 | + expect(wrapper.vm.formErrors).not.toHaveProperty('person') |
| 99 | + // The only correct way |
| 100 | + expect(wrapper.vm.formErrors).toHaveProperty('person.age') |
| 101 | + expect(wrapper.vm.formErrors['person.age']).toHaveLength(1) |
| 102 | + expect(wrapper.vm.formErrors['person.age'][0]).toBe('Field is invalid') |
| 103 | + }) |
| 104 | + |
| 105 | + it('Should remove old errors', async () => { |
| 106 | + const wrapper = mountFormGenerator(form.schema, form.model) |
| 107 | + const fieldWrapper = wrapper.findComponent(FieldObject) |
| 108 | + |
| 109 | + const ageInput = fieldWrapper.find('input[type=number]') |
| 110 | + await ageInput.setValue(15) |
| 111 | + await ageInput.trigger('blur') |
| 112 | + expect(wrapper.vm.formErrors).toHaveProperty('person.age') |
| 113 | + |
| 114 | + await ageInput.setValue(18) |
| 115 | + await ageInput.trigger('blur') |
| 116 | + expect(wrapper.vm.formErrors).not.toHaveProperty('person.age') |
| 117 | + }) |
| 118 | + |
| 119 | +}) |
0 commit comments