Skip to content

Commit 0ccdf0a

Browse files
Feature/store test (#10)
* refactoring store * store itself refactor * bugfix for App and store * 1.2.1
1 parent e75539d commit 0ccdf0a

File tree

11 files changed

+82
-39
lines changed

11 files changed

+82
-39
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cli-react-redux",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "React Redux CLI",
55
"main": "index.js",
66
"scripts": {

src/templates/App

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { Provider } from 'react-redux';
33

44
import Navigator from './Navigator';
5-
import getStore from './store';
5+
import { getStore } from './store';
66

77
const App = () => (
88
<Provider store={getStore()}>

src/templates/App.test

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import App from '../App';
55
// ./node_modules/.bin/jest ./src/__tests__/App.test.js --env=jsdom
66
describe('<App />', () => {
77
it('renders without crashing', () => {
8-
window.fetch = () => ({
9-
ok: true,
10-
json: () => ({ foo: 'bar' }),
11-
});
128
const div = document.createElement('div');
139
ReactDOM.render(<App />, div);
1410
});

src/templates/Navigator

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import React from 'react';
33
import { ConnectedRouter } from 'react-router-redux';
44
import { BrowserRouter, Switch, Route } from 'react-router-dom';
55

6-
import { history } from './store';
6+
import { getHistory } from './store';
77
import '<NameOf>' from './containers/'<nameof>'';
88

99
const Navigator = () => (
10-
<ConnectedRouter history={history}>
10+
<ConnectedRouter history={getHistory()}>
1111
<BrowserRouter>
1212
<Switch>
1313
<Route exact path="/" component={'<NameOf>'} />

src/templates/reducer

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const initialState = {
77
const '<nameOf>'Reducer = (state = initialState, action) => {
88
switch (action.type) {
99
case actionType.FETCH_DATA_SUCCESS: {
10-
const data = action.payload.results;
10+
const { data } = action.payload.results;
1111
return {
1212
...state,
1313
data,

src/templates/reducer.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ describe('<'<NameOf>' /> reducer', () => {
1717
});
1818

1919
it('should handle the fetchDataSuccess action correctly', () => {
20-
const fixture = { foo: 'bar' };
20+
const fixture = { data: { foo: 'bar' } };
2121
const expectedResult = {
22-
data: fixture,
22+
data: fixture.data,
2323
};
2424

2525
expect('<nameOf>'Reducer(state, {

src/templates/sagas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { put, call, takeEvery } from 'redux-saga/effects';
2+
import axios from 'axios';
23
import * as actionType from './actionTypes';
34

45
export function* fetchDataAsync() {

src/templates/sagas.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { call, put, takeEvery } from 'redux-saga/effects';
2+
import axios from 'axios';
23
import { fetchDataAsync, watch'<NameOf>'Sagas } from '../sagas';
34
import * as actionType from '../actionTypes';
45

src/templates/store

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,50 @@ import createHistory from 'history/createBrowserHistory'; // eslint-disable-line
55
import rootReducer from './rootReducer';
66
import rootSaga from './rootSaga';
77

8-
export const history = createHistory();
9-
10-
const initialState = {};
11-
const enhancers = [];
12-
const sagaMiddleware = createSagaMiddleware();
13-
const middleware = [
14-
sagaMiddleware,
15-
routerMiddleware(history),
16-
];
17-
18-
if (process.env.NODE_ENV === 'development') {
19-
const { devToolsExtension } = window;
20-
if (typeof devToolsExtension === 'function') {
21-
enhancers.push(devToolsExtension());
8+
export const storeElements = {
9+
history: undefined,
10+
store: undefined,
11+
};
12+
13+
export default function configureStore() {
14+
storeElements.history = createHistory();
15+
16+
const initialState = {};
17+
const enhancers = [];
18+
const sagaMiddleware = createSagaMiddleware();
19+
const middleware = [
20+
sagaMiddleware,
21+
routerMiddleware(storeElements.history),
22+
];
23+
24+
if (process.env.NODE_ENV === 'development') {
25+
const { devToolsExtension } = window;
26+
if (typeof devToolsExtension === 'function') {
27+
enhancers.push(devToolsExtension());
28+
}
2229
}
23-
}
2430

25-
const composedEnhancers = compose(
26-
applyMiddleware(...middleware),
27-
...enhancers,
28-
);
31+
const composedEnhancers = compose(
32+
applyMiddleware(...middleware),
33+
...enhancers,
34+
);
35+
36+
storeElements.store = createStore(
37+
rootReducer(),
38+
initialState,
39+
composedEnhancers,
40+
);
2941

30-
const store = createStore(
31-
rootReducer(),
32-
initialState,
33-
composedEnhancers,
34-
);
42+
sagaMiddleware.run(rootSaga);
43+
}
44+
45+
export function getStore() {
46+
if(!storeElements.store) {
47+
configureStore();
48+
}
49+
return storeElements.store;
50+
}
3551

36-
sagaMiddleware.run(rootSaga);
37-
export default function getStore() {
38-
return store;
52+
export function getHistory() {
53+
return storeElements.history;
3954
}

0 commit comments

Comments
 (0)