Skip to content
This repository was archived by the owner on Mar 11, 2024. It is now read-only.
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
9 changes: 6 additions & 3 deletions packages/store/src/generateStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const composeSagas = sagas =>
* @param {object[]} config.appSagas=[] - application sagas to be managed by drizzle's saga middleware
* @param {object[]} config.appMiddlewares=[] - application middlewares to be managed by drizzle's saga middleware
* @param {boolean} config.disableReduxDevTools=false - disable redux devtools hook
* @param {object} config.initialAppState={} - application initial state to include in drizzle's redux store
* @returns {object} Redux store
*
*/
Expand All @@ -30,14 +31,15 @@ export function generateStore ({
appSagas = [],
appMiddlewares = [],
disableReduxDevTools = false,
initialAppState = {},
...options
}) {
// Note: Preserve backwards compatibility for passing options to
// `generateStore`. in drizzle v1.3.3 and prior of generate had a signature
// of `generateStore(options)`.
//
// The updated signature looks for `drizzleOptions`, `appReducers`,
// `appSagas`, `initialAppStore` and `disableReduxDevTools` while
// `appSagas`, `appMiddlewares`, `disableReduxDevTools` and `initialAppState` while
// {...options} captures the previous release's signature.
//
// Resolve drizzleOptions. If called by dapps written to previous API, then
Expand All @@ -50,17 +52,18 @@ export function generateStore ({
? global.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
: compose

let initialContractsState = {
const initialContractsState = {
contracts: generateContractsInitialState(drizzleOptions)
}

const sagaMiddleware = createSagaMiddleware()
const allMiddlewares = [...appMiddlewares, sagaMiddleware, drizzleMW]
const allReducers = { ...drizzleReducers, ...appReducers }
const allState = { ...initialAppState, ...initialContractsState }

const store = createStore(
combineReducers(allReducers),
initialContractsState,
allState,
composeEnhancers(applyMiddleware(...allMiddlewares))
)

Expand Down
16 changes: 16 additions & 0 deletions packages/store/test/generateStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,21 @@ describe('generateStore', () => {
expect(state).toHaveProperty('myState')
expect(state.myState).toBe(initialState)
})

test('when invoked with initialAppState', () => {
const initialState = 'This is the initial app State'
const myState = jest.fn((state = '') => state)
const appReducers = { myState }
const initialAppState = { myState: initialState }
const store = generateStore({
drizzleOptions,
appReducers,
initialAppState
})
const state = store.getState()
hasBasicShape(state)
expect(state).toHaveProperty('myState')
expect(state.myState).toBe(initialState)
})
})
})
1 change: 1 addition & 0 deletions packages/store/types/generateStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface IStoreConfig {
appSagas?: any[];
appMiddlewares?: any[];
disableReduxDevTools?: boolean;
initialAppState?: any;
}

export function generateStore(config: IStoreConfig): Store;