Skip to content

Commit

Permalink
cleanup: Remove checks for KeycloakSso test group in frontend code (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored Aug 6, 2024
1 parent a9a862e commit 44c33ec
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 126 deletions.
32 changes: 13 additions & 19 deletions assets/src/components/nav/navMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as BsIcon from "../../helpers/bsIcons"
import { joinClasses } from "../../helpers/dom"
import { reload } from "../../models/browser"
import { LoggedInAs } from "../loggedInAs"
import inTestGroup, { TestGroups } from "../../userInTestGroup"
import getEmailAddress from "../../userEmailAddress"

interface Props {
Expand All @@ -16,7 +15,6 @@ interface Props {
}

const NavMenu: React.FC<Props> = ({ mobileMenuIsOpen, toggleMobileMenu }) => {
const keycloakEnabled = inTestGroup(TestGroups.KeycloakSso)
const email = getEmailAddress()

return (
Expand Down Expand Up @@ -49,7 +47,7 @@ const NavMenu: React.FC<Props> = ({ mobileMenuIsOpen, toggleMobileMenu }) => {
</button>
</div>
<div className="p-3">
{keycloakEnabled && email && (
{email && (
<>
<LoggedInAs email={email} className="px-3" />
<hr />
Expand Down Expand Up @@ -96,22 +94,18 @@ const NavMenu: React.FC<Props> = ({ mobileMenuIsOpen, toggleMobileMenu }) => {
<BsIcon.GearFill /> Settings
</Nav.Link>
</Nav.Item>
{keycloakEnabled && (
<>
<Nav.Item>
<hr />
</Nav.Item>
<Nav.Item>
<Nav.Link
className="icon-link"
as="a"
href="/auth/keycloak/logout"
>
<BsIcon.BoxArrowRight /> Logout
</Nav.Link>
</Nav.Item>
</>
)}
<Nav.Item>
<hr />
</Nav.Item>
<Nav.Item>
<Nav.Link
className="icon-link"
as="a"
href="/auth/keycloak/logout"
>
<BsIcon.BoxArrowRight /> Logout
</Nav.Link>
</Nav.Item>
</Nav>
</div>
</div>
Expand Down
73 changes: 33 additions & 40 deletions assets/src/components/nav/topNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import { LoggedInAs } from "../loggedInAs"
import getEmailAddress from "../../userEmailAddress"
import { CircleButton } from "../circleButton"
import { UserAvatar } from "../userAvatar"
import inTestGroup, { TestGroups } from "../../userInTestGroup"

const TopNav = (): JSX.Element => {
const email = getEmailAddress()
const [showUserPopover, setShowUserPopover] = useState<boolean>(false)
const userButtonRef = useRef(null)
const showLoggedInUser = inTestGroup(TestGroups.KeycloakSso)

return (
<div className="c-top-nav">
Expand All @@ -31,46 +29,41 @@ const TopNav = (): JSX.Element => {
<RefreshIcon className="c-top-nav__icon" />
</button>
</li>
{showLoggedInUser && (
<li>
<div ref={userButtonRef}>
<CircleButton
isActive={showUserPopover}
onClick={() => {
setShowUserPopover(!showUserPopover)
}}
title="User Info"
>
<UserAvatar userName={email} />
</CircleButton>
</div>
<Overlay
target={userButtonRef.current}
show={showUserPopover}
placement="bottom"
<li>
<div ref={userButtonRef}>
<CircleButton
isActive={showUserPopover}
onClick={() => {
setShowUserPopover(!showUserPopover)
}}
title="User Info"
>
<Popover className="c-top-nav__popover inherit-box border-box">
<Popover.Body className="p-0">
<Dropdown.Menu
show
className="position-static border border-0"
<UserAvatar userName={email} />
</CircleButton>
</div>
<Overlay
target={userButtonRef.current}
show={showUserPopover}
placement="bottom"
>
<Popover className="c-top-nav__popover inherit-box border-box">
<Popover.Body className="p-0">
<Dropdown.Menu show className="position-static border border-0">
<Dropdown.ItemText>
<LoggedInAs email={email} />
</Dropdown.ItemText>
<Dropdown.Divider />
<Dropdown.Item
href="/auth/keycloak/logout"
className="icon-link"
>
<Dropdown.ItemText>
<LoggedInAs email={email} />
</Dropdown.ItemText>
<Dropdown.Divider />
<Dropdown.Item
href="/auth/keycloak/logout"
className="icon-link"
>
<BsIcon.BoxArrowRight className="me-2" /> Log out
</Dropdown.Item>
</Dropdown.Menu>
</Popover.Body>
</Popover>
</Overlay>
</li>
)}
<BsIcon.BoxArrowRight className="me-2" /> Log out
</Dropdown.Item>
</Dropdown.Menu>
</Popover.Body>
</Popover>
</Overlay>
</li>
</ul>
</div>
)
Expand Down
1 change: 0 additions & 1 deletion assets/src/userInTestGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum TestGroups {
DemoMode = "demo-mode",
DetoursList = "detours-list",
DetoursPilot = "detours-pilot",
KeycloakSso = "keycloak-sso",
MinimalLadderPage = "minimal-ladder-page",
LateView = "late-view",
}
Expand Down
3 changes: 0 additions & 3 deletions assets/tests/components/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import { OpenView, PagePath } from "../../src/state/pagePanelState"
import { viewFactory } from "../factories/pagePanelStateFactory"
import userEvent from "@testing-library/user-event"
import { mockUsePanelState } from "../testHelpers/usePanelStateMocks"
import getTestGroups from "../../src/userTestGroups"
import { TestGroups } from "../../src/userInTestGroup"

jest.mock("../../src/hooks/useDataStatus", () => ({
__esModule: true,
Expand All @@ -52,7 +50,6 @@ beforeEach(() => {

describe("App", () => {
test("renders", () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])
const result = render(<App />)
expect(result.asFragment()).toMatchSnapshot()
})
Expand Down
3 changes: 0 additions & 3 deletions assets/tests/components/appStateWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { test, expect, jest } from "@jest/globals"
import React from "react"
import { render } from "@testing-library/react"
import AppStateWrapper from "../../src/components/appStateWrapper"
import getTestGroups from "../../src/userTestGroups"
import { TestGroups } from "../../src/userInTestGroup"

jest.mock("userTestGroups", () => ({
__esModule: true,
default: jest.fn(() => []),
}))

test("renders", () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])
const result = render(<AppStateWrapper />)
expect(result.asFragment()).toMatchSnapshot()
})
30 changes: 0 additions & 30 deletions assets/tests/components/nav/navMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import * as browser from "../../../src/models/browser"
import { openDrift } from "../../../src/helpers/drift"
import NavMenu from "../../../src/components/nav/navMenu"
import { BrowserRouter } from "react-router-dom"
import getTestGroups from "../../../src/userTestGroups"
import { TestGroups } from "../../../src/userInTestGroup"
import getEmailAddress from "../../../src/userEmailAddress"

jest.mock("../../../src/helpers/drift", () => ({
Expand Down Expand Up @@ -81,7 +79,6 @@ describe("NavMenu", () => {
})

test("shows who is logged in", async () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])
jest.mocked(getEmailAddress).mockReturnValue("[email protected]")

render(
Expand All @@ -93,20 +90,7 @@ describe("NavMenu", () => {
expect(await screen.findByText("Logged in as")).toBeVisible()
})

test("does not show who is logged in if the user isn't in the Keycloak test group", () => {
jest.mocked(getTestGroups).mockReturnValue([])

render(
<BrowserRouter>
<NavMenu toggleMobileMenu={jest.fn()} mobileMenuIsOpen={true} />
</BrowserRouter>
)

expect(screen.queryByText("Logged in as")).not.toBeInTheDocument()
})

test("shows logout button", async () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])
jest.mocked(getEmailAddress).mockReturnValue("[email protected]")

render(
Expand All @@ -118,20 +102,6 @@ describe("NavMenu", () => {
expect(screen.getByRole("link", { name: "Logout" })).toBeVisible()
})

test("doesn't show logout button if the user isn't in the Keycloak test group", async () => {
jest.mocked(getTestGroups).mockReturnValue([])

render(
<BrowserRouter>
<NavMenu toggleMobileMenu={jest.fn()} mobileMenuIsOpen={true} />
</BrowserRouter>
)

expect(
screen.queryByRole("link", { name: "Logout" })
).not.toBeInTheDocument()
})

test("refresh button reloads the page", async () => {
const reloadSpy = jest
.spyOn(browser, "reload")
Expand Down
30 changes: 0 additions & 30 deletions assets/tests/components/nav/topNav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import { initialState } from "../../../src/state"
import { BrowserRouter } from "react-router-dom"
import "@testing-library/jest-dom/jest-globals"
import * as browser from "../../../src/models/browser"
import getTestGroups from "../../../src/userTestGroups"
import { TestGroups } from "../../../src/userInTestGroup"

jest.mock("userTestGroups", () => ({
__esModule: true,
default: jest.fn(() => []),
}))

describe("TopNav", () => {
test("refresh button reloads the page", async () => {
Expand All @@ -38,26 +31,7 @@ describe("TopNav", () => {
})

describe("User info", () => {
test("does not have a 'User Info' button if the user isn't in the right test group", () => {
jest.mocked(getTestGroups).mockReturnValue([])

const dispatch = jest.fn()
render(
<StateDispatchProvider state={initialState} dispatch={dispatch}>
<BrowserRouter>
<TopNav />
</BrowserRouter>
</StateDispatchProvider>
)

expect(
screen.queryByRole("button", { name: "User Info" })
).not.toBeInTheDocument()
})

test("has a 'User Info' button", () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])

const dispatch = jest.fn()
render(
<StateDispatchProvider state={initialState} dispatch={dispatch}>
Expand All @@ -73,8 +47,6 @@ describe("TopNav", () => {
})

test("brings up an element with 'logged in as' text and a logout link when clicked", async () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])

const dispatch = jest.fn()
const user = userEvent.setup()
render(
Expand All @@ -99,8 +71,6 @@ describe("TopNav", () => {
})

test("clicking the 'User Info' button again makes the 'Logged in as' popover disappear", async () => {
jest.mocked(getTestGroups).mockReturnValue([TestGroups.KeycloakSso])

const dispatch = jest.fn()
const user = userEvent.setup()
render(
Expand Down

0 comments on commit 44c33ec

Please sign in to comment.