|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import React, { useState, useRef } from 'react'; |
| 5 | +import ReactDOM from 'react-dom'; |
| 6 | +import { render, fireEvent } from '@testing-library/react'; |
| 7 | +import { useFocusVisible } from '..'; |
| 8 | + |
| 9 | +function Fixture() { |
| 10 | + const ref = useRef<HTMLButtonElement | null>(null); |
| 11 | + useFocusVisible(ref); |
| 12 | + return <button ref={ref}>Test</button>; |
| 13 | +} |
| 14 | + |
| 15 | +function FramePortal({ children }: { children: React.ReactNode }) { |
| 16 | + const [iframeElement, setIframeElement] = useState<HTMLIFrameElement | null>(null); |
| 17 | + |
| 18 | + return ( |
| 19 | + <iframe ref={setIframeElement}> |
| 20 | + {iframeElement?.contentDocument && ReactDOM.createPortal(children, iframeElement.contentDocument.body)} |
| 21 | + </iframe> |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +test('should disable focus by default', () => { |
| 26 | + render( |
| 27 | + <FramePortal> |
| 28 | + <Fixture /> |
| 29 | + </FramePortal> |
| 30 | + ); |
| 31 | + const frame = window.frames[0]!; |
| 32 | + expect(frame.document.body).not.toHaveAttribute('data-awsui-focus-visible'); |
| 33 | +}); |
| 34 | + |
| 35 | +test('should enable focus when keyboard interaction happened in the main document', () => { |
| 36 | + render( |
| 37 | + <> |
| 38 | + {/* Component in the main document to make sure listeners are added here too */} |
| 39 | + <Fixture /> |
| 40 | + <FramePortal> |
| 41 | + <Fixture /> |
| 42 | + </FramePortal> |
| 43 | + </> |
| 44 | + ); |
| 45 | + const frame = window.frames[0]!; |
| 46 | + fireEvent.keyDown(document.body); |
| 47 | + expect(document.body).toHaveAttribute('data-awsui-focus-visible', 'true'); |
| 48 | + expect(frame.document.body).toHaveAttribute('data-awsui-focus-visible', 'true'); |
| 49 | +}); |
| 50 | + |
| 51 | +test('should enable focus when keyboard interaction happened in the frame document', () => { |
| 52 | + render( |
| 53 | + <> |
| 54 | + {/* Component in the main document to make sure listeners are added here too */} |
| 55 | + <Fixture /> |
| 56 | + <FramePortal> |
| 57 | + <Fixture /> |
| 58 | + </FramePortal> |
| 59 | + </> |
| 60 | + ); |
| 61 | + const frame = window.frames[0]!; |
| 62 | + fireEvent.keyDown(frame.document.body); |
| 63 | + expect(document.body).toHaveAttribute('data-awsui-focus-visible', 'true'); |
| 64 | + expect(frame.document.body).toHaveAttribute('data-awsui-focus-visible', 'true'); |
| 65 | +}); |
| 66 | + |
| 67 | +test('should disable focus when mouse is used in the main document after keyboard', () => { |
| 68 | + render( |
| 69 | + <> |
| 70 | + {/* Component in the main document to make sure listeners are added here too */} |
| 71 | + <Fixture /> |
| 72 | + <FramePortal> |
| 73 | + <Fixture /> |
| 74 | + </FramePortal> |
| 75 | + </> |
| 76 | + ); |
| 77 | + const frame = window.frames[0]!; |
| 78 | + fireEvent.keyDown(document.body); |
| 79 | + fireEvent.mouseDown(document.body); |
| 80 | + expect(document.body).not.toHaveAttribute('data-awsui-focus-visible'); |
| 81 | + expect(frame.document.body).not.toHaveAttribute('data-awsui-focus-visible'); |
| 82 | +}); |
| 83 | + |
| 84 | +test('should disable focus when mouse is used in the frame document after keyboard', () => { |
| 85 | + render( |
| 86 | + <FramePortal> |
| 87 | + <Fixture /> |
| 88 | + </FramePortal> |
| 89 | + ); |
| 90 | + const frame = window.frames[0]!; |
| 91 | + fireEvent.keyDown(frame.document.body); |
| 92 | + fireEvent.mouseDown(frame.document.body); |
| 93 | + expect(frame.document.body).not.toHaveAttribute('data-awsui-focus-visible'); |
| 94 | +}); |
| 95 | + |
| 96 | +test('should remove listeners only from frame when the frame is unmounted', () => { |
| 97 | + const outerAddEventListenerSpy = jest.spyOn(document, 'addEventListener'); |
| 98 | + const { rerender } = render( |
| 99 | + <> |
| 100 | + <Fixture /> |
| 101 | + <FramePortal> |
| 102 | + <span /> |
| 103 | + </FramePortal> |
| 104 | + </> |
| 105 | + ); |
| 106 | + |
| 107 | + const frame = window.frames[0]!; |
| 108 | + const innerAddEventListenerSpy = jest.spyOn(frame.document, 'addEventListener'); |
| 109 | + rerender( |
| 110 | + <> |
| 111 | + <Fixture /> |
| 112 | + <FramePortal> |
| 113 | + <Fixture /> |
| 114 | + </FramePortal> |
| 115 | + </> |
| 116 | + ); |
| 117 | + |
| 118 | + expect(outerAddEventListenerSpy).toHaveBeenCalledTimes(2); |
| 119 | + const outerSignal = (outerAddEventListenerSpy.mock.calls[0][2] as AddEventListenerOptions).signal!; |
| 120 | + expect(outerSignal.aborted).toBe(false); |
| 121 | + |
| 122 | + expect(innerAddEventListenerSpy).toHaveBeenCalledTimes(2); |
| 123 | + const innerSignal = (innerAddEventListenerSpy.mock.calls[0][2] as AddEventListenerOptions).signal!; |
| 124 | + expect(innerSignal.aborted).toBe(false); |
| 125 | + |
| 126 | + rerender( |
| 127 | + <> |
| 128 | + <Fixture /> |
| 129 | + <FramePortal> |
| 130 | + <span /> |
| 131 | + </FramePortal> |
| 132 | + </> |
| 133 | + ); |
| 134 | + expect(innerSignal.aborted).toBe(true); |
| 135 | + expect(outerSignal.aborted).toBe(false); |
| 136 | +}); |
0 commit comments