|
| 1 | +/** |
| 2 | + * #1647: scroll keys do nothing on load because the document scrolls inside |
| 3 | + * <main>, not the window. These tests guard the routing that fills that gap |
| 4 | + * and, just as much, every owner it must stay out of the way of. |
| 5 | + * |
| 6 | + * DOM-gated: run with DOM_TESTS=1 (listed in .github/workflows/test.yml). |
| 7 | + */ |
| 8 | +import { afterEach, describe, expect, test } from 'bun:test'; |
| 9 | +import React, { act } from 'react'; |
| 10 | +import { createRoot, type Root } from 'react-dom/client'; |
| 11 | +import { scrollKeyAction, useScrollKeyRouting, type ScrollKeyInput } from './useScrollKeyRouting'; |
| 12 | + |
| 13 | +const hasDom = typeof document !== 'undefined'; |
| 14 | +let root: Root | null = null; |
| 15 | +let host: HTMLElement | null = null; |
| 16 | + |
| 17 | +interface ScrollCall { readonly top?: number } |
| 18 | + |
| 19 | +/** A viewport that reports overflow and records the scrolls asked of it. */ |
| 20 | +function makeViewport(): { el: HTMLElement; calls: ScrollCall[] } { |
| 21 | + const el = document.createElement('main'); |
| 22 | + el.style.overflowY = 'auto'; |
| 23 | + Object.defineProperty(el, 'scrollHeight', { configurable: true, value: 5000 }); |
| 24 | + Object.defineProperty(el, 'clientHeight', { configurable: true, value: 800 }); |
| 25 | + const calls: ScrollCall[] = []; |
| 26 | + el.scrollBy = ((options: ScrollToOptions) => { calls.push({ top: options.top }); }) as HTMLElement['scrollBy']; |
| 27 | + el.scrollTo = ((options: ScrollToOptions) => { calls.push({ top: options.top }); }) as HTMLElement['scrollTo']; |
| 28 | + const paragraph = document.createElement('p'); |
| 29 | + paragraph.textContent = 'Document text'; |
| 30 | + el.appendChild(paragraph); |
| 31 | + document.body.appendChild(el); |
| 32 | + return { el, calls }; |
| 33 | +} |
| 34 | + |
| 35 | +function Harness({ viewport, enabled }: { viewport: HTMLElement; enabled: boolean }) { |
| 36 | + useScrollKeyRouting({ viewport, enabled }); |
| 37 | + return null; |
| 38 | +} |
| 39 | + |
| 40 | +async function mount(viewport: HTMLElement, enabled = true): Promise<void> { |
| 41 | + host = document.createElement('div'); |
| 42 | + document.body.appendChild(host); |
| 43 | + root = createRoot(host); |
| 44 | + await act(async () => root?.render(<Harness viewport={viewport} enabled={enabled} />)); |
| 45 | +} |
| 46 | + |
| 47 | +function key(target: EventTarget, init: KeyboardEventInit): KeyboardEvent { |
| 48 | + const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init }); |
| 49 | + target.dispatchEvent(event); |
| 50 | + return event; |
| 51 | +} |
| 52 | + |
| 53 | +function pointerDown(target: EventTarget): void { |
| 54 | + target.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, cancelable: true })); |
| 55 | +} |
| 56 | + |
| 57 | +// Plain objects, not `new KeyboardEvent`: this block runs in the non-DOM job too. |
| 58 | +const k = (init: Partial<ScrollKeyInput> & { key: string }) => scrollKeyAction({ |
| 59 | + isComposing: false, ctrlKey: false, metaKey: false, altKey: false, shiftKey: false, ...init, |
| 60 | +}); |
| 61 | + |
| 62 | +describe('scrollKeyAction', () => { |
| 63 | + test('maps the plain scroll keys and Shift+Space', () => { |
| 64 | + expect(k({ key: 'ArrowDown' })).toEqual({ kind: 'line', direction: 1 }); |
| 65 | + expect(k({ key: 'ArrowUp' })).toEqual({ kind: 'line', direction: -1 }); |
| 66 | + expect(k({ key: 'PageDown' })).toEqual({ kind: 'page', direction: 1 }); |
| 67 | + expect(k({ key: 'PageUp' })).toEqual({ kind: 'page', direction: -1 }); |
| 68 | + expect(k({ key: ' ' })).toEqual({ kind: 'page', direction: 1 }); |
| 69 | + expect(k({ key: ' ', shiftKey: true })).toEqual({ kind: 'page', direction: -1 }); |
| 70 | + expect(k({ key: 'Home' })).toEqual({ kind: 'edge', edge: 'start' }); |
| 71 | + expect(k({ key: 'End' })).toEqual({ kind: 'edge', edge: 'end' }); |
| 72 | + }); |
| 73 | + |
| 74 | + test('leaves chords, selection-extending keys and IME composition alone', () => { |
| 75 | + expect(k({ key: 'ArrowDown', metaKey: true })).toBeNull(); |
| 76 | + expect(k({ key: 'ArrowDown', ctrlKey: true })).toBeNull(); |
| 77 | + expect(k({ key: 'ArrowDown', altKey: true })).toBeNull(); |
| 78 | + expect(k({ key: 'ArrowDown', shiftKey: true })).toBeNull(); |
| 79 | + expect(k({ key: 'End', shiftKey: true })).toBeNull(); |
| 80 | + expect(k({ key: ' ', isComposing: true })).toBeNull(); |
| 81 | + expect(k({ key: 'Enter' })).toBeNull(); |
| 82 | + expect(k({ key: 'j' })).toBeNull(); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('useScrollKeyRouting', () => { |
| 87 | + afterEach(async () => { |
| 88 | + const mounted = root; |
| 89 | + if (mounted) await act(async () => mounted.unmount()); |
| 90 | + root = null; |
| 91 | + host = null; |
| 92 | + if (hasDom) document.body.replaceChildren(); |
| 93 | + }); |
| 94 | + |
| 95 | + test.skipIf(!hasDom)('scrolls the document viewport when nothing owns the key', async () => { |
| 96 | + const { el, calls } = makeViewport(); |
| 97 | + await mount(el); |
| 98 | + |
| 99 | + const pageDown = key(document.body, { key: 'PageDown' }); |
| 100 | + expect(pageDown.defaultPrevented).toBe(true); |
| 101 | + key(document.body, { key: 'ArrowDown' }); |
| 102 | + key(document.body, { key: ' ', shiftKey: true }); |
| 103 | + |
| 104 | + expect(calls.map(c => Math.sign(c.top ?? 0))).toEqual([1, 1, -1]); |
| 105 | + expect(calls[0].top).toBeGreaterThan(calls[1].top ?? 0); |
| 106 | + }); |
| 107 | + |
| 108 | + test.skipIf(!hasDom)('never takes a key from a focused control, input or editor', async () => { |
| 109 | + const { el, calls } = makeViewport(); |
| 110 | + await mount(el); |
| 111 | + |
| 112 | + const input = document.createElement('textarea'); |
| 113 | + document.body.appendChild(input); |
| 114 | + input.focus(); |
| 115 | + const space = key(input, { key: ' ' }); |
| 116 | + expect(space.defaultPrevented).toBe(false); |
| 117 | + |
| 118 | + const button = document.createElement('button'); |
| 119 | + document.body.appendChild(button); |
| 120 | + button.focus(); |
| 121 | + expect(key(button, { key: ' ' }).defaultPrevented).toBe(false); |
| 122 | + |
| 123 | + expect(calls).toEqual([]); |
| 124 | + }); |
| 125 | + |
| 126 | + test.skipIf(!hasDom)('yields to a listener that already handled the key', async () => { |
| 127 | + const { el, calls } = makeViewport(); |
| 128 | + await mount(el); |
| 129 | + const consume = (event: KeyboardEvent) => event.preventDefault(); |
| 130 | + window.addEventListener('keydown', consume, true); |
| 131 | + try { |
| 132 | + key(document.body, { key: 'ArrowDown' }); |
| 133 | + } finally { |
| 134 | + window.removeEventListener('keydown', consume, true); |
| 135 | + } |
| 136 | + expect(calls).toEqual([]); |
| 137 | + }); |
| 138 | + |
| 139 | + test.skipIf(!hasDom)('does not scroll the document behind a dialog, menu or listbox', async () => { |
| 140 | + const { el, calls } = makeViewport(); |
| 141 | + await mount(el); |
| 142 | + |
| 143 | + for (const role of ['dialog', 'menu', 'listbox']) { |
| 144 | + const overlay = document.createElement('div'); |
| 145 | + overlay.setAttribute('role', role); |
| 146 | + document.body.appendChild(overlay); |
| 147 | + expect(key(document.body, { key: 'PageDown' }).defaultPrevented).toBe(false); |
| 148 | + overlay.remove(); |
| 149 | + } |
| 150 | + const legacyModal = document.createElement('div'); |
| 151 | + legacyModal.className = 'fixed inset-0'; |
| 152 | + document.body.appendChild(legacyModal); |
| 153 | + key(document.body, { key: ' ' }); |
| 154 | + legacyModal.remove(); |
| 155 | + |
| 156 | + expect(calls).toEqual([]); |
| 157 | + }); |
| 158 | + |
| 159 | + test.skipIf(!hasDom)('leaves the key to the browser once a click gave it a scroll target', async () => { |
| 160 | + const { el, calls } = makeViewport(); |
| 161 | + await mount(el); |
| 162 | + |
| 163 | + // A click in the document: the browser now scrolls <main> natively, so |
| 164 | + // routing too would move it twice. |
| 165 | + pointerDown(el.querySelector('p')!); |
| 166 | + expect(key(document.body, { key: 'PageDown' }).defaultPrevented).toBe(false); |
| 167 | + expect(calls).toEqual([]); |
| 168 | + |
| 169 | + // A click on chrome with nothing scrollable above it (the header). |
| 170 | + const header = document.createElement('header'); |
| 171 | + document.body.appendChild(header); |
| 172 | + pointerDown(header); |
| 173 | + key(document.body, { key: 'PageDown' }); |
| 174 | + expect(calls).toHaveLength(1); |
| 175 | + |
| 176 | + // A click on a node that then goes away (a dismissed first-run dialog). |
| 177 | + const dialogButton = document.createElement('button'); |
| 178 | + el.appendChild(dialogButton); |
| 179 | + pointerDown(dialogButton); |
| 180 | + dialogButton.remove(); |
| 181 | + key(document.body, { key: 'PageDown' }); |
| 182 | + expect(calls).toHaveLength(2); |
| 183 | + }); |
| 184 | + |
| 185 | + test.skipIf(!hasDom)('does nothing while disabled', async () => { |
| 186 | + const { el, calls } = makeViewport(); |
| 187 | + await mount(el, false); |
| 188 | + expect(key(document.body, { key: 'PageDown' }).defaultPrevented).toBe(false); |
| 189 | + expect(calls).toEqual([]); |
| 190 | + }); |
| 191 | +}); |
0 commit comments