From 75ddb8760a43403f2da5f37c54f00fe017051c64 Mon Sep 17 00:00:00 2001 From: zombiej Date: Sat, 29 Feb 2020 13:41:59 +0800 Subject: [PATCH 1/3] fix onFinish --- src/useForm.ts | 2 +- tests/validate.test.js | 87 ++++++++++++++++++++++++++++++++---------- 2 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/useForm.ts b/src/useForm.ts index 79cbe893..21a00a27 100644 --- a/src/useForm.ts +++ b/src/useForm.ts @@ -517,7 +517,7 @@ export class FormStore { errors: string[]; }>[] = []; - this.getFieldEntities().forEach((field: FieldEntity) => { + this.getFieldEntities(true).forEach((field: FieldEntity) => { // Add field if not provide `nameList` if (!provideNameList) { namePathList.push(field.getNamePath()); diff --git a/tests/validate.test.js b/tests/validate.test.js index 6e65fac4..3106b460 100644 --- a/tests/validate.test.js +++ b/tests/validate.test.js @@ -272,35 +272,80 @@ describe('Form.Validate', () => { expect(form.getFieldError('title')).toEqual(['Title should be 3+ characters']); }); - it('validate only accept exist fields', async () => { - let form; - const onFinish = jest.fn(); + describe('validate only accept exist fields', () => { + it('skip init value', async () => { + let form; + const onFinish = jest.fn(); - const wrapper = mount( -
+ const wrapper = mount( +
+
{ + form = instance; + }} + initialValues={{ user: 'light', pass: 'bamboo' }} + > + + + + +
+
, + ); + + // Validate callback + expect(await form.validateFields(['user'])).toEqual({ user: 'light' }); + expect(await form.validateFields()).toEqual({ user: 'light' }); + + // Submit callback + wrapper.find('button').simulate('submit'); + await timeout(); + expect(onFinish).toHaveBeenCalledWith({ user: 'light' }); + }); + + it('remove from fields', async () => { + const onFinish = jest.fn(); + const wrapper = mount(
{ - form = instance; + initialValues={{ + switch: true, + ignore: 'test', }} - initialValues={{ user: 'light', pass: 'bamboo' }} > - - + + + + {(_, __, { getFieldValue }) => + getFieldValue('switch') && ( + + + + ) + } + - -
, - ); - - // Validate callback - expect(await form.validateFields(['user'])).toEqual({ user: 'light' }); - expect(await form.validateFields()).toEqual({ user: 'light' }); + , + ); - // Submit callback - wrapper.find('button').simulate('submit'); - await timeout(); - expect(onFinish).toHaveBeenCalledWith({ user: 'light' }); + // Submit callback + wrapper.find('button').simulate('submit'); + await timeout(); + expect(onFinish).toHaveBeenCalledWith({ switch: true, ignore: 'test' }); + onFinish.mockReset(); + + // Hide one + wrapper.find('input.switch').simulate('change', { + target: { + checked: false, + }, + }); + wrapper.find('button').simulate('submit'); + await timeout(); + expect(onFinish).toHaveBeenCalledWith({ switch: false }); + }); }); it('should error in console if user script failed', async () => { From 9682c7ac95d40a99789ea614b826166283746a07 Mon Sep 17 00:00:00 2001 From: zombiej Date: Sat, 29 Feb 2020 13:49:48 +0800 Subject: [PATCH 2/3] add compile ci --- .circleci/config.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c10f9d15..7e3649d5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -29,9 +29,24 @@ jobs: - node_modules key: v1-dependencies-{{ checksum "package.json" }} - run: npm test -- --coverage && bash <(curl -s https://codecov.io/bash) + compile: + docker: + - image: circleci/node:latest + steps: + - checkout + - restore_cache: + keys: + - v1-dependencies-{{ checksum "package.json" }} + - run: npm install + - save_cache: + paths: + - node_modules + key: v1-dependencies-{{ checksum "package.json" }} + - run: npm run compile workflows: version: 2 build_and_test: jobs: - lint - - test \ No newline at end of file + - test + - compile \ No newline at end of file From e449fdc631313c277d115557d460d279f436d1cc Mon Sep 17 00:00:00 2001 From: zombiej Date: Sat, 29 Feb 2020 14:10:51 +0800 Subject: [PATCH 3/3] fix ts --- src/Form.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form.tsx b/src/Form.tsx index 3cec3c8c..93c702ec 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -31,7 +31,7 @@ export interface FormProps extends BaseFormProps { onFinishFailed?: Callbacks['onFinishFailed']; } -const Form: React.FunctionComponent = ( +const Form: React.ForwardRefRenderFunction = ( { name, initialValues,