|
| 1 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { |
| 4 | + observeSettingsSectionVisibility, |
| 5 | + type SettingsSectionVisibilityEnvironment, |
| 6 | +} from "./settingsSectionVisibility"; |
| 7 | + |
| 8 | +type VisibilityEntry = Pick< |
| 9 | + IntersectionObserverEntry, |
| 10 | + "intersectionRatio" | "isIntersecting" | "target" |
| 11 | +>; |
| 12 | + |
| 13 | +function createHarness( |
| 14 | + targetIds: ReadonlyArray<string>, |
| 15 | + initialRoot: Element | null = { name: "initial-root" } as unknown as Element, |
| 16 | +) { |
| 17 | + const targets = new Map( |
| 18 | + targetIds.map((targetId) => [targetId, { targetId } as unknown as Element]), |
| 19 | + ); |
| 20 | + const observed = new Set<Element>(); |
| 21 | + const unobserve = vi.fn((target: Element) => observed.delete(target)); |
| 22 | + const disconnectIntersections = vi.fn(); |
| 23 | + const disconnectMutations = vi.fn(); |
| 24 | + const intersectionCallbacks: Array<(entries: ReadonlyArray<VisibilityEntry>) => void> = []; |
| 25 | + const intersectionRoots: Element[] = []; |
| 26 | + let root = initialRoot; |
| 27 | + let onMutation = () => {}; |
| 28 | + |
| 29 | + const environment: SettingsSectionVisibilityEnvironment = { |
| 30 | + findRoot: () => root, |
| 31 | + findTarget: (_root, targetId) => targets.get(targetId) ?? null, |
| 32 | + createIntersectionObserver(callback, observedRoot) { |
| 33 | + intersectionCallbacks.push(callback); |
| 34 | + intersectionRoots.push(observedRoot); |
| 35 | + return { |
| 36 | + observe: (target) => observed.add(target), |
| 37 | + unobserve, |
| 38 | + disconnect: disconnectIntersections, |
| 39 | + }; |
| 40 | + }, |
| 41 | + createMutationObserver(callback) { |
| 42 | + onMutation = callback; |
| 43 | + return { disconnect: disconnectMutations }; |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + return { |
| 48 | + environment, |
| 49 | + targets, |
| 50 | + observed, |
| 51 | + unobserve, |
| 52 | + disconnectIntersections, |
| 53 | + disconnectMutations, |
| 54 | + intersectionCallbacks, |
| 55 | + intersectionRoots, |
| 56 | + intersect(entries: ReadonlyArray<VisibilityEntry>) { |
| 57 | + intersectionCallbacks.at(-1)?.(entries); |
| 58 | + }, |
| 59 | + mutate() { |
| 60 | + onMutation(); |
| 61 | + }, |
| 62 | + replaceRoot(nextRoot: Element | null) { |
| 63 | + root = nextRoot; |
| 64 | + onMutation(); |
| 65 | + }, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +function visibleEntry( |
| 70 | + target: Element, |
| 71 | + { isIntersecting = true, intersectionRatio = 1 } = {}, |
| 72 | +): VisibilityEntry { |
| 73 | + return { target, isIntersecting, intersectionRatio }; |
| 74 | +} |
| 75 | + |
| 76 | +describe("settings section visibility", () => { |
| 77 | + it("accumulates visible sections and emits them in sidebar order", () => { |
| 78 | + const harness = createHarness(["one", "two", "three"]); |
| 79 | + const emissions: ReadonlyArray<string>[] = []; |
| 80 | + observeSettingsSectionVisibility({ |
| 81 | + container: {} as Element, |
| 82 | + targetIds: ["one", "two", "three"], |
| 83 | + onChange: (visible) => emissions.push(visible), |
| 84 | + environment: harness.environment, |
| 85 | + }); |
| 86 | + |
| 87 | + harness.intersect([visibleEntry(harness.targets.get("two")!)]); |
| 88 | + harness.intersect([visibleEntry(harness.targets.get("one")!)]); |
| 89 | + |
| 90 | + expect(emissions).toEqual([[], ["two"], ["one", "two"]]); |
| 91 | + }); |
| 92 | + |
| 93 | + it("treats zero-ratio and non-intersecting entries as hidden", () => { |
| 94 | + const harness = createHarness(["one", "two"]); |
| 95 | + const emissions: ReadonlyArray<string>[] = []; |
| 96 | + observeSettingsSectionVisibility({ |
| 97 | + container: {} as Element, |
| 98 | + targetIds: ["one", "two"], |
| 99 | + onChange: (visible) => emissions.push(visible), |
| 100 | + environment: harness.environment, |
| 101 | + }); |
| 102 | + |
| 103 | + const one = harness.targets.get("one")!; |
| 104 | + const two = harness.targets.get("two")!; |
| 105 | + harness.intersect([visibleEntry(one), visibleEntry(two)]); |
| 106 | + harness.intersect([visibleEntry(one, { intersectionRatio: 0 })]); |
| 107 | + harness.intersect([visibleEntry(two, { isIntersecting: false })]); |
| 108 | + |
| 109 | + expect(emissions).toEqual([[], ["one", "two"], ["two"], []]); |
| 110 | + }); |
| 111 | + |
| 112 | + it("resyncs replaced and removed targets without retaining stale visibility", () => { |
| 113 | + const harness = createHarness(["dynamic"]); |
| 114 | + const emissions: ReadonlyArray<string>[] = []; |
| 115 | + observeSettingsSectionVisibility({ |
| 116 | + container: {} as Element, |
| 117 | + targetIds: ["dynamic"], |
| 118 | + onChange: (visible) => emissions.push(visible), |
| 119 | + environment: harness.environment, |
| 120 | + }); |
| 121 | + |
| 122 | + const firstTarget = harness.targets.get("dynamic")!; |
| 123 | + harness.intersect([visibleEntry(firstTarget)]); |
| 124 | + const replacementTarget = { targetId: "dynamic-replacement" } as unknown as Element; |
| 125 | + harness.targets.set("dynamic", replacementTarget); |
| 126 | + harness.mutate(); |
| 127 | + |
| 128 | + expect(harness.unobserve).toHaveBeenCalledWith(firstTarget); |
| 129 | + expect(harness.observed.has(replacementTarget)).toBe(true); |
| 130 | + expect(emissions.at(-1)).toEqual([]); |
| 131 | + |
| 132 | + harness.intersect([visibleEntry(firstTarget)]); |
| 133 | + expect(emissions.at(-1)).toEqual([]); |
| 134 | + harness.intersect([visibleEntry(replacementTarget)]); |
| 135 | + expect(emissions.at(-1)).toEqual(["dynamic"]); |
| 136 | + |
| 137 | + harness.targets.delete("dynamic"); |
| 138 | + harness.mutate(); |
| 139 | + expect(harness.unobserve).toHaveBeenCalledWith(replacementTarget); |
| 140 | + expect(emissions.at(-1)).toEqual([]); |
| 141 | + }); |
| 142 | + |
| 143 | + it("rebinds when navigation replaces the settings scroll root", () => { |
| 144 | + const harness = createHarness(["section"]); |
| 145 | + const emissions: ReadonlyArray<string>[] = []; |
| 146 | + observeSettingsSectionVisibility({ |
| 147 | + container: {} as Element, |
| 148 | + targetIds: ["section"], |
| 149 | + onChange: (visible) => emissions.push(visible), |
| 150 | + environment: harness.environment, |
| 151 | + }); |
| 152 | + |
| 153 | + const firstTarget = harness.targets.get("section")!; |
| 154 | + harness.intersect([visibleEntry(firstTarget)]); |
| 155 | + const firstObserverCallback = harness.intersectionCallbacks[0]!; |
| 156 | + const nextRoot = { name: "next-root" } as unknown as Element; |
| 157 | + const nextTarget = { targetId: "next-section" } as unknown as Element; |
| 158 | + harness.targets.set("section", nextTarget); |
| 159 | + harness.replaceRoot(nextRoot); |
| 160 | + |
| 161 | + expect(harness.disconnectIntersections).toHaveBeenCalledOnce(); |
| 162 | + expect(harness.intersectionRoots.at(-1)).toBe(nextRoot); |
| 163 | + expect(harness.observed.has(nextTarget)).toBe(true); |
| 164 | + expect(emissions.at(-1)).toEqual([]); |
| 165 | + |
| 166 | + firstObserverCallback([visibleEntry(firstTarget)]); |
| 167 | + expect(emissions.at(-1)).toEqual([]); |
| 168 | + harness.intersect([visibleEntry(nextTarget)]); |
| 169 | + expect(emissions.at(-1)).toEqual(["section"]); |
| 170 | + }); |
| 171 | + |
| 172 | + it("starts observing when the settings scroll root mounts later", () => { |
| 173 | + const harness = createHarness(["section"], null); |
| 174 | + const emissions: ReadonlyArray<string>[] = []; |
| 175 | + observeSettingsSectionVisibility({ |
| 176 | + container: {} as Element, |
| 177 | + targetIds: ["section"], |
| 178 | + onChange: (visible) => emissions.push(visible), |
| 179 | + environment: harness.environment, |
| 180 | + }); |
| 181 | + |
| 182 | + expect(emissions).toEqual([[]]); |
| 183 | + expect(harness.intersectionRoots).toEqual([]); |
| 184 | + |
| 185 | + const root = { name: "mounted-root" } as unknown as Element; |
| 186 | + harness.replaceRoot(root); |
| 187 | + harness.intersect([visibleEntry(harness.targets.get("section")!)]); |
| 188 | + |
| 189 | + expect(harness.intersectionRoots).toEqual([root]); |
| 190 | + expect(emissions.at(-1)).toEqual(["section"]); |
| 191 | + }); |
| 192 | + |
| 193 | + it("disconnects both observers and ignores callbacks after cleanup", () => { |
| 194 | + const harness = createHarness(["one"]); |
| 195 | + const onChange = vi.fn(); |
| 196 | + const cleanup = observeSettingsSectionVisibility({ |
| 197 | + container: {} as Element, |
| 198 | + targetIds: ["one"], |
| 199 | + onChange, |
| 200 | + environment: harness.environment, |
| 201 | + }); |
| 202 | + |
| 203 | + cleanup(); |
| 204 | + harness.intersect([visibleEntry(harness.targets.get("one")!)]); |
| 205 | + harness.mutate(); |
| 206 | + |
| 207 | + expect(harness.disconnectIntersections).toHaveBeenCalledOnce(); |
| 208 | + expect(harness.disconnectMutations).toHaveBeenCalledOnce(); |
| 209 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 210 | + expect(onChange).toHaveBeenLastCalledWith([]); |
| 211 | + }); |
| 212 | +}); |