Open History to see the last commit
1import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";
2
3import { preserveReadingPosition } from "./preserveReadingPosition";
4
5describe("preserveReadingPosition", () => {
6 it("keeps the reader's line through a resize that lands before the first layout report", async () => {
7 const scroller = document.createElement("div");
8 scroller.style.cssText =
9 "width: 600px; height: 300px; overflow-y: auto; font: 16px/20px serif;";
10 // The marker fits one line at both widths, so whichever caret the probe picks shares its top.
11 scroller.innerHTML = Array.from({ length: 30 }, (_, index) =>
12 index === 15
13 ? "<p>Marker line.</p>"
14 : `<p>${"Filler words that wrap across several lines. ".repeat(8)}</p>`,
15 ).join("");
16 document.body.append(scroller);
17 onTestFinished(() => scroller.remove());
18 const markerText = [...scroller.querySelectorAll("p")][15]!.firstChild!;
19 const markerTop = () => {
20 const range = document.createRange();
21 range.setStart(markerText, 0);
22 range.collapse(true);
23 return range.getBoundingClientRect().top - scroller.getBoundingClientRect().top;
24 };
25
26 const stop = preserveReadingPosition(scroller);
27 onTestFinished(stop);
28 scroller.scrollTop += markerTop() - 24;
29 scroller.dispatchEvent(new Event("scroll"));
30 const before = markerTop();
31 scroller.style.width = "300px";
32
33 await vi.waitFor(() => expect(Math.abs(markerTop() - before)).toBeLessThan(2));
34 });
35 it.each(["input", "textarea"])(
36 "anchors on text below a <%s> under the probe point",
37 async (tag) => {
38 const scroller = document.createElement("div");
39 scroller.style.cssText =
40 "width: 400px; height: 200px; overflow-y: auto; overflow-anchor: none; font: 16px/20px serif;";
41 scroller.innerHTML = `<p style="margin: 0">${"Filler words that wrap across several lines. ".repeat(12)}</p><${tag} style="display: block; width: 350px; height: 40px; margin: 0; box-sizing: border-box"></${tag}><p style="margin: 0">Below the field.</p><div style="height: 400px"></div>`;
42 document.body.append(scroller);
43 onTestFinished(() => scroller.remove());
44 const field = scroller.querySelector<HTMLInputElement | HTMLTextAreaElement>(tag)!;
45 field.value = "hello world from inside a text field";
46 const belowText = scroller.querySelectorAll("p")[1]!.firstChild!;
47 const belowTop = () => {
48 const range = document.createRange();
49 range.setStart(belowText, 0);
50 range.collapse(true);
51 return range.getBoundingClientRect().top - scroller.getBoundingClientRect().top;
52 };
53 const errors: unknown[] = [];
54 const onError = (event: ErrorEvent) => {
55 errors.push(event.error);
56 event.preventDefault();
57 };
58 window.addEventListener("error", onError);
59 onTestFinished(() => window.removeEventListener("error", onError));
60
61 const stop = preserveReadingPosition(scroller);
62 onTestFinished(stop);
63 scroller.scrollTop = field.offsetTop - scroller.offsetTop;
64 scroller.dispatchEvent(new Event("scroll"));
65 expect(errors).toEqual([]);
66 const before = belowTop();
67 scroller.style.width = "300px";
68 // Created after the helper's observer, so this delivery runs after its synchronous restore.
69 await new Promise<void>((resolve) => {
70 const observer = new ResizeObserver(() => {
71 observer.disconnect();
72 resolve();
73 });
74 observer.observe(scroller);
75 });
76
77 expect(scroller.clientWidth).toBeLessThan(400);
78 expect(Math.abs(belowTop() - before)).toBeLessThan(2);
79 expect(errors).toEqual([]);
80 },
81 );
82});