From ab801e121fb25d4b5d2373d9c614f0253a3db923 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 24 Aug 2022 14:17:03 +0200 Subject: [PATCH] core-app-api: add back support for absolute route paths in app Co-authored-by: blam Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .../src/routing/collectors.compat.test.tsx | 12 +++-- .../src/routing/collectors.stable.test.tsx | 44 ++++++++++++++++++- .../core-app-api/src/routing/collectors.tsx | 12 ++--- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/packages/core-app-api/src/routing/collectors.compat.test.tsx b/packages/core-app-api/src/routing/collectors.compat.test.tsx index 7ee19b9fd2..e1333a070a 100644 --- a/packages/core-app-api/src/routing/collectors.compat.test.tsx +++ b/packages/core-app-api/src/routing/collectors.compat.test.tsx @@ -187,7 +187,9 @@ describe.each(['beta', 'stable'])('react-router %s', rrVersion => {
{null}
- } /> + + } /> +
{false} @@ -211,7 +213,7 @@ describe.each(['beta', 'stable'])('react-router %s', rrVersion => { expect(sortedEntries(routing.paths)).toEqual([ [ref1, 'foo'], [ref2, 'bar/:id'], - [ref3, 'baz'], + [ref3, rrVersion === 'beta' ? '/baz' : 'baz'], [ref4, 'divsoup'], [ref5, 'blop'], ]); @@ -227,7 +229,11 @@ describe.each(['beta', 'stable'])('react-router %s', rrVersion => { 'foo', [ref1], [ - routeObj('bar/:id', [ref2], [routeObj('baz', [ref3])]), + routeObj( + 'bar/:id', + [ref2], + [routeObj(rrVersion === 'beta' ? '/baz' : 'baz', [ref3])], + ), routeObj('blop', [ref5]), ], ), diff --git a/packages/core-app-api/src/routing/collectors.stable.test.tsx b/packages/core-app-api/src/routing/collectors.stable.test.tsx index 6c2767621f..2324ea5b69 100644 --- a/packages/core-app-api/src/routing/collectors.stable.test.tsx +++ b/packages/core-app-api/src/routing/collectors.stable.test.tsx @@ -249,6 +249,46 @@ describe('discovery', () => { ]); }); + it('should handle absolute route paths', () => { + const root = ( + + + }> + + } /> + + + }> + } /> + } /> + + + + ); + + const { routing } = traverseElementTree({ + root, + discoverers: [childDiscoverer, routeElementDiscoverer], + collectors: { + routing: routingV2Collector, + }, + }); + expect(sortedEntries(routing.paths)).toEqual([ + [ref1, 'foo'], + [ref2, 'bar/:id'], + [ref3, 'baz'], + [ref4, 'divsoup'], + [ref5, 'blop'], + ]); + expect(sortedEntries(routing.parents)).toEqual([ + [ref1, undefined], + [ref2, ref1], + [ref3, undefined], + [ref4, ref3], + [ref5, ref3], + ]); + }); + it('should use the route aggregator key to bind child routes to the same path', () => { const root = ( @@ -309,11 +349,11 @@ describe('discovery', () => { }> - + }> - } /> + } /> diff --git a/packages/core-app-api/src/routing/collectors.tsx b/packages/core-app-api/src/routing/collectors.tsx index 81a263eb70..1a2bd2f431 100644 --- a/packages/core-app-api/src/routing/collectors.tsx +++ b/packages/core-app-api/src/routing/collectors.tsx @@ -87,10 +87,10 @@ export const routingV2Collector = createCollector( objects: new Array(), }), (acc, node, parent, ctx?: RoutingV2CollectorContext) => { - const path: unknown = node.props?.path; + const pathProp: unknown = node.props?.path; const mountPoint = getComponentData(node, 'core.mountPoint'); - if (mountPoint && path) { + if (mountPoint && pathProp) { throw new Error( `Path property may not be set directly on a routable extension "${stringifyNode( node, @@ -109,13 +109,15 @@ export const routingV2Collector = createCollector( const parentChildren = ctx?.obj?.children ?? acc.objects; - if (path) { - if (typeof path !== 'string') { + if (pathProp) { + if (typeof pathProp !== 'string') { throw new Error( `Element path must be a string at "${stringifyNode(node)}"`, ); } + const path = pathProp.startsWith('/') ? pathProp.slice(1) : pathProp; + const elementProp = node.props.element; if (getComponentData(node, 'core.gatherMountPoints')) { @@ -149,7 +151,7 @@ export const routingV2Collector = createCollector( const [extension, ...others] = collectSubTree(elementProp); if (others.length > 0) { throw new Error( - `Route element with path "${path}" may not contain multiple routable extensions`, + `Route element with path "${pathProp}" may not contain multiple routable extensions`, ); } if (!extension) {