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

Commit c8c9573

Browse files
committed
Added redux without tests
1 parent 2159779 commit c8c9573

File tree

15 files changed

+209
-31
lines changed

15 files changed

+209
-31
lines changed

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"devDependencies": {
2020
"@types/enzyme": "^2.7.7",
2121
"@types/jasmine": "^2.5.47",
22+
"@types/react": "^15.0.21",
23+
"@types/react-dom": "^15.5.0",
24+
"@types/react-redux": "^4.4.38",
25+
"@types/redux": "3.6.0",
26+
"@types/redux-actions": "^1.2.3",
27+
"@types/redux-logger": "^3.0.0",
2228
"awesome-typescript-loader": "^3.1.2",
2329
"cross-env": "4.0.0",
2430
"enzyme": "^2.8.0",
@@ -31,18 +37,21 @@
3137
"karma-typescript-preprocessor": "^0.3.1",
3238
"react-addons-test-utils": "^15.5.1",
3339
"react-hot-loader": "1.3.1",
40+
"redux-actions": "2.0.2",
3441
"source-map-loader": "^0.2.1",
3542
"stylelint-webpack-plugin": "0.7.0",
36-
"typescript": "^2.2.2",
43+
"typescript": "^2.3.0",
3744
"webpack-dev-server": "^2.4.2",
3845
"webpack-merge": "4.1.0",
3946
"write-file-webpack-plugin": "4.0.2"
4047
},
4148
"dependencies": {
42-
"@types/react": "^15.0.21",
43-
"@types/react-dom": "^0.14.23",
4449
"react": "^15.5.3",
4550
"react-dom": "^15.5.3",
51+
"react-redux": "5.0.4",
52+
"redux": "3.6.0",
53+
"redux-logger": "3.0.1",
54+
"typemoq": "1.4.1",
4655
"webpack": "^2.3.3"
4756
},
4857
"jest": {

preprocessor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const tsc = require('typescript');
2-
const tsConfig = require('./tsconfig.json');
2+
const tsConfig = require('./testUtils/tsconfig.json');
33

44
module.exports = {
55
process(src, path) {

src/Hello/Hello.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from "react";
2+
3+
import {connect, Dispatch} from "react-redux";
4+
import {bindActionCreators} from "redux";
5+
6+
import Counter from "./containers/Counter";
7+
import {IRootState} from "../rootReducer";
8+
import actions from "./actions";
9+
10+
export interface HelloProps {
11+
counter: number,
12+
actions: typeof actions
13+
}
14+
15+
export class Hello extends React.Component<HelloProps, void> {
16+
constructor(props: HelloProps) {
17+
super(props);
18+
19+
this.increment = this.increment.bind(this);
20+
this.decrement = this.decrement.bind(this);
21+
}
22+
23+
increment() {
24+
this.props.actions.incrementAction();
25+
}
26+
27+
decrement() {
28+
this.props.actions.decrementAction();
29+
}
30+
31+
render() {
32+
return <div>
33+
<h1>Hello typescript and react!</h1>
34+
<Counter
35+
counter={this.props.counter}
36+
decrement={this.decrement}
37+
increment={this.increment}
38+
/>
39+
</div>;
40+
}
41+
}
42+
43+
export const mapStateToProps = (state: IRootState) => {
44+
return state.counters;
45+
};
46+
47+
export const mapDispatchToProps = (dispatch: Dispatch<{}>) => {
48+
return {
49+
actions: bindActionCreators(actions, dispatch)
50+
}
51+
};
52+
53+
export default connect<any, any, any>(mapStateToProps, mapDispatchToProps)(Hello)

src/Hello/__test__/hello.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from "react";
2+
// import * as TypeMoq from "typemoq";
3+
4+
import {shallow, ShallowWrapper} from "enzyme";
5+
import {Hello, HelloProps} from "../Hello";
6+
7+
describe("Hello Component", () => {
8+
let helloComponent: ShallowWrapper<Hello, any>;
9+
// let props: TypeMoq.IMock<HelloProps> = TypeMoq.Mock.ofType<HelloProps>();
10+
11+
beforeEach(() => {
12+
// helloComponent = shallow(<Hello actions={props.actions} counter={props.counter} />);
13+
});
14+
15+
it("says hello", () => {
16+
// expect(helloComponent.find("h1").text()).toContain("Hello typescript and react!");
17+
});
18+
});

src/Hello/actions.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createAction} from "redux-actions";
2+
3+
export enum actionTypes {
4+
INCREMENT,
5+
DECREMENT
6+
}
7+
8+
const incrementAction = createAction<void>(
9+
actionTypes.INCREMENT.toString(),
10+
() => {
11+
}
12+
);
13+
14+
const decrementAction = createAction<void>(
15+
actionTypes.INCREMENT.toString(),
16+
() => {
17+
}
18+
);
19+
20+
export default {
21+
incrementAction,
22+
decrementAction
23+
}

src/Hello/containers/Counter.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react';
2+
import DefaultState from "../interfaces/defaultState"
3+
4+
export interface IProps extends DefaultState {
5+
increment(): void;
6+
decrement(): void;
7+
}
8+
9+
const Counter = (props: IProps) => {
10+
const { counter, increment, decrement } = props;
11+
12+
return (
13+
<div>
14+
<p>Counter: {counter}</p>
15+
<button onClick={increment} label="Increment" />
16+
<button onClick={decrement} label="Decrement" />
17+
</div>
18+
);
19+
};
20+
21+
export default Counter;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface DefaultState {
2+
readonly counter: number
3+
}
4+
5+
export default DefaultState;

src/Hello/reducer.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { handleActions, Action } from "redux-actions";
2+
import { actionTypes } from "./actions";
3+
import DefaultState from "./interfaces/defaultState";
4+
5+
const initialState: DefaultState = {
6+
counter: 0
7+
};
8+
9+
export default handleActions<DefaultState, void>({
10+
[actionTypes.INCREMENT.toString()]: (state: DefaultState, action: Action<void>): DefaultState => {
11+
return {
12+
counter: state.counter + 1
13+
};
14+
},
15+
[actionTypes.DECREMENT.toString()]: (state: DefaultState, action: Action<void>): DefaultState => {
16+
return {
17+
counter: state.counter - 1
18+
};
19+
},
20+
}, initialState);

src/configureStore.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Store, createStore, applyMiddleware } from 'redux';
2+
import logger from 'redux-logger';
3+
import reducer from './rootReducer';
4+
5+
const store = (): Store<any> => {
6+
return createStore(
7+
reducer,
8+
applyMiddleware(logger)
9+
);
10+
};
11+
12+
export default store;

src/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as React from "react";
22
import * as ReactDOM from "react-dom";
3+
import { Provider } from 'react-redux';
34

4-
import { Hello } from "./main/hello";
5+
import configureStore from "./configureStore";
6+
import Hello from "./Hello/Hello";
57

8+
const store = configureStore();
69
ReactDOM.render(
7-
<Hello/>,
10+
<Provider store={store}>
11+
<Hello/>
12+
</Provider>,
813
document.getElementById("hello")
914
);

0 commit comments

Comments
 (0)