import { cleanup, render, screen, waitForElementToBeRemoved, } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { expect } from 'chai'; import React from 'react'; import { ToastArea, useToast } from './use-toast'; import type { ToastProperties } from './use-toast'; const OpenToastButton = ({ namespace, id, ...toastProps }: { namespace: string; id: string; } & ToastProperties) => { const { openToast } = useToast(namespace); return ( ); }; const CloseToastButton = ({ namespace, id, }: { namespace: string; id: string; }) => { const { closeToast } = useToast(namespace); return ( ); }; describe('useToast', function () { afterEach(cleanup); it('opens and closes a toast', async function () { render( ); userEvent.click(screen.getByText('Open Toast')); expect(await screen.findByText('My Toast')).to.exist; userEvent.click(screen.getByText('Close Toast')); await waitForElementToBeRemoved(() => { return screen.queryByText('My Toast'); }); expect(screen.queryByText('My Toast')).to.not.exist; }); it('is dismissible by default', async function () { render( ); userEvent.click(screen.getByText('Open Toast')); await screen.findByText('My Toast'); userEvent.click(screen.getByLabelText('Close Message')); await waitForElementToBeRemoved(() => { return screen.queryByText('My Toast'); }); expect(screen.queryByText('My Toast')).to.not.exist; }); describe('with timeout', function () { it('closes a toast after timeout expires', async function () { render( ); userEvent.click(screen.getByText('Open Toast')); await screen.findByText('My Toast'); await waitForElementToBeRemoved( () => { return screen.queryByText('My Toast'); }, { timeout: 5_000 } ); expect(screen.queryByText('My Toast')).to.not.exist; }); }); });