|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { initStickySidebar } from '@components/scripts/stickySidebar' |
| 4 | + |
| 5 | +/** |
| 6 | + * The sticky sidebar pins SHORT sidebars just below the fixed chrome: |
| 7 | + * translateY = max(0, chromeBottom + topPadding - naturalTop). |
| 8 | + * These tests drive that path with a sidebar shorter than the viewport. |
| 9 | + */ |
| 10 | + |
| 11 | +const SIDEBAR_HEIGHT = 400 |
| 12 | +const TALL_SIDEBAR_HEIGHT = 1200 |
| 13 | +const NATURAL_TOP = 100 |
| 14 | +const CONTAINER_BOTTOM = 10_000 |
| 15 | + |
| 16 | +interface PageState { |
| 17 | + chromeBottom: number |
| 18 | + /** Document offset of the sidebar before transforms; natural top = docTop - scrollTop */ |
| 19 | + docTop: number |
| 20 | + sidebarHeight: number |
| 21 | +} |
| 22 | + |
| 23 | +let headerEl: HTMLElement |
| 24 | +let progressEl: HTMLElement |
| 25 | +let scroller: HTMLElement |
| 26 | +let container: HTMLElement |
| 27 | +let sidebar: HTMLElement |
| 28 | +let state: PageState |
| 29 | + |
| 30 | +/** ResizeObserver test double: captures the callback so tests can fire it. */ |
| 31 | +let roCallback: (() => void) | null |
| 32 | +let observeSpy: ReturnType<typeof vi.fn> |
| 33 | +let disconnectSpy: ReturnType<typeof vi.fn> |
| 34 | + |
| 35 | +class FakeResizeObserver { |
| 36 | + constructor(callback: () => void) { |
| 37 | + roCallback = callback |
| 38 | + } |
| 39 | + observe = observeSpy |
| 40 | + unobserve = vi.fn() |
| 41 | + disconnect = disconnectSpy |
| 42 | +} |
| 43 | + |
| 44 | +const rect = (top: number, height: number): DOMRect => |
| 45 | + ({ |
| 46 | + top, |
| 47 | + bottom: top + height, |
| 48 | + height, |
| 49 | + left: 0, |
| 50 | + right: 300, |
| 51 | + width: 300, |
| 52 | + x: 0, |
| 53 | + y: top, |
| 54 | + toJSON: () => ({}), |
| 55 | + }) as DOMRect |
| 56 | + |
| 57 | +/** Sidebar top tracks the scroll position plus any applied transform. */ |
| 58 | +function currentSidebarTop(): number { |
| 59 | + const match = /translateY\((-?\d+(?:\.\d+)?)px\)/.exec(sidebar.style.transform) |
| 60 | + return state.docTop - scroller.scrollTop + (match ? Number(match[1]) : 0) |
| 61 | +} |
| 62 | + |
| 63 | +/** Wait long enough for the rAF-debounced update to run. */ |
| 64 | +const flushUpdate = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 40)) |
| 65 | + |
| 66 | +function setupDom(): void { |
| 67 | + document.body.innerHTML = ` |
| 68 | + <header class="header-fixed"><span id="header-child"></span></header> |
| 69 | + <div data-progress-bar></div> |
| 70 | + <div id="scroll-viewport"><div id="container"><aside id="sidebar"></aside></div></div> |
| 71 | + ` |
| 72 | + headerEl = document.querySelector('.header-fixed') as HTMLElement |
| 73 | + progressEl = document.querySelector('[data-progress-bar]') as HTMLElement |
| 74 | + scroller = document.getElementById('scroll-viewport') as HTMLElement |
| 75 | + container = document.getElementById('container') as HTMLElement |
| 76 | + sidebar = document.getElementById('sidebar') as HTMLElement |
| 77 | + |
| 78 | + state = { chromeBottom: 104, docTop: NATURAL_TOP, sidebarHeight: SIDEBAR_HEIGHT } |
| 79 | + |
| 80 | + headerEl.getBoundingClientRect = () => rect(0, state.chromeBottom) |
| 81 | + progressEl.getBoundingClientRect = () => rect(0, 0) |
| 82 | + sidebar.getBoundingClientRect = () => rect(currentSidebarTop(), state.sidebarHeight) |
| 83 | + container.getBoundingClientRect = () => rect(-1000, CONTAINER_BOTTOM + 1000) |
| 84 | + |
| 85 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1440 }) |
| 86 | + Object.defineProperty(window, 'innerHeight', { configurable: true, value: 900 }) |
| 87 | +} |
| 88 | + |
| 89 | +beforeEach(() => { |
| 90 | + roCallback = null |
| 91 | + observeSpy = vi.fn() |
| 92 | + disconnectSpy = vi.fn() |
| 93 | + vi.stubGlobal('ResizeObserver', FakeResizeObserver) |
| 94 | + setupDom() |
| 95 | +}) |
| 96 | + |
| 97 | +afterEach(() => { |
| 98 | + vi.unstubAllGlobals() |
| 99 | + document.body.innerHTML = '' |
| 100 | +}) |
| 101 | + |
| 102 | +describe('initStickySidebar chrome tracking', () => { |
| 103 | + it('pins a short sidebar below the measured chrome on init', () => { |
| 104 | + initStickySidebar(sidebar, container) |
| 105 | + // visibleTop = 104 + 16 = 120; naturalTop = 100 -> translateY(20px) |
| 106 | + expect(sidebar.style.transform).toBe('translateY(20px)') |
| 107 | + expect(observeSpy).toHaveBeenCalledWith(headerEl) |
| 108 | + expect(observeSpy).toHaveBeenCalledWith(progressEl) |
| 109 | + }) |
| 110 | + |
| 111 | + it('re-measures when the header resizes (WAAPI squish path)', async () => { |
| 112 | + initStickySidebar(sidebar, container) |
| 113 | + expect(sidebar.style.transform).toBe('translateY(20px)') |
| 114 | + |
| 115 | + // Header squishes after the scroll stops: chrome bottom 104 -> 60 |
| 116 | + state.chromeBottom = 60 |
| 117 | + roCallback?.() |
| 118 | + await flushUpdate() |
| 119 | + |
| 120 | + // visibleTop = 60 + 16 = 76 < naturalTop 100 -> back to natural position |
| 121 | + expect(sidebar.style.transform).toBe('translateY(0px)') |
| 122 | + }) |
| 123 | + |
| 124 | + it('re-measures when the header transform transition ends', async () => { |
| 125 | + initStickySidebar(sidebar, container) |
| 126 | + state.chromeBottom = 60 |
| 127 | + headerEl.dispatchEvent(new Event('transitionend')) |
| 128 | + await flushUpdate() |
| 129 | + |
| 130 | + expect(sidebar.style.transform).toBe('translateY(0px)') |
| 131 | + }) |
| 132 | + |
| 133 | + it('ignores transitionend events bubbling from header children', async () => { |
| 134 | + initStickySidebar(sidebar, container) |
| 135 | + const child = document.getElementById('header-child') as HTMLElement |
| 136 | + state.chromeBottom = 60 |
| 137 | + child.dispatchEvent(new Event('transitionend', { bubbles: true })) |
| 138 | + await flushUpdate() |
| 139 | + |
| 140 | + // Target filter rejects the event; stale transform remains |
| 141 | + expect(sidebar.style.transform).toBe('translateY(20px)') |
| 142 | + }) |
| 143 | + |
| 144 | + it('cleanup disconnects the observer and removes the transition listener', async () => { |
| 145 | + const destroy = initStickySidebar(sidebar, container) |
| 146 | + destroy() |
| 147 | + |
| 148 | + expect(disconnectSpy).toHaveBeenCalledTimes(1) |
| 149 | + expect(sidebar.style.transform).toBe('') |
| 150 | + |
| 151 | + state.chromeBottom = 60 |
| 152 | + headerEl.dispatchEvent(new Event('transitionend')) |
| 153 | + await flushUpdate() |
| 154 | + expect(sidebar.style.transform).toBe('') |
| 155 | + }) |
| 156 | + |
| 157 | + it('still tracks scroll when ResizeObserver is unavailable', async () => { |
| 158 | + vi.stubGlobal('ResizeObserver', undefined) |
| 159 | + initStickySidebar(sidebar, container) |
| 160 | + expect(sidebar.style.transform).toBe('translateY(20px)') |
| 161 | + |
| 162 | + state.chromeBottom = 60 |
| 163 | + scroller.dispatchEvent(new Event('scroll')) |
| 164 | + await flushUpdate() |
| 165 | + expect(sidebar.style.transform).toBe('translateY(0px)') |
| 166 | + }) |
| 167 | +}) |
| 168 | + |
| 169 | +describe('initStickySidebar tall sidebar pin tracking', () => { |
| 170 | + const scrollTo = (top: number): void => { |
| 171 | + scroller.scrollTop = top |
| 172 | + scroller.dispatchEvent(new Event('scroll')) |
| 173 | + } |
| 174 | + |
| 175 | + it('re-attaches a top pin when the chrome moves without a scroll event', async () => { |
| 176 | + state.sidebarHeight = TALL_SIDEBAR_HEIGHT |
| 177 | + state.docTop = 600 |
| 178 | + initStickySidebar(sidebar, container) |
| 179 | + |
| 180 | + // Scroll down past the sidebar: it drifts, then bottom-pins |
| 181 | + scrollTo(800) |
| 182 | + await flushUpdate() |
| 183 | + expect(sidebar.style.transform).toBe('') |
| 184 | + scrollTo(1000) |
| 185 | + await flushUpdate() |
| 186 | + // bottom pin: 884 - 1200 - (600 - 1000) = 84 |
| 187 | + expect(sidebar.style.transform).toBe('translateY(84px)') |
| 188 | + |
| 189 | + // Scroll back up: releases from the bottom, then top-pins |
| 190 | + scrollTo(600) |
| 191 | + await flushUpdate() |
| 192 | + expect(sidebar.style.transform).toBe('translateY(84px)') |
| 193 | + scrollTo(500) |
| 194 | + await flushUpdate() |
| 195 | + // top pin: visibleTop 120 - naturalTop (600 - 500) = 20 |
| 196 | + expect(sidebar.style.transform).toBe('translateY(20px)') |
| 197 | + |
| 198 | + // Header EXPANDS after the scroll stops (chrome bottom 104 -> 140). |
| 199 | + // The top-pinned sidebar must track to the new pin line without a scroll. |
| 200 | + state.chromeBottom = 140 |
| 201 | + roCallback?.() |
| 202 | + await flushUpdate() |
| 203 | + // visibleTop = 140 + 16 = 156 -> 156 - 100 = 56 |
| 204 | + expect(sidebar.style.transform).toBe('translateY(56px)') |
| 205 | + |
| 206 | + // Header then squishes below the natural position: clamped to natural |
| 207 | + state.chromeBottom = 60 |
| 208 | + roCallback?.() |
| 209 | + await flushUpdate() |
| 210 | + expect(sidebar.style.transform).toBe('translateY(0px)') |
| 211 | + }) |
| 212 | +}) |
0 commit comments