/* * 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, pointerMap, render, within} from '@react-spectrum/test-utils-internal'; import {Button} from '../src/Button'; import {composeStories} from '@storybook/react'; import {Dialog, DialogTrigger} from '../src/Dialog'; import {Heading} from '../src/Heading'; import {Input} from '../src/Input'; import {Label} from '../src/Label'; import {Menu, MenuItem, MenuTrigger} from '../src/Menu'; import {Modal, ModalOverlay} from '../src/Modal'; import {OverlayArrow} from '../src/OverlayArrow'; import {Popover} from '../src/Popover'; import React, {useRef} from 'react'; import * as stories from '../stories/Modal.stories'; import {Text} from '../src/Text'; import {TextField} from '../src/TextField'; import {UNSAFE_PortalProvider} from 'react-aria/PortalProvider'; import {User} from '@react-aria/test-utils'; import userEvent from '@testing-library/user-event'; let {DateRangePickerInsideModalStory: DateRangePickerInsideModal} = composeStories(stories); describe('Dialog', () => { let user; let testUtilUser = new User({advanceTimer: jest.advanceTimersByTime}); beforeAll(() => { user = userEvent.setup({delay: null, pointerMap}); jest.useFakeTimers(); }); it('should have a base default set of attributes', () => { let {getByRole} = render( Title ); let dialog = getByRole('dialog'); expect(dialog).toHaveClass('react-aria-Dialog'); expect(dialog).toHaveAttribute('data-rac'); }); it('works with modal', async () => { let {getByRole} = render( Delete… {({close}) => ( <> Alert This is the alert message. Close > )} ); let button = getByRole('button'); let dialogTester = testUtilUser.createTester('Dialog', {root: button, overlayType: 'modal'}); await dialogTester.open(); let dialog = dialogTester.getDialog(); expect(dialog).toHaveAttribute('role', 'alertdialog'); let heading = getByRole('heading'); expect(dialog).toHaveAttribute('aria-labelledby', heading.id); expect(dialog).toHaveAttribute('data-test', 'dialog'); expect(dialog.closest('.react-aria-Modal')).toHaveAttribute('data-test', 'modal'); expect(dialog.closest('.react-aria-ModalOverlay')).toBeInTheDocument(); let close = within(dialog).getByRole('button'); await user.click(close); expect(dialog).not.toBeInTheDocument(); }); it('should set aria-describedby when Text slot="description" is used in alertdialog', async () => { let {getByRole} = render( Open {({close}) => ( <> Alert Title This is the alert message. OK > )} ); let button = getByRole('button'); let dialogTester = testUtilUser.createTester('Dialog', {root: button, overlayType: 'modal'}); await dialogTester.open(); let dialog = dialogTester.getDialog(); expect(dialog).toHaveAttribute('role', 'alertdialog'); expect(dialog).toHaveAttribute('aria-describedby'); let descId = dialog.getAttribute('aria-describedby'); let descEl = document.getElementById(descId); expect(descEl).not.toBeNull(); expect(descEl.textContent).toBe('This is the alert message.'); }); it('works with modal and custom underlay', async () => { let {getByRole} = render( Delete… {({close}) => ( <> Alert This is the alert message. Close > )} ); let button = getByRole('button'); await user.click(button); let dialog = getByRole('alertdialog'); let heading = getByRole('heading'); expect(dialog).toHaveAttribute('aria-labelledby', heading.id); expect(dialog).toHaveAttribute('data-test', 'dialog'); expect(dialog.closest('.modal')).toHaveAttribute('data-test', 'modal'); expect(dialog.closest('.underlay')).toHaveAttribute('data-test', 'underlay'); let close = within(dialog).getByRole('button'); await user.click(close); expect(dialog).not.toBeInTheDocument(); }); it('has dismiss button when isDismissable', async () => { let {getByRole, getByLabelText} = render( Delete… {({close}) => ( <> Alert This is the alert message. Close > )} ); let button = getByRole('button'); await user.click(button); let dialog = getByRole('alertdialog'); let dismiss = getByLabelText('Dismiss'); await user.click(dismiss); expect(dialog).not.toBeInTheDocument(); }); it('works with popover', async () => { let {getByRole} = render( ?⃝ Help For help accessing your account, please contact support. ); let button = getByRole('button'); expect(button).not.toHaveAttribute('data-pressed'); let dialogTester = testUtilUser.createTester('Dialog', {root: button, overlayType: 'popover'}); await dialogTester.open(); expect(button).toHaveAttribute('data-pressed'); let dialog = dialogTester.getDialog(); let heading = getByRole('heading'); expect(dialog).toHaveAttribute('aria-labelledby', heading.id); expect(dialog).toHaveAttribute('data-test', 'dialog'); expect(dialog).toHaveClass('react-aria-Dialog'); let popover = dialog.closest('.react-aria-Popover'); expect(popover).toHaveStyle('position: absolute'); expect(popover).toHaveAttribute('data-test', 'popover'); let arrow = popover.querySelector('.react-aria-OverlayArrow'); expect(arrow).toHaveStyle('position: absolute'); expect(arrow).toHaveAttribute('data-test', 'arrow'); await user.click(document.body); expect(dialog).not.toBeInTheDocument(); }); it('should get default aria label from trigger', async () => { let {getByRole} = render( Settings Test ); let button = getByRole('button'); await user.click(button); let dialog = getByRole('dialog'); expect(dialog).toHaveAttribute('aria-labelledby', button.id); await user.click(document.body); expect(dialog).not.toBeInTheDocument(); }); it('should support render props', async () => { let {getByRole} = render( ?⃝ {({close}) => ( <> Help For help accessing your account, please contact support. close()}>Dismiss > )} ); let button = getByRole('button'); await user.click(button); let dialog = getByRole('dialog'); expect(dialog).toBeInTheDocument(); let dismiss = within(dialog).getByRole('button'); await user.click(dismiss); expect(dialog).not.toBeInTheDocument(); }); it('should support Modal being used standalone', async () => { let onOpenChange = jest.fn(); let {getByRole} = render( A modal ); let dialog = getByRole('dialog'); expect(dialog).toHaveTextContent('A modal'); await user.click(document.body); expect(onOpenChange).toHaveBeenCalledTimes(1); expect(onOpenChange).toHaveBeenCalledWith(false); }); it('isOpen and defaultOpen should override state from context', async () => { let onOpenChange = jest.fn(); let {getByRole} = render( <> A modal > ); let dialog = getByRole('dialog'); expect(dialog).toHaveTextContent('A modal'); await user.click(document.body); expect(onOpenChange).toHaveBeenCalledTimes(1); expect(onOpenChange).toHaveBeenCalledWith(false); }); it('supports isEntering and isExiting props', async () => { function TestModal(props) { return ( A modal ); } let {getByRole, rerender} = render(); let button = getByRole('button'); await user.click(button); let modal = getByRole('dialog').closest('.react-aria-ModalOverlay'); expect(modal).toHaveAttribute('data-entering'); rerender(); expect(modal).not.toHaveAttribute('data-entering'); rerender(); await user.click(button); expect(modal).toBeInTheDocument(); expect(modal).toHaveAttribute('data-exiting'); rerender(); expect(modal).not.toBeInTheDocument(); }); describe('onEnter and onExit', () => { beforeEach(() => { // jsdom skips enter and exit animations unless getAnimations is implemented. Element.prototype.getAnimations = () => []; }); afterEach(() => { delete Element.prototype.getAnimations; }); function deferred() { let resolve; let promise = new Promise(r => (resolve = r)); return {promise, resolve}; } function TestModal({overlayProps = {}, modalProps = {}}) { return ( Open Close ); } it('calls onEnter on ModalOverlay and Modal with their elements when opening', async () => { let onOverlayEnter = jest.fn(); let onModalEnter = jest.fn(); let {getByRole, getByTestId} = render( ); expect(onOverlayEnter).not.toHaveBeenCalled(); expect(onModalEnter).not.toHaveBeenCalled(); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); let modal = getByTestId('modal'); expect(onOverlayEnter).toHaveBeenCalledTimes(1); expect(onOverlayEnter).toHaveBeenCalledWith(overlay); expect(onModalEnter).toHaveBeenCalledTimes(1); expect(onModalEnter).toHaveBeenCalledWith(modal); // Synchronous callbacks complete the entry immediately. expect(overlay).not.toHaveAttribute('data-entering'); expect(modal).not.toHaveAttribute('data-entering'); }); it('remains entering until the promise returned by ModalOverlay onEnter resolves', async () => { let {promise, resolve} = deferred(); let onEnter = jest.fn(() => promise); let {getByRole, getByTestId} = render(); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); expect(onEnter).toHaveBeenCalledTimes(1); expect(overlay).toHaveAttribute('data-entering'); expect(getByTestId('modal')).not.toHaveAttribute('data-entering'); await act(async () => resolve()); expect(overlay).not.toHaveAttribute('data-entering'); expect(onEnter).toHaveBeenCalledTimes(1); }); it('remains entering until the promise returned by Modal onEnter resolves', async () => { let {promise, resolve} = deferred(); let onEnter = jest.fn(() => promise); let {getByRole, getByTestId} = render(); await user.click(getByRole('button')); let modal = getByTestId('modal'); expect(onEnter).toHaveBeenCalledTimes(1); expect(modal).toHaveAttribute('data-entering'); expect(getByTestId('overlay')).not.toHaveAttribute('data-entering'); await act(async () => resolve()); expect(modal).not.toHaveAttribute('data-entering'); expect(onEnter).toHaveBeenCalledTimes(1); }); it('calls onExit on ModalOverlay and Modal with their elements when closing', async () => { let onOverlayExit = jest.fn(); let onModalExit = jest.fn(); let {getByRole, getByTestId, queryByTestId} = render( ); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); let modal = getByTestId('modal'); expect(onOverlayExit).not.toHaveBeenCalled(); expect(onModalExit).not.toHaveBeenCalled(); await user.click(getByRole('button', {name: 'Close'})); expect(onOverlayExit).toHaveBeenCalledTimes(1); expect(onOverlayExit).toHaveBeenCalledWith(overlay); expect(onModalExit).toHaveBeenCalledTimes(1); expect(onModalExit).toHaveBeenCalledWith(modal); // Synchronous callbacks complete the exit immediately. expect(queryByTestId('overlay')).toBeNull(); }); it('remains mounted until the promise returned by ModalOverlay onExit resolves', async () => { let {promise, resolve} = deferred(); let onExit = jest.fn(() => promise); let {getByRole, getByTestId} = render(); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); let modal = getByTestId('modal'); await user.click(getByRole('button', {name: 'Close'})); expect(onExit).toHaveBeenCalledTimes(1); expect(onExit).toHaveBeenCalledWith(overlay); expect(overlay).toBeInTheDocument(); expect(overlay).toHaveAttribute('data-exiting'); expect(modal).toHaveAttribute('data-exiting'); await act(async () => resolve()); expect(overlay).not.toBeInTheDocument(); expect(onExit).toHaveBeenCalledTimes(1); }); it('remains mounted until the promise returned by Modal onExit resolves', async () => { let {promise, resolve} = deferred(); let onExit = jest.fn(() => promise); let {getByRole, getByTestId} = render(); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); let modal = getByTestId('modal'); await user.click(getByRole('button', {name: 'Close'})); expect(onExit).toHaveBeenCalledTimes(1); expect(onExit).toHaveBeenCalledWith(modal); expect(overlay).toBeInTheDocument(); expect(overlay).toHaveAttribute('data-exiting'); expect(modal).toHaveAttribute('data-exiting'); await act(async () => resolve()); expect(overlay).not.toBeInTheDocument(); expect(onExit).toHaveBeenCalledTimes(1); }); it('waits for both ModalOverlay and Modal onExit promises before unmounting', async () => { let overlayExit = deferred(); let modalExit = deferred(); let {getByRole, getByTestId} = render( overlayExit.promise}} modalProps={{onExit: () => modalExit.promise}} /> ); await user.click(getByRole('button')); let overlay = getByTestId('overlay'); await user.click(getByRole('button', {name: 'Close'})); await act(async () => overlayExit.resolve()); expect(overlay).toBeInTheDocument(); expect(overlay).toHaveAttribute('data-exiting'); await act(async () => modalExit.resolve()); expect(overlay).not.toBeInTheDocument(); }); it('calls the latest onEnter and onExit callbacks', async () => { let enterPromise = deferred(); let exitPromise = deferred(); let initialEnter = jest.fn(() => enterPromise.promise); let initialExit = jest.fn(() => exitPromise.promise); let {getByRole, getByTestId, queryByTestId, rerender} = render( ); await user.click(getByRole('button')); expect(initialEnter).toHaveBeenCalledTimes(1); // Re-rendering during the entry does not restart it. let onEnter = jest.fn(); let onExit = jest.fn(); rerender(); expect(onEnter).not.toHaveBeenCalled(); expect(getByTestId('modal')).toHaveAttribute('data-entering'); await act(async () => enterPromise.resolve()); expect(getByTestId('modal')).not.toHaveAttribute('data-entering'); await user.click(getByRole('button', {name: 'Close'})); expect(initialExit).not.toHaveBeenCalled(); expect(onExit).toHaveBeenCalledTimes(1); expect(queryByTestId('overlay')).toBeNull(); }); it('supports onEnter and onExit on a standalone Modal', async () => { let enter = deferred(); let exit = deferred(); let onEnter = jest.fn(() => enter.promise); let onExit = jest.fn(() => exit.promise); function StandaloneModal(props) { return ( A modal ); } let {getByTestId, rerender} = render(); let modal = getByTestId('modal'); expect(onEnter).toHaveBeenCalledTimes(1); expect(onEnter).toHaveBeenCalledWith(modal); expect(modal).toHaveAttribute('data-entering'); await act(async () => enter.resolve()); expect(modal).not.toHaveAttribute('data-entering'); rerender(); expect(onExit).toHaveBeenCalledTimes(1); expect(onExit).toHaveBeenCalledWith(modal); expect(modal).toBeInTheDocument(); expect(modal).toHaveAttribute('data-exiting'); await act(async () => exit.resolve()); expect(modal).not.toBeInTheDocument(); }); }); describe('portalProvider', () => { function InfoDialog() { return ( Delete… {({close}) => ( <> Alert This is the alert message. Close > )} ); } function App() { let container = useRef(null); return ( <> container.current}> > ); } it('should render the dialog in the portal container provided by the PortalProvider', async () => { let {getByRole, getByTestId} = render(); let button = getByRole('button'); await user.click(button); expect(getByRole('alertdialog').closest('[data-testid="custom-container"]')).toBe( getByTestId('custom-container') ); await user.click(document.body); }); }); // TODO: delete this test when we get rid of the deprecated prop describe('portalContainer', () => { function InfoDialog(props) { return ( Delete… {({close}) => ( <> Alert This is the alert message. Close > )} ); } function App() { let [container, setContainer] = React.useState(); return ( <> > ); } it('should render the dialog in the portal container', async () => { let {getByRole, getByTestId} = render(); let button = getByRole('button'); await user.click(button); expect(getByRole('alertdialog').closest('[data-testid="custom-container"]')).toBe( getByTestId('custom-container') ); await user.click(document.body); }); }); it('ensure Input autoFocus works when opening Modal from MenuItem via keyboard', async () => { function App() { const [isOpen, setOpen] = React.useState(false); return ( <> Open menu setOpen(true)}>Add account Sign out Sign up Email Password > ); } const {getAllByRole, getByRole, getByTestId} = render(); const button = getByRole('button'); await user.tab(); expect(document.activeElement).toBe(button); await user.keyboard('{Enter}'); act(() => { jest.runAllTimers(); }); const menuitem = getAllByRole('menuitem')[0]; expect(document.activeElement).toBe(menuitem); await user.keyboard('{Enter}'); act(() => { jest.runAllTimers(); }); const input = getByTestId('email'); expect(document.activeElement).toBe(input); }); it('should not close Modal when DateRangePicker is dismissed by outside click', async () => { let {getAllByRole, getByRole} = render(); await user.click(getByRole('button')); let modal = getByRole('dialog').closest('.react-aria-ModalOverlay'); expect(modal).toBeInTheDocument(); let button = getByRole('group').querySelector('.react-aria-Button'); expect(button).toHaveAttribute('aria-label', 'Calendar'); await user.click(button); let popover = getByRole('dialog').closest('.react-aria-Popover'); expect(popover).toBeInTheDocument(); expect(popover).toHaveAttribute('data-trigger', 'DateRangePicker'); let cells = getAllByRole('gridcell'); await user.click(cells[5].children[0]); await user.click(document.body); expect(popover).not.toBeInTheDocument(); expect(modal).toBeInTheDocument(); }); });
For help accessing your account, please contact support.