From 014a636be3597576c22137536ded473fb3d134e4 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Sun, 17 Jul 2016 14:13:28 -0300 Subject: [PATCH 1/3] Added module initial state tests. --- test/bootstrap.test.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/bootstrap.test.js b/test/bootstrap.test.js index 5c42efc..da3a5b1 100644 --- a/test/bootstrap.test.js +++ b/test/bootstrap.test.js @@ -44,6 +44,23 @@ 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 app = boot(null, [testModule]) + + app.then(({action, store}) => { + + assert.deepEqual( + store.getState(), + testModule.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 +229,4 @@ test('Boostrap new app with a module implementing an enhancer', assert => { assert.end() }) -}) \ No newline at end of file +}) From 602fbf4d5f7bfcc431f42de6c6c948ce2a075bc1 Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Mon, 29 Aug 2016 22:28:32 -0300 Subject: [PATCH 2/3] Added module initial state. --- src/bootstrap.js | 10 ++++++---- src/processModules.js | 7 ++++++- test/bootstrap.test.js | 5 +++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index 9ef96bf..097d372 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -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, 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 da3a5b1..bd54a53 100644 --- a/test/bootstrap.test.js +++ b/test/bootstrap.test.js @@ -45,7 +45,8 @@ 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]) @@ -53,7 +54,7 @@ test('Boostrap new app with initial state on modules', assert => { assert.deepEqual( store.getState(), - testModule.initialState, + initialState, 'State is equal to module\'s initial state' ) From 2ff5f771be72c2f90647f38e70bc00236ebdf2dc Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 3 Sep 2016 15:18:14 -0300 Subject: [PATCH 3/3] Removed mutation of the state passed as reference. --- src/bootstrap.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bootstrap.js b/src/bootstrap.js index 097d372..bd0283d 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -4,10 +4,10 @@ import configureStore from './configureStore' export const BOOT = 'redux-boot/BOOT' -export default function boot(state = {}, modules = []) { - const {reducers, middlewares, enhancers, initialState} = processModules(modules) +export default function boot(initialState = {}, modules = []) { + const {reducers, middlewares, enhancers, initialState: modulesInitialState} = processModules(modules) - state = {...initialState, ...state} + const state = {...initialState, ...modulesInitialState} let store = configureStore(state, reducers, middlewares, enhancers)