|
| 1 | +import React from 'react'; |
| 2 | +import { mount } from 'enzyme'; |
| 3 | + |
| 4 | +import FormRenderer from '../../files/form-renderer'; |
| 5 | +import componentTypes from '../../files/component-types'; |
| 6 | +import FormTemplate from '../../../../../__mocks__/mock-form-template'; |
| 7 | +import useFieldApi from '../../files/use-field-api'; |
| 8 | +import { act } from 'react-dom/test-utils'; |
| 9 | + |
| 10 | +describe('FormRenderer validator', () => { |
| 11 | + const TextField = (props) => { |
| 12 | + const { input, meta, ...rest } = useFieldApi(props); |
| 13 | + return ( |
| 14 | + <div> |
| 15 | + <input {...input} {...rest} /> |
| 16 | + {meta.error && <div id="error">{meta.error}</div>} |
| 17 | + </div> |
| 18 | + ); |
| 19 | + }; |
| 20 | + |
| 21 | + it('pass value, allvalues, meta to custom validator func', async () => { |
| 22 | + expect.assertions(3); |
| 23 | + |
| 24 | + const VALUE = 'some-value'; |
| 25 | + const NAME = 'field1'; |
| 26 | + const META = expect.any(Object); |
| 27 | + |
| 28 | + const validator = (value, allValues, meta) => { |
| 29 | + if (value) { |
| 30 | + //skip initial validation |
| 31 | + expect(value).toEqual(VALUE); |
| 32 | + expect(allValues).toEqual({ |
| 33 | + other: '111', |
| 34 | + [NAME]: VALUE |
| 35 | + }); |
| 36 | + expect(meta).toEqual(META); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + const wrapper = mount( |
| 41 | + <FormRenderer |
| 42 | + FormTemplate={(props) => <FormTemplate {...props} />} |
| 43 | + componentMapper={{ |
| 44 | + [componentTypes.TEXT_FIELD]: TextField |
| 45 | + }} |
| 46 | + schema={{ |
| 47 | + fields: [{ component: 'text-field', name: NAME, validate: [validator] }] |
| 48 | + }} |
| 49 | + onSubmit={jest.fn()} |
| 50 | + initialValues={{ other: '111' }} |
| 51 | + /> |
| 52 | + ); |
| 53 | + |
| 54 | + await act(async () => { |
| 55 | + wrapper.find('input').simulate('change', { target: { value: VALUE } }); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments