Skip to content

Commit d77c5b7

Browse files
committed
feat(redux-saga): multiple things
- remove redux-promise-middleware - create makeRequest() instead - tests for Sagas
1 parent d99caaf commit d77c5b7

15 files changed

Lines changed: 98 additions & 28 deletions

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
"redux": "^3.6.0",
113113
"redux-connect": "^5.1.0",
114114
"redux-logger": "^3.0.0",
115-
"redux-promise-middleware": "^4.3.0",
116115
"redux-saga": "^0.15.3",
117116
"redux-thunk": "^2.2.0",
118117
"serve-favicon": "^2.3.2",

src/app/containers/Stars.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import {IStars, IStarsAction} from "../models/starsModel";
3-
import {STARS_LOAD} from "../redux/modules/starsModule";
3+
import {STARS_REQUEST} from "../redux/modules/starsModule";
44
const {connect} = require("react-redux");
55
const {asyncConnect} = require("redux-connect");
66

@@ -11,7 +11,7 @@ interface IProps {
1111

1212
@asyncConnect([{
1313
promise: ({store: {dispatch}}) => {
14-
return dispatch({type: STARS_LOAD});
14+
return dispatch({type: STARS_REQUEST});
1515
}
1616
}])
1717
@connect(

src/app/helpers/promiseReducer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import IBaseAction from "../models/IBaseAction";
22

33
export default function promiseReducer<TState, TAction>(baseAction: string, state: TState, action: IBaseAction & TAction): TState {
44
switch (action.type) {
5-
case baseAction + "_PENDING":
5+
case baseAction + "_REQUEST":
66
return Object.assign({}, state, {
77
isFetching: true
88
});
99

10-
case baseAction + "_FULFILLED":
10+
case baseAction + "_SUCCESS":
1111
return Object.assign({}, state, {
1212
isFetching: false,
1313
payload: action.payload
1414
});
1515

16-
case baseAction + "_REJECTED":
16+
case baseAction + "_FAILURE":
1717
return Object.assign({}, state, {
1818
error: true,
1919
isFetching: false,
20-
payload: action.payload
20+
message: action.message
2121
});
2222

2323
default:

src/app/models/IBaseAction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import {Action} from "redux";
22

33
interface IBaseAction extends Action {
44
payload?: any;
5+
message?: string;
56
}
67
export default IBaseAction;

src/app/models/starsModel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
export interface IStars {
22
isFetching?: boolean;
33
error?: boolean;
4+
message?: string;
45
payload?: {
56
stargazers_count: number;
67
};
78
}
89

910
export interface IStarsAction {
1011
type: string;
12+
message?: string;
1113
payload?: {
1214
stargazers_count: number;
1315
};

src/app/redux/configureStore.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const appConfig = require("../../../config/main");
33
import {routerMiddleware} from "react-router-redux";
44
import {applyMiddleware, compose, createStore} from "redux";
55
import {createLogger} from "redux-logger";
6-
import reduxPromiseMiddleware from "redux-promise-middleware";
76
import createSagaMiddleware from "redux-saga";
87
import thunk from "redux-thunk";
98
import rootSaga from "../sagas/rootSaga";
@@ -16,7 +15,6 @@ export function configureStore(history: History, initialState?: IStore): Redux.S
1615
const middlewares: Redux.Middleware[] = [
1716
routerMiddleware(history),
1817
thunk,
19-
reduxPromiseMiddleware(),
2018
sagaMiddleware
2119
];
2220

src/app/redux/modules/starsModule.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {STARS_REQUEST, starsReducer} from "./starsModule";
1+
import {STARS_FAILURE, STARS_REQUEST, STARS_SUCCESS, starsReducer} from "./starsModule";
22

33
describe("Stars Reducer", () => {
44

55
it("handles action of type STARS_REQUEST_PENDING", () => {
66
const action = {
7-
type: STARS_REQUEST + "_PENDING"
7+
type: STARS_REQUEST
88
};
99
const stateBefore = {};
1010
const stateAfter = {
@@ -18,7 +18,7 @@ describe("Stars Reducer", () => {
1818
payload: {
1919
stargazers_count: 99
2020
},
21-
type: STARS_REQUEST + "_FULFILLED"
21+
type: STARS_SUCCESS
2222
};
2323
const stateBefore = {};
2424
const stateAfter = {
@@ -32,7 +32,7 @@ describe("Stars Reducer", () => {
3232

3333
it("handles action of type STARS_REQUEST_REJECTED", () => {
3434
const action = {
35-
type: STARS_REQUEST + "_REJECTED"
35+
type: STARS_FAILURE
3636
};
3737
const stateBefore = {};
3838
const stateAfter = {

src/app/redux/modules/starsModule.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import {IStars, IStarsAction} from "models/starsModel";
22
import promiseReducer from "../../helpers/promiseReducer";
33

44
/** Action Types */
5-
export const STARS_LOAD: string = "stars/STARS_LOAD";
65
export const STARS_REQUEST: string = "stars/STARS_REQUEST";
6+
export const STARS_SUCCESS: string = "stars/STARS_SUCCESS";
7+
export const STARS_FAILURE: string = "stars/STARS_FAILURE";
78

89
/** Initial State */
910
const initialState: IStars = {
@@ -12,5 +13,5 @@ const initialState: IStars = {
1213

1314
/** Reducer */
1415
export function starsReducer(state: IStars = initialState, action: IStarsAction): IStars {
15-
return promiseReducer<IStars, IStarsAction>(STARS_REQUEST, state, action);
16+
return promiseReducer<IStars, IStarsAction>("stars/STARS", state, action);
1617
}

src/app/sagas/TestApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {Promise} from "es6-promise";
12
import {IStars} from "../models/starsModel";
23

34
export default class TestApi {

src/app/sagas/makeRequest.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Promise} from "es6-promise";
2+
import {call, put} from "redux-saga/effects";
3+
import makeRequest from "./makeRequest";
4+
5+
describe("makeRequest", () => {
6+
const promiseFunction = () => Promise.resolve("success!");
7+
const gen = makeRequest(promiseFunction, "SUCCESS_ACTION", "FAILURE_ACTION");
8+
9+
it("must call apiMethod", () => {
10+
expect(gen.next().value).toEqual(call(promiseFunction));
11+
});
12+
13+
it("must dispatch actionSuccess if promise is resolved", () => {
14+
expect(gen.next("data").value).toEqual(put({type: "SUCCESS_ACTION", payload: "data"}));
15+
});
16+
17+
it("must dispatch actionFailure if promise is rejected", () => {
18+
expect(gen.throw({message: "error!"}).value).toEqual(put({type: "FAILURE_ACTION", message: "error!"}));
19+
});
20+
21+
it("must be done", () => {
22+
expect(gen.next()).toEqual({done: true, value: undefined});
23+
});
24+
});

0 commit comments

Comments
 (0)