Skip to content

Commit

Permalink
Add test for withLayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
caalador committed Aug 22, 2024
1 parent b4f8c78 commit ebac3be
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,16 @@ export class RouterConfigurationBuilder {
if (!routes) {
return routes;
}
const withLayout = routes.filter(
const withLayout = routes.filter((route) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(route) => typeof route.handle === 'object' && 'flowLayout' in route.handle && route.handle.flowLayout,
);
const layout = typeof route.handle === 'object' && 'flowLayout' in route.handle && route.handle.flowLayout;
return layout;
});
const allRoutes = routes.filter((route) => !withLayout.includes(route));
withLayout.push(routes[routes.length - 1]); // Add * fallback to all child routes
const catchAll = routes.filter((route) => route.path === '*');
if (catchAll.length > 0) {
withLayout.push(...catchAll); // Add * fallback to all child routes
}

allRoutes.unshift(...applyLayouts(withLayout));
return allRoutes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect, use } from '@esm-bundle/chai';
import chaiLike from 'chai-like';
import { createElement } from 'react';
import sinonChai from 'sinon-chai';
import { RouterConfigurationBuilder } from '../../src/runtime/RouterConfigurationBuilder.js';
import { mockDocumentBaseURI } from '../mocks/dom.js';
Expand Down Expand Up @@ -81,6 +82,68 @@ describe('RouterBuilder', () => {
]);
});

it('should add layout routes under layout component', () => {
const serverWildcard = {
path: '*',
element: <Server />,
handle: { title: 'Server' },
};
const serverIndex = {
index: true,
element: <Server />,
handle: { title: 'Server' },
};

const serverRoutes = [serverWildcard, serverIndex];

const { routes } = builder
.withReactRoutes([
{
path: '',
handle: {
flowLayout: true,
},
},
{
path: '/test',
handle: {
flowLayout: true,
},
},
])
.withLayouts(Server)
.build();

expect(routes).to.be.like([
{
children: [
{
path: '',
handle: {
flowLayout: true,
},
},
{
path: '/test',
handle: {
flowLayout: true,
},
},
],
element: createElement(Server),
},
{
children: [
{
path: '/test',
element: <div>Test</div>,
},
],
path: '',
},
]);
});

it('should merge file routes deeply', () => {
const { routes } = builder
.withFileRoutes([
Expand Down

0 comments on commit ebac3be

Please sign in to comment.