Skip to content

Commit f5f36c4

Browse files
VIA-512 Change header when logged out to remove link on logo and service name
1 parent e669ed4 commit f5f36c4

File tree

2 files changed

+75
-34
lines changed

2 files changed

+75
-34
lines changed

src/app/_components/nhs-frontend/AppHeader.test.tsx

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ jest.mock("@src/utils/auth/user-logout", () => ({
1717
userLogout: jest.fn(),
1818
}));
1919

20-
const testHeader = (expectedVisible: boolean) => {
20+
const expectHeaderVisible = (expectedVisible: boolean) => {
2121
const serviceLink = screen.queryByRole("link", {
2222
name: "Check and book an RSV vaccination",
2323
});
2424
const logoLink = screen.queryByRole("link", { name: "NHS homepage" });
25+
2526
if (expectedVisible) {
2627
expect(serviceLink).toBeVisible();
27-
expect(serviceLink?.getAttribute("href")).toEqual("/check-and-book-rsv");
2828
expect(logoLink).toBeVisible();
29-
expect(logoLink?.getAttribute("href")).toEqual("/check-and-book-rsv");
3029
} else {
3130
expect(serviceLink).toBeNull();
3231
expect(logoLink).toBeNull();
@@ -46,28 +45,61 @@ describe("AppHeader", () => {
4645
});
4746
});
4847

49-
it("shows the app header with logout link when authenticated", async () => {
50-
mockSession = { status: "authenticated" };
51-
render(<AppHeader />);
52-
testHeader(true);
53-
const logoutLink = screen.getByRole("link", { name: "Log out" });
54-
expect(logoutLink).toBeVisible();
55-
expect(logoutLink?.getAttribute("href")).toEqual("#");
56-
});
48+
describe("when authenticated", () => {
49+
beforeEach(() => {
50+
mockSession = { status: "authenticated" };
51+
});
5752

58-
it("shows the app header without logout link when unauthenticated", async () => {
59-
mockSession = { status: "unauthenticated" };
60-
render(<AppHeader />);
61-
testHeader(true);
62-
const logoutLink = screen.queryByRole("link", { name: "Log out" });
63-
expect(logoutLink).toBeNull();
53+
it("shows the app header with logout link", async () => {
54+
render(<AppHeader />);
55+
expectHeaderVisible(true);
56+
const logoutLink = screen.getByRole("link", { name: "Log out" });
57+
expect(logoutLink).toBeVisible();
58+
expect(logoutLink?.getAttribute("href")).toEqual("#");
59+
});
60+
61+
it("logo and service name should link to service homepage", async () => {
62+
render(<AppHeader />);
63+
64+
const logoLink = screen.queryByRole("link", { name: "NHS homepage" });
65+
const serviceLink = screen.queryByRole("link", {
66+
name: "Check and book an RSV vaccination",
67+
});
68+
expect(serviceLink?.getAttribute("href")).toEqual("/check-and-book-rsv");
69+
expect(logoLink?.getAttribute("href")).toEqual("/check-and-book-rsv");
70+
});
71+
72+
it("logs out on click", async () => {
73+
render(<AppHeader />);
74+
screen.getByRole("link", { name: "Log out" }).click();
75+
expect(userLogout).toHaveBeenCalled();
76+
});
6477
});
6578

66-
it("logs out on click", async () => {
67-
mockSession = { status: "authenticated" };
68-
render(<AppHeader />);
69-
screen.getByRole("link", { name: "Log out" }).click();
70-
expect(userLogout).toHaveBeenCalled();
79+
describe("when unauthenticated", () => {
80+
beforeEach(() => {
81+
mockSession = { status: "unauthenticated" };
82+
});
83+
84+
it("shows the app header without logout link", async () => {
85+
mockSession = { status: "unauthenticated" };
86+
render(<AppHeader />);
87+
expectHeaderVisible(true);
88+
const logoutLink = screen.queryByRole("link", { name: "Log out" });
89+
expect(logoutLink).toBeNull();
90+
});
91+
92+
it("should not include link destination for logo and service name", async () => {
93+
mockSession = { status: "unauthenticated" };
94+
render(<AppHeader />);
95+
96+
const logoLink = screen.queryByRole("link", { name: "NHS homepage" });
97+
const serviceLink = screen.queryByRole("link", {
98+
name: "Check and book an RSV vaccination",
99+
});
100+
expect(serviceLink?.getAttribute("href")).toEqual("#");
101+
expect(logoLink?.getAttribute("href")).toEqual("#");
102+
});
71103
});
72104
});
73105

@@ -81,7 +113,7 @@ describe("AppHeader", () => {
81113
});
82114

83115
it("hides the app header", async () => {
84-
testHeader(false);
116+
expectHeaderVisible(false);
85117
});
86118
});
87119

@@ -95,7 +127,7 @@ describe("AppHeader", () => {
95127
});
96128

97129
it("hides the app header", async () => {
98-
testHeader(false);
130+
expectHeaderVisible(false);
99131
});
100132
});
101133
});

src/app/_components/nhs-frontend/AppHeader.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,35 @@ const handleClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
1414

1515
const AppHeader = () => {
1616
const { hasContextLoaded, isOpenInMobileApp } = useBrowserContext();
17-
const [showLogout, setShowLogout] = useState<boolean>(false);
17+
const [userIsLoggedIn, setUserIsLoggedIn] = useState<boolean>(false);
1818
const { status } = useSession();
1919

2020
useEffect(() => {
21-
setShowLogout(status === "authenticated");
21+
setUserIsLoggedIn(status === "authenticated");
2222
}, [status]);
2323

2424
if (!hasContextLoaded || isOpenInMobileApp) return null;
2525

2626
return (
2727
<Header transactional>
2828
<Header.Container>
29-
<Header.Logo href={`${VACCINATIONS_HUB_PAGE_ROUTE}`} />
30-
<Header.ServiceName href={`${VACCINATIONS_HUB_PAGE_ROUTE}`}>{SERVICE_HEADING}</Header.ServiceName>
31-
{showLogout && (
32-
<div className={"nhsuk-header__transactional-service-name appHeaderLogoutContainer"}>
33-
<a className="nhsuk-link--reverse" href="#" onClick={handleClick}>
34-
Log out
35-
</a>
36-
</div>
29+
{userIsLoggedIn && (
30+
<>
31+
<Header.Logo href={`${VACCINATIONS_HUB_PAGE_ROUTE}`} />
32+
<Header.ServiceName href={`${VACCINATIONS_HUB_PAGE_ROUTE}`}>{SERVICE_HEADING}</Header.ServiceName>
33+
<div className={"nhsuk-header__transactional-service-name appHeaderLogoutContainer"}>
34+
<a className="nhsuk-link--reverse" href="#" onClick={handleClick}>
35+
Log out
36+
</a>
37+
</div>
38+
</>
39+
)}
40+
41+
{!userIsLoggedIn && (
42+
<>
43+
<Header.Logo href="#" />
44+
<Header.ServiceName href="#">{SERVICE_HEADING}</Header.ServiceName>
45+
</>
3746
)}
3847
</Header.Container>
3948
</Header>

0 commit comments

Comments
 (0)