Skip to content

Commit 828c882

Browse files
committed
Use createStore() factory instead of singleton
The need for a factory function comes from our desire to use not-mocked production-like store in tests. Since each test may change store in unpredicted way, we will want to create a store instance for each test independently, hence this factory function. Otherwise, having globally available singleton instance may introduce unpredicted test failures.
1 parent c123122 commit 828c882

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { BrowserRouter } from 'react-router-dom';
44
import { Provider } from 'react-redux';
55

66
import App from './components/App';
7-
import store from './store';
7+
import createStore from './store';
8+
9+
const store = createStore();
810

911
ReactDOM.render(
1012
(

src/store/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import thunk from 'redux-thunk';
33

44
import reducer from '../reducers';
55

6-
// Redux Thunk middleware allows you to write action creators that return a
7-
// function instead of an action. The thunk can be used to delay the dispatch
8-
// of an action, or to dispatch only if a certain condition is met. The former
9-
// is the case for XSnippet Web since we need to fetch data via HTTP API first
10-
// and then dispatch it to store.
11-
export default redux.createStore(reducer, redux.applyMiddleware(thunk));
6+
function createStore() {
7+
// Redux Thunk middleware allows you to write action creators that return a
8+
// function instead of an action. The thunk can be used to delay the dispatch
9+
// of an action, or to dispatch only if a certain condition is met. The
10+
// former is the case for XSnippet Web since we need to fetch data via HTTP
11+
// API first and then dispatch it to store.
12+
return redux.createStore(reducer, redux.applyMiddleware(thunk));
13+
}
14+
15+
export default createStore;

0 commit comments

Comments
 (0)