Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit 566556c

Browse files
committed
Refactor shallow render tests; Updated enzyme
Signed-off-by: Joe Lee <[email protected]>
1 parent c9031e2 commit 566556c

File tree

17 files changed

+236
-57
lines changed

17 files changed

+236
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
*.iml
33
.idea/*
44
dist/*
5+
src/**/*.js

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,22 @@
2020
"@types/enzyme": "^2.7.7",
2121
"@types/jest": "19.2.2",
2222
"@types/react": "^15.0.21",
23-
"@types/react-addons-test-utils": "0.14.18",
2423
"@types/react-dom": "^15.5.0",
2524
"@types/react-redux": "^4.4.38",
26-
"@types/redux": "3.6.0",
2725
"@types/redux-actions": "^1.2.3",
2826
"@types/redux-logger": "^3.0.0",
2927
"awesome-typescript-loader": "^3.1.2",
3028
"cross-env": "4.0.0",
31-
"enzyme": "^2.8.0",
29+
"enzyme": "^2.8.2",
3230
"extract-text-webpack-plugin": "2.1.0",
3331
"html-webpack-plugin": "2.28.0",
3432
"jest": "^19.0.2",
3533
"karma": "^1.6.0",
3634
"karma-chrome-launcher": "^2.0.0",
3735
"karma-typescript": "^3.0.0",
3836
"karma-typescript-preprocessor": "^0.3.1",
39-
"react-addons-test-utils": "15.5.1",
4037
"react-hot-loader": "1.3.1",
38+
"react-test-renderer": "^15.5.4",
4139
"redux-actions": "2.0.2",
4240
"source-map-loader": "^0.2.1",
4341
"stylelint-webpack-plugin": "0.7.0",

src/Hello/__test__/actions.test.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as ReactDOM from "react-dom";
33
import {Provider, Store} from 'react-redux';
44

55
import configureStore from "./configureStore";
6-
import Hello from "./Hello/Hello";
6+
import Hello from "./main/components/Hello";
77

