Skip to content

Commit

Permalink
fix(@uform/validator): Fix validate threshold check (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Aug 6, 2019
1 parent 3896efb commit 3ca7c08
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
40 changes: 38 additions & 2 deletions packages/react/src/__tests__/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,16 @@ test('test idcard rules', async () => {
const value5 = '12345678912345678X'
// 18位数字
const value6 = '123456789123456789'

const element = await waitForElement(() => queryByTestId('test-input'))
waitForDomChange({ container: element }).then(mutationsList => {
const mutation = mutationsList[0]
const { value } = mutation.target
const errorTipsElement = queryByText('idCard is not an idcard format')
if (value === value1 || value === value3) {
errorTipsElement.toBeVisible()
expect(errorTipsElement).toBeVisible()
} else {
errorTipsElement.toBeNull()
expect(errorTipsElement).toBeNull()
}
})
fireEvent.change(element, { target: { value: value1 } })
Expand All @@ -311,3 +312,38 @@ test('test idcard rules', async () => {
fireEvent.change(element, { target: { value: value5 } })
fireEvent.change(element, { target: { value: value6 } })
})

test('dynamic switch visible', async () => {
const TestComponent = () => {
return (
<SchemaForm
initialValues={{ aa: '', bb: '' }}
effects={($, { setFieldState }) => {
$('onFieldChange', 'aa').subscribe(({ value }) => {
setFieldState('bb', state => {
state.visible = value == 'aa'
})
})
}}
>
<Field name="aa" type="string" />
<Field name="bb" type="string" required />
</SchemaForm>
)
}
const { queryAllByTestId, queryByText } = render(<TestComponent />)
await sleep(33)
fireEvent.change(queryAllByTestId('test-input')[0], {
target: { value: 'aa' }
})
await sleep(33)
fireEvent.change(queryAllByTestId('test-input')[0], {
target: { value: 'bb' }
})
await sleep(33)
fireEvent.change(queryAllByTestId('test-input')[0], {
target: { value: 'aa' }
})
await sleep(33)
expect(queryByText('bb is required')).toBeNull()
})
11 changes: 8 additions & 3 deletions packages/validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isArr,
isEqual,
clone,
format
format,
isEmpty
} from './utils'
import { validate } from './validators'
import { ValidateHandler, IValidateResponse, IFieldMap } from '@uform/types'
Expand Down Expand Up @@ -49,9 +50,13 @@ export const runValidation = async (
) {
return
}
if (isEqual(field.lastValidateValue, value) && !forceUpdate) {
return
if (!forceUpdate) {
if (isEmpty(field.lastValidateValue) && isEmpty(value)) return
if (isEqual(field.lastValidateValue, value)) {
return
}
}

const title = field.props && field.props.title
const rafId = setTimeout(() => {
field.loading = true
Expand Down

0 comments on commit 3ca7c08

Please sign in to comment.