From e4782fd3b92b1f56754ba99298081d07e963004c Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 30 Nov 2020 14:20:05 +0100 Subject: [PATCH] core-api: rename routes to routePaths Co-authored-by: blam --- .../core-api/src/routing/collectors.test.tsx | 8 ++-- packages/core-api/src/routing/collectors.tsx | 2 +- packages/core-api/src/routing/hooks.test.tsx | 38 ++++++++++--------- packages/core-api/src/routing/hooks.tsx | 14 +++---- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/packages/core-api/src/routing/collectors.test.tsx b/packages/core-api/src/routing/collectors.test.tsx index c2eefa7e82..2595b8c0a0 100644 --- a/packages/core-api/src/routing/collectors.test.tsx +++ b/packages/core-api/src/routing/collectors.test.tsx @@ -15,7 +15,7 @@ */ import React, { PropsWithChildren } from 'react'; -import { routeCollector, routeParentCollector } from './collectors'; +import { routePathCollector, routeParentCollector } from './collectors'; import { traverseElementTree, @@ -96,7 +96,7 @@ describe('discovery', () => { root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routes: routePathCollector, routeParents: routeParentCollector, }, }); @@ -147,7 +147,7 @@ describe('discovery', () => { root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routes: routePathCollector, routeParents: routeParentCollector, }, }); @@ -184,7 +184,7 @@ describe('discovery', () => { ), discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routes: routePathCollector, routeParents: routeParentCollector, }, }), diff --git a/packages/core-api/src/routing/collectors.tsx b/packages/core-api/src/routing/collectors.tsx index 34edd8a0d5..37f9252329 100644 --- a/packages/core-api/src/routing/collectors.tsx +++ b/packages/core-api/src/routing/collectors.tsx @@ -19,7 +19,7 @@ import { RouteRef } from '../routing/types'; import { getComponentData } from '../extensions'; import { createCollector } from '../extensions/traversal'; -export const routeCollector = createCollector( +export const routePathCollector = createCollector( () => new Map(), (acc, node, parent) => { if (parent.props.element === node) { diff --git a/packages/core-api/src/routing/hooks.test.tsx b/packages/core-api/src/routing/hooks.test.tsx index c2b8679e5f..695cd35907 100644 --- a/packages/core-api/src/routing/hooks.test.tsx +++ b/packages/core-api/src/routing/hooks.test.tsx @@ -24,12 +24,16 @@ import { traverseElementTree, } from '../extensions/traversal'; import { createPlugin } from '../plugin'; -import { routeCollector, routeParentCollector } from './collectors'; +import { routePathCollector, routeParentCollector } from './collectors'; import { useRouteRef, RoutingProvider, validateRoutes } from './hooks'; import { createRouteRef } from './RouteRef'; -import { RouteRef } from './types'; +import { RouteRef, RouteRefConfig } from './types'; -const mockConfig = () => ({ path: '/unused', title: 'Unused' }); +const mockConfig = (extra?: Partial) => ({ + path: '/unused', + title: 'Unused', + ...extra, +}); const MockComponent = ({ children }: PropsWithChildren<{}>) => <>{children}; const plugin = createPlugin({ id: 'my-plugin' }); @@ -83,17 +87,17 @@ describe('discovery', () => { ); - const { routes, routeParents } = traverseElementTree({ + const { routePaths, routeParents } = traverseElementTree({ root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routePaths: routePathCollector, routeParents: routeParentCollector, }, }); const rendered = render( - + {root} , ); @@ -123,17 +127,17 @@ describe('discovery', () => { ); - const { routes, routeParents } = traverseElementTree({ + const { routePaths, routeParents } = traverseElementTree({ root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routePaths: routePathCollector, routeParents: routeParentCollector, }, }); const rendered = render( - + {root} , ); @@ -146,7 +150,7 @@ describe('discovery', () => { ).toBeInTheDocument(); }); - it('should handle relative routing within parameterized routes', () => { + it('should handle relative routing within parameterized routePaths', () => { const root = ( @@ -158,17 +162,17 @@ describe('discovery', () => { ); - const { routes, routeParents } = traverseElementTree({ + const { routePaths, routeParents } = traverseElementTree({ root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routePaths: routePathCollector, routeParents: routeParentCollector, }, }); const rendered = render( - + {root} , ); @@ -178,7 +182,7 @@ describe('discovery', () => { ).toBeInTheDocument(); }); - it('should handle relative routing of parameterized routes with duplicate param names', () => { + it('should handle relative routing of parameterized routePaths with duplicate param names', () => { const root = ( @@ -189,16 +193,16 @@ describe('discovery', () => { ); - const { routes, routeParents } = traverseElementTree({ + const { routePaths, routeParents } = traverseElementTree({ root, discoverers: [childDiscoverer, routeElementDiscoverer], collectors: { - routes: routeCollector, + routePaths: routePathCollector, routeParents: routeParentCollector, }, }); - expect(() => validateRoutes(routes, routeParents)).toThrow( + expect(() => validateRoutes(routePaths, routeParents)).toThrow( 'Parameter :id is duplicated in path /foo/:id/bar/:id', ); }); diff --git a/packages/core-api/src/routing/hooks.tsx b/packages/core-api/src/routing/hooks.tsx index 0c36928f10..22299e3305 100644 --- a/packages/core-api/src/routing/hooks.tsx +++ b/packages/core-api/src/routing/hooks.tsx @@ -22,7 +22,7 @@ export type RouteFunc = (params?: Record) => string; class RouteResolver { constructor( - private readonly routes: Map, + private readonly routePaths: Map, private readonly routeParents: Map, ) {} @@ -31,7 +31,7 @@ class RouteResolver { let fullPath = ''; while (currentRouteRef) { - const path = this.routes.get(currentRouteRef); + const path = this.routePaths.get(currentRouteRef); if (!path) { throw new Error(`No path for ${currentRouteRef}`); } @@ -58,17 +58,17 @@ export function useRouteRef(routeRef: RouteRef): RouteFunc { } type ProviderProps = { - routes: Map; + routePaths: Map; routeParents: Map; children: ReactNode; }; export const RoutingProvider = ({ - routes, + routePaths, routeParents, children, }: ProviderProps) => { - const resolver = new RouteResolver(routes, routeParents); + const resolver = new RouteResolver(routePaths, routeParents); return ( {children} @@ -77,7 +77,7 @@ export const RoutingProvider = ({ }; export function validateRoutes( - routes: Map, + routePaths: Map, routeParents: Map, ) { const notLeafRoutes = new Set(routeParents.values()); @@ -92,7 +92,7 @@ export function validateRoutes( let fullPath = ''; while (currentRouteRef) { - const path = routes.get(currentRouteRef); + const path = routePaths.get(currentRouteRef); if (!path) { throw new Error(`No path for ${currentRouteRef}`); }