forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.test.tsx
More file actions
215 lines (181 loc) · 8.35 KB
/
Copy patherror.test.tsx
File metadata and controls
215 lines (181 loc) · 8.35 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
205
206
207
208
209
210
211
212
213
214
215
/**
* Tests for app/error.js — the route-level error boundary.
*
* Strategy:
* - Mock ErrorBanner and reportError so tests are isolated from their
* implementations and we can assert on call arguments.
* - Verify copy strings, ARIA roles, reset handler wiring, and a11y.
*/
import "@testing-library/jest-dom";
import { act, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { axe } from "jest-axe";
import React from "react";
// ── Module mocks ──────────────────────────────────────────────────────────────
jest.mock("../lib/observability/reportError", () => ({
reportError: jest.fn(),
}));
jest.mock("../components/ErrorBanner", () => {
function MockErrorBanner({ title, description, actionLabel, onAction, previewLabel, variant }) {
return (
<div
role="alert"
aria-live="assertive"
data-testid="error-banner"
data-variant={variant}
data-preview={previewLabel}
>
<h2>{title}</h2>
<p>{description}</p>
{actionLabel && (
<button type="button" onClick={onAction} data-testid="error-action-btn">
{actionLabel}
</button>
)}
</div>
);
}
MockErrorBanner.displayName = "MockErrorBanner";
return MockErrorBanner;
});
// ── Import SUT after mocks are wired ─────────────────────────────────────────
import GlobalError from "./error";
import { reportError } from "../lib/observability/reportError";
import { copy } from "./copy/en";
// ── Helpers ───────────────────────────────────────────────────────────────────
function makeError(message = "Test error", digest = undefined) {
const err = new Error(message);
if (digest !== undefined) err.digest = digest;
return err;
}
function renderError(error = makeError(), reset = jest.fn()) {
return render(<GlobalError error={error} reset={reset} />);
}
// ── Tests ─────────────────────────────────────────────────────────────────────
describe("GlobalError (app/error.js)", () => {
beforeEach(() => {
jest.clearAllMocks();
});
// ── Rendering ───────────────────────────────────────────────────────────────
describe("rendering", () => {
it("renders the branded error page container", () => {
renderError();
expect(screen.getByTestId("error-boundary-page")).toBeInTheDocument();
});
it("renders an ErrorBanner with the correct copy", () => {
renderError();
const banner = screen.getByTestId("error-banner");
expect(banner).toBeInTheDocument();
// title appears in both the sr-only h1 and the ErrorBanner mock's h2
expect(screen.getAllByText(copy.error.title).length).toBeGreaterThanOrEqual(1);
expect(screen.getByText(copy.error.description)).toBeInTheDocument();
});
it("passes the 'server' variant to ErrorBanner", () => {
renderError();
expect(screen.getByTestId("error-banner")).toHaveAttribute("data-variant", "server");
});
it("passes previewLabel copy to ErrorBanner", () => {
renderError();
expect(screen.getByTestId("error-banner")).toHaveAttribute(
"data-preview",
copy.error.previewLabel
);
});
it("renders the action button with the correct copy label", () => {
renderError();
expect(screen.getByTestId("error-action-btn")).toHaveTextContent(copy.error.actionLabel);
});
it("renders the visually-hidden h1 heading", () => {
renderError();
// The sr-only h1 should be present in the DOM for screen readers
const h1 = document.querySelector("h1.sr-only");
expect(h1).toBeInTheDocument();
expect(h1).toHaveTextContent(copy.error.title);
});
it("renders a main landmark with the correct aria-labelledby", () => {
renderError();
const main = screen.getByRole("main");
expect(main).toHaveAttribute("aria-labelledby", "error-boundary-heading");
});
});
// ── Error reporting ──────────────────────────────────────────────────────────
describe("error reporting", () => {
it("calls reportError with the error on mount", () => {
const error = makeError("boom");
renderError(error);
expect(reportError).toHaveBeenCalledTimes(1);
expect(reportError).toHaveBeenCalledWith(error, { digest: undefined });
});
it("forwards error.digest to reportError when present", () => {
const error = makeError("server boom", "abc-123");
renderError(error);
expect(reportError).toHaveBeenCalledWith(error, { digest: "abc-123" });
});
it("does not crash when error has no digest property", () => {
const error = makeError("no digest");
delete error.digest;
expect(() => renderError(error)).not.toThrow();
expect(reportError).toHaveBeenCalledWith(error, { digest: undefined });
});
it("re-reports when the error prop changes", () => {
const error1 = makeError("first");
const { rerender } = render(<GlobalError error={error1} reset={jest.fn()} />);
expect(reportError).toHaveBeenCalledTimes(1);
const error2 = makeError("second");
act(() => {
rerender(<GlobalError error={error2} reset={jest.fn()} />);
});
expect(reportError).toHaveBeenCalledTimes(2);
expect(reportError).toHaveBeenLastCalledWith(error2, { digest: undefined });
});
});
// ── Reset handler ────────────────────────────────────────────────────────────
describe("reset handler", () => {
it("calls the reset prop when the action button is clicked", async () => {
const reset = jest.fn();
renderError(makeError(), reset);
await userEvent.click(screen.getByTestId("error-action-btn"));
expect(reset).toHaveBeenCalledTimes(1);
});
it("allows reset to be called multiple times (idempotent)", async () => {
const reset = jest.fn();
renderError(makeError(), reset);
const btn = screen.getByTestId("error-action-btn");
await userEvent.click(btn);
await userEvent.click(btn);
await userEvent.click(btn);
expect(reset).toHaveBeenCalledTimes(3);
});
it("does not call reportError again when reset is clicked", async () => {
const reset = jest.fn();
renderError(makeError(), reset);
jest.clearAllMocks();
await userEvent.click(screen.getByTestId("error-action-btn"));
expect(reportError).not.toHaveBeenCalled();
});
});
// ── Accessibility ────────────────────────────────────────────────────────────
describe("accessibility", () => {
it("the error banner region has role=alert", () => {
renderError();
expect(screen.getByRole("alert")).toBeInTheDocument();
});
it("the error banner region has aria-live=assertive", () => {
renderError();
expect(screen.getByRole("alert")).toHaveAttribute("aria-live", "assertive");
});
it("has no axe violations", async () => {
const { container } = renderError();
const results = await axe(container);
expect(results).toHaveNoViolations();
});
});
// ── Dark theme background ────────────────────────────────────────────────────
describe("theme / styling", () => {
it("applies the dark slate-950 background class to the page wrapper", () => {
renderError();
const page = screen.getByTestId("error-boundary-page");
expect(page.className).toContain("bg-slate-950");
});
});
});