Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 3de4239

Browse files
committed
Ensuring sync/async validators work together if specified. Fixes #993 + unit tests
1 parent ff368bf commit 3de4239

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

src/components/control-component-factory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function createControlClass(s) {
274274
model,
275275
modelValue,
276276
updateOn,
277-
validateOn,
277+
validateOn = updateOn,
278278
asyncValidateOn,
279279
dispatch,
280280
getValue,

test/control-component-spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,77 @@ Object.keys(testContexts).forEach((testKey) => {
10421042
});
10431043
});
10441044

1045+
describe('simultaneous sync and async validation', () => {
1046+
it('should execute sync and async validation simultaneously'
1047+
+ ' if specified', () => {
1048+
const initialState = getInitialState({ foo: '' });
1049+
const reducer = formReducer('test');
1050+
const store = testCreateStore({
1051+
testForm: reducer,
1052+
test: modelReducer('test', initialState),
1053+
});
1054+
const syncValid = () => true;
1055+
const asyncValid = () => true;
1056+
const syncValidSpy = sinon.spy(syncValid);
1057+
const asyncValidSpy = sinon.spy(asyncValid);
1058+
1059+
const field = TestUtils.renderIntoDocument(
1060+
<Provider store={store}>
1061+
<Control.text
1062+
model="test.foo"
1063+
validators={{ syncValidSpy }}
1064+
validateOn="change"
1065+
asyncValidators={{ asyncValidSpy }}
1066+
asyncValidateOn="change"
1067+
/>
1068+
</Provider>
1069+
);
1070+
1071+
const control = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
1072+
1073+
control.value = 'testing';
1074+
1075+
TestUtils.Simulate.change(control);
1076+
1077+
assert.isTrue(syncValidSpy.called);
1078+
assert.isTrue(asyncValidSpy.called);
1079+
});
1080+
1081+
it('should execute sync and async validation simultaneously'
1082+
+ ' if specified (with default validateOn)', () => {
1083+
const initialState = getInitialState({ foo: '' });
1084+
const reducer = formReducer('test');
1085+
const store = testCreateStore({
1086+
testForm: reducer,
1087+
test: modelReducer('test', initialState),
1088+
});
1089+
const syncValid = () => true;
1090+
const asyncValid = () => true;
1091+
const syncValidSpy = sinon.spy(syncValid);
1092+
const asyncValidSpy = sinon.spy(asyncValid);
1093+
1094+
const field = TestUtils.renderIntoDocument(
1095+
<Provider store={store}>
1096+
<Control.text
1097+
model="test.foo"
1098+
validators={{ syncValidSpy }}
1099+
asyncValidators={{ asyncValidSpy }}
1100+
asyncValidateOn="change"
1101+
/>
1102+
</Provider>
1103+
);
1104+
1105+
const control = TestUtils.findRenderedDOMComponentWithTag(field, 'input');
1106+
1107+
control.value = 'testing';
1108+
1109+
TestUtils.Simulate.change(control);
1110+
1111+
assert.isTrue(syncValidSpy.called);
1112+
assert.isTrue(asyncValidSpy.called);
1113+
});
1114+
});
1115+
10451116
describe('validation after reset', () => {
10461117
const initialState = getInitialState({ foo: '' });
10471118
const reducer = formReducer('test');

0 commit comments

Comments
 (0)