-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
services/ui-src/src/components/reports/StandardReportPage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useFindRoute } from "./routing"; | ||
import { | ||
mockFlattenedReportRoutes, | ||
mockReportJson, | ||
} from "utils/testing/setupJest"; | ||
|
||
const mockFallbackRoute = mockReportJson.basePath; | ||
const mockFlatRoutesArray = mockFlattenedReportRoutes; | ||
|
||
jest.mock("react-router-dom", () => ({ | ||
useLocation: jest | ||
.fn() | ||
.mockReturnValueOnce({ pathname: "/mock/mock-route-1" }) // first path | ||
.mockReturnValueOnce({ pathname: "/mock/mock-route-2a" }) // middle path | ||
.mockReturnValueOnce({ pathname: "/mock/mock-route-3" }), // last path | ||
})); | ||
|
||
describe("Test useFindRoute behavior at first route in array (with no previous routes)", () => { | ||
it("Returns fallback as previousRoute when there are no preceding routes", () => { | ||
const { previousRoute } = useFindRoute( | ||
mockFlatRoutesArray, | ||
mockFallbackRoute | ||
); | ||
expect(previousRoute).toEqual(mockFallbackRoute); | ||
}); | ||
}); | ||
|
||
describe("Test useFindRoute behavior at middle route in array (with both previous and next routes)", () => { | ||
it("Returns previous path when there are preceding routes and next path when there are subsequent routes", () => { | ||
const { previousRoute, nextRoute } = useFindRoute( | ||
mockFlatRoutesArray, | ||
mockFallbackRoute | ||
); | ||
expect(previousRoute).toEqual("/mock/mock-route-1"); | ||
expect(nextRoute).toEqual("/mock/mock-route-2b"); | ||
}); | ||
}); | ||
|
||
describe("Test useFindRoute behavior at last route in array (with no subsequent routes)", () => { | ||
it("Returns fallback if there are no subsequent routes", () => { | ||
const { nextRoute } = useFindRoute(mockFlatRoutesArray, mockFallbackRoute); | ||
expect(nextRoute).toEqual(mockFallbackRoute); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useLocation } from "react-router-dom"; | ||
import { ReportRoute, ReportType } from "types"; | ||
|
||
/** | ||
* WARNING: You probably want ReportContext.isReportPage instead. | ||
* This function is called from outside the ReportContext, | ||
* so it can only make a best guess. | ||
*/ | ||
export const isApparentReportPage = (pathname: string): boolean => { | ||
const yes = Object.values(ReportType).some((reportType) => { | ||
const prefix = `/${reportType.toLowerCase()}/`; | ||
/* | ||
* Report pages look like "/wp/some-path", or "/sar/some-other-path" | ||
* Two exceptions are the Get Started page, and the root (Dashboard) page for that report type. | ||
*/ | ||
return ( | ||
pathname.startsWith(prefix) && | ||
!pathname.startsWith(`/${prefix}/get-started`) && | ||
pathname.length > prefix.length | ||
); | ||
}); | ||
return yes; | ||
}; | ||
|
||
export const useFindRoute = ( | ||
flatRouteArray: ReportRoute[] | undefined, | ||
fallbackRoute: string = "/" | ||
) => { | ||
const { pathname } = useLocation(); | ||
let calculatedRoutes = { | ||
previousRoute: fallbackRoute, | ||
nextRoute: fallbackRoute, | ||
}; | ||
if (flatRouteArray) { | ||
// find current route and position in array | ||
const currentRouteObject = flatRouteArray.find( | ||
(route: ReportRoute) => route.path === pathname | ||
); | ||
if (currentRouteObject) { | ||
const currentPosition = flatRouteArray.indexOf(currentRouteObject); | ||
// set previousRoute to previous path or fallback route | ||
const previousRoute = | ||
flatRouteArray[currentPosition - 1]?.path || fallbackRoute; | ||
calculatedRoutes.previousRoute = previousRoute; | ||
// set nextRoute to next path or fallback route | ||
const nextRoute = | ||
flatRouteArray[currentPosition + 1]?.path || fallbackRoute; | ||
calculatedRoutes.nextRoute = nextRoute; | ||
} | ||
} | ||
return calculatedRoutes; | ||
}; |