Skip to content

Commit 3dc99a9

Browse files
authored
Merge pull request #41 from frankyma/Testing
Testing
2 parents c43252a + 69b0e50 commit 3dc99a9

File tree

11 files changed

+570
-10
lines changed

11 files changed

+570
-10
lines changed

src/actions/components.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export const parentReassignment = (({ index, id, parent }) => ({
8181
},
8282
}));
8383

84-
export const addComponent = ({ title }) => (dispatch) => {
85-
dispatch({ type: ADD_COMPONENT, payload: { title } });
84+
export const addComponent = title => (dispatch) => {
85+
dispatch({ type: ADD_COMPONENT, payload: title });
8686
dispatch({ type: SET_SELECTABLE_PARENTS });
8787
};
8888

src/components/__tests__/App.test.js renamed to src/components/__tests__.1/App.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
// import '../../setupTests';
3-
import { shallow } from 'enzyme';
3+
import { shallow, mount } from 'enzyme';
44
import App from '../App.jsx';
55
import AppContainer from '../../containers/AppContainer.jsx';
66

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { shallow, mount } from 'enzyme';
3+
import renderer from 'react-test-renderer';
4+
import RouteDisplay from '../RouteDisplay.jsx';
5+
6+
describe('React unit tests', () => {
7+
describe('RouteDisplay', () => {
8+
let wrapper;
9+
const handleDeleteRoute = jest.fn();
10+
const classes = {};
11+
12+
const props = {
13+
componentTitle: 'TestComp',
14+
pathName: 'TestPath',
15+
routeCompId: '2',
16+
handleDeleteRoute,
17+
classes,
18+
routerCompId: '1',
19+
};
20+
21+
beforeAll(() => {
22+
wrapper = mount(<RouteDisplay {...props} />);
23+
});
24+
25+
it('snapshot test', () => {
26+
const tree = renderer.create(<RouteDisplay {...props} />).toJSON();
27+
expect(tree).toMatchSnapshot();
28+
});
29+
30+
it('Should show the path name and comp name, with one line break', () => {
31+
expect(wrapper.text()).toEqual('Path: / TestPathComponent: TestComp');
32+
expect(wrapper.find('br')).toHaveLength(1);
33+
});
34+
35+
it('clicks delete handler when button clicked', () => {
36+
wrapper.find('button').simulate('click');
37+
expect(handleDeleteRoute.mock.calls.length).toEqual(1);
38+
});
39+
});
40+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`React unit tests RouteDisplay snapshot test 1`] = `
4+
<div
5+
align="stretch"
6+
className="MuiGrid-container-1 MuiGrid-align-items-xs-baseline-12"
7+
>
8+
<div
9+
className="MuiGrid-item-2 MuiGrid-grid-xs-10-39"
10+
>
11+
Path: /
12+
TestPath
13+
<br />
14+
Component:
15+
TestComp
16+
</div>
17+
<div
18+
className="MuiGrid-item-2 MuiGrid-grid-xs-2-31"
19+
>
20+
<button
21+
aria-label="Delete"
22+
className="MuiButtonBase-root-104 MuiIconButton-root-98"
23+
disabled={false}
24+
onBlur={[Function]}
25+
onClick={[Function]}
26+
onFocus={[Function]}
27+
onKeyDown={[Function]}
28+
onKeyUp={[Function]}
29+
onMouseDown={[Function]}
30+
onMouseLeave={[Function]}
31+
onMouseUp={[Function]}
32+
onTouchEnd={[Function]}
33+
onTouchMove={[Function]}
34+
onTouchStart={[Function]}
35+
tabIndex="0"
36+
type="button"
37+
>
38+
<span
39+
className="MuiIconButton-label-103"
40+
>
41+
<svg
42+
aria-hidden="true"
43+
className="MuiSvgIcon-root-107"
44+
focusable="false"
45+
viewBox="0 0 24 24"
46+
>
47+
<path
48+
d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"
49+
/>
50+
<path
51+
d="M0 0h24v24H0z"
52+
fill="none"
53+
/>
54+
</svg>
55+
</span>
56+
<span
57+
className="MuiTouchRipple-root-114"
58+
/>
59+
</button>
60+
</div>
61+
</div>
62+
`;

src/containers/LeftContainer.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import createModal from '../utils/createModal.util';
1313
import * as actions from '../actions/components';
1414

1515
const mapDispatchToProps = dispatch => ({
16-
addComponent: ({ title }) => dispatch(actions.addComponent({ title })),
16+
addComponent: title => dispatch(actions.addComponent(title)),
1717
updateComponent:
1818
({
1919
id, index, parent = null, newParentId = null, color = null, stateful = null, router = null,
@@ -91,7 +91,7 @@ class LeftContainer extends Component {
9191
}
9292

9393
handleAddComponent = () => {
94-
this.props.addComponent({ title: this.state.componentName });
94+
this.props.addComponent(this.state.componentName);
9595
this.setState({
9696
componentName: '',
9797
});

src/containers/RoutesContainer.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const mapStateToProps = ({ workspace: { components } }) => ({
2424
components,
2525
});
2626

27-
class RoutesContainer extends Component {
27+
export class RoutesContainer extends Component {
2828
constructor(props) {
2929
super(props);
3030
this.state = {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import React, { Component, Children } from 'react';
2+
import renderer from 'react-test-renderer';
3+
import TextField from '@material-ui/core/TextField';
4+
import { shallow, mount } from 'enzyme';
5+
import Select from '@material-ui/core/Select';
6+
import Button from '@material-ui/core/Button';
7+
import ConnectedRoutesContainer, { RoutesContainer } from '../RoutesContainer.jsx';
8+
import { styles } from '../LeftComponentContainer.jsx';
9+
import RouteDisplay from '../../components/RouteDisplay.jsx';
10+
11+
describe('Routes Container Tests', () => {
12+
let wrapper;
13+
const addRoute = jest.fn();
14+
15+
const props = {
16+
classes: {},
17+
addRoute,
18+
deleteRoute: () => null,
19+
setSelectableRoutes: () => null,
20+
setVisible: () => null,
21+
component: {
22+
id: '3',
23+
selectableRoutes: [
24+
{
25+
id: '1',
26+
title: 'route1',
27+
},
28+
{
29+
id: '2',
30+
title: 'route2',
31+
},
32+
],
33+
routes: [
34+
{
35+
routeCompId: '3',
36+
title: 'route3',
37+
},
38+
{
39+
routeCompId: '4',
40+
title: 'route4',
41+
},
42+
],
43+
},
44+
};
45+
46+
beforeAll(() => {
47+
wrapper = mount(<RoutesContainer {...props} />);
48+
});
49+
50+
it('snapshot test', () => {
51+
const tree = renderer.create(<RoutesContainer {...props} />).toJSON();
52+
expect(tree).toMatchSnapshot();
53+
});
54+
55+
it('should contain an input field that calls handleUpdate when changed', () => {
56+
const inputField = wrapper.find(TextField);
57+
expect(inputField).toHaveLength(1);
58+
const instance = wrapper.instance();
59+
60+
expect(inputField.prop('onChange')).toBeDefined();
61+
expect(inputField.prop('onChange')).toBeInstanceOf(Function);
62+
expect(inputField.prop('onChange')).toEqual(instance.handleChange);
63+
});
64+
65+
it('should have a handleUpdate function which sets the state to the current string', () => {
66+
expect(wrapper.state().pathName).toBe('');
67+
wrapper.find(TextField).prop('onChange')({ target: { value: 'a' }, preventDefault: () => null });
68+
expect(wrapper.state().pathName).toBe('a');
69+
});
70+
71+
it('should contain two RoutesContainer MUI components', () => {
72+
expect(wrapper.find(RouteDisplay)).toHaveLength(2);
73+
});
74+
75+
it('should contain three route child options', () => {
76+
const optionslist = wrapper.find('option');
77+
expect(optionslist).toHaveLength(3);
78+
expect(optionslist.at(0).text()).toBe('None');
79+
expect(optionslist.at(1).text()).toBe('route1');
80+
expect(optionslist.at(2).text()).toBe('route2');
81+
});
82+
83+
it('should set the selectedRouteId in state when routeChild is selected', () => {
84+
const selectComp = wrapper.find('Select');
85+
selectComp.prop('onChange')({ target: { value: '2' } });
86+
expect(wrapper.state().selectedRouteId).toBe('2');
87+
});
88+
89+
it('should run handleAddRoute when the add button is clicked', () => {
90+
const instance = wrapper.instance();
91+
const buttonClickHandler = wrapper.find(Button).prop('onClick');
92+
expect(buttonClickHandler).toEqual(instance.handleAddRoute);
93+
});
94+
95+
it('should pass route object into AddRoute when the add button is clicked', () => {
96+
wrapper.find(Button).prop('onClick')();
97+
expect(addRoute).toBeCalledWith({ path: 'a', routeCompId: '2', routerCompId: '3' });
98+
});
99+
100+
it('should contain two RoutesContainer MUI components', () => {
101+
expect(wrapper.find(RouteDisplay)).toHaveLength(2);
102+
});
103+
});

0 commit comments

Comments
 (0)