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

Commit 15190af

Browse files
committed
Fixed logged errors and warnings in unit tests
1 parent 53e2317 commit 15190af

7 files changed

+167
-14
lines changed

src/components/control-component.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import isNative from '../utils/is-native';
2828
import initialFieldState from '../constants/initial-field-state';
2929
import containsEvent from '../utils/contains-event';
3030

31+
import ComponentWrapper from './control-strip-defaults-component';
32+
3133
const findDOMNode = !isNative
3234
? require('react-dom').findDOMNode
3335
: null;
@@ -609,23 +611,29 @@ function createControlClass(s = defaultStrategy) {
609611
const mappedProps = omit(this.getMappedProps(), disallowedProps);
610612

611613
if (getRef) {
612-
mappedProps.ref = getRef;
614+
mappedProps.getRef = getRef;
613615
}
614616

615617
// If there is an existing control, clone it
616618
if (control) {
617619
return cloneElement(
618620
control,
619-
mappedProps,
620-
controlProps.children);
621+
{
622+
...mappedProps,
623+
defaultValue: undefined,
624+
defaultChecked: undefined,
625+
},
626+
controlProps.children
627+
);
621628
}
622-
623629
return createElement(
624-
component,
630+
ComponentWrapper,
625631
{
632+
component,
626633
...controlProps,
627634
...mappedProps,
628-
});
635+
}
636+
);
629637
}
630638
}
631639

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
// Prevents the defaultValue/defaultChecked fields from rendering with value/checked
5+
class ComponentWrapper extends Component {
6+
render() {
7+
/* eslint-disable no-unused-vars */
8+
const {
9+
defaultValue,
10+
defaultChecked,
11+
component,
12+
getRef,
13+
...otherProps,
14+
} = this.props;
15+
/* eslint-enable */
16+
17+
if (getRef) {
18+
otherProps.ref = getRef;
19+
}
20+
const WrappedComponent = component;
21+
return <WrappedComponent {...otherProps} />;
22+
}
23+
}
24+
ComponentWrapper.propTypes = {
25+
component: PropTypes.any,
26+
defaultValue: PropTypes.any,
27+
defaultChecked: PropTypes.any,
28+
getRef: PropTypes.func,
29+
};
30+
export default ComponentWrapper;

src/constants/control-props-map.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ function isChecked(props) {
1717
return props.modelValue.some((item) =>
1818
item === props.value);
1919
}
20+
if (typeof props.modelValue === 'undefined') {
21+
if (typeof props.defaultChecked !== 'undefined') {
22+
return props.defaultChecked;
23+
}
24+
}
2025

2126
return !!props.modelValue;
2227
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* eslint react/no-multi-comp:0 react/jsx-no-bind:0 */
2+
import { assert } from 'chai';
3+
import React from 'react';
4+
import TestUtils from 'react-dom/test-utils';
5+
6+
import { testCreateStore, testRender } from './utils';
7+
8+
import {
9+
modelReducer,
10+
formReducer,
11+
} from '../src';
12+
13+
import ComponentWrapper from '../src/components/control-strip-defaults-component';
14+
15+
describe('<ComponentWrapper> component', () => {
16+
describe('existence check', () => {
17+
it('should exist', () => {
18+
assert.ok(ComponentWrapper);
19+
});
20+
});
21+
describe('should give proper references', () => {
22+
const store = testCreateStore({
23+
test: modelReducer('test', {}),
24+
testForm: formReducer('test', {}),
25+
});
26+
27+
let instance;
28+
29+
function getRef(node) {
30+
instance = node;
31+
}
32+
33+
it('should give a ref', () => {
34+
const control = testRender(
35+
<ComponentWrapper
36+
component="input"
37+
getRef={getRef}
38+
/>, store);
39+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
40+
41+
assert.equal(instance, input);
42+
});
43+
});
44+
describe('should prevent the defaultValue and defaultChecked from propagating', () => {
45+
const store = testCreateStore({
46+
test: modelReducer('test', {}),
47+
testForm: formReducer('test', {}),
48+
});
49+
describe('Text components', () => {
50+
it('should still allow default values on basic input elements', () => {
51+
const control = testRender(
52+
<input
53+
defaultValue="blah"
54+
/>, store);
55+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
56+
assert.equal(input.defaultValue, 'blah');
57+
});
58+
it('should work on basic input component', () => {
59+
const control = testRender(
60+
<ComponentWrapper
61+
component="input"
62+
defaultValue="blah"
63+
/>, store);
64+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
65+
assert.equal(input.defaultValue, '');
66+
});
67+
const TEXT_TYPES = ['email', 'password', 'tel', 'text', 'url'];
68+
TEXT_TYPES.forEach((type) => {
69+
it(`should work on all text input component types: ${type}`, () => {
70+
const control = testRender(
71+
<ComponentWrapper
72+
component="input"
73+
type={type}
74+
defaultValue="blah"
75+
/>, store);
76+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
77+
assert.equal(input.defaultValue, '');
78+
});
79+
});
80+
});
81+
describe('Checkbox component', () => {
82+
it('should work on basic default checked checkbox component', () => {
83+
const control = testRender(
84+
<ComponentWrapper
85+
component="input"
86+
type="checkbox"
87+
defaultChecked
88+
/>, store);
89+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
90+
assert.equal(input.defaultChecked, false);
91+
});
92+
it('should work on basic default unchecked checkbox component', () => {
93+
const control = testRender(
94+
<ComponentWrapper
95+
component="input"
96+
type="checkbox"
97+
defaultChecked={false}
98+
/>, store);
99+
const input = TestUtils.findRenderedDOMComponentWithTag(control, 'input');
100+
assert.equal(input.defaultChecked, false);
101+
});
102+
});
103+
});
104+
});

test/custom-control-component-spec.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('custom <Control /> components', () => {
4040
}
4141
}
4242

43-
FamiliarText.propTypes = { onChange: PropTypes.function };
43+
FamiliarText.propTypes = { onChange: PropTypes.func };
4444

4545
class CustomCheckbox extends Component {
4646
render() {
@@ -69,7 +69,7 @@ describe('custom <Control /> components', () => {
6969
}
7070
};
7171

72-
MinifiedText.propTypes = { onChange: PropTypes.function };
72+
MinifiedText.propTypes = { onChange: PropTypes.func };
7373

7474
it('should handle custom prop mappings', () => {
7575
const store = testCreateStore({
@@ -237,11 +237,14 @@ describe('custom <Control /> components', () => {
237237

238238
class TextInput extends React.Component {
239239
render() {
240+
/* eslint-disable no-unused-vars */
241+
const { onChangeText, defaultValue, focus, touched, ...otherProps } = this.props;
242+
/* eslint-enable */
240243
return (
241244
<div>
242245
<input
243-
{...this.props}
244-
onChange={this.props.onChangeText}
246+
{...otherProps}
247+
onChange={onChangeText}
245248
/>
246249
</div>
247250
);
@@ -250,6 +253,9 @@ describe('custom <Control /> components', () => {
250253

251254
TextInput.propTypes = {
252255
onChangeText: PropTypes.func,
256+
defaultValue: PropTypes.string,
257+
focus: PropTypes.bool,
258+
touched: PropTypes.bool,
253259
};
254260

255261
const mapProps = {
@@ -296,7 +302,7 @@ describe('custom <Control /> components', () => {
296302

297303
class TextInput extends React.Component {
298304
render() {
299-
const { focus, touched } = this.props;
305+
const { focus, touched, ...otherProps } = this.props;
300306
const className = [
301307
focus ? 'focus' : '',
302308
touched ? 'touched' : '',
@@ -306,7 +312,7 @@ describe('custom <Control /> components', () => {
306312
<div>
307313
<input
308314
className={className}
309-
{...this.props}
315+
{...otherProps}
310316
onChange={this.props.onChangeText}
311317
/>
312318
</div>

test/field-component-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ Object.keys(testContexts).forEach((testKey) => {
16901690

16911691
// TODO: control
16921692
it('should remove the item at the specified index of the array'
1693-
+ 'represented by the model', (done) => {
1693+
+ ' represented by the model', (done) => {
16941694
const initialState = {
16951695
foo: [
16961696
{ val: 1 },

test/form-component-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ Object.keys(testContexts).forEach((testKey) => {
16421642
}}
16431643
>
16441644
{get(initialState, 'items').map((item, i) =>
1645-
<Control model={`test.items[${i}].name`} />
1645+
<Control model={`test.items[${i}].name`} key={`test.items[${i}].name`} />
16461646
)}
16471647
</Form>
16481648
</Provider>

0 commit comments

Comments
 (0)