diff --git a/src/getAllComponents.js b/src/getAllComponents.js new file mode 100644 index 0000000..cf7bc3b --- /dev/null +++ b/src/getAllComponents.js @@ -0,0 +1,12 @@ +export default function getAllComponents(components) { + const arr = Array.isArray(components) ? components : [components]; + const result = []; + arr.forEach(component => { + if (typeof component === 'object') { + Object.keys(component).forEach(key => result.push(component[key])); + } else { + result.push(component); + } + }); + return result; +} diff --git a/src/triggerHooks.js b/src/triggerHooks.js index 0d132ee..0d04cd6 100644 --- a/src/triggerHooks.js +++ b/src/triggerHooks.js @@ -3,6 +3,7 @@ import isPlainObject from 'lodash.isplainobject'; import createMap from './createMap'; import getRoutesProps from './getRoutesProps'; import getLocals from './getLocals'; +import getAllComponents from './getAllComponents'; export default function triggerHooks({ hooks, @@ -40,7 +41,7 @@ export default function triggerHooks({ ...getLocals(component, locals), }); - const hookComponents = components || renderProps.components; + const hookComponents = getAllComponents(components || renderProps.components); return hooks.reduce((promise, parallelHooks) => promise.then(() => { diff --git a/tests/getAllComponents.test.js b/tests/getAllComponents.test.js new file mode 100644 index 0000000..059e73a --- /dev/null +++ b/tests/getAllComponents.test.js @@ -0,0 +1,37 @@ +import expect from 'expect'; +import React from 'react'; +import { Route, IndexRoute, match } from 'react-router'; + +import getAllComponents from '../src/getAllComponents'; + +describe('getAllComponents', () => { + it('should return the correct length of route components', () => { + const MockComponent = () => ( +
+ ); + const MockNamed1Component = () => ( + + ); + const MockNamed2Component = () => ( + + ); + + const routes = ( +