core-app-api: fix root route handling in FlatRoutes

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-07-16 12:00:44 +02:00
parent 0041be8c51
commit ea249c6e63
3 changed files with 12 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-app-api': patch
---
Fix a bug in `FlatRoutes` that prevented outlets from working with the root route, as well as matching root routes too broadly.
@@ -100,7 +100,6 @@ describe('FlatRoutes', () => {
return <>Outlet: {useOutlet()}</>;
};
// The '/*' suffixes here are intentional and will be ignored by FlatRoutes
const routes = (
<>
<Route path="/a" element={<MyPage />}>
@@ -112,11 +111,15 @@ describe('FlatRoutes', () => {
<Route path="/b" element={<MyPage />}>
b
</Route>
<Route path="/" element={<MyPage />}>
c
</Route>
</>
);
const renderRoute = makeRouteRenderer(<FlatRoutes>{routes}</FlatRoutes>);
expect(renderRoute('/a').getByText('Outlet: a')).toBeInTheDocument();
expect(renderRoute('/a/b').getByText('Outlet: a-b')).toBeInTheDocument();
expect(renderRoute('/b').getByText('Outlet: b')).toBeInTheDocument();
expect(renderRoute('/').getByText('Outlet: c')).toBeInTheDocument();
});
});
@@ -49,8 +49,10 @@ export const FlatRoutes = (props: FlatRoutesProps): JSX.Element | null => {
element: child,
children: child.props.children
? [
// These are the children of each route, which we all add in under a catch-all
// subroute in order to make them available to `useOutlet`
{
path: '/*',
path: path === '/' ? '/' : '/*', // The root path must require an exact match
element: child.props.children,
},
]