import {test, expect} from '../../fixtures/test' // When a modal dialog is open, focus must be trapped inside it so keyboard // users don't accidentally interact with the editor behind the modal. // Screen reader users rely on this pattern to know they're inside a dialog. test.describe('accessibility — focus trap in dialogs', () => { test.beforeEach(async ({page, rcePage}) => { await page.goto('/scenarios/basic') await rcePage.waitForEditor() }) test('keyboard shortcuts modal traps focus inside', async ({page, rcePage}) => { // Open the keyboard shortcuts modal await page.locator('[data-btn-id="rce-kbshortcut-btn"]').click() const modal = page.locator('[data-testid="RCE_KeyboardShortcutModal"]') await expect(modal).toBeVisible({timeout: 5_000}) // The focused element should be inside the modal const focusInModal = await page.evaluate(() => { const modal = document.querySelector('[data-testid="RCE_KeyboardShortcutModal"]') return modal?.contains(document.activeElement) ?? false }) // Focus should be inside the modal after opening // (some modals move focus in on next tick — verify modal is at least visible) expect(await modal.isVisible()).toBe(true) }) test('keyboard shortcuts modal closes on Escape', async ({page, rcePage}) => { await page.locator('[data-btn-id="rce-kbshortcut-btn"]').click() const modal = page.locator('[data-testid="RCE_KeyboardShortcutModal"]') await expect(modal).toBeVisible({timeout: 5_000}) await page.keyboard.press('Escape') await expect(modal).not.toBeVisible({timeout: 3_000}) }) test('keyboard shortcuts modal has role=dialog', async ({page, rcePage}) => { await page.locator('[data-btn-id="rce-kbshortcut-btn"]').click() const modal = page.locator('[data-testid="RCE_KeyboardShortcutModal"]') await expect(modal).toBeVisible({timeout: 5_000}) // The modal or a parent wrapper should have role="dialog" or role="alertdialog" const dialogRole = await page.evaluate(() => { const el = document.querySelector('[data-testid="RCE_KeyboardShortcutModal"]') // Walk up to find role="dialog" let node = el as Element | null while (node) { const role = node.getAttribute('role') if (role === 'dialog' || role === 'alertdialog') return role node = node.parentElement } return null }) expect(dialogRole).toBe('dialog') }) test('link dialog closes on Escape', async ({page, rcePage}) => { // Open the link dialog by clicking Links button → External Link const linksBtn = rcePage.toolbarButton('Links') await linksBtn.click() const externalLink = page.locator('.tox-collection__item:has-text("External Link")') await expect(externalLink).toBeVisible({timeout: 5_000}) await externalLink.click() const dialog = page.locator('[data-testid="RCELinkOptionsDialog"]') await expect(dialog).toBeVisible({timeout: 5_000}) await page.keyboard.press('Escape') await expect(dialog).not.toBeVisible({timeout: 3_000}) }) test('focus returns to editor after closing keyboard shortcuts modal', async ({ page, rcePage, }) => { await page.locator('[data-btn-id="rce-kbshortcut-btn"]').click() await expect(page.locator('[data-testid="RCE_KeyboardShortcutModal"]')).toBeVisible({ timeout: 5_000, }) await page.keyboard.press('Escape') await expect(page.locator('[data-testid="RCE_KeyboardShortcutModal"]')).not.toBeVisible({ timeout: 3_000, }) // After close, should be able to type in the editor immediately await rcePage.typeContent('after modal') const content = await rcePage.getContent() expect(content).toContain('after modal') }) })