/* * Copyright 2020 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 { fireEvent, installPointerEvent, pointerMap, render, simulateDesktop, simulateMobile, within } from '@react-spectrum/test-utils-internal'; import {Item} from 'react-stately/Item'; import {List} from '../../stories/selection/List'; import React from 'react'; import userEvent from '@testing-library/user-event'; describe('useSelectableCollection', () => { let user; beforeAll(() => { user = userEvent.setup({delay: null, pointerMap}); }); beforeEach(() => { simulateDesktop(750); }); it('selects the first item it focuses if selectOnFocus', async () => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); expect(options[0]).not.toHaveAttribute('aria-selected'); await user.tab(); expect(document.activeElement).toBe(options[0]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); }); it.each` autoFocus | index ${'first'} | ${0} ${'last'} | ${2} `( 'selects the autofocused item if selectOnFocus with autoFocus=$autoFocus', ({autoFocus, index}) => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); expect(document.activeElement).toBe(options[index]); expect(options[index]).toHaveAttribute('aria-selected', 'true'); } ); it('does not change the selection when autofocusing an already selected collection', () => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); expect(options[2]).toHaveAttribute('aria-selected', 'true'); expect(options[0]).not.toHaveAttribute('aria-selected'); }); it('can navigate without replacing the selection in multiple selection selectOnFocus', async () => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); expect(options[0]).not.toHaveAttribute('aria-selected'); await user.tab(); expect(document.activeElement).toBe(options[0]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).not.toHaveAttribute('aria-selected'); await user.keyboard('{Control>}{ArrowDown}{/Control}'); expect(document.activeElement).toBe(options[1]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).not.toHaveAttribute('aria-selected'); await user.keyboard('{Control>}{ArrowDown}{/Control}'); expect(document.activeElement).toBe(options[2]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).not.toHaveAttribute('aria-selected'); await user.keyboard('[Space]'); expect(options[0]).not.toHaveAttribute('aria-selected'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).toHaveAttribute('aria-selected', 'true'); }); it('wraps focus with ArrowUp and ArrowDown when shouldFocusWrap is set', async () => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); await user.tab(); expect(document.activeElement).toBe(options[0]); // ArrowUp from the first item wraps to the last. await user.keyboard('{ArrowUp}'); expect(document.activeElement).toBe(options[2]); // ArrowDown from the last item wraps back to the first. await user.keyboard('{ArrowDown}'); expect(document.activeElement).toBe(options[0]); }); it('navigates by page with PageDown and PageUp', async () => { let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); await user.tab(); expect(document.activeElement).toBe(options[0]); await user.keyboard('{PageDown}'); expect(document.activeElement).toBe(options[2]); await user.keyboard('{PageUp}'); expect(document.activeElement).toBe(options[0]); }); describe.each` type | prepare | actions ${'VO Events'} | ${installPointerEvent} | ${[el => fireEvent.pointerDown(el, {button: 0, pointerType: 'virtual'}), el => { fireEvent.pointerUp(el, {button: 0, pointerType: 'virtual'}); fireEvent.click(el, {detail: 1}); }]} ${'Touch Pointer Events'} | ${installPointerEvent} | ${[el => fireEvent.pointerDown(el, {button: 0, pointerType: 'touch', pointerId: 1}), el => { fireEvent.pointerUp(el, {button: 0, pointerType: 'touch', pointerId: 1}); fireEvent.click(el, {detail: 1}); }]} `('always uses toggle for $type', ({prepare, actions: [start, end]}) => { prepare(); it('uses toggle mode when the interaction is touch', () => { simulateMobile(700); let {getAllByRole} = render( Paco de Lucia Vicente Amigo Gerardo Nunez ); let options = getAllByRole('option'); expect(options[0]).not.toHaveAttribute('aria-selected'); start(options[0]); end(options[0]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).not.toHaveAttribute('aria-selected'); start(options[2]); end(options[2]); expect(options[0]).toHaveAttribute('aria-selected', 'true'); expect(options[1]).not.toHaveAttribute('aria-selected'); expect(options[2]).toHaveAttribute('aria-selected', 'true'); }); it('focuses first/last selected item on focus enter and does not change the selection', async () => { let onSelectionChange1 = jest.fn(); let onSelectionChange2 = jest.fn(); let {getByText, getAllByRole} = render( <> Paco de Lucia Vicente Amigo Gerardo Nunez Paco de Lucia Vicente Amigo Gerardo Nunez ); let [firstList, secondList] = getAllByRole('listbox'); await user.click(getByText('focus stop')); await user.tab(); expect(document.activeElement).toBe( within(secondList).getByRole('option', {name: 'Vicente Amigo'}) ); await user.click(getByText('focus stop')); await user.tab({shift: true}); expect(document.activeElement).toBe( within(firstList).getByRole('option', {name: 'Gerardo Nunez'}) ); expect(onSelectionChange1).not.toHaveBeenCalled(); expect(onSelectionChange2).not.toHaveBeenCalled(); }); }); });