|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | + |
| 4 | +import FormStateManager from '../../files/form-state-manager'; |
| 5 | +import Field from '../../files/field'; |
| 6 | + |
| 7 | +describe('<Field />', () => { |
| 8 | + let wrapper; |
| 9 | + |
| 10 | + const initialProps = { name: 'some-field', initialValue: 'some-value' }; |
| 11 | + |
| 12 | + it('renders with children', () => { |
| 13 | + wrapper = mount(<FormStateManager>{() => <Field {...initialProps}>{(props) => <input {...props.input} />}</Field>}</FormStateManager>); |
| 14 | + |
| 15 | + expect(wrapper.find('input').props()).toEqual({ |
| 16 | + name: initialProps.name, |
| 17 | + onBlur: expect.any(Function), |
| 18 | + onChange: expect.any(Function), |
| 19 | + onFocus: expect.any(Function), |
| 20 | + value: initialProps.initialValue |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('renders with string component', () => { |
| 25 | + const ref = React.createRef(); |
| 26 | + |
| 27 | + wrapper = mount(<FormStateManager>{() => <Field {...initialProps} component="input" ref={ref} />}</FormStateManager>); |
| 28 | + |
| 29 | + expect(wrapper.find('input').props()).toEqual({ |
| 30 | + name: initialProps.name, |
| 31 | + onBlur: expect.any(Function), |
| 32 | + onChange: expect.any(Function), |
| 33 | + onFocus: expect.any(Function), |
| 34 | + value: initialProps.initialValue |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('renders with component as ComponentType', () => { |
| 39 | + const ref = React.createRef(); |
| 40 | + const Component = ({ input }) => <input {...input} />; |
| 41 | + |
| 42 | + wrapper = mount(<FormStateManager>{() => <Field {...initialProps} component={Component} />}</FormStateManager>); |
| 43 | + |
| 44 | + expect(wrapper.find('input').props()).toEqual({ |
| 45 | + name: initialProps.name, |
| 46 | + onBlur: expect.any(Function), |
| 47 | + onChange: expect.any(Function), |
| 48 | + onFocus: expect.any(Function), |
| 49 | + value: initialProps.initialValue |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('renders with render function', () => { |
| 54 | + wrapper = mount(<FormStateManager>{() => <Field {...initialProps} render={(props) => <input {...props.input} />} />}</FormStateManager>); |
| 55 | + |
| 56 | + expect(wrapper.find('input').props()).toEqual({ |
| 57 | + name: initialProps.name, |
| 58 | + onBlur: expect.any(Function), |
| 59 | + onChange: expect.any(Function), |
| 60 | + onFocus: expect.any(Function), |
| 61 | + value: initialProps.initialValue |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments