From 0f661bc0bc19f1218cb3718862d22d01e3cadb36 Mon Sep 17 00:00:00 2001 From: Liberty Date: Mon, 11 Sep 2023 18:27:46 +0800 Subject: [PATCH] test(subform): validator --- packages/hooks/use-logic/index.js | 3 +- test/logic/validator/Subform.test.js | 69 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/logic/validator/Subform.test.js diff --git a/packages/hooks/use-logic/index.js b/packages/hooks/use-logic/index.js index aecff19..2efd72b 100644 --- a/packages/hooks/use-logic/index.js +++ b/packages/hooks/use-logic/index.js @@ -318,7 +318,8 @@ const listenEvent = (state) => { subform.options.defaultValue = utils.getSubFormValues(subform) }, { immediate: true, - deep: true + deep: true, + flush: 'sync' }) }) watch(() => targetFields.map(e => e.options.defaultValue), (values) => { diff --git a/test/logic/validator/Subform.test.js b/test/logic/validator/Subform.test.js new file mode 100644 index 0000000..33ea604 --- /dev/null +++ b/test/logic/validator/Subform.test.js @@ -0,0 +1,69 @@ +import { describe, assert, expect, test, beforeAll } from 'vitest' +import { validator } from '@ER/hooks/use-logic' +import _ from 'lodash-es' +import erGeneratorData from '@ER/formEditor/generatorData.js' +import * as erComponentsConfig from '@ER/formEditor/componentsConfig.js' +describe('Subform', () => { + let field = {} + beforeAll(() => { + field = erGeneratorData(_.cloneDeep(erComponentsConfig.fieldsConfig[2].list[5]), true, 'en') + }) + test('empty', () => { + expect( + validator({ + operator: 'empty' + }, + '', + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + undefined, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + null, + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + [], + field)).toBeTruthy() + expect( + validator({ + operator: 'empty' + }, + [{}], + field)).toBeFalsy() + }) + test('not_empty', () => { + expect( + validator({ + operator: 'not_empty' + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + undefined, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'not_empty' + }, + [{}], + field)).toBeTruthy() + }) +})