Commit 31d4ef2e authored by Jure's avatar Jure

Merge branch 'feat/configurable-store' into 'master'

make store configurable

Closes #39

See merge request !111
parents 856b857b 04375fed
Pipeline #2630 canceled with stage
image: node:7
image: pubsweet/pubsweet-test-base
before_script:
- yarn
......
......@@ -11,12 +11,13 @@ require('../components/reducers').forEach(
componentReducers => Object.assign(reducers, componentReducers)
)
function configureStore (env) {
return function configureStore (history, initialState) {
function createConfigureStore (env) {
return function configureStore (history, initialState = {}, customReducers = {}, customMiddlewares = []) {
const reducer = combineReducers({
...reducers,
form: formReducer,
routing: routerReducer
routing: routerReducer,
...customReducers
})
let logger
......@@ -27,7 +28,8 @@ function configureStore (env) {
const middleware = [
applyMiddleware(thunk),
logger,
applyMiddleware(routerMiddleware(history))
applyMiddleware(routerMiddleware(history)),
...customMiddlewares.map(mw => applyMiddleware(mw))
].filter(value => value)
// https://github.com/zalmoxisus/redux-devtools-extension#12-advanced-store-setup
......@@ -52,7 +54,7 @@ function configureStore (env) {
}
if (process.env.NODE_ENV === 'production') {
module.exports = configureStore('production')
module.exports = createConfigureStore('production')
} else {
module.exports = configureStore('development')
module.exports = createConfigureStore('development')
}
describe('configureStore', () => {
afterEach(() => jest.resetModules())
it('allows initialization of store with custom initialState, reducers and middleware', () => {
global.PUBSWEET_COMPONENTS = []
const configureStore = require('../../src/store/configureStore')
const customHistory = {}
const customInitialState = { 'collections': ['initial'] }
const customReducers = {
test: (state, action) => {
return {
...state,
fired: action.fired
}
}
}
const customMiddlewares = [
() => next => action => {
action.fired = true
return next(action)
}
]
const store = configureStore(customHistory, customInitialState, customReducers, customMiddlewares)
expect(store.getState().collections[0]).toBe(customInitialState.collections[0])
store.dispatch({ type: 'TEST', fired: false })
expect(store.getState().test.fired).toBe(true)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment