Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial state functionality to modules. #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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
20 changes: 19 additions & 1 deletion test/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -212,4 +230,4 @@ test('Boostrap new app with a module implementing an enhancer', assert => {

assert.end()
})
})
})