Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(navigation): Prevent secondary navigation from displaying without auth MAASENG-1986 #5067

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion src/app/base/components/PageContent/PageContent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import PageContent from "./PageContent";

import { renderWithBrowserRouter, screen, within } from "testing/utils";
import { preferencesNavItems } from "app/preferences/constants";
import { settingsNavItems } from "app/settings/constants";
import {
getTestState,
renderWithBrowserRouter,
screen,
within,
} from "testing/utils";

const state = getTestState();

it("displays sidebar with provided content", () => {
renderWithBrowserRouter(
Expand Down Expand Up @@ -29,3 +38,79 @@ it("displays hidden sidebar when no content provided", () => {
);
expect(screen.queryByRole("complementary")).toHaveClass("is-collapsed");
});

it("shows the secondary navigation for settings", () => {
state.status.authenticated = true;
state.status.connected = true;
renderWithBrowserRouter(
<PageContent
header="Settings"
sidePanelContent={null}
sidePanelTitle={null}
>
content
</PageContent>,
{ route: "/settings/configuration/general", state }
);

expect(screen.getByRole("navigation")).toBeInTheDocument();

settingsNavItems.forEach((item) => {
expect(screen.getByText(item.label)).toBeInTheDocument();
});
});

it("shows the secondary navigation for preferences", () => {
state.status.authenticated = true;
state.status.connected = true;
renderWithBrowserRouter(
<PageContent
header="Preferences"
sidePanelContent={null}
sidePanelTitle={null}
>
content
</PageContent>,
{ route: "/account/prefs/details", state }
);
Comment on lines +42 to +75
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as is, but generally I'd be for merging these 2 tests into 1.
https://kentcdodds.com/blog/write-fewer-longer-tests


expect(screen.getByRole("navigation")).toBeInTheDocument();

preferencesNavItems.forEach((item) => {
expect(screen.getByText(item.label)).toBeInTheDocument();
});
});

it("doesn't show the side nav if not authenticated", () => {
state.status.authenticated = false;
state.status.connected = true;
renderWithBrowserRouter(
<PageContent
header="Preferences"
sidePanelContent={null}
sidePanelTitle={null}
>
content
</PageContent>,
{ route: "/account/prefs/details", state }
);

expect(screen.queryByRole("navigation")).not.toBeInTheDocument();
});

it("doesn't show the side nav if not connected", () => {
state.status.authenticated = true;
state.status.connected = false;
renderWithBrowserRouter(
<PageContent
header="Preferences"
sidePanelContent={null}
sidePanelTitle={null}
>
content
</PageContent>,
{ route: "/account/prefs/details", state }
);

expect(screen.queryByRole("navigation")).not.toBeInTheDocument();
});
11 changes: 8 additions & 3 deletions src/app/base/components/PageContent/PageContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { HTMLProps, ReactNode } from "react";

import classNames from "classnames";
import { useSelector } from "react-redux";
import { matchPath, useLocation } from "react-router-dom-v5-compat";

import AppSidePanel from "../AppSidePanel";
Expand All @@ -11,6 +12,7 @@ import SecondaryNavigation from "../SecondaryNavigation";
import { useThemeContext } from "app/base/theme-context";
import { preferencesNavItems } from "app/preferences/constants";
import { settingsNavItems } from "app/settings/constants";
import status from "app/store/status/selectors";

export type Props = {
children?: ReactNode;
Expand All @@ -35,18 +37,21 @@ const PageContent = ({
const { pathname } = useLocation();
const isSettingsPage = matchPath("settings/*", pathname);
const isPreferencesPage = matchPath("account/prefs/*", pathname);
const isSideNavVisible = isSettingsPage || isPreferencesPage;
const authenticated = useSelector(status.authenticated);
const connected = useSelector(status.connected);
const hasSecondaryNav = isSettingsPage || isPreferencesPage;
const isSecondaryNavVisible = hasSecondaryNav && authenticated && connected;
const { theme } = useThemeContext();

return (
<>
<main className="l-main">
{isSideNavVisible ? (
{isSecondaryNavVisible ? (
<div
className={classNames("l-main__nav", `is-maas-${theme}--accent`)}
>
<SecondaryNavigation
isOpen={!!isSideNavVisible}
isOpen={!!isSecondaryNavVisible}
items={
isSettingsPage
? settingsNavItems
Expand Down
Loading