Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2import { readThreadFindPosition } from "./threadFindPosition";
3
4afterEach(() => vi.restoreAllMocks());
5beforeEach(() => {
6 Object.defineProperty(Range.prototype, "getBoundingClientRect", {
7 configurable: true,
8 writable: true,
9 value: () => new DOMRect(),
10 });
11});
12
13function viewport(text = "COD4 COD4") {
14 const container = document.createElement("div");
15 container.innerHTML = `<div data-timeline-row-id="above"><div data-thread-find-text>COD4</div></div>
16 <div data-timeline-row-id="reading"><div data-thread-find-text>${text}</div></div>`;
17 vi.spyOn(container, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 100, 500, 400));
18 vi.spyOn(container.children[0]!, "getBoundingClientRect").mockReturnValue(
19 new DOMRect(0, 0, 500, 80),
20 );
21 vi.spyOn(container.children[1]!, "getBoundingClientRect").mockReturnValue(
22 new DOMRect(0, 50, 500, 300),
23 );
24 return container;
25}
26
27describe("reading position for find", () => {
28 it("skips earlier rows and occurrences above the viewport within the current message", () => {
29 const container = viewport();
30 vi.spyOn(Range.prototype, "getBoundingClientRect").mockImplementation(function (this: Range) {
31 return new DOMRect(0, this.startOffset === 0 ? 60 : 160, 40, 20);
32 });
33 expect(readThreadFindPosition(container, "COD4", (id) => id, 100)).toEqual({
34 entryId: "reading",
35 occurrence: 1,
36 });
37 });
38
39 it("starts after the current message when all its occurrences are above the viewport", () => {
40 const container = viewport();
41 vi.spyOn(Range.prototype, "getBoundingClientRect").mockReturnValue(new DOMRect(0, 60, 40, 20));
42 expect(readThreadFindPosition(container, "COD4", (id) => id, 100)).toEqual({
43 entryId: "reading",
44 occurrence: 2,
45 });
46 });
47
48 it("anchors unmatched or folded content and excludes rows hidden behind the composer", () => {
49 const container = viewport("nothing");
50 expect(readThreadFindPosition(container, "COD4", () => "folded-message", 100)).toEqual({
51 entryId: "folded-message",
52 occurrence: 0,
53 });
54 vi.spyOn(container.children[1]!, "getBoundingClientRect").mockReturnValue(
55 new DOMRect(0, 420, 500, 300),
56 );
57 expect(readThreadFindPosition(container, "COD4", (id) => id, 100)).toBeUndefined();
58 });
59});