import React from 'react'; import { cleanup, render, waitFor, waitForElementToBeRemoved, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { axe } from 'jest-axe'; import { toastPortalClassName, ToastProvider } from '../ToastContext'; import { ControlledToast as Toast } from './ControlledToast'; async function expectElementToNotBeRemoved(element: HTMLElement) { try { await waitForElementToBeRemoved(element); throw new Error('Expected to catch error.'); } catch (error) { if (error instanceof Error) { expect(error.toString()).toMatch( 'Timed out in waitForElementToBeRemoved.', ); } } } /** * This suite checks rendering specific to the controlled behavior. * i.e. `open` and `onClose` props. * * Other individual toast behavior is tested in `InternalToast.spec` * and multi-toast behavior is tested in `ToastContainer.spec` */ describe('packages/toast/controlled', () => { // Clear the portals after each test afterEach(() => { cleanup(); const portal = document.querySelector('.' + toastPortalClassName); if (portal) { document.body.removeChild(portal); } }); describe('a11y', () => { test('does not have basic accessibility issues', async () => { const { container } = render( {}} title={'hello world'} />, ); const results = await axe(container); expect(results).toHaveNoViolations(); }); }); describe('`open` prop', () => { test('renders when `open` is true', async () => { const { findByTestId } = render( {}} data-testid="test-toast1" /> , ); const toast = await findByTestId('test-toast1'); expect(toast).toBeInTheDocument(); }); test('handles rerender when `open` is true without hanging', async () => { const { findByTestId, rerender } = render( , { wrapper: ({ children }) => {children}, }, ); rerender( , ); const toast = await findByTestId('test-toast-rerender'); expect(toast).toBeInTheDocument(); }); test('does not render when `open` is true and component is unmounted', async () => { const { queryByTestId } = render( {false && ( // eslint-disable-line no-constant-binary-expression {}} data-testid="test-toast1b" /> )} , ); const toast = await waitFor(() => queryByTestId('test-toast1b')); expect(toast).not.toBeInTheDocument(); }); test('unmounts when `open` is true and component render is toggled to false', async () => { let shouldRender = true; const { findByTestId, rerender } = render( {shouldRender && ( {}} data-testid="test-toast1" /> )} , ); const toast = await findByTestId('test-toast1'); expect(toast).toBeInTheDocument(); shouldRender = false; rerender( {shouldRender && ( {}} data-testid="test-toast1" /> )} , ); await waitForElementToBeRemoved(toast); expect(toast).not.toBeInTheDocument(); }); test('does not render when `open` is false', async () => { const { queryByTestId } = render( {}} data-testid="test-toast2" /> , ); const toast = await waitFor(() => queryByTestId('test-toast2')); expect(toast).not.toBeInTheDocument(); }); test('is closed when `open` is set back to false', async () => { const { findByTestId, rerender } = render( {}} data-testid="test-toast3" /> , ); const toast = await findByTestId('test-toast3'); rerender( {}} data-testid="test-toast3" /> , ); await waitForElementToBeRemoved(toast); expect(toast).not.toBeInTheDocument(); }); test('does not close until manually closed', async () => { const { findByTestId } = render( {}} data-testid="toast4" timeout={100} /> , ); const toast = await findByTestId('toast4'); await expectElementToNotBeRemoved(toast); }); }); describe('onClose', () => { test('`onClose` method is called when the close button is clicked', async () => { const handleClose = jest.fn(); const { findByTestId } = render( , ); const closeBtn = await findByTestId('lg-toast-dismiss-button'); userEvent.click(closeBtn); expect(handleClose).toHaveBeenCalled(); }); test('`onClose` method is called after timeout', () => { const handleClose = jest.fn(); render( , ); setTimeout(() => { expect(handleClose).toHaveBeenCalled(); }, 101); }); }); });