Skip to content

Commit

Permalink
more generic breadcrumbs generator with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enguerranws committed Sep 25, 2024
1 parent 32d0662 commit 8bee1bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
5 changes: 4 additions & 1 deletion front/src/app/routes/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthenticatedUserQueryParams, frontRoutes } from "shared";
import { AuthenticatedUserQueryParams, ValueOf, frontRoutes } from "shared";
import { icUserAgencyDashboardTabSerializer } from "src/app/routes/routeParams/agencyDashboardTabs";
import { icUserEstablishmentDashboardTabSerializer } from "src/app/routes/routeParams/establishmentDashboardTabs";
import { ValueSerializer, createRouter, defineRoute, param } from "type-route";
Expand Down Expand Up @@ -69,6 +69,9 @@ export const searchParams = {
...acquisitionParams,
};

export type FrontRouteUnion = ValueOf<typeof routes>;
export type FrontRouteKeys = keyof typeof routes;

export const { RouteProvider, useRoute, routes } = createRouter({
addAgency: defineRoute(`/${frontRoutes.addAgency}`),
adminRoot: defineRoute(`/${frontRoutes.admin}`),
Expand Down
2 changes: 1 addition & 1 deletion front/src/app/utils/IsAllowedConventionTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export const isAllowedConventionTransition = (
actingRoles.some((actingRole) =>
transitionConfig.validRoles.includes(actingRole),
) &&
(!transitionConfig.refine?.(convention).isError ?? true)
!transitionConfig.refine?.(convention).isError
);
};
14 changes: 11 additions & 3 deletions front/src/app/utils/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import {
BreadcrumbsItem,
} from "src/app/contents/breadcrumbs/breadcrumbs";

type RoutesKeysFromBreadcrumbs<T extends Record<keyof T, BreadcrumbsItem>> = {
[K in keyof T]:
| K
| (T[K]["children"] extends Record<keyof T[K]["children"], BreadcrumbsItem>
? RoutesKeysFromBreadcrumbs<T[K]["children"]>
: never);
}[keyof T];

export const makeBreadcrumbsSegments =
<K>(
breadcrumbsSet: Breadcrumbs<K extends string ? K : never>,
<T extends Record<keyof T, BreadcrumbsItem>>(
breadcrumbsSet: Breadcrumbs<keyof T extends string ? keyof T : never>,
rootAncestor: BreadcrumbProps["segments"][0],
) =>
({
currentRouteKey,
}: {
currentRouteKey: K;
currentRouteKey: RoutesKeysFromBreadcrumbs<T>;
}): BreadcrumbProps["segments"] => {
const currentRouteAncestor = keys(breadcrumbsSet).find((key) => {
const currentRouteInBreadcrumbs = breadcrumbsSet[key];
Expand Down
16 changes: 16 additions & 0 deletions front/src/app/utils/breadcrumbs.type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getTestBreadcrumbs } from "src/app/utils/breadcrumbs.unit.test";

getTestBreadcrumbs({
// @ts-expect-error
currentRouteKey: "not-known-route-name",
});

getTestBreadcrumbs({
// root at level 1
currentRouteKey: "homeCandidates",
});

getTestBreadcrumbs({
// deep level
currentRouteKey: "searchDiagoriente",
});
7 changes: 3 additions & 4 deletions front/src/app/utils/breadcrumbs.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ const testRootAncestor: BreadcrumbProps["segments"][0] = {
},
};

const getTestBreadcrumbs = makeBreadcrumbsSegments(
testBreadcrumbsSet,
testRootAncestor,
);
export const getTestBreadcrumbs = makeBreadcrumbsSegments<
typeof testBreadcrumbsSet
>(testBreadcrumbsSet, testRootAncestor);

0 comments on commit 8bee1bc

Please sign in to comment.