Skip to content

Commit

Permalink
fix: support layout routes in v6 (#35)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Schrader <[email protected]>
  • Loading branch information
icd2k3 and jschrader-nr authored Nov 11, 2021
1 parent bc717c0 commit 4fa09f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-react-router-breadcrumbs",
"version": "3.0.0",
"version": "3.0.1",
"description": "A hook for displaying and setting breadcrumbs for react router",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
Expand Down
41 changes: 39 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const components = {
return <span>Class</span>;
}
},
Layout: React.memo(({ children }) => <div>{children}</div>),
};

const getHOC = () => {
Expand Down Expand Up @@ -129,15 +130,23 @@ components.Breadcrumbs.propTypes = {
routes: PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.shape({
path: PropTypes.string.isRequired,
path: PropTypes.string,
breadcrumb: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func,
PropTypes.object,
]),
}),
PropTypes.shape({
index: PropTypes.bool.isRequired,
index: PropTypes.bool,
breadcrumb: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func,
PropTypes.object,
]),
}),
PropTypes.shape({
children: PropTypes.arrayOf(PropTypes.shape()).isRequired,
breadcrumb: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func,
Expand Down Expand Up @@ -174,6 +183,14 @@ components.BreadcrumbExtraPropsTest.propTypes = {
bar: PropTypes.string.isRequired,
};

components.Layout.propTypes = {
children: PropTypes.node,
};

components.Layout.defaultProps = {
children: null,
};

describe('use-react-router-breadcrumbs', () => {
describe('Valid routes', () => {
it('Should render breadcrumb components as expected', () => {
Expand Down Expand Up @@ -314,6 +331,26 @@ describe('use-react-router-breadcrumbs', () => {
expect(breadcrumbs).toBe('Home / One / TwoCustom / ThreeCustom');
});

it('Should allow layout routes', () => {
const routes = [
{
element: <components.Layout />,
children: [
{
path: 'about',
breadcrumb: 'About',
},
],
},
{
index: true,
breadcrumb: 'Home',
},
];
const { breadcrumbs } = render({ pathname: '/about', routes });
expect(breadcrumbs).toBe('Home / About');
});

it('Should use the breadcrumb provided by parent if the index route dose not provide one', () => {
const routes = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function flattenRoutes(
parentPath = '',
): BreadcrumbsRouteBranch[] {
routes.forEach((route, index) => {
if (typeof route.path !== 'string' && !route.index) {
if (typeof route.path !== 'string' && !route.index && !route.children?.length) {
throw new Error(
'useBreadcrumbs: `path` or `index` must be provided in every route object',
);
Expand Down

0 comments on commit 4fa09f5

Please sign in to comment.