|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * |
| 4 | + * A row action the server would refuse stays visible and greyed rather than |
| 5 | + * disappearing, so the row can say why. That only works if three things hold at |
| 6 | + * once: the item is actually disabled (Radix greys it), the reason reaches |
| 7 | + * pointer users through the platform tooltip — which needs the wrapping span, |
| 8 | + * since a disabled item is `pointer-events-none` and never sees the hover — and |
| 9 | + * the reason reaches assistive tech through the accessible name, since Radix |
| 10 | + * skips disabled items in a menu's roving focus. |
| 11 | + */ |
| 12 | +import { act, type ReactNode } from 'react' |
| 13 | +import { createRoot, type Root } from 'react-dom/client' |
| 14 | +import { afterEach, describe, expect, it } from 'vitest' |
| 15 | +import { RowActionsMenu } from '@/app/workspace/[workspaceId]/settings/components/row-actions-menu/row-actions-menu' |
| 16 | + |
| 17 | +const LOCK_REASON = 'Organization admins are automatically workspace admins.' |
| 18 | + |
| 19 | +let root: Root | null = null |
| 20 | +let container: HTMLDivElement | null = null |
| 21 | + |
| 22 | +function mount(ui: ReactNode) { |
| 23 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 24 | + container = document.createElement('div') |
| 25 | + document.body.appendChild(container) |
| 26 | + root = createRoot(container) |
| 27 | + act(() => root?.render(ui)) |
| 28 | +} |
| 29 | + |
| 30 | +/** Opens the `...` menu the way a pointer does — Radix opens on `pointerdown`. */ |
| 31 | +function openMenu() { |
| 32 | + const trigger = container?.querySelector('button') |
| 33 | + if (!trigger) throw new Error('Menu trigger did not render') |
| 34 | + act(() => { |
| 35 | + trigger.dispatchEvent(new MouseEvent('pointerdown', { bubbles: true, button: 0 })) |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +function item(): HTMLElement { |
| 40 | + const node = document.querySelector('[role="menuitem"]') |
| 41 | + if (!node) throw new Error('No menu item rendered') |
| 42 | + return node as HTMLElement |
| 43 | +} |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + if (root) act(() => root?.unmount()) |
| 47 | + container?.remove() |
| 48 | + root = null |
| 49 | + container = null |
| 50 | +}) |
| 51 | + |
| 52 | +describe('a disabled row action explains itself', () => { |
| 53 | + function mountLockedRemove() { |
| 54 | + mount( |
| 55 | + <RowActionsMenu |
| 56 | + label='Teammate actions' |
| 57 | + actions={[ |
| 58 | + { |
| 59 | + label: 'Remove', |
| 60 | + destructive: true, |
| 61 | + disabled: true, |
| 62 | + tooltip: LOCK_REASON, |
| 63 | + onSelect: () => {}, |
| 64 | + }, |
| 65 | + ]} |
| 66 | + /> |
| 67 | + ) |
| 68 | + openMenu() |
| 69 | + } |
| 70 | + |
| 71 | + it('greys the item out instead of hiding it', () => { |
| 72 | + mountLockedRemove() |
| 73 | + |
| 74 | + const remove = item() |
| 75 | + expect(remove.textContent).toBe('Remove') |
| 76 | + expect(remove.getAttribute('data-disabled')).not.toBeNull() |
| 77 | + expect(remove.className).toContain('data-[disabled]:opacity-50') |
| 78 | + }) |
| 79 | + |
| 80 | + it('shows the reason in the platform tooltip on hover', () => { |
| 81 | + mountLockedRemove() |
| 82 | + |
| 83 | + expect(document.querySelector('[role="tooltip"]')).toBeNull() |
| 84 | + |
| 85 | + /* The wrapping span, not the item — a disabled item is `pointer-events-none`. */ |
| 86 | + const hoverTarget = item().parentElement |
| 87 | + if (!hoverTarget) throw new Error('Tooltip trigger wrapper did not render') |
| 88 | + act(() => { |
| 89 | + hoverTarget.dispatchEvent( |
| 90 | + new MouseEvent('pointerover', { bubbles: true, clientX: 120, clientY: 120 }) |
| 91 | + ) |
| 92 | + }) |
| 93 | + |
| 94 | + expect(document.querySelector('[role="tooltip"]')?.textContent).toBe(LOCK_REASON) |
| 95 | + }) |
| 96 | + |
| 97 | + it('folds the reason into the accessible name for assistive tech', () => { |
| 98 | + mountLockedRemove() |
| 99 | + |
| 100 | + expect(item().getAttribute('aria-label')).toBe(`Remove — ${LOCK_REASON}`) |
| 101 | + }) |
| 102 | + |
| 103 | + it('leaves an enabled action unlabelled and untooltipped', () => { |
| 104 | + mount( |
| 105 | + <RowActionsMenu |
| 106 | + label='Teammate actions' |
| 107 | + actions={[{ label: 'Copy email', onSelect: () => {} }]} |
| 108 | + /> |
| 109 | + ) |
| 110 | + openMenu() |
| 111 | + |
| 112 | + expect(item().getAttribute('aria-label')).toBeNull() |
| 113 | + expect(item().getAttribute('data-disabled')).toBeNull() |
| 114 | + }) |
| 115 | +}) |
0 commit comments