Add invariant if both checkedLink and valueLink are provided
Johannes Baiter committed
Jan 15, 2014 at 11:57 UTC
79995a05c7128e430a0a6152f50b703b5b8d4f99
2 files changed
+28
src/dom/components/LinkedValueUtils.js
+9
@@ -27,7 +27,15 @@ var hasReadOnlyValue = {
27
'radio': true
28
};
29
30
+function _assertSingleLink(input) {
31
+ invariant(
32
+ input.props.checkedLink == null || input.props.valueLink == null,
33
+ 'Cannot provide a checkedLink and a valueLink. If you want to use ' +
34
+ 'checkedLink, you probably don\'t want to use valueLink and vice versa.'
35
+ );
36
+}
37
function _assertValueLink(input) {
38
+ _assertSingleLink(input);
39
invariant(
40
input.props.value == null && input.props.onChange == null,
41
'Cannot provide a valueLink and a value or onChange event. If you want ' +
@@ -36,6 +44,7 @@ function _assertValueLink(input) {
44
}
45
46
function _assertCheckedLink(input) {
47
+ _assertSingleLink(input);
48
invariant(
49
input.props.checked == null && input.props.onChange == null,
50
'Cannot provide a checkedLink and a checked property or onChange event. ' +
src/dom/components/__tests__/ReactDOMInput-test.js
+19
@@ -325,4 +325,23 @@ describe('ReactDOMInput', function() {
325
expect(React.renderComponent.bind(React, instance, node)).toThrow();
326
327
});
328
+
329
+ it('should throw if both checkedLink and valueLink are provided', function() {
330
+ // Silences console.error messages
331
+ // ReactErrorUtils.guard is applied to all methods of a React component
332
+ // and calls console.error in __DEV__ (true for test environment)
333
+ spyOn(console, 'error');
334
+
335
+ var node = document.createElement('div');
336
+ var link = new ReactLink(true, mocks.getMockFunction());
337
+ var instance = <input type="checkbox" checkedLink={link} />;
338
+
339
+ expect(React.renderComponent.bind(React, instance, node)).not.toThrow();
340
+
341
+ instance = <input type="checkbox" valueLink={link} />;
342
+ expect(React.renderComponent.bind(React, instance, node)).not.toThrow();
343
+
344
+ instance = <input type="checkbox" checkedLink={link} valueLink={emptyFunction} />;
345
+ expect(React.renderComponent.bind(React, instance, node)).toThrow();
346
+ });
347
});