Skip to content

Commit e646219

Browse files
shaodahongzombieJ
authored andcommitted
fix: validateFields function not resolve or reject (#340)
* fix: validateFields function not resolve or reject * test: add validateFieldsAndScroll promise test suite * test: add validateFieldsAndScroll promise test suite * test: add validateFieldsAndScroll promise test suite * test: add validateFieldsAndScroll promise test suite * test: add validateFieldsAndScroll.spec.js file
1 parent 7a2fb62 commit e646219

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/createBaseForm.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ function createBaseForm(option = {}, mixins = []) {
537537
callback = (errors, values) => {
538538
if (oldCb) {
539539
oldCb(errors, values);
540-
} else if (errors) {
540+
}
541+
if (errors) {
541542
reject({ errors, values });
542543
} else {
543544
resolve(values);
@@ -572,7 +573,9 @@ function createBaseForm(option = {}, mixins = []) {
572573
}, callback);
573574
});
574575
pending.catch((e) => {
576+
// eslint-disable-next-line no-console
575577
if (console.error && process.env.NODE_ENV !== 'production') {
578+
// eslint-disable-next-line no-console
576579
console.error(e);
577580
}
578581
return e;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable react/prop-types */
2+
import React from 'react'
3+
import ReactDOM from 'react-dom'
4+
import { Simulate } from 'react-dom/test-utils'
5+
import createDOMForm from '../src/createDOMForm'
6+
7+
class Test extends React.Component {
8+
check = (rule, value, callback) => {
9+
setTimeout(() => {
10+
if (value === '1') {
11+
callback()
12+
} else {
13+
callback(new Error('must be 1'))
14+
}
15+
}, 100)
16+
}
17+
18+
render() {
19+
const { getFieldProps } = this.props.form
20+
return (
21+
<div>
22+
<input {...getFieldProps('normal')} />
23+
<input
24+
{...getFieldProps('async', {
25+
rules: [this.check],
26+
})}
27+
/>
28+
</div>
29+
)
30+
}
31+
}
32+
33+
Test = createDOMForm({
34+
withRef: true,
35+
})(Test)
36+
37+
describe('Test validateFieldsAndScroll', () => {
38+
let container
39+
let component
40+
let form
41+
42+
beforeEach(() => {
43+
container = document.createElement('div')
44+
document.body.appendChild(container)
45+
component = ReactDOM.render(<Test />, container)
46+
component = component.refs.wrappedComponent
47+
form = component.props.form
48+
})
49+
50+
afterEach(() => {
51+
ReactDOM.unmountComponentAtNode(container)
52+
document.body.removeChild(container)
53+
})
54+
55+
it('validate validateFieldsAndScroll promise', (done) => {
56+
form.getFieldInstance('async').value = '1'
57+
Simulate.change(form.getFieldInstance('async'))
58+
return form.validateFieldsAndScroll().then(values => {
59+
expect(values.normal).toBe(undefined)
60+
expect(values.async).toBe('1')
61+
done()
62+
})
63+
})
64+
})

0 commit comments

Comments
 (0)