|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React, { useRef } from 'react'; |
| 5 | +import { render, act } from '@testing-library/react'; |
| 6 | + |
| 7 | +import { SingleTabStopNavigationContext, useSingleTabStopNavigation } from '../'; |
| 8 | +import { renderWithSingleTabStopNavigation } from './utils'; |
| 9 | + |
| 10 | +function Button(props: React.HTMLAttributes<HTMLButtonElement>) { |
| 11 | + const buttonRef = useRef<HTMLButtonElement>(null); |
| 12 | + const { tabIndex } = useSingleTabStopNavigation(buttonRef, { tabIndex: props.tabIndex }); |
| 13 | + return <button {...props} ref={buttonRef} tabIndex={tabIndex} />; |
| 14 | +} |
| 15 | + |
| 16 | +test('subscribed components can be rendered outside single tab stop navigation context', () => { |
| 17 | + render(<Button />); |
| 18 | + expect(document.querySelector('button')).not.toHaveAttribute('tabIndex'); |
| 19 | +}); |
| 20 | + |
| 21 | +test('does not override tab index when keyboard navigation is not active', () => { |
| 22 | + renderWithSingleTabStopNavigation(<Button id="button" />, { navigationActive: false }); |
| 23 | + expect(document.querySelector('#button')).not.toHaveAttribute('tabIndex'); |
| 24 | +}); |
| 25 | + |
| 26 | +test('does not override tab index for suppressed elements', () => { |
| 27 | + const { setCurrentTarget } = renderWithSingleTabStopNavigation( |
| 28 | + <div> |
| 29 | + <Button id="button1" /> |
| 30 | + <Button id="button2" /> |
| 31 | + <Button id="button3" tabIndex={-1} /> |
| 32 | + <Button id="button4" /> |
| 33 | + <Button id="button5" tabIndex={-1} /> |
| 34 | + </div>, |
| 35 | + { navigationActive: true } |
| 36 | + ); |
| 37 | + act(() => { |
| 38 | + setCurrentTarget(document.querySelector('#button1'), [ |
| 39 | + document.querySelector('#button1'), |
| 40 | + document.querySelector('#button2'), |
| 41 | + document.querySelector('#button3'), |
| 42 | + ]); |
| 43 | + }); |
| 44 | + |
| 45 | + expect(document.querySelector('#button1')).toHaveAttribute('tabIndex', '0'); |
| 46 | + expect(document.querySelector('#button2')).toHaveAttribute('tabIndex', '0'); |
| 47 | + expect(document.querySelector('#button3')).toHaveAttribute('tabIndex', '-1'); |
| 48 | + expect(document.querySelector('#button4')).toHaveAttribute('tabIndex', '-1'); |
| 49 | + expect(document.querySelector('#button5')).toHaveAttribute('tabIndex', '-1'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('overrides tab index when keyboard navigation is active', () => { |
| 53 | + const { setCurrentTarget } = renderWithSingleTabStopNavigation( |
| 54 | + <div> |
| 55 | + <Button id="button1" /> |
| 56 | + <Button id="button2" /> |
| 57 | + </div> |
| 58 | + ); |
| 59 | + act(() => { |
| 60 | + setCurrentTarget(document.querySelector('#button1')); |
| 61 | + }); |
| 62 | + expect(document.querySelector('#button1')).toHaveAttribute('tabIndex', '0'); |
| 63 | + expect(document.querySelector('#button2')).toHaveAttribute('tabIndex', '-1'); |
| 64 | +}); |
| 65 | + |
| 66 | +test('does not override explicit tab index with 0', () => { |
| 67 | + const { setCurrentTarget } = renderWithSingleTabStopNavigation( |
| 68 | + <div> |
| 69 | + <Button id="button1" tabIndex={-2} /> |
| 70 | + <Button id="button2" tabIndex={-2} /> |
| 71 | + </div> |
| 72 | + ); |
| 73 | + act(() => { |
| 74 | + setCurrentTarget(document.querySelector('#button1')); |
| 75 | + }); |
| 76 | + expect(document.querySelector('#button1')).toHaveAttribute('tabIndex', '-2'); |
| 77 | + expect(document.querySelector('#button2')).toHaveAttribute('tabIndex', '-2'); |
| 78 | +}); |
| 79 | + |
| 80 | +test('propagates and suppresses navigation active state', () => { |
| 81 | + function Component() { |
| 82 | + const { navigationActive } = useSingleTabStopNavigation(null); |
| 83 | + return <div>{String(navigationActive)}</div>; |
| 84 | + } |
| 85 | + function Test({ navigationActive }: { navigationActive: boolean }) { |
| 86 | + return ( |
| 87 | + <SingleTabStopNavigationContext.Provider value={{ navigationActive, registerFocusable: () => () => {} }}> |
| 88 | + <Component /> |
| 89 | + </SingleTabStopNavigationContext.Provider> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + const { rerender } = render(<Test navigationActive={true} />); |
| 94 | + expect(document.querySelector('div')).toHaveTextContent('true'); |
| 95 | + |
| 96 | + rerender(<Test navigationActive={false} />); |
| 97 | + expect(document.querySelector('div')).toHaveTextContent('false'); |
| 98 | +}); |
0 commit comments