Skip to content

Commit 71b8149

Browse files
test coverage
1 parent d34876e commit 71b8149

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from "react"
2+
import {
3+
MemoryRouter,
4+
Route,
5+
Routes,
6+
useLocation
7+
} from "react-router-dom"
8+
import {render, screen} from "@testing-library/react"
9+
import "@testing-library/jest-dom"
10+
11+
import EpsTabs, {TabHeader} from "@/components/EpsTabs"
12+
13+
function LocationIndicator() {
14+
const location = useLocation()
15+
return <div data-testid="current-path">{location.pathname}</div>
16+
}
17+
18+
function Harness({variant = "default" as const}) {
19+
const location = useLocation()
20+
const tabHeaderArray: Array<TabHeader> = [
21+
{title: "(1)", link: "/prescription-list-current"},
22+
{title: "(2)", link: "/prescription-list-future"},
23+
{title: "(3)", link: "/prescription-list-past"}
24+
]
25+
26+
return (
27+
<EpsTabs
28+
activeTabPath={location.pathname}
29+
tabHeaderArray={tabHeaderArray}
30+
variant={variant}
31+
>
32+
<div>
33+
<input data-testid="dummy-input" />
34+
<LocationIndicator />
35+
<div data-testid="panel-content">Panel</div>
36+
</div>
37+
</EpsTabs>
38+
)
39+
}
40+
41+
describe("EpsTabs", () => {
42+
it("navigates between tabs with ArrowRight/ArrowLeft", () => {
43+
render(
44+
<MemoryRouter initialEntries={["/prescription-list-current"]}>
45+
<Routes>
46+
<Route path="*" element={<Harness />} />
47+
</Routes>
48+
</MemoryRouter>
49+
)
50+
51+
// Starts on current
52+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-current")
53+
54+
// ArrowRight to future
55+
window.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowRight"}))
56+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-future")
57+
58+
// ArrowRight to past
59+
window.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowRight"}))
60+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-past")
61+
62+
// ArrowRight at last stays on past
63+
window.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowRight"}))
64+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-past")
65+
66+
// ArrowLeft goes back to future
67+
window.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowLeft"}))
68+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-future")
69+
})
70+
71+
it("does not navigate when focus is inside an input", () => {
72+
render(
73+
<MemoryRouter initialEntries={["/prescription-list-current"]}>
74+
<Routes>
75+
<Route path="*" element={<Harness />} />
76+
</Routes>
77+
</MemoryRouter>
78+
)
79+
80+
// Focus input then press ArrowRight – should not change tab
81+
const input = screen.getByTestId("dummy-input") as HTMLInputElement
82+
input.focus()
83+
window.dispatchEvent(new KeyboardEvent("keydown", {key: "ArrowRight"}))
84+
expect(screen.getByTestId("current-path")).toHaveTextContent("/prescription-list-current")
85+
})
86+
87+
it("applies the large variant class", () => {
88+
render(
89+
<MemoryRouter initialEntries={["/prescription-list-current"]}>
90+
<Routes>
91+
<Route path="*" element={<Harness variant="large" />} />
92+
</Routes>
93+
</MemoryRouter>
94+
)
95+
96+
// Container should have the large variant class
97+
const contentsTitle = screen.getByText("Contents")
98+
const container = contentsTitle.closest(".nhsuk-tabs--large")
99+
expect(container).toBeInTheDocument()
100+
})
101+
})

0 commit comments

Comments
 (0)