Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsLayout.test.tsx

100 lines3.3 KB

1import { renderToStaticMarkup } from "react-dom/server";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3​
4import {
5 scrollToSettingsTarget,
6 SettingsRow,
7 SettingsSearchTargetProvider,
8} from "./settingsLayout";
9​
10afterEach(() => {
11 vi.unstubAllGlobals();
12});
13​
14describe("settings search targets", () => {
15 it("does not persist destination styling in the rendered row", () => {
16 const markup = renderToStaticMarkup(
17 <SettingsSearchTargetProvider targetId="word-wrap">
18 <SettingsRow id="word-wrap" title="Word wrap" description="Wrap long lines." />
19 <SettingsRow id="time-format" title="Time format" description="Choose a clock." />
20 </SettingsSearchTargetProvider>,
21 );
22​
23 expect(markup).toContain('id="word-wrap" tabindex="-1"');
24 expect(markup).not.toContain("data-settings-search-target");
25 expect(markup).not.toContain("settings-search-target-pulse");
26 });
27​
28 it("scrolls directly to a section header and restarts the destination pulse", () => {
29 const sectionScrollIntoView = vi.fn();
30 const headerScrollIntoView = vi.fn();
31 const focus = vi.fn();
32 const remove = vi.fn();
33 const add = vi.fn();
34 const addEventListener = vi.fn();
35 const target = {
36 tagName: "SECTION",
37 firstElementChild: { scrollIntoView: headerScrollIntoView },
38 scrollIntoView: sectionScrollIntoView,
39 focus,
40 classList: { remove, add },
41 addEventListener,
42 offsetWidth: 100,
43 } as unknown as HTMLElement;
44 vi.stubGlobal("document", {
45 getElementById: vi.fn(() => target),
46 });
47 vi.stubGlobal("window", {
48 matchMedia: vi.fn(() => ({ matches: false })),
49 });
50​
51 expect(scrollToSettingsTarget("providers")).toBe(true);
52 expect(headerScrollIntoView).toHaveBeenCalledWith({
53 behavior: "smooth",
54 block: "center",
55 });
56 expect(sectionScrollIntoView).not.toHaveBeenCalled();
57 expect(focus).toHaveBeenCalledWith({ preventScroll: true });
58 expect(remove).toHaveBeenCalledWith("settings-search-target-pulse");
59 expect(add).toHaveBeenCalledWith("settings-search-target-pulse");
60 expect(addEventListener).toHaveBeenCalledWith("blur", expect.any(Function), { once: true });
61 });
62​
63 it("does not animate the destination when reduced motion is requested", () => {
64 const scrollIntoView = vi.fn();
65 const focus = vi.fn();
66 const remove = vi.fn();
67 const add = vi.fn();
68 const target = {
69 tagName: "DIV",
70 firstElementChild: null,
71 scrollIntoView,
72 focus,
73 classList: { remove, add },
74 offsetWidth: 100,
75 } as unknown as HTMLElement;
76 vi.stubGlobal("document", {
77 getElementById: vi.fn(() => target),
78 });
79 vi.stubGlobal("window", {
80 matchMedia: vi.fn(() => ({ matches: true })),
81 });
82​
83 expect(scrollToSettingsTarget("word-wrap")).toBe(true);
84 expect(scrollIntoView).toHaveBeenCalledWith({
85 behavior: "auto",
86 block: "center",
87 });
88 expect(focus).toHaveBeenCalledWith({ preventScroll: true });
89 expect(remove).toHaveBeenCalledWith("settings-search-target-pulse");
90 expect(add).not.toHaveBeenCalled();
91 });
92​
93 it("leaves not-yet-mounted destinations to their mount lifecycle", () => {
94 vi.stubGlobal("document", {
95 getElementById: vi.fn(() => null),
96 });
97​
98 expect(scrollToSettingsTarget("archive")).toBe(false);
99 });
100});