Skip to content

Commit

Permalink
Add accumulateRouteValues (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Feb 17, 2018
1 parent b9a57cd commit 8cec26f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 8 deletions.
45 changes: 45 additions & 0 deletions src/ResolverUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,48 @@ export function getComponents(routeMatches) {
},
);
}

function accumulateRouteValuesImpl(
routeValues,
routeIndices,
callback,
initialValue,
) {
const accumulated = [];
let value = initialValue;

for (const routeIndex of routeIndices) {
if (typeof routeIndex === 'object') {
// eslint-disable-next-line no-loop-func
Object.values(routeIndex).forEach(groupRouteIndices => {
accumulated.push(
...accumulateRouteValuesImpl(
routeValues,
groupRouteIndices,
callback,
value,
),
);
});
} else {
value = callback(value, routeValues.shift());
accumulated.push(value);
}
}

return accumulated;
}

export function accumulateRouteValues(
routeValues,
routeIndices,
callback,
initialValue,
) {
return accumulateRouteValuesImpl(
[...routeValues],
routeIndices,
callback,
initialValue,
);
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export { matcherShape, matchShape, routerShape } from './PropTypes';
export Redirect from './Redirect';
export RedirectException from './RedirectException';
export resolver from './resolver';
export ResolverUtils from './ResolverUtils';
export * as ResolverUtils from './ResolverUtils';
export Route from './Route';
export withRouter from './withRouter';
86 changes: 79 additions & 7 deletions test/ResolverUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
import { checkResolved, isResolved } from '../src/ResolverUtils';
import Matcher from '../src/Matcher';
import {
accumulateRouteValues,
checkResolved,
getComponents,
getRouteMatches,
getRouteValues,
isResolved,
} from '../src/ResolverUtils';

describe('ResolverUtils', () => {
let match;

const Foo = () => null;
const Bar = () => null;

beforeEach(() => {
const matcher = new Matcher([
{
path: 'foo',
Component: Foo,
value: 9,
children: [
{
path: 'bar',
getComponent: () => Bar,
getValue: ({ params }) => params.quux,
children: {
nav: [{ path: '(.*)?' }],
main: [{ path: 'baz' }, { path: 'qux/:quux' }],
},
},
],
},
]);

match = matcher.match({ pathname: '/foo/bar/qux/a' });
match.routes = matcher.getRoutes(match);
});

describe('checkResolved, isResolved', () => {
it('should return true for non-promises', async () => {
expect(isResolved(await checkResolved({}))).toBe(true);
Expand Down Expand Up @@ -35,15 +72,50 @@ describe('ResolverUtils', () => {
});
});

describe.skip('getRouteMatches', () => {
test('untested');
describe('getRouteMatches', () => {
it('should get per-route match information', () => {
expect(getRouteMatches(match)).toMatchObject([
{ route: { path: 'foo' }, params: { quux: 'a' } },
{ route: { path: 'bar' }, params: { quux: 'a' } },
{ route: { path: '(.*)?' }, params: { quux: 'a' } },
{ route: { path: 'qux/:quux' }, params: { quux: 'a' } },
]);
});
});

describe.skip('getRouteValues', () => {
test('untested');
describe('getRouteValues', () => {
it('should get static and computed route values', () => {
expect(
getRouteValues(
getRouteMatches(match),
route => route.getValue,
route => route.value,
),
).toEqual([9, 'a', undefined, undefined]);
});
});

describe('getComponents', () => {
it('should get static and computed route components', () => {
expect(getComponents(getRouteMatches(match))).toEqual([
Foo,
Bar,
undefined,
undefined,
]);
});
});

describe.skip('getComponents', () => {
test('untested');
describe('accumulateRouteValues', () => {
it('should accumulate route values along match tree', () => {
expect(
accumulateRouteValues(
getRouteMatches(match),
match.routeIndices,
(value, { route: { path } }) => `${value}/${path}`,
'',
),
).toEqual(['/foo', '/foo/bar', '/foo/bar/(.*)?', '/foo/bar/qux/:quux']);
});
});
});

0 comments on commit 8cec26f

Please sign in to comment.