Skip to content

Commit fad0ddd

Browse files
committed
Added example of tests outside of renderer.
1 parent 1d88109 commit fad0ddd

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<CustomComponent /> outside renderer should render component in error state to snapshot 1`] = `
4+
<CustomComponent
5+
label="custom-component"
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-component
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 /> outside renderer should render component to snapshot 1`] = `
37+
<CustomComponent
38+
label="custom-component"
39+
name="custom-component"
40+
sideEffect={[Function]}
41+
>
42+
<div
43+
className="input-wrapper"
44+
>
45+
<label
46+
className="input-label"
47+
>
48+
custom-component
49+
</label>
50+
<input
51+
name="custom-component"
52+
onBlur={[Function]}
53+
onChange={[Function]}
54+
onFocus={[Function]}
55+
value=""
56+
/>
57+
</div>
58+
</CustomComponent>
59+
`;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import Form from '@data-driven-forms/react-form-renderer/dist/cjs/form';
4+
import RendererContext from '@data-driven-forms/react-form-renderer/dist/cjs/renderer-context';
5+
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 FormWrapper = ({ props, children }) => (
35+
<Form onSubmit={() => {}} {...props}>
36+
{() => (
37+
<form>
38+
<RendererContext.Provider value={{ formOptions: {} }}>{children}</RendererContext.Provider>
39+
</form>
40+
)}
41+
</Form>
42+
);
43+
44+
describe('<CustomComponent /> outside renderer', () => {
45+
it('should render component to snapshot', () => {
46+
const wrapper = mount(
47+
<FormWrapper>
48+
<CustomComponent name="custom-component" label="custom-component" />
49+
</FormWrapper>
50+
);
51+
expect(toJson(wrapper.find(CustomComponent))).toMatchSnapshot();
52+
});
53+
it('should render component in error state to snapshot', () => {
54+
const wrapper = mount(
55+
<FormWrapper>
56+
<CustomComponent name="custom-component" label="custom-component" validate={(value) => (!value ? 'Required' : undefined)} />
57+
</FormWrapper>
58+
);
59+
expect(toJson(wrapper.find(CustomComponent))).toMatchSnapshot();
60+
});
61+
62+
it('should call sideEffect when the input change', () => {
63+
const sideEffect = jest.fn();
64+
const wrapper = mount(
65+
<FormWrapper>
66+
<CustomComponent name="custom-component" label="custom-component" sideEffect={sideEffect} />
67+
</FormWrapper>
68+
);
69+
wrapper.find('input[name="custom-component"]').simulate('change', { target: { value: 'foo' } });
70+
expect(sideEffect).toHaveBeenCalledTimes(1);
71+
});
72+
});

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ Set up your renderer to make it easier to test the component specific features.
2929

3030
<CodeExample source="tests/custom-component-with-renderer.test" />
3131

32+
### Outside renderer
33+
34+
Rendering components outside of the renderer will require some additional set up which is not traditionally used when using form renderer and require some additional knowledge of the library. Most notably, you need to wrap the component inside the `Form` component and `RendererContext`. No functinality provided by the renderer like validations or conditions will be provided so it must be configured.
35+
36+
<CodeExample source="tests/custom-component-outside-renderer.test" />
37+
3238
</Grid>
3339
<Grid item xs={false} md={2}>
3440
<ListOfContents file="testing" />

0 commit comments

Comments
 (0)