Skip to content

Commit eb03e15

Browse files
committed
fix: stabilize footer disclosure tests
1 parent 7590fbb commit eb03e15

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/__tests__/ui-design-contract.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ describe("restored UI design contract", () => {
144144
expect(navSource).toContain('label: "Powered by Convex"');
145145

146146
expect(footerSource).toContain('className="footer-col-toggle"');
147-
expect(footerSource).toContain("aria-expanded={isOpen}");
147+
expect(footerSource).toContain("const ariaExpanded = isMobile ? isOpen : true");
148+
expect(footerSource).toContain("aria-expanded={ariaExpanded}");
148149
expect(footerSource).toContain("data-open={isOpen}");
149150
expect(footerSource).toContain("toggleSection(section.title)");
150151

src/components/Footer.test.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* @vitest-environment jsdom */
22

3-
import { fireEvent, render, screen, within } from "@testing-library/react";
3+
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
44
import type { ReactNode } from "react";
5-
import { describe, expect, it, vi } from "vitest";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
66

77
vi.mock("@tanstack/react-router", () => ({
88
Link: (props: { children: ReactNode; to?: string }) => (
@@ -13,6 +13,21 @@ vi.mock("@tanstack/react-router", () => ({
1313
import { Footer } from "./Footer";
1414

1515
describe("Footer", () => {
16+
afterEach(() => {
17+
vi.unstubAllGlobals();
18+
});
19+
20+
function mockMatchMedia(matches: boolean) {
21+
vi.stubGlobal(
22+
"matchMedia",
23+
vi.fn().mockImplementation(() => ({
24+
matches,
25+
addEventListener: vi.fn(),
26+
removeEventListener: vi.fn(),
27+
})),
28+
);
29+
}
30+
1631
it("renders the restored four-column public footer", () => {
1732
const { container } = render(<Footer />);
1833

@@ -72,7 +87,8 @@ describe("Footer", () => {
7287
).toBe("https://www.convex.dev");
7388
});
7489

75-
it("collapses footer sections by heading until toggled open", () => {
90+
it("collapses footer sections by heading until toggled open", async () => {
91+
mockMatchMedia(true);
7692
render(<Footer />);
7793

7894
const browseToggle = screen.getByRole("button", { name: "Browse" });
@@ -82,7 +98,7 @@ describe("Footer", () => {
8298

8399
expect(browseLinks).not.toBeNull();
84100
expect(platformLinks).not.toBeNull();
85-
expect(browseToggle.getAttribute("aria-expanded")).toBe("false");
101+
await waitFor(() => expect(browseToggle.getAttribute("aria-expanded")).toBe("false"));
86102
expect(browseLinks?.getAttribute("data-open")).toBe("false");
87103
expect(platformToggle.getAttribute("aria-expanded")).toBe("false");
88104
expect(platformLinks?.getAttribute("data-open")).toBe("false");

src/components/Footer.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function Footer() {
2020
const [isMobile, setIsMobile] = useState(false);
2121

2222
useEffect(() => {
23+
if (typeof window.matchMedia !== "function") {
24+
setIsMobile(false);
25+
return () => {};
26+
}
27+
2328
const mq = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT}px)`);
2429
setIsMobile(mq.matches);
2530
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);

0 commit comments

Comments
 (0)