forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.test.tsx
More file actions
204 lines (174 loc) · 5.8 KB
/
Copy pathlayout.test.tsx
File metadata and controls
204 lines (174 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Tests for the skip-to-content link and root layout shell (app/layout.js).
*
* Strategy:
* - Unit tests use a minimal SkipLinkFixture that mirrors the exact markup
* injected by RootLayout, avoiding html/body nesting issues in jsdom.
* - Integration test imports the real RootLayout to confirm the link is present.
*/
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import { axe } from "jest-axe";
import Link from "next/link";
import React from "react";
// ── Mock RootLayout's external dependencies ───────────────────────────────────
jest.mock("../components/Footer", () => {
function MockFooter() {
return <footer data-testid="mock-footer" />;
}
MockFooter.displayName = "MockFooter";
return MockFooter;
});
jest.mock("../components/ToastProvider", () => {
const React = require("react");
const ToastContext = React.createContext(null);
return {
ToastProvider({ children }: { children: React.ReactNode }) {
return <>{children}</>;
},
ToastContext,
};
});
jest.mock("../components/ShortcutHelpDialog", () => ({
__esModule: true,
default: function MockShortcutHelpDialog() {
return null;
},
}));
jest.mock("../components/WalletProvider", () => ({
WalletProvider({ children }) {
const React = require("react");
return React.createElement(React.Fragment, null, children);
},
}));
// ── Minimal fixture matching the skip link markup in RootLayout ───────────────
function SkipLinkFixture({ children }: { children?: React.ReactNode }) {
return (
<div>
<a href="#main-content" className="skip-link">
Skip to content
</a>
{children}
</div>
);
}
// ── Unit tests: skip link properties and DOM ordering ────────────────────────
describe("Skip-to-content link", () => {
it("renders with an accessible label", () => {
render(<SkipLinkFixture />);
expect(screen.getByRole("link", { name: /skip to content/i })).toBeInTheDocument();
});
it("targets the #main-content landmark", () => {
render(<SkipLinkFixture />);
expect(screen.getByRole("link", { name: /skip to content/i })).toHaveAttribute(
"href",
"#main-content"
);
});
it("is the first focusable element in the document", () => {
render(
<SkipLinkFixture>
<main id="main-content">
<Link href="/invoices">Invoices</Link>
<button type="button">Click me</button>
</main>
</SkipLinkFixture>
);
const skipLink = screen.getByRole("link", { name: /skip to content/i });
const allFocusable = Array.from(
document.querySelectorAll<HTMLElement>(
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
)
);
expect(allFocusable.length).toBeGreaterThan(0);
expect(allFocusable[0]).toBe(skipLink);
});
it("comes before any other links in the DOM", () => {
render(
<SkipLinkFixture>
<main id="main-content">
<a href="/invoices">Invoices</a>
<Link href="/invest">Invest</Link>
</main>
</SkipLinkFixture>
);
const allLinks = screen.getAllByRole("link");
expect(allLinks[0]).toHaveAttribute("href", "#main-content");
});
it("programmatic focus reaches #main-content when skip link is activated", () => {
render(
<SkipLinkFixture>
{/* tabIndex={-1} lets the element receive programmatic focus */}
<main id="main-content" tabIndex={-1}>
Page content
</main>
</SkipLinkFixture>
);
const main = document.getElementById("main-content") as HTMLElement;
main.focus();
expect(document.activeElement).toBe(main);
});
it("has no axe violations", async () => {
const { container } = render(
<SkipLinkFixture>
<main id="main-content">
<h1>Page heading</h1>
<p>Page content</p>
</main>
</SkipLinkFixture>
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
});
// ── Integration: confirm RootLayout renders the skip link ────────────────────
import RootLayout from "./layout";
describe("RootLayout", () => {
it("renders the skip link", () => {
render(
<RootLayout>
<main id="main-content">Page</main>
</RootLayout>
);
expect(screen.getByRole("link", { name: /skip to content/i })).toBeInTheDocument();
});
it("skip link in RootLayout targets #main-content", () => {
render(
<RootLayout>
<main id="main-content">Page</main>
</RootLayout>
);
expect(screen.getByRole("link", { name: /skip to content/i })).toHaveAttribute(
"href",
"#main-content"
);
});
it("skip link in RootLayout is the first focusable element", () => {
render(
<RootLayout>
<main id="main-content">
<Link href="/invoices">Invoices</Link>
</main>
</RootLayout>
);
const skipLink = screen.getByRole("link", { name: /skip to content/i });
const allFocusable = Array.from(
document.querySelectorAll<HTMLElement>(
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
)
);
expect(allFocusable[0]).toBe(skipLink);
});
it("has no axe violations", async () => {
const { container } = render(
<RootLayout>
<main id="main-content">
<h1>Page heading</h1>
<p>Content</p>
</main>
</RootLayout>
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
});