From c5cb0c4386cc6328634ce50d70a7c8438596c46e Mon Sep 17 00:00:00 2001 From: Tim Ellenberger Date: Tue, 10 Nov 2026 09:41:15 -0700 Subject: [PATCH 1/7] test(keys): expect keys to act only from inside the lightbox (#65) The lightbox listens for keys on document, so it acts on keys meant for something else. It pages while someone types in a field inside it, blocks Home and End there, closes on an Escape that a control already handled, and pages an inline carousel from anywhere on the page. The #65 tests that recorded these bugs with test.fails now expect the fix. New tests cover a menu that a header button portals to , an arrow key with Alt, Ctrl, or Meta, Escape while an input method composes text, Space on the dialog and on a button, a held key, an arrow button inside the open lightbox, and an inline carousel that pages only while focus is inside it. Others check that the modal still blocks scroll keys that have Alt, Ctrl, or Meta held, such as Cmd+Down, which scrolls the page behind it in macOS browsers. pressKey now sends each key to the focused element, as a browser does. These tests fail until the next commit fixes the key handling. Co-Authored-By: Claude Opus 5.5 --- e2e/inline.spec.ts | 14 ++- e2e/opening-and-closing.spec.ts | 13 ++- e2e/paging.spec.ts | 1 - src/__tests__/devices.ts | 13 ++- src/__tests__/embedding.test.tsx | 17 +-- src/__tests__/inline.test.tsx | 40 ++++++- src/__tests__/opening-and-closing.test.tsx | 120 +++++++++++++++++++-- src/__tests__/paging.test.tsx | 93 +++++++++++++++- 8 files changed, 283 insertions(+), 28 deletions(-) diff --git a/e2e/inline.spec.ts b/e2e/inline.spec.ts index af4114d..257f098 100644 --- a/e2e/inline.spec.ts +++ b/e2e/inline.spec.ts @@ -63,9 +63,21 @@ test.describe('with a mouse and keyboard', () => { await page.keyboard.press('PageDown'); - test.fail(); await expect.poll(() => scrollY(page), { timeout: 1000 }).toBeGreaterThan(before); }); + + test('→ pages while its Next button has focus, and does nothing while the page has it', async ({ page }) => { + const carousel = await openCarousel(page); + + await page.keyboard.press('ArrowRight'); + await settle(page); + expect((await calls(page)).onNext).toBe(0); + + await carousel.getByRole('button', { name: 'Next' }).focus(); + await page.keyboard.press('ArrowRight'); + await settle(page); + expect((await calls(page)).onNext).toBe(1); + }); }); test.describe('on a touchscreen', () => { diff --git a/e2e/opening-and-closing.spec.ts b/e2e/opening-and-closing.spec.ts index a902575..fb9d1dc 100644 --- a/e2e/opening-and-closing.spec.ts +++ b/e2e/opening-and-closing.spec.ts @@ -117,10 +117,20 @@ test.describe('with a mouse and keyboard', () => { await page.waitForTimeout(300); const scrolled = await scrollY(page); - test.fail(true, 'Space is missing from the keys the lightbox blocks'); expect(scrolled).toBe(0); }); + test('scroll keys with Alt, Ctrl, or Meta do not scroll the page behind', async ({ page }) => { + await openHarness(page); + + for (const key of ['Meta+ArrowDown', 'Alt+ArrowDown', 'Control+End']) { + await page.keyboard.press(key); + } + await page.waitForTimeout(300); + + expect(await scrollY(page)).toBe(0); + }); + test('a text field in the footer keeps its Home and End keys (#65)', async ({ page }) => { await openHarness(page, 'footerInput'); const caption = page.getByRole('textbox', { name: 'Caption' }); @@ -137,7 +147,6 @@ test.describe('with a mouse and keyboard', () => { await page.waitForTimeout(100); const prevented = await page.evaluate(() => (window as unknown as { prevented: boolean[] }).prevented); - test.fail(); expect(prevented).toEqual([false, false]); }); }); diff --git a/e2e/paging.spec.ts b/e2e/paging.spec.ts index d67fd9b..34fa9b4 100644 --- a/e2e/paging.spec.ts +++ b/e2e/paging.spec.ts @@ -368,7 +368,6 @@ test.describe('the keyboard', () => { await settle(page); const { onNext } = await calls(page); - test.fail(); expect(onNext).toBe(0); }); }); diff --git a/src/__tests__/devices.ts b/src/__tests__/devices.ts index 3d4b875..ad8d489 100644 --- a/src/__tests__/devices.ts +++ b/src/__tests__/devices.ts @@ -242,9 +242,12 @@ export const safariPinch = async (target: Element, { at, scales }: { at: Point; safariGesture(target, 'gestureend', at, scales[scales.length - 1]); }; -/** Presses and releases `key`. Returns false when the lightbox blocked the key's default action, such as scrolling. */ -export const pressKey = (key: string, target: Element | Document = document) => { - const allowed = fireEvent.keyDown(target, { key }); - fireEvent.keyUp(target, { key }); - return allowed; +export const pressKey = ( + key: string, + target: Element | Document = document.activeElement ?? document, + init: Omit = {} +) => { + const allowed = fireEvent.keyDown(target, { ...init, key }); + fireEvent.keyUp(target, { ...init, key }); + return { defaultPrevented: !allowed }; }; diff --git a/src/__tests__/embedding.test.tsx b/src/__tests__/embedding.test.tsx index 0e62131..1b3556f 100644 --- a/src/__tests__/embedding.test.tsx +++ b/src/__tests__/embedding.test.tsx @@ -11,6 +11,7 @@ import { advance, containerAppearance, image, + lightboxContainer, lightboxProps, mockLayout, pageOf, @@ -178,21 +179,23 @@ describe('at the last image, as a looping gallery sees it', () => { }); }); -test('holding an arrow key pages once, when the key is released', () => { +test('holding an arrow key pages once, when it goes down', () => { const props = lightboxProps({ currentIndex: 2 }); render(); + const dialog = lightboxContainer(); - for (let repeat = 0; repeat < 5; repeat += 1) { - fireEvent.keyDown(document, { key: 'ArrowRight', repeat: repeat > 0 }); + fireEvent.keyDown(dialog, { key: 'ArrowRight' }); + expect(props.onNext).toHaveBeenCalledTimes(1); + for (let repeat = 0; repeat < 4; repeat += 1) { + fireEvent.keyDown(dialog, { key: 'ArrowRight', repeat: true }); } - expect(props.onNext).not.toHaveBeenCalled(); - fireEvent.keyUp(document, { key: 'ArrowRight' }); + fireEvent.keyUp(dialog, { key: 'ArrowRight' }); expect(props.onNext).toHaveBeenCalledTimes(1); }); describe('more than one lightbox on a page', () => { - test.fails('a key press pages at most one of two inline carousels (#65)', () => { + test('a key press pages at most one of two inline carousels (#65)', () => { const firstNext = vi.fn(); const secondNext = vi.fn(); const first = lightboxProps({ inline: true, onNext: firstNext }); @@ -209,7 +212,7 @@ describe('more than one lightbox on a page', () => { expect(firstNext.mock.calls.length + secondNext.mock.calls.length).toBeLessThanOrEqual(1); }); - test.fails('a key press pages only the open modal, not an inline carousel behind it (#65)', () => { + test('a key press pages only the open modal, not an inline carousel behind it (#65)', () => { const carousel = lightboxProps({ inline: true }); const modal = lightboxProps(); render( diff --git a/src/__tests__/inline.test.tsx b/src/__tests__/inline.test.tsx index b56473a..03f4d50 100644 --- a/src/__tests__/inline.test.tsx +++ b/src/__tests__/inline.test.tsx @@ -9,6 +9,8 @@ import { CENTER, image, lightboxProps, mockLayout, pageOf, settle } from './help const inlineProps = (currentIndex = 0) => lightboxProps({ currentIndex, inline: true }); +const nextButton = () => ; + describe('before the stage is measured', () => { test('shows only the current image', () => { render(); @@ -108,10 +110,42 @@ describe('once measured', () => { expect(image(1)).toHaveStyle({ maxHeight: '600px' }); }); - test.fails('leaves the keyboard to the page around it (#65)', () => { + test('leaves the keyboard to the page around it (#65)', () => { render(); - expect(pressKey('ArrowDown')).toBe(true); - expect(pressKey('PageDown')).toBe(true); + expect(pressKey('ArrowDown').defaultPrevented).toBe(false); + expect(pressKey('PageDown').defaultPrevented).toBe(false); + }); + + test('pages with → only while focus is inside it, as on its Next button', () => { + const props = inlineProps(2); + render( + + ); + + pressKey('ArrowRight'); + expect(props.onNext).not.toHaveBeenCalled(); + + screen.getByRole('button', { name: 'Next' }).focus(); + pressKey('ArrowRight'); + expect(props.onNext).toHaveBeenCalledTimes(1); }); + + test.each(['ArrowUp', 'ArrowDown', 'End', 'Home', 'PageUp', 'PageDown'])( + 'leaves %s to the page while one of its controls has focus', + (key) => { + render( + + ); + screen.getByRole('button', { name: 'Next' }).focus(); + + expect(pressKey(key).defaultPrevented).toBe(false); + } + ); }); diff --git a/src/__tests__/opening-and-closing.test.tsx b/src/__tests__/opening-and-closing.test.tsx index dcda918..61ab63b 100644 --- a/src/__tests__/opening-and-closing.test.tsx +++ b/src/__tests__/opening-and-closing.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, within } from '@testing-library/react'; +import { fireEvent, render, screen, within } from '@testing-library/react'; import React from 'react'; import { renderToString } from 'react-dom/server'; import { beforeEach, describe, expect, test, vi } from 'vitest'; @@ -142,6 +142,64 @@ describe('closing', () => { expect(props.onClose).toHaveBeenCalledTimes(1); }); + test('Esc in a text field inside asks to close', () => { + const props = lightboxProps({ renderFooter: () => }); + render(); + + pressKey('Escape', screen.getByRole('textbox', { name: 'Caption' })); + + expect(props.onClose).toHaveBeenCalledTimes(1); + }); + + test('Esc that a control inside handles itself leaves it open', () => { + const props = lightboxProps({ + renderHeader: () => ( + { + if (event.key === 'Escape') { + event.preventDefault(); + } + }} + /> + ), + }); + render(); + + pressKey('Escape', screen.getByRole('textbox', { name: 'Search' })); + + expect(props.onClose).not.toHaveBeenCalled(); + }); + + test('Esc that cancels input method composition leaves it open', () => { + const props = lightboxProps({ renderFooter: () => }); + render(); + + pressKey('Escape', screen.getByRole('textbox', { name: 'Caption' }), { isComposing: true }); + + expect(props.onClose).not.toHaveBeenCalled(); + }); + + test('ignores keys that reach it while it fades out', async () => { + const props = lightboxProps(); + const { rerender } = render(); + await settle(); + + rerender( + + ); + await advance(FRAME); + expect(lightboxContainer()).toHaveFocus(); + pressKey('Escape'); + pressKey('ArrowRight'); + + expect(props.onClose).not.toHaveBeenCalled(); + expect(props.onNext).not.toHaveBeenCalled(); + }); + test.each([ ['clicking', mouse.click], ['tapping', touchscreen.tap], @@ -271,22 +329,70 @@ describe('the page behind it', () => { test.each(['ArrowUp', 'ArrowDown', 'End', 'Home', 'PageUp', 'PageDown'])('does not scroll with %s', (key) => { render(); - expect(pressKey(key)).toBe(false); + expect(pressKey(key).defaultPrevented).toBe(true); + }); + + test.each([ + ['Meta+ArrowDown', 'ArrowDown', { metaKey: true }], + ['Alt+ArrowDown', 'ArrowDown', { altKey: true }], + ['Ctrl+End', 'End', { ctrlKey: true }], + ])('does not scroll with %s', (_, key, modifier) => { + render(); + + expect(pressKey(key, lightboxContainer(), modifier).defaultPrevented).toBe(true); + }); + + test('does not scroll with Space', () => { + render(); + + expect(pressKey(' ').defaultPrevented).toBe(true); + }); + + test.each([ + ['Alt', { altKey: true }], + ['Ctrl', { ctrlKey: true }], + ['Meta', { metaKey: true }], + ])('leaves %s+Space alone, since it does not scroll', (_, modifier) => { + render(); + + expect(pressKey(' ', lightboxContainer(), modifier).defaultPrevented).toBe(false); + }); + + test.each([ + ['a button', () => ], + [ + 'a summary', + () => ( +
+ Press me +
+ ), + ], + ])('leaves Space to %s inside, which it presses', (_, renderFooter) => { + render(); + + expect(pressKey(' ', screen.getByText('Press me')).defaultPrevented).toBe(false); + }); + + test('does not scroll as a held key repeats', () => { + render(); + + expect(fireEvent.keyDown(lightboxContainer(), { key: 'ArrowDown', repeat: true })).toBe(false); }); test('scrolls with the keyboard again once closed', () => { render(); - expect(pressKey('ArrowDown')).toBe(true); + expect(pressKey('ArrowDown').defaultPrevented).toBe(false); }); test('still gets keys that do not scroll', () => { render(); - expect(pressKey('a')).toBe(true); + expect(pressKey('a').defaultPrevented).toBe(false); }); - test.fails('lets a text field inside the lightbox move its cursor with Home and End (#65)', () => { + test('lets a text field inside the lightbox move its cursor with Home and End (#65)', () => { render( { ); const caption = screen.getByRole('textbox', { name: 'Caption' }); - expect(pressKey('Home', caption)).toBe(true); - expect(pressKey('End', caption)).toBe(true); + expect(pressKey('Home', caption).defaultPrevented).toBe(false); + expect(pressKey('End', caption).defaultPrevented).toBe(false); }); }); diff --git a/src/__tests__/paging.test.tsx b/src/__tests__/paging.test.tsx index 41160c5..89ea0be 100644 --- a/src/__tests__/paging.test.tsx +++ b/src/__tests__/paging.test.tsx @@ -1,5 +1,6 @@ import { fireEvent, render, screen } from '@testing-library/react'; -import React from 'react'; +import React, { useState } from 'react'; +import { createPortal } from 'react-dom'; import { beforeEach, describe, expect, test, vi } from 'vitest'; import type { Point } from './helpers'; @@ -12,6 +13,7 @@ import { Gallery, advance, image, + lightboxContainer, lightboxProps, mockLayout, pageOf, @@ -71,6 +73,22 @@ const openGallery = async (initialIndex = START) => { await settle(); }; +const HeaderMenu = () => { + const [isOpen, setIsOpen] = useState(false); + + return ( + <> + + {isOpen && createPortal(, document.body)} + + ); +}; + describe('dragging with a mouse', () => { test('moves the pages along with the pointer', async () => { await openGallery(); @@ -504,6 +522,19 @@ describe('the keyboard', () => { expect(shownImage()).toBe('Image 2'); }); + test('→ pages once while an arrow button beside the image has focus', () => { + const props = lightboxProps({ + currentIndex: START, + renderNextButton: () => , + }); + render(); + screen.getByRole('button', { name: 'Next' }).focus(); + + pressKey('ArrowRight'); + + expect(props.onNext).toHaveBeenCalledTimes(1); + }); + test('arrow keys do nothing while closed', () => { const props = lightboxProps({ currentIndex: START, isOpen: false }); render(); @@ -536,7 +567,7 @@ describe('the keyboard', () => { expect(props.onNext).not.toHaveBeenCalled(); }); - test.fails('leaves arrow keys to a text field inside the lightbox (#65)', () => { + test('leaves arrow keys to a text field inside the lightbox (#65)', () => { const props = lightboxProps({ currentIndex: START, renderFooter: () => , @@ -547,6 +578,64 @@ describe('the keyboard', () => { expect(props.onNext).not.toHaveBeenCalled(); }); + + test.each([ + ['a textarea', () =>