Skip to content

Commit 8beb3df

Browse files
committed
Add tests fo MutlipleChoice list MUI
1 parent 357ec55 commit 8beb3df

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

packages/mui-component-mapper/src/form-fields/multiple-choice-list.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import FormHelperText from '@material-ui/core/FormHelperText';
1111

1212
import MultipleChoiceListCommon, { wrapperProps } from '@data-driven-forms/common/src/multiple-choice-list';
1313

14-
const FinalCheckbox = (props) => (
14+
const FinalCheckbox = ({ isDisabled, label, ...props }) => (
1515
<FormControlLabel
1616
control={ <Checkbox
1717
{ ...props }
18-
disabled={ props.isDisabled }
18+
disabled={ isDisabled }
1919
>
20-
{ props.label }
20+
{ label }
2121
</Checkbox> }
22-
label={ props.label }
22+
label={ label }
2323
/>
2424
);
2525

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
4+
import Grid from '@material-ui/core/Grid';
5+
import Checkbox from '@material-ui/core/Checkbox';
6+
import FormControlLabel from '@material-ui/core/FormControlLabel';
7+
import FormLabel from '@material-ui/core/FormLabel';
8+
import FormGroup from '@material-ui/core/FormGroup';
9+
import FormControl from '@material-ui/core/FormControl';
10+
import FormHelperText from '@material-ui/core/FormHelperText';
11+
12+
import MultipleChoiceList from '../form-fields/multiple-choice-list';
13+
import MockFieldProvider from '../../../../__mocks__/mock-field-provider';
14+
15+
describe('<MultipleChoiceList />', () => {
16+
let initialProps;
17+
let changeSpy = jest.fn();
18+
beforeEach(() => {
19+
initialProps = {
20+
FieldProvider: props => <MockFieldProvider { ...props } input={{ onChange: changeSpy, value: props.value || []}} />,
21+
options: [{
22+
label: 'Foo',
23+
value: 0,
24+
}, {
25+
label: 'Bar',
26+
value: 1,
27+
}],
28+
};
29+
});
30+
31+
afterEach(() => {
32+
changeSpy.mockReset();
33+
});
34+
35+
it('should render correctly', () => {
36+
const wrapper = mount(<MultipleChoiceList { ...initialProps } />);
37+
38+
expect(wrapper.find(FormControl)).toHaveLength(1);
39+
expect(wrapper.find(FormGroup)).toHaveLength(1);
40+
expect(wrapper.find(FormLabel)).toHaveLength(1);
41+
expect(wrapper.find(FormControlLabel)).toHaveLength(initialProps.options.length);
42+
expect(wrapper.find(Grid)).toHaveLength(1);
43+
expect(wrapper.find(Checkbox)).toHaveLength(initialProps.options.length);
44+
});
45+
46+
it('should call FieldProvider on change method', () => {
47+
const wrapper = mount(<MultipleChoiceList { ...initialProps } />);
48+
49+
wrapper.find('input').last().simulate('change', { target: { checked: true }});
50+
expect(changeSpy).toHaveBeenCalledWith([ 1 ]);
51+
});
52+
53+
it('should call FieldProvider on change method and remove option value form all values', () => {
54+
const wrapper = mount(
55+
<MultipleChoiceList
56+
{ ...initialProps }
57+
FieldProvider={ props => <MockFieldProvider { ...props } input={{ onChange: changeSpy, value: props.value || [ 1 ]}} /> }
58+
/>
59+
);
60+
61+
wrapper.find('input').last().simulate('change', { target: { checked: true }});
62+
expect(changeSpy).toHaveBeenCalledWith([]);
63+
});
64+
65+
it('should render in error state', () => {
66+
const ERROR_MESSAGE = 'Error message';
67+
68+
const wrapper = mount(
69+
<MultipleChoiceList
70+
{ ...initialProps }
71+
FieldProvider={ props => (
72+
<MockFieldProvider
73+
{ ...props }
74+
input={{ onChange: changeSpy, value: []}}
75+
meta={{
76+
error: ERROR_MESSAGE,
77+
touched: true,
78+
}}
79+
/>) }
80+
/>
81+
);
82+
83+
expect(wrapper.find(FormHelperText).text()).toEqual(ERROR_MESSAGE);
84+
});
85+
});

0 commit comments

Comments
 (0)