/* * Copyright 2023 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, pointerMap, render, waitFor} from '@react-spectrum/test-utils-internal'; import {createPortal} from 'react-dom'; import {FocusScope, useFocusManager} from '../../src/focus/FocusScope'; import React from 'react'; import userEvent from '@testing-library/user-event'; describe('FocusScope', function () { let iframe; let iframeRoot; let user; const IframeExample = ({children}) => { return createPortal(<>{children}, iframeRoot); }; beforeAll(() => { jest.useFakeTimers(); }); beforeEach(async () => { // Iframe setup iframe = document.createElement('iframe'); window.document.body.appendChild(iframe); const iframeDocument = iframe.contentWindow.document; iframeRoot = iframeDocument.createElement('div'); iframeDocument.body.appendChild(iframeRoot); }); afterEach(() => { // make sure to clean up any raf's that may be running to restore focus on unmount act(() => { jest.runAllTimers(); }); // Iframe teardown iframe.remove(); }); describe('focus containment', function () { it('should contain focus within the scope', async function () { render( ); await waitFor(() => { expect( document .querySelector('iframe') .contentWindow.document.body.querySelector('input[data-testid="input1"]') ).toBeTruthy(); }); const iframeDocument = iframe.contentWindow.document; user = userEvent.setup({delay: null, pointerMap, document: iframeDocument}); const input1 = iframeDocument.querySelector('input[data-testid="input1"]'); const input2 = iframeDocument.querySelector('input[data-testid="input2"]'); const input3 = iframeDocument.querySelector('input[data-testid="input3"]'); act(() => { input1.focus(); }); expect(iframeDocument.activeElement).toBe(input1); await user.tab(); expect(iframeDocument.activeElement).toBe(input2); await user.tab(); expect(iframeDocument.activeElement).toBe(input3); await user.tab(); expect(iframeDocument.activeElement).toBe(input1); await user.tab({shift: true}); expect(iframeDocument.activeElement).toBe(input3); await user.tab({shift: true}); expect(iframeDocument.activeElement).toBe(input2); await user.tab({shift: true}); expect(iframeDocument.activeElement).toBe(input1); }); // This test setup is a bit contrived to just purely simulate the blur/focus events that would happen in a case like this it('focus properly moves into child iframe on click', async function () { render( ); await waitFor(() => { expect( document .querySelector('iframe') .contentWindow.document.body.querySelector('input[data-testid="input1"]') ).toBeTruthy(); }); const iframeDocument = iframe.contentWindow.document; const input1 = iframeDocument.querySelector('input[data-testid="input1"]'); const input2 = iframeDocument.querySelector('input[data-testid="input2"]'); act(() => { input1.focus(); }); fireEvent.focusIn(input1); // jsdom doesn't fire this automatically expect(iframeDocument.activeElement).toBe(input1); act(() => { // set document.activeElement to input2 input2.focus(); // if onBlur didn't fallback to checking document.activeElement, this would reset focus to input1 fireEvent.blur(input1, {relatedTarget: null}); }); expect(iframeDocument.activeElement).toBe(input2); }); }); describe('focus restoration', function () { it('should restore focus to the previously focused node on unmount', async function () { function Test({show}) { return (
{show && ( )}
); } let {getByTestId, rerender} = render(); let outside = getByTestId('outside'); act(() => { outside.focus(); }); rerender(); await waitFor(() => { expect( document .querySelector('iframe') .contentWindow.document.body.querySelector('input[data-testid="input1"]') ).toBeTruthy(); }); const iframeDocument = iframe.contentWindow.document; const input1 = iframeDocument.querySelector('input[data-testid="input1"]'); expect(iframeDocument.activeElement).toBe(input1); rerender(); iframe.remove(); act(() => { jest.runAllTimers(); }); expect(document.activeElement).toBe(outside); }); describe('focusable first in scope', function () { it('should restore focus to the first focusable or tabbable element within the scope when focus is lost within the scope', async function () { render(
Remove me! Remove me, too! Remove me, three!
); function Item(props) { let focusManager = useFocusManager(); let onClick = e => { focusManager.focusNext(); act(() => { // remove fails to fire blur event in jest-dom e.target.blur(); e.target.remove(); jest.runAllTimers(); }); }; return