|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import { userEvent } from 'vitest/browser' |
| 4 | +import { defineComponent, h } from 'vue' |
| 5 | +import { DialogClose, DialogContent, DialogOverlay, DialogPortal, DialogRoot, DialogTitle, DialogTrigger } from '.' |
| 6 | + |
| 7 | +function mountDialog() { |
| 8 | + const onOutsideClick = vi.fn() |
| 9 | + const Fixture = defineComponent({ |
| 10 | + setup() { |
| 11 | + return () => h('div', [ |
| 12 | + h('button', { 'data-testid': 'outside', 'onClick': onOutsideClick }, 'Outside'), |
| 13 | + h(DialogRoot, null, () => [ |
| 14 | + h(DialogTrigger, { 'data-testid': 'trigger' }, () => 'Open'), |
| 15 | + h(DialogPortal, null, () => [ |
| 16 | + h(DialogOverlay, { style: 'position: fixed; inset: 0;' }), |
| 17 | + h(DialogContent, { |
| 18 | + 'aria-describedby': undefined, |
| 19 | + 'data-testid': 'content', |
| 20 | + 'style': 'position: fixed; top: 100px; left: 100px; width: 200px; background: white;', |
| 21 | + }, () => [ |
| 22 | + h(DialogTitle, null, () => 'Title'), |
| 23 | + h('input', { 'data-testid': 'first' }), |
| 24 | + h(DialogClose, { 'data-testid': 'close' }, () => 'Close'), |
| 25 | + ]), |
| 26 | + ]), |
| 27 | + ]), |
| 28 | + ]) |
| 29 | + }, |
| 30 | + }) |
| 31 | + mount(Fixture, { attachTo: document.body }) |
| 32 | + const get = (id: string) => document.querySelector<HTMLElement>(`[data-testid="${id}"]`) |
| 33 | + return { get, onOutsideClick } |
| 34 | +} |
| 35 | + |
| 36 | +describe('given a modal Dialog, in a real browser', () => { |
| 37 | + it('traps Tab focus, closes on Escape and returns focus to the trigger', async () => { |
| 38 | + const { get } = mountDialog() |
| 39 | + |
| 40 | + await userEvent.click(get('trigger')!) |
| 41 | + await expect.poll(() => document.activeElement).toBe(get('first')) |
| 42 | + |
| 43 | + await userEvent.keyboard('{Tab}') |
| 44 | + expect(document.activeElement).toBe(get('close')) |
| 45 | + // Tab from the last element wraps to the first instead of leaving the dialog. |
| 46 | + await userEvent.keyboard('{Tab}') |
| 47 | + expect(document.activeElement).toBe(get('first')) |
| 48 | + await userEvent.keyboard('{Shift>}{Tab}{/Shift}') |
| 49 | + expect(document.activeElement).toBe(get('close')) |
| 50 | + |
| 51 | + await userEvent.keyboard('{Escape}') |
| 52 | + await expect.poll(() => get('content')).toBeNull() |
| 53 | + expect(document.activeElement).toBe(get('trigger')) |
| 54 | + }) |
| 55 | + |
| 56 | + it('blocks pointer interaction outside, and restores it after closing', async () => { |
| 57 | + const { get, onOutsideClick } = mountDialog() |
| 58 | + |
| 59 | + await userEvent.click(get('trigger')!) |
| 60 | + await expect.poll(() => get('content')).not.toBeNull() |
| 61 | + expect(getComputedStyle(document.body).pointerEvents).toBe('none') |
| 62 | + // Hit testing is real here: the point over the outside button resolves to the overlay. |
| 63 | + const rect = get('outside')!.getBoundingClientRect() |
| 64 | + const hit = document.elementFromPoint(rect.left + 2, rect.top + 2) |
| 65 | + expect(get('outside')!.contains(hit)).toBe(false) |
| 66 | + |
| 67 | + await userEvent.keyboard('{Escape}') |
| 68 | + await expect.poll(() => get('content')).toBeNull() |
| 69 | + expect(getComputedStyle(document.body).pointerEvents).not.toBe('none') |
| 70 | + |
| 71 | + await userEvent.click(get('outside')!) |
| 72 | + expect(onOutsideClick).toHaveBeenCalledTimes(1) |
| 73 | + }) |
| 74 | +}) |
0 commit comments