Skip to content

Commit db17980

Browse files
authored
Merge pull request #278 from rvsia/addMissingTests
Add missing tests
2 parents 8df00ca + 079fd85 commit db17980

File tree

18 files changed

+535
-12
lines changed

18 files changed

+535
-12
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
command: yarn test:ci
2323
- codecov/upload:
2424
file: coverage/*.json
25+
token: aa1b6b4d-79a1-49f6-bcb2-d6069134a772
2526
- persist_to_workspace:
2627
root: ~/react-forms
2728
paths:

packages/mui-component-mapper/src/tests/demo-test.test.js

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { validationError } from '../form-fields/helpers';
2+
3+
describe('form fields helpers', () => {
4+
describe('validationError', () => {
5+
it('returns meta.error when validateOnMount', () => {
6+
expect(validationError({ error: 'error' }, true)).toEqual('error');
7+
});
8+
9+
it('returns meta.error || meta.touched when validateOnMount', () => {
10+
expect(validationError({ error: 'error', touched: false }, false)).toEqual(false);
11+
expect(validationError({ error: 'error', touched: true }, false)).toEqual('error');
12+
});
13+
});
14+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import Typography from '@material-ui/core/Typography';
4+
5+
import PlainText from '../form-fields/plain-text';
6+
7+
describe('PlainText component', () => {
8+
it('renders correctly with three paragraphs', () => {
9+
const label = 'One \n Two \n Three';
10+
const name = 'name';
11+
12+
const wrapper = mount(<PlainText name={ name } label={ label }/>);
13+
14+
expect(wrapper.find(Typography)).toHaveLength(3);
15+
});
16+
17+
it('renders correctly with one paragraphs', () => {
18+
const label = 'One';
19+
const name = 'name';
20+
21+
const wrapper = mount(<PlainText name={ name } label={ label }/>);
22+
23+
expect(wrapper.find(Typography)).toHaveLength(1);
24+
});
25+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import Grid from '@material-ui/core/Grid';
4+
import Typography from '@material-ui/core/Typography';
5+
6+
import Subform from '../form-fields/sub-form';
7+
8+
describe('subform', () => {
9+
const props = {
10+
fields: [
11+
{
12+
key: 'cosiKey',
13+
title: 'cosiTitle',
14+
name: 'cosiName',
15+
fields: [],
16+
},
17+
{
18+
key: 'cosiKey2',
19+
title: 'cosiTitle2',
20+
name: 'cosiName2',
21+
fields: [],
22+
},
23+
],
24+
formOptions: {
25+
renderForm: jest.fn().mockImplementation(() => <h1>Content</h1>),
26+
},
27+
};
28+
29+
const TITLE = 'TIIIITLE';
30+
const DESCRIPTION = 'THIS IS DESCRIPTION';
31+
32+
it('should render correctly', () => {
33+
const wrapper = mount(<Subform { ...props } />);
34+
35+
expect(wrapper.find(Grid)).toHaveLength(2);
36+
expect(wrapper.find(Typography)).toHaveLength(0);
37+
expect(wrapper.find('h1')).toHaveLength(1);
38+
});
39+
40+
it('should render correctly with title', () => {
41+
const wrapper = mount(<Subform { ...props } title={ TITLE }/>);
42+
43+
expect(wrapper.find(Grid)).toHaveLength(3);
44+
expect(wrapper.find(Typography)).toHaveLength(1);
45+
expect(wrapper.find(Typography).text().includes(TITLE)).toEqual(true);
46+
expect(wrapper.find('h1')).toHaveLength(1);
47+
});
48+
49+
it('should render correctly with description', () => {
50+
const wrapper = mount(<Subform { ...props } description={ DESCRIPTION }/>);
51+
52+
expect(wrapper.find(Grid)).toHaveLength(3);
53+
expect(wrapper.find(Typography)).toHaveLength(1);
54+
expect(wrapper.find(Typography).text().includes(DESCRIPTION)).toEqual(true);
55+
expect(wrapper.find('h1')).toHaveLength(1);
56+
});
57+
58+
it('should render correctly with title and description', () => {
59+
const wrapper = mount(<Subform { ...props } title={ TITLE } description={ DESCRIPTION }/>);
60+
61+
expect(wrapper.find(Grid)).toHaveLength(4);
62+
expect(wrapper.find(Typography)).toHaveLength(2);
63+
expect(wrapper.find(Typography).first().text().includes(TITLE)).toEqual(true);
64+
expect(wrapper.find(Typography).last().text().includes(DESCRIPTION)).toEqual(true);
65+
expect(wrapper.find('h1')).toHaveLength(1);
66+
});
67+
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import AppBar from '@material-ui/core/AppBar';
4+
import Tabs from '@material-ui/core/Tabs';
5+
import Tab from '@material-ui/core/Tab';
6+
7+
import FormTabs from '../form-fields/tabs';
8+
9+
describe('tabs', () => {
10+
const props = {
11+
fields: [
12+
{
13+
key: 'cosiKey',
14+
title: 'cosiTitle',
15+
name: 'cosiName',
16+
fields: [],
17+
},
18+
{
19+
key: 'cosiKey2',
20+
title: 'cosiTitle2',
21+
name: 'cosiName2',
22+
fields: [],
23+
},
24+
],
25+
formOptions: {
26+
renderForm: jest.fn().mockImplementation(() => <h1>Content</h1>),
27+
},
28+
};
29+
30+
it('should render tabs correctly', () => {
31+
const wrapper = mount(<FormTabs { ...props } />);
32+
33+
expect(wrapper.find(AppBar)).toHaveLength(1);
34+
expect(wrapper.find(Tabs)).toHaveLength(1);
35+
expect(wrapper.find(Tab)).toHaveLength(2);
36+
expect(wrapper.find('h1')).toHaveLength(1);
37+
});
38+
39+
it('should switch tabs correctly', () => {
40+
const wrapper = mount(<FormTabs { ...props } />);
41+
expect(wrapper.instance().state.activeTab).toEqual(0);
42+
43+
const secondTabButton = wrapper.find('button').last();
44+
secondTabButton.simulate('click');
45+
46+
expect(wrapper.instance().state.activeTab).toEqual(1);
47+
});
48+
});

packages/pf4-component-mapper/src/tests/form-fields.test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ import {
1111
SwitchField,
1212
} from '../form-fields/form-fields';
1313
import { mount, shallow } from 'enzyme';
14-
15-
const FieldProvider = ({ render, ...props }) => <div>{ render({ input: { name: 'Foo', onChange: jest.fn() }, meta: { error: false, touched: false }, ...props }) }</div>;
14+
import { Radio, Checkbox } from '@patternfly/react-core';
1615

1716
describe('FormFields', () => {
17+
let FieldProvider;
18+
let onChangeSpy;
19+
20+
beforeEach(() => {
21+
onChangeSpy = jest.fn();
22+
23+
FieldProvider = ({ render, ...props }) => <div>
24+
{ render({ input: { name: 'Foo', onChange: onChangeSpy }, meta: { error: false, touched: false }, ...props }) }
25+
</div>;
26+
});
27+
1828
const props = {
1929
input: {
2030
name: 'Name of the field',
@@ -91,6 +101,18 @@ describe('FormFields', () => {
91101
expect(toJson(wrapper)).toMatchSnapshot();
92102
});
93103

104+
it('Multiple checkbox - should call on change correctly', () => {
105+
const wrapper = mount(
106+
<CheckboxField { ...propsWithOptions } FieldProvider={ FieldProvider } />
107+
);
108+
109+
expect(onChangeSpy).not.toHaveBeenCalled();
110+
111+
wrapper.find(Checkbox).first().props().onChange();
112+
113+
expect(onChangeSpy).toHaveBeenCalled();
114+
});
115+
94116
it('should render TextArea correctly', () => {
95117
const wrapper = mount(
96118
<TextAreaField { ...props } />
@@ -119,6 +141,16 @@ describe('FormFields', () => {
119141
expect(toJson(wrapper)).toMatchSnapshot();
120142
});
121143

144+
it('radio - should call fieldProvider onChange correctly', () => {
145+
const wrapper = mount(
146+
<RadioField { ...propsWithOptions } FieldProvider={ FieldProvider } disabled={ true } />
147+
);
148+
149+
wrapper.find(Radio).first().props().onChange();
150+
151+
expect(onChangeSpy).toHaveBeenCalled();
152+
});
153+
122154
it('should render Select correctly', () => {
123155
const wrapper = shallow(
124156
<SelectField { ...propsWithOptions } />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import { TextContent, Text } from '@patternfly/react-core';
4+
5+
import PlainText from '../form-fields/plain-text';
6+
7+
describe('PlainText component', () => {
8+
it('renders correctly with three paragraphs', () => {
9+
const label = 'One \n Two \n Three';
10+
const name = 'name';
11+
12+
const wrapper = mount(<PlainText name={ name } label={ label }/>);
13+
14+
expect(wrapper.find(TextContent)).toHaveLength(1);
15+
expect(wrapper.find(Text)).toHaveLength(3);
16+
});
17+
18+
it('renders correctly with one paragraphs', () => {
19+
const label = 'One';
20+
const name = 'name';
21+
22+
const wrapper = mount(<PlainText name={ name } label={ label }/>);
23+
24+
expect(wrapper.find(TextContent)).toHaveLength(1);
25+
expect(wrapper.find(Text)).toHaveLength(1);
26+
});
27+
});

packages/pf4-component-mapper/src/tests/wizard/wizard.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mount } from 'enzyme';
33
import toJSon from 'enzyme-to-json';
44

55
import FormRenderer, { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer';
6+
import * as enterHandle from '@data-driven-forms/common/src/wizard/enter-handler';
67

78
import { formFieldsMapper, layoutMapper } from '../../index';
89
import Wizard from '../../form-fields/wizard/wizard';
@@ -149,6 +150,33 @@ describe('<Wizard />', () => {
149150
expect(toJSon(wrapper)).toMatchSnapshot();
150151
});
151152

153+
it('should call enter handler when pressing enter', () => {
154+
enterHandle.default = jest.fn();
155+
156+
const wrapper = mount(<Wizard { ...initialProps } />);
157+
158+
expect(enterHandle.default).not.toHaveBeenCalled();
159+
160+
const wizard = wrapper.find('.pf-c-wizard');
161+
162+
const event = { someEvent: true };
163+
const formOptions = expect.any(Object);
164+
const handleNext = expect.any(Function);
165+
const handleSubmit = expect.any(Function);
166+
const findCurrentStep = expect.any(Function);
167+
168+
wizard.props().onKeyDown(event);
169+
170+
expect(enterHandle.default).toHaveBeenCalledWith(
171+
event,
172+
formOptions,
173+
'1',
174+
findCurrentStep,
175+
handleNext,
176+
handleSubmit
177+
);
178+
});
179+
152180
it('should render correctly in modal and unmount', () => {
153181
const wrapper = mount(<Wizard inModal { ...initialProps } />);
154182
expect(toJSon(wrapper)).toMatchSnapshot();

packages/react-form-renderer/src/form-renderer/field-provider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ FieldProvider.propTypes = {
8787
getFieldState: PropTypes.func,
8888
clearedValue: PropTypes.any,
8989
}),
90-
component: PropTypes.oneOfType(PropTypes.node, PropTypes.element, PropTypes.func),
90+
component: PropTypes.oneOfType([ PropTypes.node, PropTypes.element, PropTypes.func ]),
9191
render: PropTypes.func,
92-
children: PropTypes.oneOfType(PropTypes.node, PropTypes.element, PropTypes.func),
92+
children: PropTypes.oneOfType([ PropTypes.node, PropTypes.element, PropTypes.func ]),
9393
dataType: PropTypes.oneOf(Object.values(dataTypes)),
9494
name: PropTypes.string,
9595
clearOnUnmount: PropTypes.bool,

0 commit comments

Comments
 (0)