Skip to content

Commit

Permalink
Added module initial state.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasconstantino committed Aug 30, 2016
1 parent 014a636 commit 602fbf4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import configureStore from './configureStore'

export const BOOT = 'redux-boot/BOOT'

export default function boot(initialState = {}, modules = []) {
const {reducers, middlewares, enhancers} = processModules(modules)
export default function boot(state = {}, modules = []) {
const {reducers, middlewares, enhancers, initialState} = processModules(modules)

let store = configureStore(initialState, reducers, middlewares, enhancers)
state = {...initialState, ...state}

let store = configureStore(state, reducers, middlewares, enhancers)

return store
.dispatch(bootAction(initialState))
.dispatch(bootAction(state))
.then(action => {
return {
action,
Expand Down
7 changes: 6 additions & 1 deletion src/processModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
5 changes: 3 additions & 2 deletions test/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ test('Boostrap new app with an initial state', assert => {
})

test('Boostrap new app with initial state on modules', assert => {
const testModule = { initialState: { foo: 'bar' } }
const initialState = { foo: 'bar' }
const testModule = { initialState: (state) => initialState }

const app = boot(null, [testModule])

app.then(({action, store}) => {

assert.deepEqual(
store.getState(),
testModule.initialState,
initialState,
'State is equal to module\'s initial state'
)

Expand Down

0 comments on commit 602fbf4

Please sign in to comment.