import React from 'react'; import { axe } from 'jest-axe'; import { RenderResult } from '@testing-library/react'; import { render, fireEvent } from '@testing-library/react'; import * as AlertDialog from '@radix-ui/react-alert-dialog'; const OPEN_TEXT = 'Open'; const CANCEL_TEXT = 'Cancel'; const ACTION_TEXT = 'Do it'; const TITLE_TEXT = 'Warning'; const DESC_TEXT = 'This is a warning'; const OVERLAY_TEST_ID = 'test-overlay'; const DialogTest = (props: React.ComponentProps) => ( {OPEN_TEXT} {TITLE_TEXT} {DESC_TEXT} {CANCEL_TEXT} {ACTION_TEXT} ); describe('given a default Dialog', () => { let rendered: RenderResult; let title: HTMLElement; let trigger: HTMLElement; let cancelButton: HTMLElement; beforeEach(() => { rendered = render(); trigger = rendered.getByText(OPEN_TEXT); }); it('should have no accessibility violations in default state', async () => { expect(await axe(rendered.container)).toHaveNoViolations(); }); describe('after clicking the trigger', () => { beforeEach(() => { fireEvent.click(trigger); title = rendered.getByText(TITLE_TEXT); cancelButton = rendered.getByText(CANCEL_TEXT); }); it('should open the content', () => { expect(title).toBeVisible(); }); it('should have no accessibility violations when open', async () => { expect(await axe(rendered.container)).toHaveNoViolations(); }); it('should focus the cancel button', () => { expect(cancelButton).toHaveFocus(); }); }); });