88
const store : Store<any> = configureStore();
99
ReactDOM.render(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {decrementAction, INCREMENT, incrementAction, DECREMENT} from "../actions";
2+
3+
describe("incrementAction", () => {
4+
it("returns action of type increment", () => {
5+
let action = {
6+
type: INCREMENT
7+
};
8+
expect(incrementAction()).toEqual(action);
9+
});
10+
});
11+
12+
describe("decrementAction", () => {
13+
it("returns action of type decrement", () => {
14+
let action = {
15+
type: DECREMENT
16+
};
17+
expect(decrementAction()).toEqual(action);
18+
});
19+
});

src/Hello/actions.ts renamed to src/main/actions/actions.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import {createAction} from "redux-actions";
22

3-
export enum actionTypes {
4-
INCREMENT,
5-
DECREMENT
6-
}
3+
export const INCREMENT = "INCREMENT";
4+
export const DECREMENT = "DEVREMENT";
75

86
export const incrementAction : any = createAction<void>(
9-
actionTypes.INCREMENT.toString(),
7+
INCREMENT,
108
() => {
119
}
1210
);
1311

1412
export const decrementAction : any = createAction<void>(
15-
actionTypes.DECREMENT.toString(),
13+
DECREMENT,
1614
() => {
1715
}
1816
);

src/main/components/Counter.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var React = require("react");
4+
var Counter = function (props) {
5+
var counter = props.counter, increment = props.increment, decrement = props.decrement;
6+
return (<div>
7+
<p>Counter: {counter}</p>
8+
<button onClick={increment} label="Increment" id="increment"/>
9+
<button onClick={decrement} label="Decrement" id="decrement"/>
10+
</div>);
11+
};
12+
exports.default = Counter;

src/Hello/containers/Counter.tsx renamed to src/main/components/Counter.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as React from 'react';
22
import DefaultState from "../interfaces/defaultState"
33

4-
export interface IProps extends DefaultState {
4+
export interface CounterProps extends DefaultState {
55
increment(): void;
66
decrement(): void;
77
}
88

9-
const Counter = (props: IProps) => {
10-
const { counter, increment, decrement } = props;
9+
const Counter: any = (props: CounterProps) => {
10+
const {counter, increment, decrement} = props;
1111

1212
return (
1313
<div>

src/main/components/Hello.jsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = Object.setPrototypeOf ||
4+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6+
return function (d, b) {
7+
extendStatics(d, b);
8+
function __() { this.constructor = d; }
9+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10+
};
11+
})();
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
var React = require("react");
14+
var react_redux_1 = require("react-redux");
15+
var redux_1 = require("redux");
16+
var Counter_1 = require("./Counter");
17+
var actions_1 = require("../actions/actions");
18+
var Hello = (function (_super) {
19+
__extends(Hello, _super);
20+
function Hello(props) {
21+
var _this = _super.call(this, props) || this;
22+
_this.increment = _this.increment.bind(_this);
23+
_this.decrement = _this.decrement.bind(_this);
24+
return _this;
25+
}
26+
Hello.prototype.increment = function () {
27+
this.props.actions.incrementAction();
28+
};
29+
Hello.prototype.decrement = function () {
30+
this.props.actions.decrementAction();
31+
};
32+
Hello.prototype.render = function () {
33+
return <div>
34+
<h1>Hello typescript and react!</h1>
35+
<Counter_1.default counter={this.props.counter} decrement={this.decrement} increment={this.increment}/>
36+
</div>;
37+
};
38+
return Hello;
39+
}(React.Component));
40+
exports.Hello = Hello;
41+
exports.mapStateToProps = function (state) {
42+
return state.counters;
43+
};
44+
exports.mapDispatchToProps = function (dispatch) {
45+
return {
46+
actions: redux_1.bindActionCreators(actions_1.default, dispatch)
47+
};
48+
};
49+
exports.default = react_redux_1.connect(exports.mapStateToProps, exports.mapDispatchToProps)(Hello);

src/Hello/Hello.tsx renamed to src/main/components/Hello.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as React from "react";
33
import {connect, Dispatch} from "react-redux";
44
import {bindActionCreators} from "redux";
55

6-
import Counter from "./containers/Counter";
7-
import {IRootState} from "../rootReducer";
8-
import actions from "./actions";
6+
import Counter from "./Counter";
7+
import {IRootState} from "../../rootReducer";
8+
import actions from "../actions/actions";
99

1010
export interface HelloProps {
1111
counter: number,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var React = require("react");
4+
var Counter_1 = require("../Counter");
5+
var enzyme_1 = require("enzyme");
6+
describe("counter container", function () {
7+
var subject, mockIncrement, mockDecrement;
8+
var incrementButtonProps;
9+
var decrementButtonProps;
10+
beforeEach(function () {
11+
mockIncrement = jest.fn();
12+
mockDecrement = jest.fn();
13+
subject = enzyme_1.shallow(<Counter_1.default increment={mockIncrement} decrement={mockDecrement} counter={1}/>);
14+
incrementButtonProps = subject.find("#increment").props();
15+
decrementButtonProps = subject.find("#decrement").props();
16+
});
17+
it("it shows the counter", function () {
18+
expect(subject.text()).toContain("1");
19+
});
20+
it("clicking on the increment button call property increment", function () {
21+
incrementButtonProps.onClick();
22+
expect(mockIncrement).toBeCalled();
23+
});
24+
it("clicking on the decrement button call property decrement", function () {
25+
decrementButtonProps.onClick();
26+
expect(mockDecrement).toBeCalled();
27+
});
28+
});

src/Hello/containers/__test__/Counter.test.tsx renamed to src/main/components/__test__/Counter.test.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import * as React from "react";
2-
import {shallow, ShallowWrapper} from "enzyme";
3-
42
import Counter from "../Counter";
3+
import {EnzymePropSelector, shallow, ShallowWrapper} from "enzyme";
4+
import {Hello} from "../Hello";
55

66
describe("counter container", () => {
7-
let subject: ShallowWrapper<T, any>,
7+
let subject: ShallowWrapper<Hello, any>,
88
mockIncrement: () => {},
99
mockDecrement: () => {};
10+
11+
let incrementButtonProps: EnzymePropSelector;
12+
let decrementButtonProps: EnzymePropSelector;
13+
1014
beforeEach(() => {
1115
mockIncrement = jest.fn();
1216
mockDecrement = jest.fn();
1317
subject = shallow(<Counter increment={mockIncrement} decrement={mockDecrement} counter={1}/>);
18+
19+
incrementButtonProps = subject.find("#increment").props();
20+
decrementButtonProps = subject.find("#decrement").props();
21+
1422
});
1523

1624
it("it shows the counter", () => {
1725
expect(subject.text()).toContain("1");
1826
});
1927

2028
it("clicking on the increment button call property increment", () => {
21-
subject.find("#increment").props().onClick();
29+
incrementButtonProps.onClick();
2230

2331
expect(mockIncrement).toBeCalled();
2432
});
2533

2634
it("clicking on the decrement button call property decrement", () => {
27-
subject.find("#decrement").props().onClick();
35+
decrementButtonProps.onClick();
2836

2937
expect(mockDecrement).toBeCalled();
3038
});
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
jest.mock("../../actions/actions");
4+
var React = require("react");
5+
var enzyme_1 = require("enzyme");
6+
var Hello_1 = require("../Hello");
7+
var Counter_1 = require("../Counter");
8+
var actions_1 = require("../../actions/actions");
9+
describe("Hello Component", function () {
10+
var helloComponent;
11+
var props;
12+
var increment;
13+
var decrement;
14+
var counter;
15+
var counterProps;
16+
beforeEach(function () {
17+
increment = jest.fn();
18+
decrement = jest.fn();
19+
props = {
20+
counter: 2,
21+
actions: {
22+
incrementAction: increment,
23+
decrementAction: decrement
24+
}
25+
};
26+
helloComponent = enzyme_1.shallow(<Hello_1.Hello actions={props.actions} counter={props.counter}/>);
27+
counterProps = helloComponent.find(Counter_1.default).props();
28+
});
29+
it("says hello", function () {
30+
expect(helloComponent.find("h1").text()).toContain("Hello typescript and react!");
31+
});
32+
describe("Counter component", function () {
33+
it("shows the counter component", function () {
34+
expect(helloComponent.find(Counter_1.default).length).toEqual(1);
35+
});
36+
it("passes counter property", function () {
37+
expect(counterProps.counter).toEqual(2);
38+
});
39+
it("passes functions of increment the counter", function () {
40+
counterProps.increment();
41+
expect(increment).toBeCalled();
42+
});
43+
it("passes functions of decrement the counter", function () {
44+
counterProps.decrement();
45+
expect(decrement).toBeCalled();
46+
});
47+
});
48+
describe("mapStateToProps", function () {
49+
it("maps the counter from the state", function () {
50+
var counters = {
51+
counter: 1
52+
};
53+
var state = {
54+
counters: counters
55+
};
56+
var props = Hello_1.mapStateToProps(state);
57+
expect(props.counter).toBe(1);
58+
});
59+
});
60+
describe("mapDispatchToProps", function () {
61+
it("maps action functions to decrement the counter", function () {
62+
actions_1.decrementAction.mockReturnValue("you are going down, sorry!");
63+
var dispatch = jest.fn();
64+
var props = Hello_1.mapDispatchToProps(dispatch);
65+
props.actions.decrementAction();
66+
expect(actions_1.decrementAction).toBeCalled();
67+
expect(dispatch).toBeCalledWith("you are going down, sorry!");
68+
});
69+
it("maps action functions to increment the counter", function () {
70+
actions_1.incrementAction.mockReturnValue("you are going up, congratulations!");
71+
var dispatch = jest.fn();
72+
var props = Hello_1.mapDispatchToProps(dispatch);
73+
props.actions.incrementAction();
74+
expect(actions_1.incrementAction).toBeCalled();
75+
expect(dispatch).toBeCalledWith("you are going up, congratulations!");
76+
});
77+
});
78+
});

0 commit comments

Comments
 (0)