/* * Copyright 2022 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import { act, fireEvent, installPointerEvent, mockClickDefault, pointerMap, render, setupIntersectionObserverMock, within } from '@react-spectrum/test-utils-internal'; import {Button} from '../src/Button'; import {Collection} from 'react-aria/Collection'; import {DataTransfer, DragEvent} from 'react-aria/test/dnd/mocks'; import {Dialog, DialogTrigger} from '../src/Dialog'; import {DropIndicator, useDragAndDrop} from '../src/useDragAndDrop'; import {Header} from '../src/Header'; import {Heading} from '../src/Heading'; import {ListBox, ListBoxContext, ListBoxItem, ListBoxSection} from '../src/ListBox'; import {ListBoxLoadMoreItem} from '../src/ListBox'; import {ListLayout} from 'react-stately/useVirtualizerState'; import {Modal} from '../src/Modal'; import React, {useEffect, useState} from 'react'; import {Selection} from 'react-stately/src/selection/Selection'; import {Text} from '../src/Text'; import {User} from '@react-aria/test-utils'; import userEvent from '@testing-library/user-event'; import {Virtualizer} from '../src/Virtualizer'; let TestListBox = ({listBoxProps, itemProps}) => ( Cat Dog Kangaroo ); let DraggableListBox = props => { let {dragAndDropHooks} = useDragAndDrop({ getItems: keys => [...keys].map(key => ({'text/plain': key})), ...props }); return ( Cat Dog Kangaroo ); }; let renderListbox = (listBoxProps, itemProps) => render(); let keyPress = key => { fireEvent.keyDown(document.activeElement, {key}); fireEvent.keyUp(document.activeElement, {key}); }; describe('ListBox', () => { let user; let testUtilUser = new User(); let onSelectionChange = jest.fn(); beforeAll(() => { user = userEvent.setup({delay: null, pointerMap}); jest.useFakeTimers(); }); beforeEach(() => { jest.spyOn(HTMLElement.prototype, 'scrollLeft', 'get').mockImplementation(() => 0); jest.spyOn(HTMLElement.prototype, 'scrollTop', 'get').mockImplementation(() => 0); }); afterEach(() => { act(() => { jest.runAllTimers(); }); jest.restoreAllMocks(); }); it('should have the base set of aria and data attributes', () => { let {getByRole} = render( Cat Dog Kangaroo
Fish
Salmon Tuna Cod
); let listboxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); expect(listboxTester.getListbox()).toHaveAttribute('data-rac'); let sections = listboxTester.getSections(); for (let section of sections) { expect(section).toHaveAttribute('data-rac'); } let options = listboxTester.getOptions(); for (let option of options) { expect(option).toHaveAttribute('data-rac'); } }); it('should render with default classes', () => { let {getByRole, getAllByRole} = renderListbox(); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('class', 'react-aria-ListBox'); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('class', 'react-aria-ListBoxItem'); } }); it('should render with custom classes', () => { let {getByRole, getAllByRole} = renderListbox({className: 'listbox'}, {className: 'item'}); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('class', 'listbox'); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('class', 'item'); } }); it('should support DOM props', () => { let {getByRole, getAllByRole} = renderListbox({'data-foo': 'bar'}, {'data-bar': 'foo'}); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('data-foo', 'bar'); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('data-bar', 'foo'); } }); it('should support custom render function', () => { let {getAllByRole, getByRole} = renderListbox( {render: props =>
}, {render: props =>
} ); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('data-custom', 'true'); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('data-custom', 'true'); } }); it('should support custom render function as a link', () => { let {getAllByRole, getByRole} = renderListbox( {render: props =>
}, // eslint-disable-next-line jsx-a11y/anchor-has-content {href: '#foo', render: props => } ); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('data-custom', 'true'); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('href'); expect(option).toHaveAttribute('data-custom', 'true'); } }); it('should support aria-label on the listbox items', () => { let {getAllByRole} = renderListbox({}, {'aria-label': 'test'}); for (let option of getAllByRole('option')) { expect(option).toHaveAttribute('aria-label', 'test'); } }); it('should support the slot prop', () => { let {getByRole} = render( ); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('slot', 'test'); expect(listbox).toHaveAttribute('aria-label', 'test'); }); it('should support refs', () => { let listBoxRef = React.createRef(); let sectionRef = React.createRef(); let itemRef = React.createRef(); render( Cat ); expect(listBoxRef.current).toBeInstanceOf(HTMLElement); expect(sectionRef.current).toBeInstanceOf(HTMLElement); expect(itemRef.current).toBeInstanceOf(HTMLElement); }); it('should support slots', () => { let {getByRole} = render( Read Read only ); let option = getByRole('option'); expect(option).toHaveAttribute('aria-labelledby'); expect(document.getElementById(option.getAttribute('aria-labelledby'))).toHaveTextContent( 'Read' ); expect(option).toHaveAttribute('aria-describedby'); expect(document.getElementById(option.getAttribute('aria-describedby'))).toHaveTextContent( 'Read only' ); }); it('should support sections', () => { let {getAllByRole} = render(
Veggies
Lettuce Tomato Onion
Ham Tuna Tofu
); let groups = getAllByRole('group'); expect(groups).toHaveLength(2); expect(groups[0]).toHaveClass('react-aria-ListBoxSection'); expect(groups[1]).toHaveClass('react-aria-ListBoxSection'); expect(groups[0]).toHaveAttribute('aria-labelledby'); expect(document.getElementById(groups[0].getAttribute('aria-labelledby'))).toHaveTextContent( 'Veggies' ); expect(groups[1].getAttribute('aria-label')).toEqual('Protein'); expect(groups[0]).toHaveAttribute('data-test-prop', 'test-section-1'); expect(groups[1]).toHaveAttribute('data-test-prop', 'test-section-2'); }); it('should support dynamic collections', () => { let options = [ {id: 1, name: 'Cat'}, {id: 2, name: 'Dog'} ]; let {getAllByRole, rerender} = render( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Cat', 'Dog']); options = [options[0], options[1], {id: 3, name: 'Mouse'}]; rerender( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Cat', 'Dog', 'Mouse']); options = [options[0], {id: 4, name: 'Kangaroo'}, options[1], options[2]]; rerender( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual([ 'Cat', 'Kangaroo', 'Dog', 'Mouse' ]); options = [options[0], options[1], {id: 2, name: 'Doggy'}, options[3]]; rerender( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual([ 'Cat', 'Kangaroo', 'Doggy', 'Mouse' ]); options = [options[0], options[1], options[3], options[2]]; rerender( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual([ 'Cat', 'Kangaroo', 'Mouse', 'Doggy' ]); options = [options[0], options[1], options[2]]; rerender( {item => {item.name}} ); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Cat', 'Kangaroo', 'Mouse']); }); it('should update collection when descendants update', () => { let setShowTwo; let setItemText; function Child() { let [showTwo, _setShowTwo] = useState(false); let [itemText, _setItemText] = useState('One'); useEffect(() => { setItemText = _setItemText; setShowTwo = _setShowTwo; }, [_setItemText, _setShowTwo]); return ( <> {itemText} {showTwo && Two} ); } let {getAllByRole} = render( ); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['One']); act(() => setShowTwo(true)); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['One', 'Two']); act(() => setShowTwo(false)); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['One']); act(() => setItemText('Hi')); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Hi']); }); it('should update collection when moving item to a different section', () => { let {getAllByRole, rerender} = render(
Veggies
Lettuce Tomato Onion
Meats
Ham Tuna Tofu
); let sections = getAllByRole('group'); let items = within(sections[0]).getAllByRole('option'); expect(items).toHaveLength(3); items = within(sections[1]).getAllByRole('option'); expect(items).toHaveLength(3); rerender(
Veggies
Lettuce Tomato Onion Ham
Meats
Tuna Tofu
); sections = getAllByRole('group'); items = within(sections[0]).getAllByRole('option'); expect(items).toHaveLength(4); items = within(sections[1]).getAllByRole('option'); expect(items).toHaveLength(2); }); it('should support dynamic collection with non-object entries', () => { let {getAllByRole, rerender} = render( {item => {item}} ); let items = getAllByRole('option'); expect(items).toHaveLength(3); expect(items[0].textContent).toBe('Foo'); expect(items[1].textContent).toBe('Bar'); expect(items[2].textContent).toBe('Baz'); rerender( {item => {item}} ); items = getAllByRole('option'); expect(items).toHaveLength(2); expect(items[0].textContent).toBe('Foo'); expect(items[1].textContent).toBe('Baz'); }); it('should update collection if render function changes', () => { let objects = [{name: 'Foo'}, {name: 'Bar'}, {name: 'Baz'}]; let {getAllByRole, rerender} = render( {item => {item.name}} ); let items = getAllByRole('option'); expect(items).toHaveLength(3); expect(items[0].textContent).toBe('Foo'); expect(items[1].textContent).toBe('Bar'); expect(items[2].textContent).toBe('Baz'); rerender( {item => ( Updated: {item.name} )} ); items = getAllByRole('option'); expect(items).toHaveLength(3); expect(items[0].textContent).toBe('Updated: Foo'); expect(items[1].textContent).toBe('Updated: Bar'); expect(items[2].textContent).toBe('Updated: Baz'); }); it('should support autoFocus', () => { let {getByRole} = renderListbox({autoFocus: true}); let listbox = getByRole('listbox'); expect(document.activeElement).toBe(listbox); }); it('should support hover', async () => { let hoverStartSpy = jest.fn(); let hoverChangeSpy = jest.fn(); let hoverEndSpy = jest.fn(); let {getAllByRole} = renderListbox( {selectionMode: 'multiple'}, { className: ({isHovered}) => (isHovered ? 'hover' : ''), onHoverStart: hoverStartSpy, onHoverEnd: hoverEndSpy, onHoverChange: hoverChangeSpy } ); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-hovered'); expect(option).not.toHaveClass('hover'); await user.hover(option); expect(option).toHaveAttribute('data-hovered', 'true'); expect(option).toHaveClass('hover'); expect(hoverStartSpy).toHaveBeenCalledTimes(1); expect(hoverChangeSpy).toHaveBeenCalledTimes(1); await user.unhover(option); expect(option).not.toHaveAttribute('data-hovered'); expect(option).not.toHaveClass('hover'); expect(hoverEndSpy).toHaveBeenCalledTimes(1); expect(hoverChangeSpy).toHaveBeenCalledTimes(2); }); it('should show hover state on draggable options even when not selectable/actionable', async () => { let {getAllByRole} = render(); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-hovered'); await user.hover(option); expect(option).toHaveAttribute('data-hovered', 'true'); await user.unhover(option); expect(option).not.toHaveAttribute('data-hovered'); }); it('should not show hover state when item is not interactive', async () => { let onHoverStart = jest.fn(); let onHoverChange = jest.fn(); let onHoverEnd = jest.fn(); let {getAllByRole} = renderListbox( {}, { className: ({isHovered}) => (isHovered ? 'hover' : ''), onHoverStart, onHoverEnd, onHoverChange } ); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-hovered'); expect(option).not.toHaveClass('hover'); expect(onHoverStart).not.toHaveBeenCalled(); expect(onHoverChange).not.toHaveBeenCalled(); expect(onHoverEnd).not.toHaveBeenCalled(); await user.hover(option); expect(option).not.toHaveAttribute('data-hovered'); expect(option).not.toHaveClass('hover'); expect(onHoverStart).not.toHaveBeenCalled(); expect(onHoverChange).not.toHaveBeenCalled(); expect(onHoverEnd).not.toHaveBeenCalled(); }); it('should support focus ring', async () => { let {getAllByRole} = renderListbox( {selectionMode: 'multiple', shouldFocusOnHover: true}, {className: ({isFocusVisible}) => (isFocusVisible ? 'focus' : '')} ); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-focus-visible'); expect(option).not.toHaveClass('focus'); await user.tab(); expect(document.activeElement).toBe(option); expect(option).toHaveAttribute('data-focus-visible', 'true'); expect(option).toHaveClass('focus'); expect(option).not.toHaveAttribute('data-hovered'); fireEvent.keyDown(option, {key: 'ArrowDown'}); fireEvent.keyUp(option, {key: 'ArrowDown'}); expect(option).not.toHaveAttribute('data-focus-visible'); expect(option).not.toHaveClass('focus'); }); it('should support press state', async () => { let {getAllByRole} = renderListbox( {selectionMode: 'multiple'}, {className: ({isPressed}) => (isPressed ? 'pressed' : '')} ); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-pressed'); expect(option).not.toHaveClass('pressed'); await user.pointer({target: option, keys: '[MouseLeft>]'}); expect(option).toHaveAttribute('data-pressed', 'true'); expect(option).toHaveClass('pressed'); await user.pointer({target: option, keys: '[/MouseLeft]'}); expect(option).not.toHaveAttribute('data-pressed'); expect(option).not.toHaveClass('pressed'); }); it('should not show press state when not interactive', async () => { let {getAllByRole} = renderListbox( {}, {className: ({isPressed}) => (isPressed ? 'pressed' : '')} ); let option = getAllByRole('option')[0]; expect(option).not.toHaveAttribute('data-pressed'); expect(option).not.toHaveClass('pressed'); await user.pointer({target: option, keys: '[MouseLeft>]'}); expect(option).not.toHaveAttribute('data-pressed'); expect(option).not.toHaveClass('pressed'); await user.pointer({target: option, keys: '[/MouseLeft]'}); expect(option).not.toHaveAttribute('data-pressed'); expect(option).not.toHaveClass('pressed'); }); it('should support selection state', async () => { let {getByRole} = renderListbox( {selectionMode: 'multiple'}, {className: ({isSelected}) => (isSelected ? 'selected' : '')} ); let listboxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); let option = listboxTester.getOptions()[0]; expect(option).not.toHaveAttribute('aria-selected', 'true'); expect(option).not.toHaveClass('selected'); await listboxTester.toggleOptionSelection({option}); expect(option).toHaveAttribute('aria-selected', 'true'); expect(option).toHaveClass('selected'); await listboxTester.toggleOptionSelection({option}); expect(option).not.toHaveAttribute('aria-selected', 'true'); expect(option).not.toHaveClass('selected'); }); it('should support disabled state', () => { let {getAllByRole} = renderListbox( {selectionMode: 'multiple', disabledKeys: ['cat']}, {className: ({isDisabled}) => (isDisabled ? 'disabled' : '')} ); let option = getAllByRole('option')[0]; expect(option).toHaveAttribute('aria-disabled', 'true'); expect(option).toHaveClass('disabled'); }); it('should support isDisabled prop on items', async () => { let {getAllByRole} = render( Cat Dog Kangaroo ); let items = getAllByRole('option'); expect(items[1]).toHaveAttribute('aria-disabled', 'true'); await user.tab(); expect(document.activeElement).toBe(items[0]); await user.keyboard('{ArrowDown}'); expect(document.activeElement).toBe(items[2]); }); it.each` interactionType ${'mouse'} ${'keyboard'} ${'touch'} `('should support onAction, interactionType: $interactionType ', async ({interactionType}) => { let onAction = jest.fn(); let {getByRole} = render( Cat Dog Kangaroo ); let listboxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); let options = listboxTester.getOptions(); await listboxTester.triggerOptionAction({option: options[0], interactionType}); expect(onAction).toHaveBeenCalledTimes(1); }); it('should trigger onAction on double click if selectionBehavior="replace"', async () => { let onAction = jest.fn(); let {getByRole} = render( Cat Dog Kangaroo ); let listboxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); let options = listboxTester.getOptions(); await listboxTester.triggerOptionAction({option: options[0]}); let selectedOptions = listboxTester.getSelectedOptions(); expect(selectedOptions).toHaveLength(1); expect(onAction).not.toHaveBeenCalled(); await listboxTester.triggerOptionAction({option: options[1], needsDoubleClick: true}); selectedOptions = listboxTester.getSelectedOptions(); expect(selectedOptions).toHaveLength(1); expect(onAction).toHaveBeenCalledTimes(1); }); describe('selectionBehavior="replace"', () => { // Required for proper touch detection installPointerEvent(); describe.each(['mouse', 'keyboard', 'touch'])('%s', type => { it('should perform selection with single selection', async () => { let {getByRole} = renderListbox({ selectionMode: 'single', selectionBehavior: 'replace', onSelectionChange }); let listboxTester = testUtilUser.createTester('ListBox', { root: getByRole('listbox'), interactionType: type }); let options = listboxTester.getOptions(); expect(onSelectionChange).toHaveBeenCalledTimes(0); let option2 = options[2]; await listboxTester.toggleOptionSelection({ option: 'Kangaroo', selectionBehavior: 'replace' }); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { // Called twice because initial focus will select the first keyboard focused row expect(onSelectionChange).toHaveBeenCalledTimes(2); } else { expect(onSelectionChange).toHaveBeenCalledTimes(1); } expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option2); let option1 = options[1]; await listboxTester.toggleOptionSelection({option: option1, selectionBehavior: 'replace'}); expect(option1).toHaveAttribute('aria-selected', 'true'); expect(option1).toHaveAttribute('data-selected', 'true'); expect(option2).toHaveAttribute('aria-selected', 'false'); expect(option2).not.toHaveAttribute('data-selected'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(3); } else { expect(onSelectionChange).toHaveBeenCalledTimes(2); } expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['dog'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option1); await listboxTester.toggleOptionSelection({option: option1, selectionBehavior: 'replace'}); expect(option1).toHaveAttribute('aria-selected', 'false'); expect(option1).not.toHaveAttribute('data-selected'); expect(option2).toHaveAttribute('aria-selected', 'false'); expect(option2).not.toHaveAttribute('data-selected'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(4); } else { expect(onSelectionChange).toHaveBeenCalledTimes(3); } expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set([])); expect(listboxTester.getSelectedOptions()).toHaveLength(0); }); it('should perform toggle selection in highlight mode when using modifier keys', async () => { let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let listboxTester = testUtilUser.createTester('ListBox', { root: getByRole('listbox'), interactionType: type }); let options = listboxTester.getOptions(); let option2 = options[2]; await listboxTester.toggleOptionSelection({ option: 'Kangaroo', selectionBehavior: 'replace' }); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { // Called twice because initial focus will select the first keyboard focused row, meaning we have two items selected expect(onSelectionChange).toHaveBeenCalledTimes(2); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual( new Set(['cat', 'kangaroo']) ); expect(listboxTester.getSelectedOptions()).toHaveLength(2); expect(listboxTester.getSelectedOptions()[1]).toBe(option2); } else { expect(onSelectionChange).toHaveBeenCalledTimes(1); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option2); } let option1 = options[1]; await listboxTester.toggleOptionSelection({option: option1, selectionBehavior: 'replace'}); expect(option1).toHaveAttribute('aria-selected', 'true'); expect(option1).toHaveAttribute('data-selected', 'true'); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(3); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual( new Set(['cat', 'dog', 'kangaroo']) ); expect(listboxTester.getSelectedOptions()).toHaveLength(3); expect(listboxTester.getSelectedOptions()[1]).toBe(option1); expect(listboxTester.getSelectedOptions()[2]).toBe(option2); } else { expect(onSelectionChange).toHaveBeenCalledTimes(2); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual( new Set(['dog', 'kangaroo']) ); expect(listboxTester.getSelectedOptions()).toHaveLength(2); expect(listboxTester.getSelectedOptions()[0]).toBe(option1); expect(listboxTester.getSelectedOptions()[1]).toBe(option2); } // With modifier key, you should be able to deselect on press of the same row await listboxTester.toggleOptionSelection({option: option1, selectionBehavior: 'replace'}); expect(option1).toHaveAttribute('aria-selected', 'false'); expect(option1).not.toHaveAttribute('data-selected'); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(4); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual( new Set(['cat', 'kangaroo']) ); expect(listboxTester.getSelectedOptions()).toHaveLength(2); expect(listboxTester.getSelectedOptions()[1]).toBe(option2); } else { expect(onSelectionChange).toHaveBeenCalledTimes(3); expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option2); } }); it('should perform replace selection in highlight mode when not using modifier keys', async () => { let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let listboxTester = testUtilUser.createTester('ListBox', { root: getByRole('listbox'), interactionType: type }); let options = listboxTester.getOptions(); let option2 = options[2]; await listboxTester.toggleOptionSelection({option: 'Kangaroo'}); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { // Called multiple times since selection changes on option focus as we arrow down to the target option expect(onSelectionChange).toHaveBeenCalledTimes(3); } else { expect(onSelectionChange).toHaveBeenCalledTimes(1); } expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option2); let option1 = options[1]; await listboxTester.toggleOptionSelection({option: option1}); if (type !== 'touch') { expect(option1).toHaveAttribute('aria-selected', 'true'); expect(option1).toHaveAttribute('data-selected', 'true'); expect(option2).toHaveAttribute('aria-selected', 'false'); expect(option2).not.toHaveAttribute('data-selected'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(4); } else { expect(onSelectionChange).toHaveBeenCalledTimes(2); } expect(new Set(onSelectionChange.mock.calls.at(-1)[0])).toEqual(new Set(['dog'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option1); // pressing without modifier keys won't deselect the row await listboxTester.toggleOptionSelection({option: option1}); expect(option1).toHaveAttribute('aria-selected', 'true'); expect(option1).toHaveAttribute('data-selected', 'true'); if (type === 'keyboard') { expect(onSelectionChange).toHaveBeenCalledTimes(4); } else { expect(onSelectionChange).toHaveBeenCalledTimes(2); } expect(listboxTester.getSelectedOptions()).toHaveLength(1); } else { // touch always behaves as toggle expect(option1).toHaveAttribute('aria-selected', 'true'); expect(option1).toHaveAttribute('data-selected', 'true'); expect(option2).toHaveAttribute('aria-selected', 'true'); expect(option2).toHaveAttribute('data-selected', 'true'); expect(onSelectionChange).toHaveBeenCalledTimes(2); expect(new Set(onSelectionChange.mock.calls[1][0])).toEqual(new Set(['dog', 'kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(2); expect(listboxTester.getSelectedOptions()[0]).toBe(option1); await listboxTester.toggleOptionSelection({option: option1}); expect(option1).toHaveAttribute('aria-selected', 'false'); expect(option1).not.toHaveAttribute('data-selected'); expect(onSelectionChange).toHaveBeenCalledTimes(3); expect(new Set(onSelectionChange.mock.calls[2][0])).toEqual(new Set(['kangaroo'])); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(option2); } }); }); }); describe('with pointer events', () => { installPointerEvent(); it('should trigger selection on long press if both onAction and selection exist (touch only)', async () => { let onAction = jest.fn(); let {getByRole} = render( Cat Dog Kangaroo ); let listboxTester = testUtilUser.createTester('ListBox', { root: getByRole('listbox'), advanceTimer: jest.advanceTimersByTime, interactionType: 'touch' }); await listboxTester.toggleOptionSelection({option: listboxTester.getOptions()[0]}); expect(listboxTester.getSelectedOptions()).toHaveLength(0); expect(onAction).toHaveBeenCalledTimes(1); await listboxTester.toggleOptionSelection({ option: listboxTester.getOptions()[0], needsLongPress: true }); expect(listboxTester.getSelectedOptions()).toHaveLength(1); expect(listboxTester.getSelectedOptions()[0]).toBe(listboxTester.getOptions()[0]); expect(onAction).toHaveBeenCalledTimes(1); await listboxTester.toggleOptionSelection({option: listboxTester.getOptions()[1]}); expect(listboxTester.getSelectedOptions()).toHaveLength(2); expect(listboxTester.getSelectedOptions()[1]).toBe(listboxTester.getOptions()[1]); }); }); it('should support onAction on list and list items', async () => { let onAction = jest.fn(); let itemAction = jest.fn(); let {getAllByRole} = render( Cat Dog Kangaroo ); let items = getAllByRole('option'); await user.click(items[0]); expect(onAction).toHaveBeenCalledWith('cat'); expect(itemAction).toHaveBeenCalledTimes(1); }); it('should support empty state', () => { let {getByRole} = render( 'No results'}> {[]} ); let listbox = getByRole('listbox'); expect(listbox).toHaveAttribute('data-empty', 'true'); let option = getByRole('option'); expect(option).toHaveTextContent('No results'); }); it('should support horizontal orientation', async () => { let {getAllByRole} = renderListbox({orientation: 'horizontal'}); let options = getAllByRole('option'); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('ArrowRight'); expect(document.activeElement).toBe(options[1]); keyPress('ArrowRight'); expect(document.activeElement).toBe(options[2]); keyPress('ArrowLeft'); expect(document.activeElement).toBe(options[1]); }); it('should support grid layout', async () => { let {getAllByRole} = renderListbox({layout: 'grid'}); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 200, width: 200, height: 200}; } else { let index = [...this.parentElement.children].indexOf(this); return { top: Math.floor(index / 2) * 40, left: index % 2 ? 100 : 0, bottom: Math.floor(index / 2) * 40 + 40, right: index % 2 ? 200 : 100, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('ArrowDown'); expect(document.activeElement).toBe(options[2]); keyPress('ArrowLeft'); expect(document.activeElement).toBe(options[1]); keyPress('ArrowLeft'); expect(document.activeElement).toBe(options[0]); keyPress('ArrowRight'); expect(document.activeElement).toBe(options[1]); }); it('should not throw TypeError at boundaries of vertical grid layout when keyboard navigating (up/down)', async () => { /** * The following ListBox is roughly in this shape: * * ## | 1,1 | 1,2 | 1,3 | * * ## | 2,1 | 2,2 | 2,3 | * * ## | 3,1 | 3,2 | 3,3 | */ let {getAllByRole} = render( 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 ); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 300, width: 300, height: 200}; } else { let index = [...this.parentElement.children].indexOf(this); return { top: Math.floor(index / 3) * 40, left: (index % 3) * 100, bottom: Math.floor(index / 3) * 40 + 40, right: (index % 3) * 100 + 100, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); // 1,1 keyPress('ArrowDown'); expect(document.activeElement).toBe(options[3]); // 2,1 keyPress('ArrowDown'); // the end reached expect(document.activeElement).toBe(options[6]); // 3,1 keyPress('ArrowDown'); // shouldn't throw when pressed one more time at the boundary. expect(document.activeElement).toBe(options[6]); // 3,1 keyPress('ArrowUp'); expect(document.activeElement).toBe(options[3]); // 2,1 keyPress('ArrowUp'); // the end reached expect(document.activeElement).toBe(options[0]); // 1,1 keyPress('ArrowUp'); // shouldn't throw when pressed one more time at the boundary. expect(document.activeElement).toBe(options[0]); // 1,1 }); it('should support horizontal grid layout', async () => { let {getAllByRole} = renderListbox({layout: 'grid', orientation: 'horizontal'}); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 200, width: 200, height: 200}; } else { let index = [...this.parentElement.children].indexOf(this); return { top: (index % 2) * 40, left: index < 2 ? 0 : 100, bottom: (index % 2) * 40 + 40, right: index < 2 ? 100 : 200, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('ArrowRight'); expect(document.activeElement).toBe(options[2]); keyPress('ArrowUp'); expect(document.activeElement).toBe(options[1]); keyPress('ArrowUp'); expect(document.activeElement).toBe(options[0]); keyPress('ArrowDown'); expect(document.activeElement).toBe(options[1]); }); it('should not throw TypeError at boundaries of horizontal grid layout when keyboard navigating (left/right)', async () => { /** * The following ListBox is roughly in this shape: * * ## | 1,1 | 2,1 | 3,1 | * * ## | 1,2 | 2,2 | 3,2 | * * ## | 1,3 | 3,2 | 3,3 | */ let {getAllByRole} = render( 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 ); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 300, width: 300, height: 200}; } else { let index = [...this.parentElement.children].indexOf(this); return { top: (index % 3) * 40, left: Math.floor(index / 3) * 100, bottom: (index % 3) * 40 + 40, right: Math.floor(index / 3) * 100 + 100, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); // 1,1 keyPress('ArrowRight'); expect(document.activeElement).toBe(options[3]); // 2,1 keyPress('ArrowRight'); // the end reached expect(document.activeElement).toBe(options[6]); // 3,1 keyPress('ArrowRight'); // shouldn't throw when pressed one more time at the boundary. expect(document.activeElement).toBe(options[6]); // 3,1 keyPress('ArrowLeft'); expect(document.activeElement).toBe(options[3]); // 2,1 keyPress('ArrowLeft'); // the end reached expect(document.activeElement).toBe(options[0]); // 1,1 keyPress('ArrowLeft'); // shouldn't throw when pressed one more time at the boundary. expect(document.activeElement).toBe(options[0]); // 1,1 }); it('should support keyboard navigation across grid layout via the test util', async () => { /** * The following ListBox is roughly in this shape: * * ## | 1,1 | 2,1 | 3,1 | * * ## | 1,2 | 2,2 | 3,2 | * * ## | 1,3 | 3,2 | 3,3 | */ let {getByRole} = render( 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 ); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 300, width: 300, height: 200}; } else { let index = [...this.parentElement.children].indexOf(this); return { top: (index % 3) * 40, left: Math.floor(index / 3) * 100, bottom: (index % 3) * 40 + 40, right: Math.floor(index / 3) * 100 + 100, width: 100, height: 40 }; } }); let listboxTester = testUtilUser.createTester('ListBox', { root: getByRole('listbox'), interactionType: 'keyboard', layout: 'grid' }); let options = listboxTester.getOptions(); await listboxTester.toggleOptionSelection({option: options[5]}); expect(options[5]).toHaveAttribute('aria-selected', 'true'); expect(document.activeElement).toBe(options[5]); await listboxTester.toggleOptionSelection({option: '1,1'}); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(document.activeElement).toBe(options[0]); await listboxTester.toggleOptionSelection({option: 8}); expect(options[8]).toHaveAttribute('aria-selected', 'true'); expect(document.activeElement).toBe(options[8]); }); it('should support onScroll', () => { let onScroll = jest.fn(); let {getByRole} = renderListbox({onScroll}); let listbox = getByRole('listbox'); fireEvent.scroll(listbox); expect(onScroll).toHaveBeenCalled(); }); it('should support virtualizer', async () => { let items = []; for (let i = 0; i < 50; i++) { items.push({id: i, name: 'Item ' + i}); } jest.restoreAllMocks(); // don't mock scrollTop for this test jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 100); jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 100); let {getByRole, getAllByRole} = render( {item => {item.name}} ); let options = getAllByRole('option'); expect(options).toHaveLength(7); expect(options.map(r => r.textContent)).toEqual([ 'Item 0', 'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6' ]); for (let option of options) { expect(option).toHaveAttribute('aria-setsize', '50'); expect(option).toHaveAttribute('aria-posinset'); } let listbox = getByRole('listbox'); listbox.scrollTop = 200; fireEvent.scroll(listbox); options = getAllByRole('option'); expect(options).toHaveLength(8); expect(options.map(r => r.textContent)).toEqual([ 'Item 7', 'Item 8', 'Item 9', 'Item 10', 'Item 11', 'Item 12', 'Item 13', 'Item 14' ]); await user.tab(); await user.keyboard('{End}'); options = getAllByRole('option'); expect(options).toHaveLength(9); expect(options.map(r => r.textContent)).toEqual([ 'Item 7', 'Item 8', 'Item 9', 'Item 10', 'Item 11', 'Item 12', 'Item 13', 'Item 14', 'Item 49' ]); }); it('should prevent Esc from clearing selection if escapeKeyBehavior is "none"', async () => { let {getByRole} = renderListbox({selectionMode: 'multiple', escapeKeyBehavior: 'none'}); let listboxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); let option = listboxTester.getOptions()[0]; expect(option).not.toHaveAttribute('aria-selected', 'true'); expect(option).not.toHaveClass('selected'); await listboxTester.toggleOptionSelection({option}); expect(option).toHaveAttribute('aria-selected', 'true'); await user.keyboard('{Escape}'); expect(option).toHaveAttribute('aria-selected', 'true'); }); describe('drag and drop', () => { it('should support draggable items', () => { let {getAllByRole} = render(); let options = getAllByRole('option'); expect(options[0]).toHaveAttribute('draggable'); }); it('should render drop indicators', () => { let onReorder = jest.fn(); let {getAllByRole} = render( Test} /> ); let option = getAllByRole('option')[0]; fireEvent.keyDown(option, {key: 'Enter'}); fireEvent.keyUp(option, {key: 'Enter'}); act(() => jest.runAllTimers()); let options = getAllByRole('option'); expect(options).toHaveLength(4); expect(options[0]).toHaveAttribute('class', 'react-aria-DropIndicator'); expect(options[0]).toHaveAttribute('data-drop-target', 'true'); expect(options[0]).toHaveAttribute('aria-label', 'Insert before Cat'); expect(options[0]).toHaveTextContent('Test'); expect(options[1]).toHaveAttribute('class', 'react-aria-DropIndicator'); expect(options[1]).not.toHaveAttribute('data-drop-target'); expect(options[1]).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); expect(options[2]).toHaveAttribute('class', 'react-aria-DropIndicator'); expect(options[2]).not.toHaveAttribute('data-drop-target'); expect(options[2]).toHaveAttribute('aria-label', 'Insert between Dog and Kangaroo'); expect(options[3]).toHaveAttribute('class', 'react-aria-DropIndicator'); expect(options[3]).not.toHaveAttribute('data-drop-target'); expect(options[3]).toHaveAttribute('aria-label', 'Insert after Kangaroo'); fireEvent.keyDown(document.activeElement, {key: 'ArrowDown'}); fireEvent.keyUp(document.activeElement, {key: 'ArrowDown'}); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); expect(options[0]).not.toHaveAttribute('data-drop-target', 'true'); expect(options[1]).toHaveAttribute('data-drop-target', 'true'); fireEvent.keyDown(document.activeElement, {key: 'Enter'}); fireEvent.keyUp(document.activeElement, {key: 'Enter'}); act(() => jest.runAllTimers()); expect(onReorder).toHaveBeenCalledTimes(1); }); it('should not skip drop positions before/after a disabled item', async () => { let onReorder = jest.fn(); let {getAllByRole} = render( ); let option = getAllByRole('option')[0]; fireEvent.keyDown(option, {key: 'Enter'}); fireEvent.keyUp(option, {key: 'Enter'}); act(() => jest.runAllTimers()); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert before Cat'); fireEvent.keyDown(document.activeElement, {key: 'ArrowDown'}); fireEvent.keyUp(document.activeElement, {key: 'ArrowDown'}); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); fireEvent.keyDown(document.activeElement, {key: 'ArrowDown'}); fireEvent.keyUp(document.activeElement, {key: 'ArrowDown'}); expect(document.activeElement).toHaveAttribute( 'aria-label', 'Insert between Dog and Kangaroo' ); fireEvent.keyDown(document.activeElement, {key: 'ArrowDown'}); fireEvent.keyUp(document.activeElement, {key: 'ArrowDown'}); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert after Kangaroo'); fireEvent.keyDown(document.activeElement, {key: 'Escape'}); fireEvent.keyUp(document.activeElement, {key: 'Escape'}); act(() => jest.runAllTimers()); }); it('should support dropping on options', () => { let onItemDrop = jest.fn(); let {getAllByRole} = render( <> ); let option = getAllByRole('option')[0]; fireEvent.keyDown(option, {key: 'Enter'}); fireEvent.keyUp(option, {key: 'Enter'}); act(() => jest.runAllTimers()); let listboxes = getAllByRole('listbox'); let options = within(listboxes[1]).getAllByRole('option'); expect(options).toHaveLength(3); expect(options[0]).toHaveAttribute('data-drop-target', 'true'); expect(options[1]).not.toHaveAttribute('data-drop-target'); expect(options[2]).not.toHaveAttribute('data-drop-target'); expect(document.activeElement).toBe(options[0]); fireEvent.keyDown(document.activeElement, {key: 'Enter'}); fireEvent.keyUp(document.activeElement, {key: 'Enter'}); act(() => jest.runAllTimers()); expect(onItemDrop).toHaveBeenCalledTimes(1); }); it('should support dropping on the root', () => { let onRootDrop = jest.fn(); let {getAllByRole} = render( <> ); let option = getAllByRole('option')[0]; fireEvent.keyDown(option, {key: 'Enter'}); fireEvent.keyUp(option, {key: 'Enter'}); act(() => jest.runAllTimers()); let listboxes = getAllByRole('listbox'); expect(document.activeElement).toBe(listboxes[1]); expect(listboxes[1]).toHaveAttribute('data-drop-target', 'true'); fireEvent.keyDown(document.activeElement, {key: 'Enter'}); fireEvent.keyUp(document.activeElement, {key: 'Enter'}); act(() => jest.runAllTimers()); expect(onRootDrop).toHaveBeenCalledTimes(1); }); it('should support dropping into an empty ListBox with a ListBoxLoadMoreItem', () => { let onRootDrop = jest.fn(); let onLoadMore = jest.fn(); let EmptyListBoxWithLoader = props => { let {dragAndDropHooks} = useDragAndDrop({ getItems: keys => [...keys].map(key => ({'text/plain': key})), ...props }); return ( {item => {item.name}} ); }; let {getAllByRole} = render( <> ); // Mock getBoundingClientRect for getDropTargetFromPoint jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 100, right: 100, width: 100, height: 100}; } // Item in first listbox if (this.getAttribute('data-key') === 'cat') { return {top: 0, left: 0, bottom: 30, right: 100, width: 100, height: 30}; } return {top: 0, left: 0, bottom: 0, right: 0, width: 0, height: 0}; }); let listboxes = getAllByRole('listbox'); let options = getAllByRole('option'); // Start dragging from first listbox let dataTransfer = new DataTransfer(); fireEvent(options[0], new DragEvent('dragstart', {dataTransfer, clientX: 5, clientY: 5})); act(() => jest.runAllTimers()); // Drag over the empty listbox (which only has a loader) fireEvent(listboxes[1], new DragEvent('dragenter', {dataTransfer, clientX: 50, clientY: 50})); fireEvent(listboxes[1], new DragEvent('dragover', {dataTransfer, clientX: 50, clientY: 50})); expect(listboxes[1]).toHaveAttribute('data-drop-target', 'true'); // Drop on the empty listbox fireEvent(listboxes[1], new DragEvent('drop', {dataTransfer, clientX: 50, clientY: 50})); act(() => jest.runAllTimers()); expect(onRootDrop).toHaveBeenCalledTimes(1); }); it('should support horizontal orientation', async () => { let onReorder = jest.fn(); let {getAllByRole} = render( ); let options = getAllByRole('option'); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('Enter'); act(() => jest.runAllTimers()); options = getAllByRole('option'); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); keyPress('ArrowRight'); expect(document.activeElement).toHaveAttribute( 'aria-label', 'Insert between Dog and Kangaroo' ); keyPress('Escape'); act(() => jest.runAllTimers()); }); it('should support grid layout', async () => { let onReorder = jest.fn(); let {getAllByRole} = render(); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 200, width: 200, height: 200}; } else { let index = [...this.parentElement.children] .filter(c => c.hasAttribute('data-key')) .indexOf(this); return { top: Math.floor(index / 2) * 40, left: index % 2 ? 100 : 0, bottom: Math.floor(index / 2) * 40 + 40, right: index % 2 ? 200 : 100, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('Enter'); act(() => jest.runAllTimers()); options = getAllByRole('option'); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); keyPress('ArrowDown'); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert after Kangaroo'); keyPress('Escape'); act(() => jest.runAllTimers()); }); it('should support horizontal grid layout', async () => { let onReorder = jest.fn(); let {getAllByRole} = render( ); let options = getAllByRole('option'); jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { if (this.getAttribute('role') === 'listbox') { return {top: 0, left: 0, bottom: 200, right: 200, width: 200, height: 200}; } else { let index = [...this.parentElement.children] .filter(c => c.hasAttribute('data-key')) .indexOf(this); return { top: (index % 2) * 40, left: index < 2 ? 0 : 100, bottom: (index % 2) * 40 + 40, right: index < 2 ? 100 : 200, width: 100, height: 40 }; } }); await user.tab(); expect(document.activeElement).toBe(options[0]); keyPress('Enter'); act(() => jest.runAllTimers()); options = getAllByRole('option'); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert between Cat and Dog'); keyPress('ArrowRight'); expect(document.activeElement).toHaveAttribute('aria-label', 'Insert after Kangaroo'); keyPress('Escape'); act(() => jest.runAllTimers()); }); it('should support onAction with drag and drop in virtualized list', async () => { let items = []; for (let i = 0; i < 20; i++) { items.push({id: i, name: 'Item ' + i}); } jest.restoreAllMocks(); jest.spyOn(window.HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(() => 100); jest.spyOn(window.HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(() => 100); let onAction = jest.fn(); let onReorder = jest.fn(); function VirtualizedDraggableListBox() { let {dragAndDropHooks} = useDragAndDrop({ getItems: keys => [...keys].map(key => ({'text/plain': key})), onReorder, renderDropIndicator: target => Drop }); return ( {item => {item.name}} ); } let {getAllByRole} = render(); let options = getAllByRole('option'); // Focus first item await user.tab(); expect(document.activeElement).toBe(options[0]); // Pressing Enter should trigger onAction, and not start drag keyPress('Enter'); act(() => jest.runAllTimers()); expect(onAction).toHaveBeenCalledTimes(1); expect(onAction).toHaveBeenCalledWith(0); expect(onReorder).not.toHaveBeenCalled(); // Should not be in drag mode options = getAllByRole('option'); expect( options.filter(opt => opt.classList.contains('react-aria-DropIndicator')) ).toHaveLength(0); // Now test that Alt+Enter starts drag mode expect(document.activeElement).toBe(options[0]); fireEvent.keyDown(document.activeElement, {key: 'Enter', altKey: true}); fireEvent.keyUp(document.activeElement, {key: 'Enter', altKey: true}); act(() => jest.runAllTimers()); // Verify we're in drag mode options = getAllByRole('option'); let dropIndicators = options.filter(opt => opt.classList.contains('react-aria-DropIndicator') ); expect(dropIndicators.length).toBeGreaterThan(0); expect(document.activeElement).toHaveAttribute('aria-label'); expect(document.activeElement.getAttribute('aria-label')).toContain('Insert'); // onAction should not have been called again expect(onAction).toHaveBeenCalledTimes(1); // Complete the drop keyPress('ArrowDown'); expect(document.activeElement.getAttribute('aria-label')).toContain('Insert'); keyPress('Enter'); act(() => jest.runAllTimers()); expect(onReorder).toHaveBeenCalledTimes(1); // Verify we're no longer in drag mode options = getAllByRole('option'); expect( options.filter(opt => opt.classList.contains('react-aria-DropIndicator')) ).toHaveLength(0); }); }); describe('inside modals', () => { it('should clear selection on first Escape, then allow the modal to close on the second Escape', async () => { let onOpenChange = jest.fn(); let {getAllByRole, getByRole} = render(
Sign up Cat Dog Kangaroo
); await user.tab(); expect(document.activeElement).toBe(getByRole('button')); await user.keyboard('{Enter}'); expect(onOpenChange).toHaveBeenCalledTimes(1); let options = getAllByRole('option'); await user.tab(); expect(document.activeElement).toBe(options[0]); await user.keyboard('{Enter}'); expect(options[0]).toHaveAttribute('aria-selected', 'true'); keyPress('Escape'); expect(options[0]).toBeInTheDocument(); expect(options[0]).toHaveAttribute('aria-selected', 'false'); keyPress('Escape'); expect(options[0]).not.toBeInTheDocument(); expect(onOpenChange).toHaveBeenCalledTimes(2); expect(onOpenChange).toHaveBeenCalledWith(false); }); }); describe('links', function () { describe.each(['mouse', 'keyboard'])('%s', type => { let trigger = async item => { if (type === 'mouse') { await user.click(item); } else { fireEvent.keyDown(item, {key: 'Enter'}); fireEvent.keyUp(item, {key: 'Enter'}); } }; it('should support links with selectionMode="none"', async function () { let {getAllByRole} = render( One Two ); let items = getAllByRole('option'); for (let item of items) { expect(item.tagName).toBe('A'); expect(item).toHaveAttribute('href'); } let onClick = mockClickDefault(); await trigger(items[0]); expect(onClick).toHaveBeenCalledTimes(1); expect(onClick.mock.calls[0][0].target).toBeInstanceOf(HTMLAnchorElement); expect(onClick.mock.calls[0][0].target.href).toBe('https://google.com/'); }); it.each(['single', 'multiple'])( 'should support links with selectionMode="%s"', async function (selectionMode) { let {getAllByRole} = render( One Two ); let items = getAllByRole('option'); for (let item of items) { expect(item.tagName).toBe('A'); expect(item).toHaveAttribute('href'); } let onClick = mockClickDefault(); await trigger(items[0]); expect(onClick).toHaveBeenCalledTimes(1); expect(onClick.mock.calls[0][0].target).toBeInstanceOf(HTMLAnchorElement); expect(onClick.mock.calls[0][0].target.href).toBe('https://google.com/'); expect(items[0]).not.toHaveAttribute('aria-selected', 'true'); await trigger(items[1]); expect(onClick).toHaveBeenCalledTimes(2); expect(onClick.mock.calls[1][0].target).toBeInstanceOf(HTMLAnchorElement); expect(onClick.mock.calls[1][0].target.href).toBe('https://adobe.com/'); expect(items[1]).not.toHaveAttribute('aria-selected', 'true'); } ); it.each(['single', 'multiple'])( 'should support links with selectionMode="%s" selectionBehavior="replace"', async function (selectionMode) { let {getAllByRole} = render( One Two ); let items = getAllByRole('option'); for (let item of items) { expect(item.tagName).toBe('A'); expect(item).toHaveAttribute('href'); } let onClick = mockClickDefault(); if (type === 'mouse') { await user.click(items[0]); } else { fireEvent.keyDown(items[0], {key: ' '}); fireEvent.keyUp(items[0], {key: ' '}); } expect(onClick).not.toHaveBeenCalled(); expect(items[0]).toHaveAttribute('aria-selected', 'true'); if (type === 'mouse') { await user.dblClick(items[0], {pointerType: 'mouse'}); } else { fireEvent.keyDown(items[0], {key: 'Enter'}); fireEvent.keyUp(items[0], {key: 'Enter'}); } expect(onClick).toHaveBeenCalledTimes(1); expect(onClick.mock.calls[0][0].target).toBeInstanceOf(HTMLAnchorElement); expect(onClick.mock.calls[0][0].target.href).toBe('https://google.com/'); } ); }); }); const FalsyExample = () => ( Item 0 Item 1 Item 2 ); describe('selection with falsy keys', () => { describe('keyboard', () => { it('should deselect item 0 when navigating back in replace selection mode', async () => { let {getAllByRole} = render(); let items = getAllByRole('option'); await user.click(items[1]); expect(items[1]).toHaveAttribute('aria-selected', 'true'); // Hold Shift and press ArrowUp to select item 0 await user.keyboard('{Shift>}{ArrowUp}{/Shift}'); expect(items[0]).toHaveAttribute('aria-selected', 'true'); expect(items[1]).toHaveAttribute('aria-selected', 'true'); expect(items[2]).toHaveAttribute('aria-selected', 'false'); // Hold Shift and press ArrowDown to navigate back to item 1 await user.keyboard('{Shift>}{ArrowDown}{/Shift}'); expect(items[0]).toHaveAttribute('aria-selected', 'false'); expect(items[1]).toHaveAttribute('aria-selected', 'true'); expect(items[2]).toHaveAttribute('aria-selected', 'false'); }); it('should correctly handle starting selection at item 0 and extending to item 2', async () => { let {getAllByRole} = render(); let items = getAllByRole('option'); await user.click(items[0]); expect(items[0]).toHaveAttribute('aria-selected', 'true'); // Hold Shift and press ArrowDown to select item 1 await user.keyboard('{Shift>}{ArrowDown}{/Shift}'); expect(items[0]).toHaveAttribute('aria-selected', 'true'); expect(items[1]).toHaveAttribute('aria-selected', 'true'); expect(items[2]).toHaveAttribute('aria-selected', 'false'); // Hold Shift and press ArrowDown to select item 2 await user.keyboard('{Shift>}{ArrowDown}{/Shift}'); expect(items[0]).toHaveAttribute('aria-selected', 'true'); expect(items[1]).toHaveAttribute('aria-selected', 'true'); expect(items[2]).toHaveAttribute('aria-selected', 'true'); }); }); describe('mouse', () => { it('should deselect item 0 when clicking another item in replace selection mode', async () => { let {getAllByRole} = render(); let items = getAllByRole('option'); await user.click(items[1]); expect(items[1]).toHaveAttribute('aria-selected', 'true'); await user.click(items[0]); expect(items[0]).toHaveAttribute('aria-selected', 'true'); expect(items[1]).toHaveAttribute('aria-selected', 'false'); await user.click(items[1]); expect(items[1]).toHaveAttribute('aria-selected', 'true'); expect(items[0]).toHaveAttribute('aria-selected', 'false'); }); it('should correctly handle mouse selection starting at item 0 and extending to item 2', async () => { let {getAllByRole} = render(); let items = getAllByRole('option'); await user.click(items[0]); expect(items[0]).toHaveAttribute('aria-selected', 'true'); await user.click(items[2]); expect(items[0]).toHaveAttribute('aria-selected', 'false'); expect(items[2]).toHaveAttribute('aria-selected', 'true'); }); }); }); describe('shouldSelectOnPressUp', () => { it('should select an item on pressing down when shouldSelectOnPressUp is not provided', async () => { let onSelectionChange = jest.fn(); let {getAllByRole} = renderListbox({selectionMode: 'single', onSelectionChange}); let items = getAllByRole('option'); await user.pointer({target: items[0], keys: '[MouseLeft>]'}); expect(onSelectionChange).toHaveBeenCalledTimes(1); await user.pointer({target: items[0], keys: '[/MouseLeft]'}); expect(onSelectionChange).toHaveBeenCalledTimes(1); }); it('should select an item on pressing down when shouldSelectOnPressUp is false', async () => { let onSelectionChange = jest.fn(); let {getAllByRole} = renderListbox({ selectionMode: 'single', onSelectionChange, shouldSelectOnPressUp: false }); let items = getAllByRole('option'); await user.pointer({target: items[0], keys: '[MouseLeft>]'}); expect(onSelectionChange).toHaveBeenCalledTimes(1); await user.pointer({target: items[0], keys: '[/MouseLeft]'}); expect(onSelectionChange).toHaveBeenCalledTimes(1); }); it('should select an item on pressing up when shouldSelectOnPressUp is true', async () => { let onSelectionChange = jest.fn(); let {getAllByRole} = renderListbox({ selectionMode: 'single', onSelectionChange, shouldSelectOnPressUp: true }); let items = getAllByRole('option'); await user.pointer({target: items[0], keys: '[MouseLeft>]'}); expect(onSelectionChange).toHaveBeenCalledTimes(0); await user.pointer({target: items[0], keys: '[/MouseLeft]'}); expect(onSelectionChange).toHaveBeenCalledTimes(1); }); }); describe('shouldFocusOnHover', () => { it('should focus options on hovering with shouldFocusOnHover', async () => { let {getAllByRole} = renderListbox({shouldFocusOnHover: true}); let options = getAllByRole('option'); let option1 = options[0]; let option2 = options[1]; expect(option1).not.toHaveFocus(); expect(option2).not.toHaveFocus(); await user.hover(option1); expect(option1).toHaveFocus(); keyPress('ArrowDown'); expect(option1).not.toHaveFocus(); expect(option2).toHaveFocus(); }); it.each([false, undefined])( 'should not focus options on hovering when shouldFocusOnHover is not true', async shouldFocusOnHover => { let {getAllByRole} = renderListbox({shouldFocusOnHover}); let option = getAllByRole('option')[0]; expect(option).not.toHaveFocus(); await user.hover(option); expect(option).not.toHaveFocus(); } ); }); describe('async loading', () => { let items = [{name: 'Foo'}, {name: 'Bar'}, {name: 'Baz'}]; let renderEmptyState = () => { return
empty state
; }; let AsyncListbox = props => { let {items, isLoading, onLoadMore, ...listBoxProps} = props; return ( renderEmptyState()}> {item => {item.name}} Loading... ); }; let onLoadMore = jest.fn(); let observe = jest.fn(); afterEach(() => { jest.runAllTimers(); jest.clearAllMocks(); }); it('should render the loading element when loading', async () => { let tree = render(); let listboxTester = testUtilUser.createTester('ListBox', {root: tree.getByRole('listbox')}); let options = listboxTester.getOptions(); expect(options).toHaveLength(4); let loaderRow = options[3]; expect(loaderRow).toHaveTextContent('Loading...'); let sentinel = tree.getByTestId('loadMoreSentinel'); expect(sentinel.parentElement).toHaveAttribute('inert'); }); it('should render the sentinel but not the loading indicator when not loading', async () => { let tree = render(); let listboxTester = testUtilUser.createTester('ListBox', {root: tree.getByRole('listbox')}); let options = listboxTester.getOptions(); expect(options).toHaveLength(3); expect(tree.queryByText('Loading...')).toBeFalsy(); expect(tree.getByTestId('loadMoreSentinel')).toBeInTheDocument(); }); it('should properly render the renderEmptyState if listbox is empty', async () => { let tree = render(); let listboxTester = testUtilUser.createTester('ListBox', {root: tree.getByRole('listbox')}); let options = listboxTester.getOptions(); expect(options).toHaveLength(1); expect(options[0]).toHaveTextContent('empty state'); expect(tree.queryByText('Loading...')).toBeFalsy(); expect(tree.getByTestId('loadMoreSentinel')).toBeInTheDocument(); // Even if the listbox is empty, providing isLoading will render the loader tree.rerender(); options = listboxTester.getOptions(); expect(options).toHaveLength(2); expect(options[1]).toHaveTextContent('empty state'); expect(tree.queryByText('Loading...')).toBeTruthy(); expect(tree.getByTestId('loadMoreSentinel')).toBeInTheDocument(); }); it('should only fire loadMore when intersection is detected regardless of loading state', async () => { let observer = setupIntersectionObserverMock({ observe }); let tree = render(); let sentinel = tree.getByTestId('loadMoreSentinel'); expect(observe).toHaveBeenLastCalledWith(sentinel); expect(onLoadMore).toHaveBeenCalledTimes(0); act(() => { observer.instance.triggerCallback([{isIntersecting: true}]); }); expect(onLoadMore).toHaveBeenCalledTimes(1); observe.mockClear(); tree.rerender(); expect(observe).toHaveBeenLastCalledWith(sentinel); expect(onLoadMore).toHaveBeenCalledTimes(1); act(() => { observer.instance.triggerCallback([{isIntersecting: true}]); }); expect(onLoadMore).toHaveBeenCalledTimes(2); }); describe('virtualized', () => { let items = []; for (let i = 0; i < 50; i++) { items.push({name: 'Foo' + i}); } let clientWidth, clientHeight; beforeAll(() => { clientWidth = jest .spyOn(window.HTMLElement.prototype, 'clientWidth', 'get') .mockImplementation(() => 100); clientHeight = jest .spyOn(window.HTMLElement.prototype, 'clientHeight', 'get') .mockImplementation(() => 100); }); afterEach(() => { act(() => { jest.runAllTimers(); }); }); afterAll(() => { clientWidth.mockReset(); clientHeight.mockReset(); }); function VirtualizedAsyncListbox(props) { let {items, isLoading, onLoadMore, ...listBoxProps} = props; return ( renderEmptyState()}> {item => {item.name}} Loading... ); } it('should always render the sentinel even when virtualized', () => { let tree = render(); let listboxTester = testUtilUser.createTester('ListBox', {root: tree.getByRole('listbox')}); let options = listboxTester.getOptions(); expect(options).toHaveLength(8); let loaderRow = options[7]; expect(loaderRow).toHaveTextContent('Loading...'); expect(loaderRow).not.toHaveAttribute('aria-posinset'); expect(loaderRow).not.toHaveAttribute('aria-setSize'); let loaderParentStyles = loaderRow.parentElement.style; // 50 items * 25px = 1250 expect(loaderParentStyles.top).toBe('1250px'); expect(loaderParentStyles.height).toBe('30px'); let sentinel = within(loaderRow.parentElement).getByTestId('loadMoreSentinel'); expect(sentinel.parentElement).toHaveAttribute('inert'); }); // TODO: for some reason this tree renders empty if ran with the above test... // Even if the above test doesn't do anything within it, the below tree won't render with content until the above test // is fully commented out (aka even the it(...)) // It thinks that the contextSize is 0 and never updates it.skip('should not reserve room for the loader if isLoading is false', () => { let tree = render(); let listboxTester = testUtilUser.createTester('ListBox', {root: tree.getByRole('listbox')}); let options = listboxTester.getOptions(); expect(options).toHaveLength(7); expect(within(listboxTester.getListbox()).queryByText('Loading...')).toBeFalsy(); let sentinel = within(listboxTester.getListbox()).getByTestId('loadMoreSentinel'); let sentinelParentStyles = sentinel.parentElement.parentElement.style; expect(sentinelParentStyles.top).toBe('1250px'); expect(sentinelParentStyles.height).toBe('0px'); expect(sentinel.parentElement).toHaveAttribute('inert'); tree.rerender(); options = listboxTester.getOptions(); expect(options).toHaveLength(1); let emptyStateRow = options[0]; expect(emptyStateRow).toHaveTextContent('empty state'); expect(within(listboxTester.getListbox()).queryByText('Loading...')).toBeFalsy(); sentinel = within(listboxTester.getListbox()).getByTestId('loadMoreSentinel'); sentinelParentStyles = sentinel.parentElement.parentElement.style; expect(sentinelParentStyles.top).toBe('0px'); expect(sentinelParentStyles.height).toBe('0px'); // Setting isLoading will render the loader even if the list is empty. tree.rerender(); options = listboxTester.getOptions(); expect(options).toHaveLength(2); emptyStateRow = options[1]; expect(emptyStateRow).toHaveTextContent('empty state'); let loadingRow = options[0]; expect(loadingRow).toHaveTextContent('Loading...'); sentinel = within(listboxTester.getListbox()).getByTestId('loadMoreSentinel'); sentinelParentStyles = sentinel.parentElement.parentElement.style; expect(sentinelParentStyles.top).toBe('0px'); expect(sentinelParentStyles.height).toBe('30px'); }); }); }); describe('press events', () => { it.each` interactionType ${'mouse'} ${'keyboard'} `( 'should support press events on items when using $interactionType', async function ({interactionType}) { let onAction = jest.fn(); let onPressStart = jest.fn(); let onPressEnd = jest.fn(); let onPress = jest.fn(); let onClick = jest.fn(); let {getByRole} = renderListbox({}, {onAction, onPressStart, onPressEnd, onPress, onClick}); let listBoxTester = testUtilUser.createTester('ListBox', {root: getByRole('listbox')}); await listBoxTester.triggerOptionAction({option: 1, interactionType}); expect(onAction).toHaveBeenCalledTimes(1); expect(onPressStart).toHaveBeenCalledTimes(1); expect(onPressEnd).toHaveBeenCalledTimes(1); expect(onPress).toHaveBeenCalledTimes(1); expect(onClick).toHaveBeenCalledTimes(1); } ); }); describe('onKeyDown', () => { it('should call key handler when key is pressed on item', async () => { let onKeyDown = jest.fn(e => e.continuePropagation()); let onKeyUp = jest.fn(); let onSelectionChange = jest.fn(); renderListbox({selectionMode: 'multiple', onSelectionChange}, {onKeyDown, onKeyUp}); await user.tab(); expect(onKeyUp).toHaveBeenCalledTimes(1); onKeyUp.mockClear(); await user.keyboard('{Enter}'); expect(onKeyDown).toHaveBeenCalledTimes(1); expect(onKeyUp).toHaveBeenCalledTimes(1); expect(onSelectionChange).toHaveBeenCalledTimes(1); await user.keyboard('{Escape}'); expect(onKeyDown).toHaveBeenCalledTimes(2); expect(onKeyUp).toHaveBeenCalledTimes(2); expect(onSelectionChange).toHaveBeenCalledTimes(2); }); }); if (React.version.startsWith('19')) { it('supports Activity', async () => { function ActivityListBox() { let [mode, setMode] = React.useState('hidden'); return ( <>

List should be visible

Item 1 Item 2 Item 3 Item 4 Item 5
); } let {getAllByRole, getByRole, queryAllByRole} = render(); let button = getByRole('button'); expect(queryAllByRole('option')).toHaveLength(0); await user.click(button); expect(getAllByRole('option')).toHaveLength(5); await user.click(button); expect(queryAllByRole('option')).toHaveLength(0); }); } if (React.version.startsWith('19')) { it('supports Activity into a portal', async () => { let ReactCanaryContext = React.createContext(null); let RouterContext = React.createContext(null); function Page() { let {mode} = React.useContext(ReactCanaryContext); let {pathname} = React.useContext(RouterContext); return ( <>
  1. {pathname}
{mode === 'visible' && Item 1} {mode === 'visible' && Item 2} {mode === 'visible' && Item 3} {mode === 'visible' && Item 4} {mode === 'visible' && Item 5} ); } function App({mode, pathname = '/', ready = false}) { let react = React.useMemo(() => ({mode}), [mode]); let router = React.useMemo(() => ({pathname, ready}), [pathname, ready]); let route = React.useMemo(() => , []); return ( {route} ); } let {rerender, queryAllByRole} = render(); expect(queryAllByRole('option')).toHaveLength(5); rerender(); rerender(); rerender(); expect(queryAllByRole('option')).toHaveLength(5); }); it('should only treat visible items as duplicate keys across Activity boundaries', () => { function App({first}) { return ( First A Second A B ); } let {getAllByRole, rerender, unmount} = render(); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['First A', 'B']); rerender(); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['Second A', 'B']); rerender(); expect(getAllByRole('option').map(o => o.textContent)).toEqual(['First A', 'B']); unmount(); function Reveal({mode}) { return ( Hidden A Visible A ); } let revealed = render(); expect(revealed.getAllByRole('option').map(o => o.textContent)).toEqual(['Visible A']); expect(() => revealed.rerender()).toThrow( 'Duplicate key "a" found in collection.' ); }); } }); describe('keyboard modifier keys', () => { let user; let platformMock; beforeAll(() => { user = userEvent.setup({delay: null, pointerMap}); }); // selectionMode: 'none', 'single', 'multiple' // selectionBehavior: 'toggle', 'replace' // platform: 'mac', 'windows' // modifier key: 'alt', 'ctrl', 'meta', 'shift' // key: 'arrow-up', 'arrow-down', 'arrow-left', 'arrow-right', 'home', 'end', 'page-up', 'page-down', 'enter', 'space', 'tab' // expected behavior: 'navigate', 'select', 'toggle', 'replace' describe('mac', () => { beforeAll(() => { platformMock = jest.spyOn(navigator, 'platform', 'get').mockImplementation(() => 'Mac'); }); afterAll(() => { platformMock.mockRestore(); }); it('should not navigate when using unsupported modifier keys', async () => { let {getByRole} = renderListbox({selectionMode: 'none'}); await user.tab(); let listbox = getByRole('listbox'); let options = within(listbox).getAllByRole('option'); await user.keyboard('{ArrowDown}'); expect(document.activeElement).toBe(options[1]); await user.keyboard('{Meta>}{ArrowDown}{/Meta}'); expect(document.activeElement).toBe(options[1]); await user.keyboard('{Meta>}{ArrowUp}{/Meta}'); expect(document.activeElement).toBe(options[1]); await user.keyboard('{Control>}{Home}{/Control}'); expect(document.activeElement).toBe(options[1]); }); it('does not activate an element focused during selection with Enter', async () => { let onClick = jest.fn(); let triggerRef = React.createRef(); let {getByRole} = render( <> { // oxlint-disable-next-line rsp-rules/act-events-test triggerRef.current.focus(); }}> Cat Dog ); let options = within(getByRole('listbox')).getAllByRole('option'); act(() => options[0].focus()); // Selection moves focus to the trigger during keydown. Unless Enter's default is // prevented, the browser dispatches the activation click to the newly focused // element. await user.keyboard('{Enter}'); expect(triggerRef.current).toHaveFocus(); expect(onClick).not.toHaveBeenCalled(); }); describe('multiple selection, replace behavior', () => { it('replaces selection as focus moves with arrow keys', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); await user.keyboard('{ArrowDown}'); expect(document.activeElement).toBe(options[1]); expect(onSelectionChange).toHaveBeenLastCalledWith(new Selection(['dog'], 'dog', 'dog')); }); it('navigates focus without changing selection when Alt is held (non-contiguous)', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); onSelectionChange.mockClear(); await user.keyboard('{Alt>}{ArrowDown}{/Alt}'); expect(document.activeElement).toBe(options[1]); expect(onSelectionChange).not.toHaveBeenCalled(); }); it('extends selection when Shift is held', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); await user.keyboard('{Shift>}{ArrowDown}{/Shift}'); expect(document.activeElement).toBe(options[1]); expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['cat', 'dog'], 'cat', 'dog') ); }); it('replaces selection with Home and End', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', selectionBehavior: 'replace', onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); await user.keyboard('{End}'); expect(document.activeElement).toBe(options[2]); expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['kangaroo'], 'kangaroo', 'kangaroo') ); await user.keyboard('{Home}'); expect(document.activeElement).toBe(options[0]); expect(onSelectionChange).toHaveBeenLastCalledWith(new Selection(['cat'], 'cat', 'cat')); }); }); it('selects all with Mod+A (Meta) in multiple selection mode', async () => { let onSelectionChange = jest.fn(); renderListbox({selectionMode: 'multiple', onSelectionChange}); await user.tab(); await user.keyboard('{Meta>}a{/Meta}'); expect(onSelectionChange).toHaveBeenLastCalledWith('all'); }); it('extends selection with Meta+Shift+End and Meta+Shift+Home', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', defaultSelectedKeys: ['cat'], onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); await user.keyboard('{Meta>}{Shift>}{End}{/Shift}{/Meta}'); expect(document.activeElement).toBe(options[2]); expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['cat', 'kangaroo'], 'kangaroo', 'kangaroo') ); await user.keyboard('{Meta>}{Shift>}{Home}{/Shift}{/Meta}'); expect(document.activeElement).toBe(options[0]); expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['cat', 'dog', 'kangaroo'], 'kangaroo', 'cat') ); }); it('does not select all with Mod+A when selection mode is single', async () => { let onSelectionChange = jest.fn(); renderListbox({selectionMode: 'single', onSelectionChange}); await user.tab(); await user.keyboard('{Meta>}a{/Meta}'); expect(onSelectionChange).not.toHaveBeenCalled(); }); }); describe('windows', () => { beforeAll(() => { platformMock = jest.spyOn(navigator, 'platform', 'get').mockImplementation(() => 'Win32'); }); afterAll(() => { platformMock.mockRestore(); }); it('should not navigate when using unsupported modifier keys', async () => { let {getByRole} = renderListbox({selectionMode: 'none'}); await user.tab(); let options = within(getByRole('listbox')).getAllByRole('option'); await user.keyboard('{ArrowDown}'); expect(document.activeElement).toBe(options[1]); await user.keyboard('{Meta>}{ArrowDown}{/Meta}'); expect(document.activeElement).toBe(options[1]); }); it('navigates focus when Control is held (non-contiguous)', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({selectionMode: 'multiple', onSelectionChange}); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); onSelectionChange.mockClear(); await user.keyboard('{Control>}{ArrowDown}{/Control}'); expect(document.activeElement).toBe(options[1]); expect(onSelectionChange).not.toHaveBeenCalled(); }); it('extends selection with Control+Shift+End and Control+Shift+Home', async () => { let onSelectionChange = jest.fn(); let {getByRole} = renderListbox({ selectionMode: 'multiple', defaultSelectedKeys: ['cat'], onSelectionChange }); let options = within(getByRole('listbox')).getAllByRole('option'); await user.tab(); await user.keyboard('{Control>}{Shift>}{End}{/Shift}{/Control}'); expect(document.activeElement).toBe(options[2]); // extendSelection adds to the selection rather than replacing it (cat stays selected). expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['cat', 'kangaroo'], 'kangaroo', 'kangaroo') ); await user.keyboard('{Control>}{Shift>}{Home}{/Shift}{/Control}'); expect(document.activeElement).toBe(options[0]); expect(onSelectionChange).toHaveBeenLastCalledWith( new Selection(['cat', 'dog', 'kangaroo'], 'kangaroo', 'cat') ); }); it('selects all with Mod+A (Control) in multiple selection mode', async () => { let onSelectionChange = jest.fn(); renderListbox({selectionMode: 'multiple', onSelectionChange}); await user.tab(); await user.keyboard('{Control>}a{/Control}'); expect(onSelectionChange).toHaveBeenLastCalledWith('all'); }); }); describe('navigation keys prevent default without selecting', () => { it.each(['Home', 'End', 'PageUp', 'PageDown'])('%s prevents the default', async key => { renderListbox({selectionMode: 'multiple'}); await user.tab(); let event = new KeyboardEvent('keydown', {key, bubbles: true, cancelable: true}); act(() => { document.activeElement.dispatchEvent(event); }); expect(event.defaultPrevented).toBe(true); }); }); });