diff --git a/src/bootstrap.js b/src/bootstrap.js index 9ef96bf..bd0283d 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -5,12 +5,14 @@ import configureStore from './configureStore' export const BOOT = 'redux-boot/BOOT' export default function boot(initialState = {}, modules = []) { - const {reducers, middlewares, enhancers} = processModules(modules) + const {reducers, middlewares, enhancers, initialState: modulesInitialState} = processModules(modules) - let store = configureStore(initialState, reducers, middlewares, enhancers) + const state = {...initialState, ...modulesInitialState} + + let store = configureStore(state, reducers, middlewares, enhancers) return store - .dispatch(bootAction(initialState)) + .dispatch(bootAction(state)) .then(action => { return { action, diff --git a/src/processModules.js b/src/processModules.js index f213284..b576fd5 100644 --- a/src/processModules.js +++ b/src/processModules.js @@ -23,10 +23,15 @@ export default function processModules(modules) { )) .map(module => module.enhancer) + const initialState = modules + .map(module => module.initialState || (f => f)) + .reduce((state, initialState) => initialState(state), {}) + return { reducers, middlewares, - enhancers + enhancers, + initialState, } } diff --git a/test/bootstrap.test.js b/test/bootstrap.test.js index 5c42efc..bd54a53 100644 --- a/test/bootstrap.test.js +++ b/test/bootstrap.test.js @@ -44,6 +44,24 @@ test('Boostrap new app with an initial state', assert => { }) }) +test('Boostrap new app with initial state on modules', assert => { + const initialState = { foo: 'bar' } + const testModule = { initialState: (state) => initialState } + + const app = boot(null, [testModule]) + + app.then(({action, store}) => { + + assert.deepEqual( + store.getState(), + initialState, + 'State is equal to module\'s initial state' + ) + + assert.end() + }) +}) + test('Boostrap new app with a module implementing a reducer', assert => { const initialState = { foo: 'bar' @@ -212,4 +230,4 @@ test('Boostrap new app with a module implementing an enhancer', assert => { assert.end() }) -}) \ No newline at end of file +})