import type { SubmitEvent } from 'react' import { describe, expect, it, vi } from 'vitest' import { render } from 'vitest-browser-react' import { userEvent } from 'vitest/browser' import { ConfirmDialog } from './confirm-dialog' describe('ConfirmDialog', () => { it('renders title, description, and default buttons', async () => { const { getByRole, getByText } = await render( ) await expect .element(getByRole('heading', { name: 'Delete item' })) .toBeInTheDocument() await expect .element(getByText('This action cannot be undone.')) .toBeInTheDocument() await expect .element(getByRole('button', { name: 'Cancel' })) .toBeInTheDocument() await expect .element(getByRole('button', { name: 'Continue' })) .toBeInTheDocument() }) it('calls handleConfirm when the confirm button is clicked', async () => { const handleConfirm = vi.fn() const { getByRole } = await render( ) await userEvent.click(getByRole('button', { name: 'Sign out' })) expect(handleConfirm).toHaveBeenCalledOnce() }) it('disables confirm when disabled is true', async () => { const handleConfirm = vi.fn() const { getByRole } = await render( ) const confirm = getByRole('button', { name: 'Continue' }) await expect.element(confirm).toBeDisabled() expect(handleConfirm).not.toHaveBeenCalled() }) it('when isLoading is true, disables cancel and confirm', async () => { const handleConfirm = vi.fn() const { getByRole } = await render( ) await expect.element(getByRole('button', { name: 'Cancel' })).toBeDisabled() await expect .element(getByRole('button', { name: 'Continue' })) .toBeDisabled() }) it('supports custom button texts', async () => { const { getByRole } = await render( ) await expect .element(getByRole('button', { name: 'No' })) .toBeInTheDocument() await expect .element(getByRole('button', { name: 'Yes' })) .toBeInTheDocument() }) it('renders confirm as submit button linked to desc form when `form` is set', async () => { const { getByRole } = await render(

Type DELETE to confirm.

} confirmText='Delete' destructive /> ) const deleteBtn = getByRole('button', { name: 'Delete' }) await expect.element(deleteBtn).toHaveAttribute('type', 'submit') await expect .element(deleteBtn) .toHaveAttribute('form', 'tasks-multi-delete-form') }) it('submits the desc form when confirm is clicked (form prop, no handleConfirm)', async () => { const handleFormSubmit = vi.fn((e: SubmitEvent) => { e.preventDefault() }) const { getByRole } = await render(

Confirm deletion.

} confirmText='Delete' destructive /> ) await userEvent.click(getByRole('button', { name: 'Delete' })) expect(handleFormSubmit).toHaveBeenCalledOnce() }) it('submits the form when Enter key is pressed', async () => { const handleFormSubmit = vi.fn((e: SubmitEvent) => { e.preventDefault() }) const { getByPlaceholder } = await render( } confirmText='Delete' destructive /> ) await userEvent.fill(getByPlaceholder('username'), 'test') await userEvent.keyboard('{Enter}') expect(handleFormSubmit).toHaveBeenCalledOnce() }) it('does not submit the form when confirm is disabled (typed confirmation mismatch)', async () => { const handleFormSubmit = vi.fn((e: SubmitEvent) => { e.preventDefault() }) const { getByRole } = await render(

Enter username to enable Delete.

} confirmText='Delete' destructive /> ) const deleteBtn = getByRole('button', { name: 'Delete' }) await expect.element(deleteBtn).toBeDisabled() expect(handleFormSubmit).not.toHaveBeenCalled() }) })