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