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

settingsLayout.test.tsx

115 lines3.8 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 SettingsUnavailableGroup,
9} from "./settingsLayout";
10​
11afterEach(() => {
12 vi.unstubAllGlobals();
13});
14​
15describe("unavailable settings", () => {
16 it("groups disabled controls under one reason", () => {
17 const markup = renderToStaticMarkup(
18 <SettingsUnavailableGroup message="Only available in the desktop app.">
19 <SettingsRow title="Window capture" description="Capture a window." />
20 </SettingsUnavailableGroup>,
21 );
22​
23 expect(markup).toContain("Only available in the desktop app.");
24 expect(markup).toContain("border-border/60");
25 expect(markup).toContain("[&amp;_h3]:opacity-64");
26 });
27});
28​
29describe("settings search targets", () => {
30 it("does not persist destination styling in the rendered row", () => {
31 const markup = renderToStaticMarkup(
32 <SettingsSearchTargetProvider targetId="word-wrap">
33 <SettingsRow id="word-wrap" title="Word wrap" description="Wrap long lines." />
34 <SettingsRow id="time-format" title="Time format" description="Choose a clock." />
35 </SettingsSearchTargetProvider>,
36 );
37​
38 expect(markup).toContain('id="word-wrap" tabindex="-1"');
39 expect(markup).not.toContain("data-settings-search-target");
40 expect(markup).not.toContain("settings-search-target-pulse");
41 });
42​
43 it("scrolls directly to a section header and restarts the destination pulse", () => {
44 const sectionScrollIntoView = vi.fn();
45 const headerScrollIntoView = vi.fn();
46 const focus = vi.fn();
47 const remove = vi.fn();
48 const add = vi.fn();
49 const addEventListener = vi.fn();
50 const target = {
51 tagName: "SECTION",
52 firstElementChild: { scrollIntoView: headerScrollIntoView },
53 scrollIntoView: sectionScrollIntoView,
54 focus,
55 classList: { remove, add },
56 addEventListener,
57 offsetWidth: 100,
58 } as unknown as HTMLElement;
59 vi.stubGlobal("document", {
60 getElementById: vi.fn(() => target),
61 });
62 vi.stubGlobal("window", {
63 matchMedia: vi.fn(() => ({ matches: false })),
64 });
65​
66 expect(scrollToSettingsTarget("providers")).toBe(true);
67 expect(headerScrollIntoView).toHaveBeenCalledWith({
68 behavior: "smooth",
69 block: "center",
70 });
71 expect(sectionScrollIntoView).not.toHaveBeenCalled();
72 expect(focus).toHaveBeenCalledWith({ preventScroll: true });
73 expect(remove).toHaveBeenCalledWith("settings-search-target-pulse");
74 expect(add).toHaveBeenCalledWith("settings-search-target-pulse");
75 expect(addEventListener).toHaveBeenCalledWith("blur", expect.any(Function), { once: true });
76 });
77​
78 it("does not animate the destination when reduced motion is requested", () => {
79 const scrollIntoView = vi.fn();
80 const focus = vi.fn();
81 const remove = vi.fn();
82 const add = vi.fn();
83 const target = {
84 tagName: "DIV",
85 firstElementChild: null,
86 scrollIntoView,
87 focus,
88 classList: { remove, add },
89 offsetWidth: 100,
90 } as unknown as HTMLElement;
91 vi.stubGlobal("document", {
92 getElementById: vi.fn(() => target),
93 });
94 vi.stubGlobal("window", {
95 matchMedia: vi.fn(() => ({ matches: true })),
96 });
97​
98 expect(scrollToSettingsTarget("word-wrap")).toBe(true);
99 expect(scrollIntoView).toHaveBeenCalledWith({
100 behavior: "auto",
101 block: "center",
102 });
103 expect(focus).toHaveBeenCalledWith({ preventScroll: true });
104 expect(remove).toHaveBeenCalledWith("settings-search-target-pulse");
105 expect(add).not.toHaveBeenCalled();
106 });
107​
108 it("leaves not-yet-mounted destinations to their mount lifecycle", () => {
109 vi.stubGlobal("document", {
110 getElementById: vi.fn(() => null),
111 });
112​
113 expect(scrollToSettingsTarget("archive")).toBe(false);
114 });
115});