Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cli-react-redux",
"version": "1.2.0",
"version": "1.2.1",
"description": "React Redux CLI",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/templates/App
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Provider } from 'react-redux';

import Navigator from './Navigator';
import getStore from './store';
import { getStore } from './store';

const App = () => (
<Provider store={getStore()}>
Expand Down
4 changes: 0 additions & 4 deletions src/templates/App.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import App from '../App';
// ./node_modules/.bin/jest ./src/__tests__/App.test.js --env=jsdom
describe('<App />', () => {
it('renders without crashing', () => {
window.fetch = () => ({
ok: true,
json: () => ({ foo: 'bar' }),
});
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
Expand Down
4 changes: 2 additions & 2 deletions src/templates/Navigator
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import React from 'react';
import { ConnectedRouter } from 'react-router-redux';
import { BrowserRouter, Switch, Route } from 'react-router-dom';

import { history } from './store';
import { getHistory } from './store';
import '<NameOf>' from './containers/'<nameof>'';

const Navigator = () => (
<ConnectedRouter history={history}>
<ConnectedRouter history={getHistory()}>
<BrowserRouter>
<Switch>
<Route exact path="/" component={'<NameOf>'} />
Expand Down
2 changes: 1 addition & 1 deletion src/templates/reducer
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const initialState = {
const '<nameOf>'Reducer = (state = initialState, action) => {
switch (action.type) {
case actionType.FETCH_DATA_SUCCESS: {
const data = action.payload.results;
const { data } = action.payload.results;
return {
...state,
data,
Expand Down
4 changes: 2 additions & 2 deletions src/templates/reducer.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('<'<NameOf>' /> reducer', () => {
});

it('should handle the fetchDataSuccess action correctly', () => {
const fixture = { foo: 'bar' };
const fixture = { data: { foo: 'bar' } };
const expectedResult = {
data: fixture,
data: fixture.data,
};

expect('<nameOf>'Reducer(state, {
Expand Down
1 change: 1 addition & 0 deletions src/templates/sagas
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { put, call, takeEvery } from 'redux-saga/effects';
import axios from 'axios';
import * as actionType from './actionTypes';

export function* fetchDataAsync() {
Expand Down
1 change: 1 addition & 0 deletions src/templates/sagas.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { call, put, takeEvery } from 'redux-saga/effects';
import axios from 'axios';
import { fetchDataAsync, watch'<NameOf>'Sagas } from '../sagas';
import * as actionType from '../actionTypes';

Expand Down
69 changes: 42 additions & 27 deletions src/templates/store
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@ import createHistory from 'history/createBrowserHistory'; // eslint-disable-line
import rootReducer from './rootReducer';
import rootSaga from './rootSaga';

export const history = createHistory();

const initialState = {};
const enhancers = [];
const sagaMiddleware = createSagaMiddleware();
const middleware = [
sagaMiddleware,
routerMiddleware(history),
];

if (process.env.NODE_ENV === 'development') {
const { devToolsExtension } = window;
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension());
export const storeElements = {
history: undefined,
store: undefined,
};

export default function configureStore() {
storeElements.history = createHistory();

const initialState = {};
const enhancers = [];
const sagaMiddleware = createSagaMiddleware();
const middleware = [
sagaMiddleware,
routerMiddleware(storeElements.history),
];

if (process.env.NODE_ENV === 'development') {
const { devToolsExtension } = window;
if (typeof devToolsExtension === 'function') {
enhancers.push(devToolsExtension());
}
}
}

const composedEnhancers = compose(
applyMiddleware(...middleware),
...enhancers,
);
const composedEnhancers = compose(
applyMiddleware(...middleware),
...enhancers,
);

storeElements.store = createStore(
rootReducer(),
initialState,
composedEnhancers,
);

const store = createStore(
rootReducer(),
initialState,
composedEnhancers,
);
sagaMiddleware.run(rootSaga);
}

export function getStore() {
if(!storeElements.store) {
configureStore();
}
return storeElements.store;
}

sagaMiddleware.run(rootSaga);
export default function getStore() {
return store;
export function getHistory() {
return storeElements.history;
}
30 changes: 30 additions & 0 deletions src/templates/store.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import ReactDOM from 'react-dom';
import config from '../config';
import configureStore, { getStore } from '../store';

// yarn test ./src/__tests__/store.test.js --coverage
describe('getStore', () => {
it('returns store', () => {
configureStore();
const store = getStore();
expect(typeof store.dispatch).toEqual('function');
expect(typeof store.subscribe).toEqual('function');
expect(typeof store.getState).toEqual('function');
});

it('adds devToolsExtension to enhanchers if NODE_ENV is development', () => {
// process.env.NODE_ENV = 'development';
// Object.defineProperty(window, 'devToolsExtension', {
// writable: true,
// value: jest.fn(),
// });
// configureStore();
// const devToolsExtension = jest.fn()
// window.devToolsExtension = devToolsExtension;
// getStore();

// expect(window.devToolsExtension).toHaveBeenCalled();
// process.env.NODE_ENV = env;
});
});