Skip to content

Commit 1d88109

Browse files
committed
Added test example of custo component inside renderer.
1 parent 668ee73 commit 1d88109

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<CustomComponent /> with renderer should render component in error state to snapshot 1`] = `
4+
<CustomComponent
5+
label="Custom label"
6+
name="custom-component"
7+
sideEffect={[Function]}
8+
validate={[Function]}
9+
>
10+
<div
11+
className="input-wrapper"
12+
>
13+
<label
14+
className="input-label"
15+
>
16+
Custom label
17+
</label>
18+
<input
19+
name="custom-component"
20+
onBlur={[Function]}
21+
onChange={[Function]}
22+
onFocus={[Function]}
23+
value=""
24+
/>
25+
<div
26+
className="custom-error-block"
27+
>
28+
<span>
29+
Required
30+
</span>
31+
</div>
32+
</div>
33+
</CustomComponent>
34+
`;
35+
36+
exports[`<CustomComponent /> with renderer should render component to snapshot 1`] = `
37+
<CustomComponent
38+
label="Custom label"
39+
name="custom-component"
40+
sideEffect={[Function]}
41+
validate={[Function]}
42+
>
43+
<div
44+
className="input-wrapper"
45+
>
46+
<label
47+
className="input-label"
48+
>
49+
Custom label
50+
</label>
51+
<input
52+
name="custom-component"
53+
onBlur={[Function]}
54+
onChange={[Function]}
55+
onFocus={[Function]}
56+
value=""
57+
/>
58+
</div>
59+
</CustomComponent>
60+
`;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import FormRenderer from '@data-driven-forms/react-form-renderer/dist/cjs/form-renderer';
4+
5+
import { FormTemplate } from '@data-driven-forms/mui-component-mapper/dist/cjs';
6+
import useFieldApi from '@data-driven-forms/react-form-renderer/dist/cjs/use-field-api';
7+
import toJson from 'enzyme-to-json';
8+
9+
const CustomComponent = (props) => {
10+
const { input, meta, label, sideEffect } = useFieldApi(props);
11+
return (
12+
<div className="input-wrapper">
13+
<label className="input-label">{label}</label>
14+
<input
15+
{...input}
16+
onChange={(...args) => {
17+
sideEffect(...args); // do something in addition to just changing the value in form state
18+
input.onChange(...args);
19+
}}
20+
/>
21+
{meta.error && (
22+
<div className="custom-error-block">
23+
<span>{meta.error}</span>
24+
</div>
25+
)}
26+
</div>
27+
);
28+
};
29+
30+
CustomComponent.defaultProps = {
31+
sideEffect: () => {}
32+
};
33+
34+
const createSchema = ({ label = 'Custom label', validate = [], ...rest }) => ({
35+
fields: [
36+
{
37+
name: 'custom-component',
38+
component: 'custom-component',
39+
label,
40+
validate,
41+
...rest
42+
}
43+
]
44+
});
45+
46+
const RendererWrapper = (props) => (
47+
<FormRenderer
48+
onSubmit={() => {}}
49+
FormTemplate={FormTemplate}
50+
componentMapper={{
51+
'custom-component': CustomComponent
52+
}}
53+
schema={{ fields: [] }}
54+
{...props}
55+
/>
56+
);
57+
58+
describe('<CustomComponent /> with renderer', () => {
59+
it('should render component to snapshot', () => {
60+
const wrapper = mount(<RendererWrapper schema={createSchema({})} />);
61+
expect(toJson(wrapper.find(CustomComponent))).toMatchSnapshot();
62+
});
63+
it('should render component in error state to snapshot', () => {
64+
const wrapper = mount(<RendererWrapper schema={createSchema({ validate: [{ type: 'required' }] })} />);
65+
expect(toJson(wrapper.find(CustomComponent))).toMatchSnapshot();
66+
});
67+
68+
it('should call sideEffect when the input change', () => {
69+
const sideEffect = jest.fn();
70+
const wrapper = mount(<RendererWrapper schema={createSchema({ sideEffect })} />);
71+
wrapper.find('input[name="custom-component"]').simulate('change', { target: { value: 'foo' } });
72+
expect(sideEffect).toHaveBeenCalledTimes(1);
73+
});
74+
});

packages/react-renderer-demo/src/app/pages/testing.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ Bellow is an example of a form with a async validation and a conditional field.
1919

2020
<CodeExample source="tests/form-renderer.test" />
2121

22+
## Testing custom components
23+
24+
Components that are using `useFieldApi` or `useFormApi` must be children of contexts. Therefore they must be wrapped inside these contexts when testing. The simplest way to test them, is just rendering them with the FormRenderer, but there may be cases when that is not possible. We will show you both options.
25+
26+
### With renderer
27+
28+
Set up your renderer to make it easier to test the component specific features. Use initial values to trigger falsey validation results to avoid unnecessary changes simlation.
29+
30+
<CodeExample source="tests/custom-component-with-renderer.test" />
31+
2232
</Grid>
2333
<Grid item xs={false} md={2}>
2434
<ListOfContents file="testing" />

0 commit comments

Comments
 (0)