Skip to content

Commit a4dcbee

Browse files
committed
Add tests for MUI form fields
1 parent 9cf04eb commit a4dcbee

File tree

4 files changed

+217
-16
lines changed

4 files changed

+217
-16
lines changed

packages/mui-component-mapper/src/form-fields/form-fields.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import FormControl from '@material-ui/core/FormControl';
1212
import Switch from '@material-ui/core/Switch';
1313
import FormGroup from '@material-ui/core/FormGroup';
1414
import MuiSelect from './select-field';
15+
import FormLabel from '@material-ui/core/FormLabel';
1516

1617
import { MuiPickersUtilsProvider, TimePicker, DatePicker } from 'material-ui-pickers';
1718
import MomentUtils from '@date-io/moment';
@@ -41,7 +42,6 @@ const selectComponent = ({
4142
offText,
4243
error,
4344
locale = 'en',
44-
originalLabel,
4545
...rest
4646
}) => ({
4747
[componentTypes.TEXT_FIELD]: () => (
@@ -85,9 +85,12 @@ const selectComponent = ({
8585
{ ...input }
8686
disabled={ isDisabled || isReadOnly }
8787
value={ input.name }
88+
inputProps={{
89+
readOnly: isReadOnly,
90+
}}
8891
/> }
8992
disabled={ isDisabled || isReadOnly }
90-
label={ originalLabel }
93+
label={ <FormLabel>{ label }</FormLabel> }
9194
/>
9295
{ (invalid || helperText) && <FormHelperText>{ invalid || helperText }</FormHelperText> }
9396
</FormGroup>
@@ -99,10 +102,11 @@ const selectComponent = ({
99102
options={ options }
100103
isDisabled={ isDisabled || isReadOnly }
101104
input={ input }
102-
label={ originalLabel }
105+
label={ label }
103106
isRequired={ isRequired }
104107
invalid={ invalid }
105108
helperText={ helperText }
109+
isReadOnly={ isReadOnly }
106110
/>
107111
),
108112
[componentTypes.SELECT_COMPONENT]: () => (
@@ -130,7 +134,7 @@ const selectComponent = ({
130134
onChange={ option =>
131135
input.onChange(rest.multi ? selectValue(option) : option ? option.value : undefined) } // eslint-disable-line no-nested-ternary
132136
input={ input }
133-
label={ originalLabel }
137+
label={ label }
134138
isRequired={ isRequired }
135139
helperText={ helperText }
136140
{ ...rest }
@@ -147,7 +151,7 @@ const selectComponent = ({
147151
checked={ !!input.value }
148152
onChange={ ({ target: { checked }}) => input.onChange(checked) }
149153
/> }
150-
label={ input.value ? onText || label : offText || label }
154+
label={ <FormLabel>{ input.value ? onText || label : offText || label }</FormLabel> }
151155
/>
152156
{ (invalid || helperText) && <FormHelperText>{ invalid || helperText }</FormHelperText> }
153157
</FormGroup>
@@ -195,7 +199,6 @@ const FinalFormField = ({
195199
description,
196200
hideLabel,
197201
isVisible,
198-
label,
199202
helperText,
200203
...rest
201204
}) => {
@@ -205,9 +208,7 @@ const FinalFormField = ({
205208
{ selectComponent({
206209
...rest,
207210
invalid,
208-
label,
209211
helperText: invalid ? meta.error : helperText || description,
210-
originalLabel: label,
211212
})() }
212213
</Grid>
213214
);

packages/mui-component-mapper/src/form-fields/radio.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const RadioGroup = ({
1616
isRequired,
1717
helperText,
1818
invalid,
19+
isReadOnly,
1920
}) => (
2021
<div className="mui-ddform-radio-group">
2122
<FormControl required={ isRequired } error={ !!invalid } component="fieldset">
@@ -31,7 +32,11 @@ const RadioGroup = ({
3132
control={ <Radio
3233
{ ...input }
3334
disabled={ isDisabled }
34-
onChange={ () => input.onChange(option.value) }/> }
35+
onChange={ () => input.onChange(option.value) }
36+
inputProps={{
37+
readOnly: isReadOnly,
38+
}}
39+
/> }
3540
label={ option.label }
3641
/>
3742
) }

packages/mui-component-mapper/src/form-fields/select-field.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import Paper from '@material-ui/core/Paper';
1010
import Chip from '@material-ui/core/Chip';
1111
import MenuItem from '@material-ui/core/MenuItem';
1212
import CancelIcon from '@material-ui/icons/Cancel';
13-
import Grid from '@material-ui/core/Grid';
14-
import FormGroup from '@material-ui/core/FormGroup';
1513
import FormControl from '@material-ui/core/FormControl';
1614
import FormHelperText from '@material-ui/core/FormHelperText';
15+
import FormLabel from '@material-ui/core/FormLabel';
1716

1817
const useStyles = makeStyles(theme => ({
1918
root: {
2019
flexGrow: 1,
20+
display: 'flex',
2121
},
2222
input: {
2323
display: 'flex',
@@ -324,7 +324,8 @@ function IntegrationReactSelect (props) {
324324
};
325325

326326
return (
327-
<React.Fragment>
327+
<FormControl required={ rest.isRequired } error={ !!invalid } component="fieldset" className={ classes.root }>
328+
<FormLabel>{ rest.label }</FormLabel>
328329
<NoSsr>
329330
<Select
330331
classes={ classes }
@@ -335,10 +336,8 @@ function IntegrationReactSelect (props) {
335336
isDisabled={ !!rest.isDisabled }
336337
/>
337338
</NoSsr>
338-
<FormControl required={ rest.isRequired } error={ !!invalid } component="fieldset" >
339-
{ (invalid || rest.helperText || rest.description) && <FormHelperText>{ invalid || rest.helperText || rest.description }</FormHelperText> }
340-
</FormControl>
341-
</React.Fragment>
339+
{ (invalid || rest.helperText || rest.description) && <FormHelperText>{ invalid || rest.helperText || rest.description }</FormHelperText> }
340+
</FormControl>
342341
);
343342
}
344343

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import MuiTextField from '@material-ui/core/TextField';
4+
import Switch from '@material-ui/core/Switch';
5+
import Checkbox from '@material-ui/core/Checkbox';
6+
import FormLabel from '@material-ui/core/FormLabel';
7+
import MultipleChoiceListCommon from '@data-driven-forms/common/src/multiple-choice-list';
8+
9+
import {
10+
TextField,
11+
TextareaField,
12+
Radio,
13+
CheckboxGroup,
14+
SwitchField,
15+
DatePickerField,
16+
TimePickerField,
17+
SelectField,
18+
} from '../form-fields/form-fields';
19+
import MuiSelect from '../form-fields/select-field';
20+
import RadioGroup from '../form-fields/radio';
21+
22+
import MockFieldProvider from '../../../../__mocks__/mock-field-provider';
23+
24+
describe('formFields', () => {
25+
let initialProps;
26+
27+
const errorText = 'It is required';
28+
const helperText = 'I am helper text';
29+
const description = 'This is description';
30+
const metaError = { touched: true, error: errorText };
31+
const options = [
32+
{ label: 'Cat', value: 'cats' },
33+
{ label: 'Dog', value: 'dogs' },
34+
{ label: 'Hamster', value: 'hamsters' },
35+
];
36+
37+
const componentsWithOptions = [ Radio, SelectField ];
38+
39+
const componentsOriginalMapper = {
40+
TextField: MuiTextField,
41+
TextareaField: MuiTextField,
42+
Radio: RadioGroup,
43+
CheckboxGroup: Checkbox,
44+
SwitchField: Switch,
45+
DatePickerField: MuiTextField,
46+
TimePickerField: MuiTextField,
47+
SelectField: MuiSelect,
48+
};
49+
50+
beforeEach(() => {
51+
initialProps = {
52+
input: {
53+
name: 'field-name',
54+
value: undefined,
55+
onChange: jest.fn(),
56+
},
57+
meta: {},
58+
label: 'Some label',
59+
FieldProvider: MockFieldProvider,
60+
};
61+
});
62+
63+
describe('helperText test', () => {
64+
[ TextField, TextareaField, Radio, CheckboxGroup, DatePickerField, TimePickerField, SwitchField, SelectField ].forEach(Component => {
65+
describe(`${Component.name}`, () => {
66+
beforeEach(() => {
67+
if (componentsWithOptions.includes(Component)) {
68+
initialProps = {
69+
...initialProps,
70+
options,
71+
};
72+
}
73+
});
74+
75+
it('renders correctly', () => {
76+
const wrapper = mount(<Component { ...initialProps } />);
77+
78+
expect(wrapper.find(componentsOriginalMapper[Component.name])).toHaveLength(1);
79+
expect(wrapper.find(FormLabel).text()).toEqual(initialProps.label);
80+
expect(wrapper.find('.Mui-error')).toHaveLength(0);
81+
expect(wrapper.find('.MuiFormHelperText-root')).toHaveLength(0);
82+
expect(wrapper.find('.MuiFormLabel-asterisk')).toHaveLength(0);
83+
});
84+
85+
it('renders with error', () => {
86+
const wrapper = mount(<Component { ...initialProps } meta={ metaError }/>);
87+
88+
expect(wrapper.find('.Mui-error').last().text()).toEqual(errorText);
89+
});
90+
91+
it('renders with helperText', () => {
92+
const wrapper = mount(<Component { ...initialProps } helperText={ helperText }/>);
93+
94+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(helperText);
95+
});
96+
97+
it('renders with description', () => {
98+
const wrapper = mount(<Component { ...initialProps } description={ description }/>);
99+
100+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(description);
101+
});
102+
103+
it('renders with description and helperText', () => {
104+
const wrapper = mount(<Component { ...initialProps } helperText={ helperText } description={ description }/>);
105+
106+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(helperText);
107+
});
108+
109+
it('renders with error and helperText', () => {
110+
const wrapper = mount(<Component { ...initialProps } meta={ metaError } helperText={ helperText }/>);
111+
112+
expect(wrapper.find('.Mui-error').last().text()).toEqual(errorText);
113+
});
114+
115+
it('renders isRequired', () => {
116+
const wrapper = mount(<Component { ...initialProps } isRequired={ true }/>);
117+
118+
expect(wrapper.find('.MuiFormLabel-asterisk')).toHaveLength(1);
119+
});
120+
121+
it('renders isDisabled', () => {
122+
const wrapper = mount(<Component { ...initialProps } isDisabled={ true }/>);
123+
124+
if (Component === TextareaField) {
125+
expect(wrapper.find('textarea').first().props().disabled).toEqual(true);
126+
} else {
127+
expect(wrapper.find('input').first().props().disabled).toEqual(true);
128+
}
129+
});
130+
131+
it('renders isReadOnly', () => {
132+
const wrapper = mount(<Component { ...initialProps } isReadOnly={ true }/>);
133+
134+
if (Component === TextareaField) {
135+
expect(wrapper.find('textarea').first().props().readOnly).toEqual(true);
136+
} else {
137+
expect(wrapper.find('input').first().props().readOnly).toEqual(true);
138+
}
139+
});
140+
});
141+
});
142+
});
143+
144+
describe('MultipleCheckbox', () => {
145+
beforeEach(() => {
146+
initialProps = {
147+
...initialProps,
148+
FieldProvider: MockFieldProvider,
149+
options: [
150+
{ label: 'Cat', value: 'cats' },
151+
{ label: 'Dog', value: 'dogs' },
152+
{ label: 'Hamster', value: 'hamsters' },
153+
],
154+
};
155+
});
156+
157+
it('renders correctly', () => {
158+
const wrapper = mount(<CheckboxGroup { ...initialProps } />);
159+
160+
expect(wrapper.find(MultipleChoiceListCommon)).toHaveLength(1);
161+
expect(wrapper.find('.Mui-error')).toHaveLength(0);
162+
expect(wrapper.find('.MuiFormHelperText-root')).toHaveLength(0);
163+
expect(wrapper.find('.MuiFormLabel-asterisk')).toHaveLength(0);
164+
});
165+
166+
it('renders with error', () => {
167+
const wrapper = mount(<CheckboxGroup { ...initialProps } meta={ metaError }/>);
168+
169+
expect(wrapper.find('.Mui-error').last().text()).toEqual(errorText);
170+
});
171+
172+
it('renders with helperText', () => {
173+
const wrapper = mount(<CheckboxGroup { ...initialProps } helperText={ helperText }/>);
174+
175+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(helperText);
176+
});
177+
178+
it('renders with description', () => {
179+
const wrapper = mount(<CheckboxGroup { ...initialProps } description={ description }/>);
180+
181+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(description);
182+
});
183+
184+
it('renders with description and helperText', () => {
185+
const wrapper = mount(<CheckboxGroup { ...initialProps } helperText={ helperText } description={ description }/>);
186+
187+
expect(wrapper.find('.MuiFormHelperText-root').last().text()).toEqual(helperText);
188+
});
189+
190+
it('renders with error and helperText', () => {
191+
const wrapper = mount(<CheckboxGroup { ...initialProps } meta={ metaError } helperText={ helperText }/>);
192+
193+
expect(wrapper.find('.Mui-error').last().text()).toEqual(errorText);
194+
});
195+
});
196+
});

0 commit comments

Comments
 (0)