import { render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { describe, expect, it, vi } from 'vitest' import { expectNoA11yViolations } from '../test/a11y' import { Button } from './Button' import { Dialog, Modal } from './Dialog' import { Input } from './Input' function Example(props: Partial>) { return ( Edit profile} title="Edit profile" description="Changes are visible to your team." footer={ <> }>Cancel } {...props} > ) } describe('Dialog', () => { it('opens from the trigger as a named dialog', async () => { render() expect(screen.queryByRole('dialog')).not.toBeInTheDocument() await userEvent.click(screen.getByRole('button', { name: 'Edit profile' })) const dialog = await screen.findByRole('dialog', { name: 'Edit profile' }) expect(dialog).toHaveAccessibleDescription('Changes are visible to your team.') expect(screen.getByRole('textbox', { name: 'Full name' })).toBeInTheDocument() }) it('closes on Escape and returns focus to the trigger', async () => { render() const trigger = screen.getByRole('button', { name: 'Edit profile' }) await userEvent.click(trigger) await screen.findByRole('dialog') await userEvent.keyboard('{Escape}') await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()) await waitFor(() => expect(trigger).toHaveFocus()) }) it('moves focus inside and has a labelled close button', async () => { render() await userEvent.click(screen.getByRole('button', { name: 'Edit profile' })) const dialog = await screen.findByRole('dialog') await waitFor(() => expect(dialog).toContainElement(document.activeElement as HTMLElement)) await userEvent.click(screen.getByRole('button', { name: 'Close' })) await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()) }) it('hides the close button on request', async () => { render() await screen.findByRole('dialog') expect(screen.queryByRole('button', { name: 'Close' })).not.toBeInTheDocument() }) it('works controlled', async () => { const onOpenChange = vi.fn() const { rerender } = render() await userEvent.click(screen.getByRole('button', { name: 'Edit profile' })) expect(onOpenChange).toHaveBeenCalledWith(true) expect(screen.queryByRole('dialog')).not.toBeInTheDocument() rerender() expect(await screen.findByRole('dialog')).toBeInTheDocument() await userEvent.click(screen.getByRole('button', { name: 'Cancel' })) expect(onOpenChange).toHaveBeenLastCalledWith(false) }) it('builds a custom layout from compound parts, and Modal is an alias', async () => { render( Invite people Body text , ) expect(await screen.findByRole('dialog', { name: 'Invite people' })).toHaveTextContent('Body text') }) it('has no axe violations when open', async () => { render() await screen.findByRole('dialog') await expectNoA11yViolations(document.body) }) })