From 42031c5fe7fd3e08569702a341b5c64713b789ce Mon Sep 17 00:00:00 2001 From: Liberty Date: Sun, 10 Sep 2023 11:13:39 +0800 Subject: [PATCH] test(subform): no child: Required --- .../components/Layout/SubformLayout.jsx | 18 ++++++++++++- packages/formEditor/componentsConfig.js | 4 ++- packages/hooks/use-props/index.js | 11 ++++++-- .../erGeneratorData.test.js.snap | 2 ++ test/formTypes/subform.test.js | 25 ++++++++++++++++--- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/packages/formEditor/components/Layout/SubformLayout.jsx b/packages/formEditor/components/Layout/SubformLayout.jsx index 54645b9..f15194e 100644 --- a/packages/formEditor/components/Layout/SubformLayout.jsx +++ b/packages/formEditor/components/Layout/SubformLayout.jsx @@ -58,9 +58,25 @@ export default defineComponent({ if (ExtraParams.inSubformDefaultValueComponent) { ExtraParams.handle.handleAdd = handleAdd } + const params = { + hasCopy: true, + hasDel: true, + hasDrag: true, + hasWidthScale: true, + data: props.data, + parent: props.parent + } + if (process.env.NODE_ENV === 'test') { + params['data-field-id'] = `${props.data.id}` + } return () => { return ( - +
{ } const newValue = options.isShowTrim ? value.trim() : value if (node.type === 'subform') { - if (result.required && findAllFields(node).some(e => utils.isEmpty(e.options.isShowTrim ? e.options.defaultValue.trim() : e.options.defaultValue))) { - reject(t('er.validateMsg.required')) + const allFields = findAllFields(node) + if (result.required) { + if (allFields.length) { + if (allFields.some(e => utils.isEmpty(e.options.isShowTrim ? e.options.defaultValue.trim() : e.options.defaultValue))) { + reject(t('er.validateMsg.required')) + } + } else { + reject(t('er.validateMsg.required')) + } } else { resolve() } diff --git a/test/__snapshots__/erGeneratorData.test.js.snap b/test/__snapshots__/erGeneratorData.test.js.snap index f31aabb..8e467b0 100644 --- a/test/__snapshots__/erGeneratorData.test.js.snap +++ b/test/__snapshots__/erGeneratorData.test.js.snap @@ -780,7 +780,9 @@ exports[`Generate field data: > Subform 1`] = ` ], "options": { "defaultValue": [], + "disabled": false, "isShowLabel": true, + "required": false, }, "style": { "width": { diff --git a/test/formTypes/subform.test.js b/test/formTypes/subform.test.js index 0d93c87..61ae402 100644 --- a/test/formTypes/subform.test.js +++ b/test/formTypes/subform.test.js @@ -56,11 +56,29 @@ describe('Field: subform', () => { await previewWrapper.findComponent({ ref: 'EReditorRef' }).vm.setData(data) expect(previewWrapper.find(utils.getTestId('SubformLayout:addButton')).exists()).toBe(false) expect(previewWrapper.findAll(utils.getTestId('SubformLayout:item'))).toHaveLength(0) + previewWrapper.find('[data-test="er-complete-button"] button').trigger('click') + await flushPromises() + expect([list.columns[0]]).toStrictEqual(previewWrapper.findAll('[data-field-id]').map(element => element.element.dataset.fieldId)) + expect(previewWrapper.find(`[data-field-id="${list.columns[0]}"] .el-form-item`).classes()).not.toContain('is-required') store.fields.push(subForm.columns[0]) store.sector = subForm.columns[0] await flushPromises() expect(configWrapper.find(utils.getTestId('configPanel:defaultValue:button')).exists()).toBe(false) }) + test('No child: Required', async () => { + const subForm = erGeneratorData(_.cloneDeep(erComponentsConfig.fieldsConfig[2].list[5]), true, 'en') + subForm.columns[0].options.required = true + const list = _.cloneDeep(subForm) + list.columns[0] = subForm.columns[0].id + const data = wrapLayoutDataByLayoutType([list], [subForm.columns[0]], layoutType) + await previewWrapper.findComponent({ ref: 'EReditorRef' }).vm.setData(data) + expect(previewWrapper.find(utils.getTestId('SubformLayout:addButton')).exists()).toBe(false) + expect(previewWrapper.findAll(utils.getTestId('SubformLayout:item'))).toHaveLength(0) + previewWrapper.find('[data-test="er-complete-button"] button').trigger('click') + await flushPromises() + expect([list.columns[0]]).toStrictEqual(previewWrapper.findAll('[data-field-id]').map(element => element.element.dataset.fieldId)) + expect(previewWrapper.find(`[data-field-id="${list.columns[0]}"] .el-form-item`).classes()).toContain('is-required') + }) test('Only one child', async () => { const newField = _.cloneDeep(field) newField.columns[0] = newField.columns[0].id @@ -119,7 +137,7 @@ describe('Field: subform', () => { await flushPromises() expect(configWrapper.find(utils.getTestId('configPanel:defaultValue:button')).exists()).toBe(true) await configWrapper.find(utils.getTestId('configPanel:defaultValue:button')).trigger('click') - await flushPromises() + await new Promise(resolve => setTimeout(resolve, 1000)) const setDefaultEl = new DOMWrapper(document.querySelector('.Everright-formEditor-ConfigSubformDefaultValueComponent')) expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item'))).toHaveLength(2) expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item')).map(e => e.find('input').element.value)).toEqual(values) @@ -127,7 +145,7 @@ describe('Field: subform', () => { await flushPromises() expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item'))[2].find('input').element.value).toEqual('') }) - test.only('Only one child: has 2 default contents && field has default', async () => { + test('Only one child: has 2 default contents && field has default', async () => { const values = ['1', '2'] const addValue = 'everright-formeditor' const newField = _.cloneDeep(field) @@ -163,7 +181,7 @@ describe('Field: subform', () => { await flushPromises() expect(configWrapper.find(utils.getTestId('configPanel:defaultValue:button')).exists()).toBe(true) await configWrapper.find(utils.getTestId('configPanel:defaultValue:button')).trigger('click') - await flushPromises() + await new Promise(resolve => setTimeout(resolve, 1000)) const setDefaultEl = new DOMWrapper(document.querySelector('.Everright-formEditor-ConfigSubformDefaultValueComponent')) expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item'))).toHaveLength(2) expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item')).map(e => e.find('input').element.value)).toEqual(values) @@ -171,4 +189,5 @@ describe('Field: subform', () => { await flushPromises() expect(setDefaultEl.findAll(utils.getTestId('SubformLayout:item'))[2].find('input').element.value).toEqual(addValue) }) + // test.only('Only one child: has 2 default contents && field has default', async () => {}) })