From 41aa26e842be3d51bcfe048fc60a23b7bd7cd289 Mon Sep 17 00:00:00 2001 From: monkindey Date: Thu, 11 Jul 2019 22:25:40 +0800 Subject: [PATCH] refactor: use isEqual instead of isEmpty --- packages/core/src/field.ts | 13 +++-- packages/react/src/__tests__/context.spec.js | 4 +- packages/react/src/__tests__/dynamic.spec.js | 24 ++++----- packages/react/src/__tests__/editable.spec.js | 42 ++++++++-------- packages/react/src/__tests__/effects.spec.js | 12 ++--- packages/react/src/__tests__/mutators.spec.js | 4 +- packages/react/src/__tests__/validate.spec.js | 50 +++++++++---------- .../src/__tests__/validate_relations.spec.js | 4 +- packages/react/src/__tests__/value.spec.js | 14 +++--- .../react/src/__tests__/virtualbox.spec.js | 2 +- .../react/src/__tests__/x-component.spec.js | 6 +-- 11 files changed, 88 insertions(+), 87 deletions(-) diff --git a/packages/core/src/field.ts b/packages/core/src/field.ts index c5b5dcd3098..f57309adef7 100644 --- a/packages/core/src/field.ts +++ b/packages/core/src/field.ts @@ -100,8 +100,9 @@ export class Field implements IField { public initialize(options: IFieldOptions) { const rules = this.getRulesFromProps(options.props) - this.value = clone(options.value) - + this.value = !isEqual(this.value, options.value) + ? clone(options.value) + : this.value this.name = !isEmpty(options.name) ? options.name : this.name || '' this.namePath = resolveFieldPath(this.name) @@ -110,9 +111,11 @@ export class Field implements IField { this.required = hasRequired(this.rules) if (isEmpty(options.props)) { - this.initialValue = !isEmpty(options.initialValue) ? options.initialValue : this.initialValue + this.initialValue = !isEqual(this.initialValue, options.initialValue) + ? options.initialValue + : this.initialValue } else { - this.initialValue = !isEmpty(options.initialValue) + this.initialValue = !isEqual(this.initialValue, options.initialValue) ? options.initialValue : !isEmpty(this.initialValue) ? this.initialValue @@ -141,7 +144,7 @@ export class Field implements IField { public getInitialValueFromProps(props: any) { if (props) { - if (!isEmpty(props.default)) { + if (!isEqual(this.initialValue, props.default)) { return props.default } } diff --git a/packages/react/src/__tests__/context.spec.js b/packages/react/src/__tests__/context.spec.js index f311b598499..f6d0c595531 100644 --- a/packages/react/src/__tests__/context.spec.js +++ b/packages/react/src/__tests__/context.spec.js @@ -41,9 +41,9 @@ test('submit by form consumer', async () => { ) const { queryByText } = render() - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(queryByText('Submitting')).toBeVisible() await sleep(300) expect(queryByText('Submitting')).toBeNull() diff --git a/packages/react/src/__tests__/dynamic.spec.js b/packages/react/src/__tests__/dynamic.spec.js index 2c57ee7d446..f0cf011c481 100644 --- a/packages/react/src/__tests__/dynamic.spec.js +++ b/packages/react/src/__tests__/dynamic.spec.js @@ -189,7 +189,7 @@ test('dynaimc add field with initialValue', async () => { } const { queryAllByTestId, queryByText } = render() - await sleep(100) + await sleep(33) expect(queryAllByTestId('item').length).toBe(1) expect(queryAllByTestId('input').length).toBe(1) fireEvent.click(queryByText('Add Field')) @@ -279,7 +279,7 @@ test('dynaimc add field with initialValue in virtualbox', async () => { } const { queryAllByTestId, queryByText } = render() - await sleep(100) + await sleep(33) expect(queryAllByTestId('item').length).toBe(1) expect(queryAllByTestId('input').length).toBe(1) fireEvent.click(queryByText('Add Field')) @@ -311,7 +311,7 @@ test('dynaimc add field with initialValue in virtualbox', async () => { expect(queryAllByTestId('item').length).toBe(2) expect(queryAllByTestId('input').length).toBe(2) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(submitHandler).toHaveBeenCalledWith({ container: [{ aa: '321' }, undefined] }) @@ -428,7 +428,7 @@ test('dynamic async default value', async () => { ) } const { queryAllByTestId } = render() - await sleep(100) + await sleep(33) expect(queryAllByTestId('item').length).toBe(1) expect(queryAllByTestId('input').length).toBe(2) }) @@ -471,7 +471,7 @@ test('invalid schema', async () => { ) } const { queryByText, queryAllByTestId } = render() - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Add Field')) await sleep(33) expect(queryAllByTestId('item').length).toBe(2) @@ -486,7 +486,7 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn useEffect(() => { setTimeout(() => { act(() => setState({ testA: `${Math.random()}` })) - }, 100) + }, 10) }, []) return ( @@ -555,23 +555,23 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn const { queryByTestId, queryAllByText, queryByText } = render( ) - await sleep(100) + await sleep(33) fireEvent.click(queryByTestId('radio-a2')) - await sleep(100) + await sleep(33) // onChange expect(queryAllByText('valueB-456').length).toBe(1) actions.reset() - await sleep(100) + await sleep(33) // onReset expect(queryAllByText('valueC-456').length).toBe(1) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) // onValidateFailed expect(queryAllByText('valueE-456').length).toBe(1) fireEvent.click(queryByTestId('radio-b2')) - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) // onSubmit expect(queryAllByText('valueD-456').length).toBe(1) }) diff --git a/packages/react/src/__tests__/editable.spec.js b/packages/react/src/__tests__/editable.spec.js index 5375ebffe91..186385e08e8 100644 --- a/packages/react/src/__tests__/editable.spec.js +++ b/packages/react/src/__tests__/editable.spec.js @@ -84,13 +84,13 @@ test('update editable by setFieldState', async () => { const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeNull() - await sleep(100) + await sleep(33) actions.setFieldState('aaa', state => { state.editable = true }) - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() }) @@ -119,13 +119,13 @@ test('update editable by setFieldState with initalState is not editable', async const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeNull() - await sleep(100) + await sleep(33) actions.setFieldState('aaa', state => { state.editable = true }) - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() }) @@ -142,13 +142,13 @@ test('update editable in controlled', async () => { } const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() act(() => updateEditable(false)) - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeNull() act(() => updateEditable(true)) - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() }) @@ -168,12 +168,12 @@ test('editable with x-props', async () => { } const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeNull() actions.setFieldState('aaa', state => { state.editable = true }) - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() }) @@ -200,12 +200,12 @@ test('editable with x-props in array field', async () => { } const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('empty')).toBeVisible() actions.setFieldState('array.0.aa', state => { state.editable = true }) - await sleep(100) + await sleep(33) expect(queryByText('empty')).toBeNull() }) @@ -233,12 +233,12 @@ test('editable with x-props is affected by global editable', async () => { } const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('empty')).toBeNull() actions.setFieldState('array.0.aa', state => { state.editable = false }) - await sleep(100) + await sleep(33) expect(queryByText('empty')).toBeVisible() }) @@ -280,10 +280,10 @@ test('editable conflicts that global editable props with setFieldState', async ( expect(queryByTestId('this is bbb')).toBeVisible() expect(queryByTestId('this is ccc')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() }) @@ -325,10 +325,10 @@ test('editable conflicts that props editable props with setFieldState', async () expect(queryByTestId('this is bbb')).toBeVisible() expect(queryByTestId('this is ccc')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() }) @@ -369,9 +369,9 @@ test('editable conflicts that x-props editable props with setFieldState', async expect(queryByTestId('this is bbb')).toBeVisible() expect(queryByTestId('this is ccc')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '123' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() fireEvent.change(queryByTestId('this is ccc'), { target: { value: '321' } }) - await sleep(100) + await sleep(33) expect(queryByTestId('this is bbb')).toBeVisible() }) diff --git a/packages/react/src/__tests__/effects.spec.js b/packages/react/src/__tests__/effects.spec.js index 5795388e8d2..a7549c3d34e 100644 --- a/packages/react/src/__tests__/effects.spec.js +++ b/packages/react/src/__tests__/effects.spec.js @@ -59,19 +59,19 @@ test('onFormInit setFieldState', async () => { const { getByText, getByTestId, queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text')).toBeVisible() - await sleep(100) + await sleep(33) fireEvent.click(getByTestId('btn')) - await sleep(100) + await sleep(33) expect(getByText('field is required')).toBeVisible() - await sleep(100) + await sleep(33) actions.setFieldState('aaa', state => { state.rules = [] }) - await sleep(100) + await sleep(33) fireEvent.click(getByTestId('btn')) - await sleep(100) + await sleep(33) expect(queryByText('field is required')).toBeNull() }) diff --git a/packages/react/src/__tests__/mutators.spec.js b/packages/react/src/__tests__/mutators.spec.js index a8456f7067e..511f62099e8 100644 --- a/packages/react/src/__tests__/mutators.spec.js +++ b/packages/react/src/__tests__/mutators.spec.js @@ -33,9 +33,9 @@ test('update value by ref', async () => { ) const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('123')).toBeVisible() fireEvent.click(queryByText('Change Value')) - await sleep(100) + await sleep(33) expect(queryByText('321')).toBeVisible() }) diff --git a/packages/react/src/__tests__/validate.spec.js b/packages/react/src/__tests__/validate.spec.js index 2daa33c0362..9ebbc921b31 100644 --- a/packages/react/src/__tests__/validate.spec.js +++ b/packages/react/src/__tests__/validate.spec.js @@ -42,9 +42,9 @@ test('basic validate', async () => { const { getByTestId, getByText } = render() fireEvent.click(getByTestId('btn')) - await sleep(100) + await sleep(33) fireEvent.click(getByTestId('btn')) - await sleep(300) + await sleep(33) expect(handleSubmit).toHaveBeenCalledTimes(0) expect(handleValidateFailed).toHaveBeenCalledTimes(2) expect(getByText('text is required')).toBeVisible() @@ -79,11 +79,11 @@ test('validate in init', async () => { act(() => { result = render() }) - await sleep(100) + await sleep(33) const { queryByText } = result expect(queryByText('text is required')).toBeNull() fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(handleSubmit).toHaveBeenCalledTimes(0) expect(handleValidateFailed).toHaveBeenCalledTimes(1) expect(queryByText('text is required')).toBeVisible() @@ -121,20 +121,20 @@ test('validate in editable false', async () => { act(() => { result = render() }) - await sleep(100) + await sleep(33) const { queryByText } = result expect(queryByText('editable is required')).toBeNull() fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(handleSubmit).toHaveBeenCalledTimes(0) expect(handleValidateFailed).toHaveBeenCalledTimes(1) expect(queryByText('editable is required')).toBeVisible() actions.setFieldState('editable', state => { state.value = '123' }) - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(handleSubmit).toHaveBeenCalledTimes(1) expect(handleValidateFailed).toHaveBeenCalledTimes(1) expect(queryByText('editable is required')).toBeNull() @@ -159,35 +159,35 @@ test('modify required rules by setFieldState', async () => { ) } const { queryByText } = render() - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(handleSubmit).toBeCalledTimes(1) expect(handleValidateFailed).toBeCalledTimes(0) actions.setFieldState('kk', state => { state.props.required = true }) - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(queryByText('kk is required')).toBeVisible() expect(handleSubmit).toBeCalledTimes(1) expect(handleValidateFailed).toBeCalledTimes(1) actions.setFieldState('kk', state => { state.required = false }) - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(queryByText('kk is required')).toBeNull() expect(handleSubmit).toBeCalledTimes(2) expect(handleValidateFailed).toBeCalledTimes(1) actions.setFieldState('kk', state => { state.required = true }) - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(queryByText('kk is required')).toBeVisible() expect(handleSubmit).toBeCalledTimes(2) expect(handleValidateFailed).toBeCalledTimes(2) @@ -210,9 +210,9 @@ test('modify validate rules by setFieldState', async () => { ) } const { queryByText, queryByTestId } = render() - await sleep(100) + await sleep(33) fireEvent.click(queryByText('Submit')) - await sleep(100) + await sleep(33) expect(queryByText('required')).toBeVisible() actions.setFieldState('bb', state => { state.rules = [ @@ -223,11 +223,11 @@ test('modify validate rules by setFieldState', async () => { } ] }) - await sleep(100) + await sleep(33) fireEvent.change(queryByTestId('test-input'), { target: { value: '123' } }) - await sleep(100) + await sleep(33) expect(queryByText('must have 6 numbers')).toBeVisible() }) @@ -251,17 +251,17 @@ test('dynamic update values', async () => { fireEvent.change(queryByTestId('test-input'), { target: { value: '12332123' } }) - await sleep(133) + await sleep(33) fireEvent.change(queryByTestId('test-input'), { target: { value: '12332123a' } }) - await sleep(133) + await sleep(33) fireEvent.change(queryByTestId('test-input'), { target: { value: '123321' } }) - await sleep(133) + await sleep(33) fireEvent.change(queryByTestId('test-input'), { target: { value: '12332' } }) - await sleep(133) + await sleep(33) expect(queryByText('must be number')).toBeNull() fireEvent.change(queryByTestId('test-input'), { target: { value: '12332a' } }) - await sleep(133) + await sleep(33) expect(queryByText('must be number')).toBeVisible() }) diff --git a/packages/react/src/__tests__/validate_relations.spec.js b/packages/react/src/__tests__/validate_relations.spec.js index d0bf39b8611..65a84178897 100644 --- a/packages/react/src/__tests__/validate_relations.spec.js +++ b/packages/react/src/__tests__/validate_relations.spec.js @@ -60,12 +60,12 @@ test('setFieldState will trigger validate', async () => { const { getByTestId } = render() fireEvent.click(getByTestId('btn')) - await sleep(100) + await sleep(33) expect(handleSubmit).toHaveBeenCalledTimes(0) expect(handleValidateFailed).toHaveBeenCalledTimes(1) expect(getByTestId('test-errors-2')).toHaveTextContent('text-2 is required') fireEvent.change(getByTestId('test-input-1'), { target: { value: '123' } }) - await sleep(100) + await sleep(33) expect(getByTestId('test-input-2')).toHaveAttribute('value', '123') expect(getByTestId('test-errors-2')).not.toHaveTextContent( 'text-2 is required' diff --git a/packages/react/src/__tests__/value.spec.js b/packages/react/src/__tests__/value.spec.js index ec49ec4e4ce..9b835bcd248 100644 --- a/packages/react/src/__tests__/value.spec.js +++ b/packages/react/src/__tests__/value.spec.js @@ -2,17 +2,15 @@ import React from 'react' import { render } from '@testing-library/react' import SchemaForm, { Field, registerFormField, connect } from '../index' -beforeEach(() => { - registerFormField( - 'string', - connect()(props =>
{typeof props.value}
) - ) -}) +registerFormField( + 'test-string', + connect()(props =>
{typeof props.value}
) +) test('default value', async () => { const Component = () => ( - + ) @@ -24,7 +22,7 @@ test('default value', async () => { test('initialValues', async () => { const Component = () => ( - + ) diff --git a/packages/react/src/__tests__/virtualbox.spec.js b/packages/react/src/__tests__/virtualbox.spec.js index eaa3e037811..833158627db 100644 --- a/packages/react/src/__tests__/virtualbox.spec.js +++ b/packages/react/src/__tests__/virtualbox.spec.js @@ -51,7 +51,7 @@ test('createVirtualBox', async () => { const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('card content')).toBeVisible() }) diff --git a/packages/react/src/__tests__/x-component.spec.js b/packages/react/src/__tests__/x-component.spec.js index fb69c565e1f..aecd08010c4 100644 --- a/packages/react/src/__tests__/x-component.spec.js +++ b/packages/react/src/__tests__/x-component.spec.js @@ -45,12 +45,12 @@ test('update x-component by setFieldState', async () => { const { queryByText } = render() - await sleep(100) + await sleep(33) expect(queryByText('text component')).toBeNull() - await sleep(100) + await sleep(33) actions.setFieldState('aaa', state => { state.props['x-component'] = 'text' }) - await sleep(100) + await sleep(33) expect(queryByText('text component')).toBeVisible() })