|
| 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 ViewUI from 'view-design'; |
| 6 | +import { |
| 7 | + ValidationProvider, |
| 8 | + ValidationObserver |
| 9 | +} from 'vee-validate/dist/vee-validate.full'; |
| 10 | +import { createFormBuilder } from 'src'; |
| 11 | +import { ViewFormAdaptor } from 'src/view-form-adaptor'; |
| 12 | + |
| 13 | +const TestComponent = { |
| 14 | + template: `<Form> |
| 15 | + <form-builder |
| 16 | + :form="form" |
| 17 | + :shares="formShares" |
| 18 | + :config="formConfig" |
| 19 | + :metadata="metadata" |
| 20 | + ></form-builder> |
| 21 | + </Form>`, |
| 22 | + |
| 23 | + components: { |
| 24 | + FormBuilder: createFormBuilder({ |
| 25 | + components: { |
| 26 | + ViewFormAdaptor |
| 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: 'ViewFormAdaptor', |
| 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: 'ViewFormAdaptor', |
| 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: 'CheckboxGroup' |
| 82 | + }, |
| 83 | + props: { |
| 84 | + min: 1 |
| 85 | + } |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + ] |
| 90 | + }; |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +let wrapper = null; |
| 95 | +let vm = null; |
| 96 | +beforeEach(() => { |
| 97 | + wrapper = mount(TestComponent); |
| 98 | + vm = wrapper.vm; |
| 99 | +}); |
| 100 | + |
| 101 | +afterEach(() => { |
| 102 | + wrapper.destroy(); |
| 103 | +}); |
| 104 | + |
| 105 | +beforeAll(() => { |
| 106 | + Vue.use(VueCompositionAPI); |
| 107 | + Vue.use(ViewUI); |
| 108 | + |
| 109 | + Vue.component('ValidationProvider', ValidationProvider); |
| 110 | + Vue.component('ValidationObserver', ValidationObserver); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('view ui form adaptor', () => { |
| 114 | + test('render adaptors', () => { |
| 115 | + const adaptorWrappers = wrapper.findAllComponents(ViewFormAdaptor); |
| 116 | + |
| 117 | + expect(adaptorWrappers.length).toBe(2); |
| 118 | + |
| 119 | + expect(wrapper.findAll('.ivu-input').length).toBe(1); |
| 120 | + expect(wrapper.findAll('.ivu-checkbox').length).toBe(3); |
| 121 | + }); |
| 122 | + |
| 123 | + test('update value', async () => { |
| 124 | + const inputWrapper = wrapper.findAllComponents(ViewFormAdaptor).at(0); |
| 125 | + |
| 126 | + inputWrapper.vm.updateLocalValue('foo'); |
| 127 | + |
| 128 | + await Vue.nextTick(); |
| 129 | + |
| 130 | + expect(vm.formValues.comment).toBe('foo'); |
| 131 | + |
| 132 | + vm.formValues.comment = 'bar'; |
| 133 | + |
| 134 | + await Vue.nextTick(); |
| 135 | + |
| 136 | + expect(inputWrapper.vm.localValue).toBe('bar'); |
| 137 | + }); |
| 138 | +}); |
0 commit comments