Skip to content

Commit

Permalink
Merge pull request #4 from kouhin/feature/add_support_named_components
Browse files Browse the repository at this point in the history
Add support to named components
  • Loading branch information
dlmr committed May 30, 2016
2 parents c2a3ba1 + 05101bc commit 259dea5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/getAllComponents.js
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion src/triggerHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(() => {
Expand Down
37 changes: 37 additions & 0 deletions tests/getAllComponents.test.js
Original file line number Diff line number Diff line change
@@ -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 = () => (
<div></div>
);
const MockNamed1Component = () => (
<div></div>
);
const MockNamed2Component = () => (
<div></div>
);

const routes = (
<Route path="/" component={MockComponent} myprop={'top'}>
<IndexRoute component={MockComponent} myprop={'index'} />
<Route
path="user"
components={{ name1: MockNamed1Component, named2: MockNamed2Component }}
/>
</Route>
);

match({ routes, location: '/' }, (error, redirectLocation, renderProps) => {
expect(getAllComponents(renderProps.components).length).toEqual(2);
});

match({ routes, location: '/user' }, (error, redirectLocation, renderProps) => {
expect(getAllComponents(renderProps.components).length).toEqual(3);
});
});
});

0 comments on commit 259dea5

Please sign in to comment.