Skip to content

Commit

Permalink
Warn on invalid middlewares (#3717)
Browse files Browse the repository at this point in the history
  • Loading branch information
taion authored and timdorr committed Aug 16, 2016
1 parent 2544ed1 commit 8aa5434
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 11 additions & 0 deletions modules/__tests__/applyRouterMiddleware-test.js
Expand Up @@ -5,6 +5,7 @@ import Router from '../Router'
import Route from '../Route'
import createMemoryHistory from '../createMemoryHistory'
import applyMiddleware from '../applyRouterMiddleware'
import shouldWarn from './shouldWarn'

const FOO_ROOT_CONTAINER_TEXT = 'FOO ROOT CONTAINER'
const BAR_ROOT_CONTAINER_TEXT = 'BAR ROOT CONTAINER'
Expand Down Expand Up @@ -141,4 +142,14 @@ describe('applyMiddleware', () => {
})
})

it('should warn on invalid middleware', () => {
shouldWarn('at index 0 does not appear to be a valid')
shouldWarn('at index 2 does not appear to be a valid')

applyMiddleware(
{},
{ renderRouterContext: () => {} },
{}
)
})
})
20 changes: 18 additions & 2 deletions modules/applyRouterMiddleware.js
@@ -1,9 +1,25 @@
import React, { createElement } from 'react'
import RouterContext from './RouterContext'
import warning from './routerWarning'

export default (...middlewares) => {
const withContext = middlewares.map(m => m.renderRouterContext).filter(f => f)
const withComponent = middlewares.map(m => m.renderRouteComponent).filter(f => f)
if (__DEV__) {
middlewares.forEach((middleware, index) => {
warning(
middleware.renderRouterContext || middleware.renderRouteComponent,
`The middleware specified at index ${index} does not appear to be ` +
'a valid React Router middleware.'
)
})
}

const withContext = middlewares
.map(middleware => middleware.renderRouterContext)
.filter(Boolean)
const withComponent = middlewares
.map(middleware => middleware.renderRouteComponent)
.filter(Boolean)

const makeCreateElement = (baseCreateElement = createElement) => (
(Component, props) => (
withComponent.reduceRight(
Expand Down

0 comments on commit 8aa5434

Please sign in to comment.