An open source design system that's fully customizable and agent ready
TypeScript
test(DropdownMenu): a footer stays reachable by Tab when the React root is the document (#9815)
Xinzi Zhoub215c3e2f1ab
diff --git a/packages/core/src/DropdownMenu/DropdownMenuDocumentRoot.test.tsx b/packages/core/src/DropdownMenu/DropdownMenuDocumentRoot.test.tsxnew file mode 100644index 0000000000..02b3f3586b--- /dev/null+++ b/packages/core/src/DropdownMenu/DropdownMenuDocumentRoot.test.tsx@@ -0,0 +1,119 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file DropdownMenuDocumentRoot.test.tsx+ * @input vitest, @testing-library/react, a DropdownMenu with a footer rendered+ * by a React root on the document+ * @output Tab and Shift+Tab through the footer's controls in an app whose+ * React root is the document, as a Next.js App Router app hydrates it+ * @position Own file: React attaches its root listeners to the document only+ * for the first root in a test environment, so this root must come first.+ */++import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';+import {act, fireEvent, screen, waitFor} from '@testing-library/react';+import {createRoot, type Root} from 'react-dom/client';+import {DropdownMenu} from './DropdownMenu';+import {DropdownMenuItem} from './DropdownMenuItem';++// React's key listener and the popover focus trap's listener sit on the same+// node, the document, so stopPropagation in a React handler does not keep a+// press from the trap.+let root: Root | null = null;++beforeEach(() => {+ HTMLElement.prototype.showPopover = vi.fn(function (this: HTMLElement) {+ this.setAttribute('popover-open', '');+ const event = new Event('toggle', {bubbles: false});+ Object.defineProperty(event, 'newState', {value: 'open'});+ this.dispatchEvent(event);+ });+ HTMLElement.prototype.hidePopover = vi.fn(function (this: HTMLElement) {+ this.removeAttribute('popover-open');+ const event = new Event('toggle', {bubbles: false});+ Object.defineProperty(event, 'newState', {value: 'closed'});+ this.dispatchEvent(event);+ });+});++afterEach(() => {+ act(() => root?.unmount());+ root = null;+});++async function openColumnsMenu() {+ root = createRoot(document);+ act(() =>+ root!.render(+ <html lang="en">+ <head />+ <body>+ <DropdownMenu+ button={{label: 'Columns'}}+ footer={+ <>+ <button type="button">Select all</button>+ <button type="button">Reset to default</button>+ </>+ }>+ <DropdownMenuItem label="Author" />+ <DropdownMenuItem label="Status" />+ </DropdownMenu>+ </body>+ </html>,+ ),+ );+ const trigger = screen.getByRole('button', {name: /Columns/});+ act(() => trigger.focus());+ fireEvent.keyDown(trigger, {key: 'ArrowDown'});+ // A keyboard open lands on the first row.+ await waitFor(() =>+ expect(+ screen.getByRole('menuitem', {name: 'Author', hidden: true}),+ ).toHaveFocus(),+ );+ const hide = vi.mocked(HTMLElement.prototype.hidePopover);+ hide.mockClear();+ return {+ hide,+ menu: screen.getByRole('menu', {name: 'Columns', hidden: true}),+ lastRow: screen.getByRole('menuitem', {name: 'Status', hidden: true}),+ selectAll: screen.getByRole('button', {name: 'Select all', hidden: true}),+ reset: screen.getByRole('button', {name: 'Reset to default', hidden: true}),+ };+}++describe('DropdownMenu footer in an app whose React root is the document', () => {+ it('Tab from the last row reaches each footer control in turn', async () => {+ const {lastRow, selectAll, reset, hide} = await openColumnsMenu();+ act(() => lastRow.focus());++ fireEvent.keyDown(lastRow, {key: 'Tab'});+ expect(selectAll).toHaveFocus();+ fireEvent.keyDown(selectAll, {key: 'Tab'});+ expect(reset).toHaveFocus();+ expect(hide).not.toHaveBeenCalled();+ });++ it('Shift+Tab from the last footer control goes back through the footer to the menu', async () => {+ const {menu, selectAll, reset, hide} = await openColumnsMenu();+ act(() => reset.focus());++ fireEvent.keyDown(reset, {key: 'Tab', shiftKey: true});+ expect(selectAll).toHaveFocus();+ fireEvent.keyDown(selectAll, {key: 'Tab', shiftKey: true});+ expect(menu).toHaveFocus();+ expect(hide).not.toHaveBeenCalled();+ });++ it('Tab past the last footer control wraps to the menu, and Shift+Tab wraps back', async () => {+ const {menu, reset, hide} = await openColumnsMenu();+ act(() => reset.focus());++ fireEvent.keyDown(reset, {key: 'Tab'});+ expect(menu).toHaveFocus();+ fireEvent.keyDown(menu, {key: 'Tab', shiftKey: true});+ expect(reset).toHaveFocus();+ expect(hide).not.toHaveBeenCalled();+ });+});diff --git a/packages/core/src/hooks/useFocusTrap.test.tsx b/packages/core/src/hooks/useFocusTrap.test.tsxindex 9814ac842d..05003bc074 100644--- a/packages/core/src/hooks/useFocusTrap.test.tsx+++ b/packages/core/src/hooks/useFocusTrap.test.tsx@@ -448,6 +448,103 @@ describe('useFocusTrap tabbable model (infra-8)', () => {expect(fireEvent.keyDown(outside, {key: 'Tab'})).toBe(true);});++ describe('a Tab that content inside has already handled', () => {+ // Content that walks its own Tab stops, such as a menu with a footer,+ // moves focus and cancels the default. When the React root is the+ // document (a Next.js App Router app), React's listener and the trap's+ // listener share that node, so stopPropagation does not keep the press+ // from the trap. A document listener added before the trap stands in for+ // React's root listener here.+ let removeContentHandler: (() => void) | null = null;++ function handleTabInContent(move: (event: KeyboardEvent) => void) {+ const listener = (event: KeyboardEvent) => {+ if (event.key === 'Tab') {+ event.preventDefault();+ event.stopPropagation();+ move(event);+ }+ };+ document.addEventListener('keydown', listener);+ removeContentHandler = () =>+ document.removeEventListener('keydown', listener);+ }++ afterEach(() => {+ removeContentHandler?.();+ removeContentHandler = null;+ });++ function renderThreeControls() {+ render(+ <Trap>+ <button type="button" data-testid="first">+ First+ </button>+ <button type="button" data-testid="middle">+ Middle+ </button>+ <button type="button" data-testid="last">+ Last+ </button>+ </Trap>,+ );+ return {+ first: screen.getByTestId('first'),+ middle: screen.getByTestId('middle'),+ last: screen.getByTestId('last'),+ };+ }++ it('still wraps a Tab nothing handled, at either end', () => {+ const {first, last} = renderThreeControls();+ last.focus();++ expect(fireEvent.keyDown(last, {key: 'Tab'})).toBe(false);+ expect(first).toHaveFocus();++ expect(fireEvent.keyDown(first, {key: 'Tab', shiftKey: true})).toBe(+ false,+ );+ expect(last).toHaveFocus();+ });++ it('keeps focus where the content moved it on the last control', () => {+ handleTabInContent(() => screen.getByTestId('last').focus());+ const {middle, last} = renderThreeControls();+ middle.focus();++ fireEvent.keyDown(middle, {key: 'Tab'});++ expect(last).toHaveFocus();+ });++ it('keeps focus where the content moved it on the first control by Shift+Tab', () => {+ handleTabInContent(() => screen.getByTestId('first').focus());+ const {first, middle} = renderThreeControls();+ middle.focus();++ fireEvent.keyDown(middle, {key: 'Tab', shiftKey: true});++ expect(first).toHaveFocus();+ });++ it('does not pull in focus that moves outside after the handled press', () => {+ handleTabInContent(() => screen.getByTestId('middle').focus());+ const {first, middle} = renderThreeControls();+ first.focus();+ fireEvent.keyDown(first, {key: 'Tab'});+ expect(middle).toHaveFocus();++ // No Tab carried focus out, so a later move outside (a click on the+ // page) is not an escape to redirect.+ const outside = screen.getByTestId('outside');+ outside.focus();++ expect(outside).toHaveFocus();+ });+ });});describe('FOCUSABLE_SELECTOR href matching', () => {