Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
3import { scrollToSettingsTarget } from "./settingsLayout";
4
5const PULSE = "settings-search-target-pulse";
6
7afterEach(() => {
8 document.body.replaceChildren();
9 vi.restoreAllMocks();
10});
11
12function mount(html: string) {
13 document.body.innerHTML = html;
14 return vi.spyOn(HTMLElement.prototype, "scrollIntoView").mockImplementation(() => {});
15}
16
17function reduceMotion(matches: boolean) {
18 vi.spyOn(window, "matchMedia").mockReturnValue({ matches } as MediaQueryList);
19}
20
21describe("settings search targets", () => {
22 it("scrolls a section's header into view, focuses the section, and pulses until blur", () => {
23 reduceMotion(false);
24 const scrollIntoView = mount(
25 `<section id="providers" tabindex="-1"><h2>Providers</h2><p>Body</p></section>`,
26 );
27 const section = document.getElementById("providers")!;
28
29 expect(scrollToSettingsTarget("providers")).toBe(true);
30 expect(scrollIntoView).toHaveBeenCalledOnce();
31 expect(scrollIntoView.mock.contexts[0]).toBe(section.querySelector("h2"));
32 expect(scrollIntoView).toHaveBeenCalledWith({ behavior: "smooth", block: "center" });
33 expect(document.activeElement).toBe(section);
34 expect(section.classList.contains(PULSE)).toBe(true);
35
36 section.blur();
37 expect(section.classList.contains(PULSE)).toBe(false);
38 });
39
40 it("restarts the pulse when the same destination is chosen again", () => {
41 reduceMotion(false);
42 mount(`<div id="word-wrap" tabindex="-1" class="${PULSE}"></div>`);
43 const target = document.getElementById("word-wrap")!;
44 const observer = new MutationObserver(() => {});
45 observer.observe(target, { attributeFilter: ["class"], attributeOldValue: true });
46
47 scrollToSettingsTarget("word-wrap");
48 const previousClasses = observer.takeRecords().map((record) => record.oldValue);
49 expect(previousClasses).toEqual([PULSE, ""]);
50 expect(target.classList.contains(PULSE)).toBe(true);
51 });
52
53 it("does not animate the destination when reduced motion is requested", () => {
54 reduceMotion(true);
55 const scrollIntoView = mount(`<div id="word-wrap" tabindex="-1"></div>`);
56 const target = document.getElementById("word-wrap")!;
57
58 expect(scrollToSettingsTarget("word-wrap")).toBe(true);
59 expect(scrollIntoView.mock.contexts[0]).toBe(target);
60 expect(scrollIntoView).toHaveBeenCalledWith({ behavior: "auto", block: "center" });
61 expect(document.activeElement).toBe(target);
62 expect(target.classList.contains(PULSE)).toBe(false);
63 });
64
65 it.each(["before", "after", "without animation"] as const)(
66 "fully reveals the row when the opening style clears: %s",
67 async (phase) => {
68 // Keep scrolling synchronous while the panel height transitions independently.
69 reduceMotion(true);
70 document.body.innerHTML = `
71 <div id="viewport" style="height: 200px; overflow: auto; overflow-anchor: none">
72 <div id="panel" data-slot="collapsible-panel" data-open data-starting-style
73 style="height: 0; overflow: hidden; transition: height 200ms linear">
74 <div style="height: 500px"></div>
75 <div id="legacy-sidebar" tabindex="-1" style="height: 40px">Legacy sidebar</div>
76 <div style="height: 60px"></div>
77 </div>
78 </div>`;
79 const viewport = document.getElementById("viewport")!;
80 const panel = document.getElementById("panel")!;
81 const target = document.getElementById("legacy-sidebar")!;
82
83 void panel.offsetHeight;
84 const animated = phase !== "without animation";
85 if (animated) {
86 panel.style.height = "600px";
87 } else {
88 panel.style.transition = "none";
89 }
90 const transition = panel.getAnimations()[0];
91 if (animated) {
92 expect(transition).toBeDefined();
93 transition!.pause();
94 transition!.currentTime = phase === "before" ? 0 : 100;
95 if (phase === "after") panel.removeAttribute("data-starting-style");
96 }
97
98 expect(scrollToSettingsTarget("legacy-sidebar")).toBe(true);
99 expect(document.activeElement).toBe(target);
100 const opened = animated
101 ? new Promise<void>((resolve) => {
102 panel.addEventListener("transitionend", () => resolve(), { once: true });
103 })
104 : Promise.resolve();
105 panel.style.height = "600px";
106 panel.removeAttribute("data-starting-style");
107 transition?.finish();
108 await opened;
109
110 const bounds = viewport.getBoundingClientRect();
111 const row = target.getBoundingClientRect();
112 expect(row.top).toBeGreaterThanOrEqual(bounds.top);
113 expect(row.bottom).toBeLessThanOrEqual(bounds.bottom);
114 expect(document.activeElement).toBe(target);
115 },
116 );
117
118 it("keeps the newer search destination visible when an earlier panel finishes opening", async () => {
119 reduceMotion(true);
120 document.body.innerHTML = `
121 <div id="viewport" style="height: 200px; overflow: auto; overflow-anchor: none">
122 <div id="new-target" tabindex="-1" style="height: 40px">New target</div>
123 <div style="height: 1000px"></div>
124 <div id="panel" data-slot="collapsible-panel" data-open data-starting-style
125 style="height: 0; overflow: hidden; transition: height 200ms linear">
126 <div style="height: 500px"></div>
127 <div id="legacy-sidebar" tabindex="-1" style="height: 40px">Legacy sidebar</div>
128 <div style="height: 60px"></div>
129 </div>
130 </div>`;
131 const panel = document.getElementById("panel")!;
132 void panel.offsetHeight;
133 panel.style.height = "600px";
134 const transition = panel.getAnimations()[0]!;
135 transition.pause();
136 transition.currentTime = 100;
137
138 scrollToSettingsTarget("legacy-sidebar");
139 panel.removeAttribute("data-starting-style");
140 scrollToSettingsTarget("new-target");
141 const opened = new Promise<void>((resolve) => {
142 panel.addEventListener("transitionend", () => resolve(), { once: true });
143 });
144 transition.finish();
145 await opened;
146
147 const viewport = document.getElementById("viewport")!;
148 const target = document.getElementById("new-target")!;
149 expect(target.getBoundingClientRect().top).toBeGreaterThanOrEqual(
150 viewport.getBoundingClientRect().top,
151 );
152 expect(target.getBoundingClientRect().bottom).toBeLessThanOrEqual(
153 viewport.getBoundingClientRect().bottom,
154 );
155 expect(document.activeElement).toBe(target);
156 });
157
158 it("leaves not-yet-mounted destinations to their mount lifecycle", () => {
159 expect(scrollToSettingsTarget("archive")).toBe(false);
160 });
161});