Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 1157200

Browse files
authored
add redux-devtools-extension to get rid of boilerplate types and… (#35)
add redux-devtools-extension to get rid of boilerplate types and any
2 parents 83cfd52 + 99b2c31 commit 1157200

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

app/configureStore.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,45 @@
22
* Create the store with dynamic reducers
33
*/
44

5-
import { applyMiddleware, compose, createStore } from 'redux';
5+
import { applyMiddleware, createStore } from 'redux';
66
import { routerMiddleware } from 'connected-react-router';
77
import createSagaMiddleware from 'redux-saga';
88
import createReducer from './reducers';
99
import { InjectedStore } from 'types';
1010
import { History } from 'history';
1111
import { RootState } from './containers/App/types';
12-
13-
declare interface IWindow extends Window {
14-
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any; // redux-dev-tools definitions not needed
15-
}
16-
17-
declare const window: IWindow;
12+
import { composeWithDevTools } from 'redux-devtools-extension';
1813

1914
export default function configureStore(initialState: RootState | {} = {}, history: History) {
20-
let composeEnhancers = compose;
2115
const reduxSagaMonitorOptions = {};
16+
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
17+
18+
const middlewares = [sagaMiddleware, routerMiddleware(history)];
19+
20+
let enhancer = applyMiddleware(...middlewares);
2221

2322
// If Redux Dev Tools and Saga Dev Tools Extensions are installed, enable them
2423
/* istanbul ignore next */
2524
if (process.env.NODE_ENV !== 'production' && typeof window === 'object') {
26-
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
27-
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
28-
}
29-
30-
// NOTE: Uncomment the code below to restore support for Redux Saga
31-
// Dev Tools once it supports redux-saga version 1.x.x
32-
// if (window.__SAGA_MONITOR_EXTENSION__)
33-
// reduxSagaMonitorOptions = {
34-
// sagaMonitor: window.__SAGA_MONITOR_EXTENSION__,
35-
// };
25+
enhancer = composeWithDevTools(enhancer)
3626
}
3727

38-
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
28+
// NOTE: Uncomment the code below to restore support for Redux Saga
29+
// Dev Tools once it supports redux-saga version 1.x.x
30+
// if (window.__SAGA_MONITOR_EXTENSION__)
31+
// reduxSagaMonitorOptions = {
32+
// sagaMonitor: window.__SAGA_MONITOR_EXTENSION__,
33+
// };
34+
3935

4036
// Create the store with two middlewares
4137
// 1. sagaMiddleware: Makes redux-sagas work
4238
// 2. routerMiddleware: Syncs the location/URL path to the state
43-
const middlewares = [sagaMiddleware, routerMiddleware(history)];
44-
45-
const enhancers = [applyMiddleware(...middlewares)];
4639

4740
const store = createStore(
4841
createReducer(),
4942
initialState,
50-
composeEnhancers(...enhancers),
43+
enhancer,
5144
) as InjectedStore;
5245

5346
// Extensions
@@ -65,3 +58,4 @@ export default function configureStore(initialState: RootState | {} = {}, histor
6558

6659
return store;
6760
}
61+

app/tests/store.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* Test store addons
33
*/
44
import history from '../utils/history';
5-
65
import configureStore from '../configureStore';
76
import { InjectedStore } from '../types';
7+
import { composeWithDevTools } from 'redux-devtools-extension';
88

99
describe('configureStore', () => {
1010
let store: InjectedStore;
@@ -32,11 +32,14 @@ describe('configureStore', () => {
3232
});
3333
});
3434

35+
36+
jest.mock('redux-devtools-extension', () => ({
37+
composeWithDevTools: jest.fn(),
38+
}));
39+
3540
describe('configureStore params', () => {
36-
it('should call window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__', () => {
37-
const compose = jest.fn();
38-
(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = () => compose;
41+
it('should call composeWithDevTools', () => {
3942
configureStore(undefined, history);
40-
expect(compose).toHaveBeenCalled();
43+
expect(composeWithDevTools).toHaveBeenCalled();
4144
});
4245
});

internals/templates/configureStore.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Create the store with dynamic reducers
33
*/
44

5-
import { applyMiddleware, compose, createStore } from 'redux';
5+
import { composeWithDevTools } from 'redux-devtools-extension';
6+
import { applyMiddleware, createStore } from 'redux';
67
import { routerMiddleware } from 'connected-react-router';
78
import createSagaMiddleware from 'redux-saga';
89
import createReducer from './reducers';
@@ -11,20 +12,18 @@ import { History } from 'history';
1112
import { RootState } from '../../app/containers/App/types';
1213

1314

14-
declare interface IWindow extends Window {
15-
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any; // redux-dev-tools definitions not needed
16-
}
17-
declare const window: IWindow;
18-
1915
export default function configureStore(initialState: RootState | {} = {}, history: History) {
20-
let composeEnhancers = compose;
2116
const reduxSagaMonitorOptions = {};
17+
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
18+
19+
const middlewares = [sagaMiddleware, routerMiddleware(history)];
20+
21+
let enhancer = applyMiddleware(...middlewares);
2222

2323
// If Redux Dev Tools and Saga Dev Tools Extensions are installed, enable them
2424
/* istanbul ignore next */
2525
if (process.env.NODE_ENV !== 'production' && typeof window === 'object') {
26-
if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
27-
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
26+
enhancer = composeWithDevTools(enhancer)
2827
}
2928

3029
// NOTE: Uncomment the code below to restore support for Redux Saga
@@ -33,21 +32,20 @@ export default function configureStore(initialState: RootState | {} = {}, histor
3332
// reduxSagaMonitorOptions = {
3433
// sagaMonitor: window.__SAGA_MONITOR_EXTENSION__,
3534
// };
36-
}
3735

38-
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
36+
37+
3938

4039
// Create the store with two middlewares
4140
// 1. sagaMiddleware: Makes redux-sagas work
4241
// 2. routerMiddleware: Syncs the location/URL path to the state
43-
const middlewares = [sagaMiddleware, routerMiddleware(history)];
4442

45-
const enhancers = [applyMiddleware(...middlewares)];
43+
4644

4745
const store = createStore(
4846
createReducer(),
4947
initialState,
50-
composeEnhancers(...enhancers),
48+
enhancer,
5149
) as InjectedStore;
5250

5351
// Extensions

internals/templates/tests/store.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
* Test store addons
33
*/
44

5-
// import { BrowserRouter } from 'react-router-dom';
65
import configureStore from '../configureStore';
76
import history from '../utils/history';
87
import { InjectedStore } from '../../../app/types';
8+
import { composeWithDevTools } from 'redux-devtools-extension';
99

1010
describe('configureStore', () => {
1111
let store: InjectedStore;
@@ -33,11 +33,13 @@ describe('configureStore', () => {
3333
});
3434
});
3535

36+
jest.mock('redux-devtools-extension', () => ({
37+
composeWithDevTools: jest.fn(),
38+
}));
39+
3640
describe('configureStore params', () => {
37-
it('should call window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__', () => {
38-
const compose = jest.fn();
39-
(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ = () => compose;
41+
it('should call composeWithDevTools', () => {
4042
configureStore(undefined, history);
41-
expect(compose).toHaveBeenCalled();
43+
expect(composeWithDevTools).toHaveBeenCalled();
4244
});
4345
});

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@
7171
"babel-core": "7.0.0-bridge.0"
7272
},
7373
"dependencies": {
74-
"@types/history": "^4.7.3",
75-
"@types/react-router-dom": "^5.1.0",
76-
"@types/react-test-renderer": "^16.9.0",
7774
"chalk": "2.4.2",
7875
"compression": "1.7.4",
7976
"connected-react-router": "6.5.0",
@@ -101,6 +98,10 @@
10198
"typesafe-actions": "4.4.0"
10299
},
103100
"devDependencies": {
101+
"redux-devtools-extension": "^2.13.8",
102+
"@types/history": "^4.7.3",
103+
"@types/react-router-dom": "^5.1.0",
104+
"@types/react-test-renderer": "^16.9.0",
104105
"@babel/cli": "7.5.0",
105106
"@babel/core": "7.5.4",
106107
"@babel/plugin-proposal-class-properties": "7.5.0",

0 commit comments

Comments
 (0)