Skip to content

Commit

Permalink
refactor: use isEqual instead of isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
monkindey committed Jul 11, 2019
1 parent 2446cf3 commit 41aa26e
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 87 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/__tests__/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ test('submit by form consumer', async () => {
)

const { queryByText } = render(<TestComponent />)
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()
Expand Down
24 changes: 12 additions & 12 deletions packages/react/src/__tests__/dynamic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ test('dynaimc add field with initialValue', async () => {
}

const { queryAllByTestId, queryByText } = render(<TestComponent />)
await sleep(100)
await sleep(33)
expect(queryAllByTestId('item').length).toBe(1)
expect(queryAllByTestId('input').length).toBe(1)
fireEvent.click(queryByText('Add Field'))
Expand Down Expand Up @@ -279,7 +279,7 @@ test('dynaimc add field with initialValue in virtualbox', async () => {
}

const { queryAllByTestId, queryByText } = render(<TestComponent />)
await sleep(100)
await sleep(33)
expect(queryAllByTestId('item').length).toBe(1)
expect(queryAllByTestId('input').length).toBe(1)
fireEvent.click(queryByText('Add Field'))
Expand Down Expand Up @@ -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]
})
Expand Down Expand Up @@ -428,7 +428,7 @@ test('dynamic async default value', async () => {
)
}
const { queryAllByTestId } = render(<TestComponent />)
await sleep(100)
await sleep(33)
expect(queryAllByTestId('item').length).toBe(1)
expect(queryAllByTestId('input').length).toBe(2)
})
Expand Down Expand Up @@ -471,7 +471,7 @@ test('invalid schema', async () => {
)
}
const { queryByText, queryAllByTestId } = render(<TestComponent />)
await sleep(100)
await sleep(33)
fireEvent.click(queryByText('Add Field'))
await sleep(33)
expect(queryAllByTestId('item').length).toBe(2)
Expand All @@ -486,7 +486,7 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn
useEffect(() => {
setTimeout(() => {
act(() => setState({ testA: `${Math.random()}` }))
}, 100)
}, 10)
}, [])
return (
<Fragment>
Expand Down Expand Up @@ -555,23 +555,23 @@ test('dynamic change functions onChange/onReset/onSubmit/onValidateFailed', asyn
const { queryByTestId, queryAllByText, queryByText } = render(
<TestComponent />
)
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)
})
42 changes: 21 additions & 21 deletions packages/react/src/__tests__/editable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ test('update editable by setFieldState', async () => {

const { queryByText } = render(<TestComponent />)

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()
})

Expand Down Expand Up @@ -119,13 +119,13 @@ test('update editable by setFieldState with initalState is not editable', async

const { queryByText } = render(<TestComponent />)

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()
})

Expand All @@ -142,13 +142,13 @@ test('update editable in controlled', async () => {
}

const { queryByText } = render(<TestComponent />)
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()
})

Expand All @@ -168,12 +168,12 @@ test('editable with x-props', async () => {
}

const { queryByText } = render(<TestComponent />)
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()
})

Expand All @@ -200,12 +200,12 @@ test('editable with x-props in array field', async () => {
}

const { queryByText } = render(<TestComponent />)
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()
})

Expand Down Expand Up @@ -233,12 +233,12 @@ test('editable with x-props is affected by global editable', async () => {
}

const { queryByText } = render(<TestComponent />)
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()
})

Expand Down Expand Up @@ -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()
})

Expand Down Expand Up @@ -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()
})

Expand Down Expand Up @@ -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()
})
12 changes: 6 additions & 6 deletions packages/react/src/__tests__/effects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ test('onFormInit setFieldState', async () => {

const { getByText, getByTestId, queryByText } = render(<TestComponent />)

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()
})

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/__tests__/mutators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ test('update value by ref', async () => {
)

const { queryByText } = render(<TestComponent />)
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()
})
Loading

0 comments on commit 41aa26e

Please sign in to comment.