|
| 1 | +import Vue from 'vue'; |
| 2 | +import VueCompositionAPI from '@vue/composition-api'; |
| 3 | +import { mount } from '@vue/test-utils'; |
| 4 | +import { useForm, useFormElement } from '@fext/vue-use'; |
| 5 | +import Antd from 'ant-design-vue'; |
| 6 | +import { |
| 7 | + ValidationProvider, |
| 8 | + ValidationObserver |
| 9 | +} from 'vee-validate/dist/vee-validate.full'; |
| 10 | +import { createFormBuilder } from 'src'; |
| 11 | +import { AntFormAdaptor } from 'src/ant-form-adaptor'; |
| 12 | + |
| 13 | +const TestComponent = { |
| 14 | + template: `<a-form> |
| 15 | + <form-builder |
| 16 | + :form="form" |
| 17 | + :shares="formShares" |
| 18 | + :config="formConfig" |
| 19 | + :metadata="metadata" |
| 20 | + ></form-builder> |
| 21 | + </a-form>`, |
| 22 | + |
| 23 | + components: { |
| 24 | + FormBuilder: createFormBuilder({ |
| 25 | + components: { |
| 26 | + AntFormAdaptor |
| 27 | + } |
| 28 | + }) |
| 29 | + }, |
| 30 | + |
| 31 | + setup() { |
| 32 | + const form = useForm(); |
| 33 | + const { formValues, updateFormValues } = form; |
| 34 | + |
| 35 | + return { |
| 36 | + form, |
| 37 | + formValues, |
| 38 | + updateFormValues |
| 39 | + }; |
| 40 | + }, |
| 41 | + |
| 42 | + data() { |
| 43 | + return { |
| 44 | + showResultModal: false, |
| 45 | + |
| 46 | + metadata: {}, |
| 47 | + |
| 48 | + formShares: { |
| 49 | + size: 'medium' |
| 50 | + }, |
| 51 | + |
| 52 | + formConfig: [ |
| 53 | + { |
| 54 | + component: 'div', |
| 55 | + fields: [ |
| 56 | + { |
| 57 | + name: 'comment', |
| 58 | + component: 'AntFormAdaptor', |
| 59 | + label: 'comment-label', |
| 60 | + tip: 'comment-tip', |
| 61 | + rules: { |
| 62 | + required: true, |
| 63 | + max: 50, |
| 64 | + min: 10 |
| 65 | + }, |
| 66 | + props: { |
| 67 | + placeholder: 'comment-placeholder' |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'type', |
| 72 | + component: 'AntFormAdaptor', |
| 73 | + label: 'type-label', |
| 74 | + tip: 'type-tip', |
| 75 | + items: [ |
| 76 | + { text: 'Type-1', value: '1' }, |
| 77 | + { text: 'Type-2', value: '2' }, |
| 78 | + { text: 'Type-3', value: '3' } |
| 79 | + ], |
| 80 | + extend: { |
| 81 | + component: 'a-checkbox-group' |
| 82 | + } |
| 83 | + } |
| 84 | + ] |
| 85 | + } |
| 86 | + ] |
| 87 | + }; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +let wrapper = null; |
| 92 | +let vm = null; |
| 93 | +beforeEach(() => { |
| 94 | + wrapper = mount(TestComponent); |
| 95 | + vm = wrapper.vm; |
| 96 | +}); |
| 97 | + |
| 98 | +afterEach(() => { |
| 99 | + wrapper.destroy(); |
| 100 | +}); |
| 101 | + |
| 102 | +beforeAll(() => { |
| 103 | + Vue.use(VueCompositionAPI); |
| 104 | + Vue.use(Antd); |
| 105 | + |
| 106 | + Vue.component('ValidationProvider', ValidationProvider); |
| 107 | + Vue.component('ValidationObserver', ValidationObserver); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('ant design vue form adaptor', () => { |
| 111 | + test('render adaptors', () => { |
| 112 | + const adaptorWrappers = wrapper.findAllComponents(AntFormAdaptor); |
| 113 | + |
| 114 | + expect(adaptorWrappers.length).toBe(2); |
| 115 | + |
| 116 | + expect(wrapper.findAll('.ant-input').length).toBe(1); |
| 117 | + expect(wrapper.findAll('.ant-checkbox').length).toBe(3); |
| 118 | + }); |
| 119 | + |
| 120 | + test('update value', async () => { |
| 121 | + const inputWrapper = wrapper.findAllComponents(AntFormAdaptor).at(0); |
| 122 | + |
| 123 | + inputWrapper.vm.updateAntLocalValue({ |
| 124 | + target: { |
| 125 | + value: 'ant' |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + await Vue.nextTick(); |
| 130 | + |
| 131 | + expect(vm.formValues.comment).toBe('ant'); |
| 132 | + |
| 133 | + inputWrapper.vm.updateAntLocalValue('foo'); |
| 134 | + |
| 135 | + await Vue.nextTick(); |
| 136 | + |
| 137 | + expect(vm.formValues.comment).toBe('foo'); |
| 138 | + |
| 139 | + vm.formValues.comment = 'bar'; |
| 140 | + |
| 141 | + await Vue.nextTick(); |
| 142 | + |
| 143 | + expect(inputWrapper.vm.localValue).toBe('bar'); |
| 144 | + }); |
| 145 | +}); |
0 commit comments