Skip to content

Commit

Permalink
test(subform): no child: Required
Browse files Browse the repository at this point in the history
  • Loading branch information
Liberty-liu committed Sep 10, 2023
1 parent c3f6fa1 commit 42031c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
18 changes: 17 additions & 1 deletion packages/formEditor/components/Layout/SubformLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Selection {...useAttrs()} hasCopy hasDel hasDrag hasWidthScale data={props.data} parent={props.parent}>
<Selection
{...useAttrs()}
{
...params
}
>
<div class={ns.b()}>
<el-form-item
{...typeProps.value}
Expand Down
4 changes: 3 additions & 1 deletion packages/formEditor/componentsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,9 @@ export const fieldsConfig = [
],
options: {
defaultValue: [],
isShowLabel: true
isShowLabel: true,
required: false,
disabled: false
}
}
]
Expand Down
11 changes: 9 additions & 2 deletions packages/hooks/use-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,15 @@ const addValidate = (result, node, isPc, t) => {
}
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()
}
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/erGeneratorData.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,9 @@ exports[`Generate field data: > Subform 1`] = `
],
"options": {
"defaultValue": [],
"disabled": false,
"isShowLabel": true,
"required": false,
},
"style": {
"width": {
Expand Down
25 changes: 22 additions & 3 deletions test/formTypes/subform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,15 +137,15 @@ 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)
await setDefaultEl.find(utils.getTestId('configPanel:defaultValue:addButton')).trigger('click')
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)
Expand Down Expand Up @@ -163,12 +181,13 @@ 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)
await setDefaultEl.find(utils.getTestId('configPanel:defaultValue:addButton')).trigger('click')
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 () => {})
})

0 comments on commit 42031c5

Please sign in to comment.