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

Add check for undefined middleware in configureStore #959

Merged
Merged
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
47 changes: 47 additions & 0 deletions src/configureStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ describe('configureStore', () => {
})
})

describe('given undefined middleware', () => {
it('calls createStore with default middleware', () => {
expect(configureStore({ middleware: undefined, reducer })).toBeInstanceOf(
Object
)
expect(redux.applyMiddleware).toHaveBeenCalledWith(
expect.any(Function), // thunk
expect.any(Function), // immutableCheck
expect.any(Function) // serializableCheck
)
expect(devtools.composeWithDevTools).toHaveBeenCalled()
expect(redux.createStore).toHaveBeenCalledWith(
reducer,
undefined,
expect.any(Function)
)
})
})

describe('given a middleware creation function that returns undefined', () => {
it('throws an error', () => {
const invalidBuilder = jest.fn(getDefaultMiddleware => undefined as any)
expect(() =>
configureStore({ middleware: invalidBuilder, reducer })
).toThrow(
'when using a middleware builder function, an array of middleware must be returned'
)
})
})

describe('given a middleware creation function that returns an array with non-functions', () => {
it('throws an error', () => {
const invalidBuilder = jest.fn(getDefaultMiddleware => [true] as any)
expect(() =>
configureStore({ middleware: invalidBuilder, reducer })
).toThrow('each middleware provided to configureStore must be a function')
})
})

describe('given custom middleware that contains non-functions', () => {
it('throws an error', () => {
expect(() =>
configureStore({ middleware: [true] as any, reducer })
).toThrow('each middleware provided to configureStore must be a function')
})
})

describe('given custom middleware', () => {
it('calls createStore with custom middleware and without default middleware', () => {
const thank: redux.Middleware = _store => next => action => next(action)
Expand Down
25 changes: 20 additions & 5 deletions src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,26 @@ export function configureStore<
)
}

const middlewareEnhancer = applyMiddleware(
...(typeof middleware === 'function'
? middleware(curriedGetDefaultMiddleware)
: middleware)
)
let finalMiddleware = middleware
if (typeof finalMiddleware === 'function') {
finalMiddleware = finalMiddleware(curriedGetDefaultMiddleware)

if (!IS_PRODUCTION && !Array.isArray(finalMiddleware)) {
throw new Error(
'when using a middleware builder function, an array of middleware must be returned'
)
}
}
if (
!IS_PRODUCTION &&
finalMiddleware.some(item => typeof item !== 'function')
) {
throw new Error(
'each middleware provided to configureStore must be a function'
)
}

const middlewareEnhancer = applyMiddleware(...finalMiddleware)

let finalCompose = compose

Expand Down