|
| 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