|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, createRef, type ReactNode } from 'react' |
| 3 | +import { Button, OverlayActionButton, Tooltip } from '@sim/emcn' |
| 4 | +import { createRoot, type Root } from 'react-dom/client' |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +let root: Root | null = null |
| 8 | +let container: HTMLDivElement | null = null |
| 9 | + |
| 10 | +function mount(children: ReactNode) { |
| 11 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 12 | + container = document.createElement('div') |
| 13 | + document.body.appendChild(container) |
| 14 | + root = createRoot(container) |
| 15 | + act(() => root?.render(children)) |
| 16 | + return container |
| 17 | +} |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + if (root) act(() => root?.unmount()) |
| 21 | + container?.remove() |
| 22 | + root = null |
| 23 | + container = null |
| 24 | + vi.useRealTimers() |
| 25 | +}) |
| 26 | + |
| 27 | +/** Pre-migration recipes from log details and the deployment preview. */ |
| 28 | +const PREVIOUS = [ |
| 29 | + { |
| 30 | + name: 'default 20px adaptive action', |
| 31 | + props: {}, |
| 32 | + variant: 'default', |
| 33 | + className: |
| 34 | + 'size-[20px] cursor-pointer border-[var(--border-1)] bg-transparent p-0 backdrop-blur-xs hover-hover:bg-[var(--surface-3)]', |
| 35 | + }, |
| 36 | + { |
| 37 | + name: '28px adaptive action', |
| 38 | + props: { size: 'md' }, |
| 39 | + variant: 'default', |
| 40 | + className: |
| 41 | + 'size-[28px] cursor-pointer bg-transparent p-0 backdrop-blur-xs hover-hover:bg-[var(--surface-3)]', |
| 42 | + }, |
| 43 | +] as const |
| 44 | + |
| 45 | +describe('OverlayActionButton', () => { |
| 46 | + it.each(PREVIOUS)('preserves the previous $name markup', ({ props, variant, className }) => { |
| 47 | + const view = mount( |
| 48 | + <> |
| 49 | + <Button variant={variant} aria-label='Copy' className={`${className} shrink-0`}> |
| 50 | + <svg className='size-[10px]' aria-hidden='true' /> |
| 51 | + </Button> |
| 52 | + <OverlayActionButton {...props} aria-label='Copy' className='shrink-0'> |
| 53 | + <svg className='size-[10px]' aria-hidden='true' /> |
| 54 | + </OverlayActionButton> |
| 55 | + </> |
| 56 | + ) |
| 57 | + const [previous, current] = view.querySelectorAll('button') |
| 58 | + /** The old border-1 token aliases border; class order changes when recipes are composed. */ |
| 59 | + for (const button of [previous, current]) { |
| 60 | + button.className = button.className |
| 61 | + .replaceAll('--border-1', '--border') |
| 62 | + .split(/\s+/) |
| 63 | + .sort() |
| 64 | + .join(' ') |
| 65 | + } |
| 66 | + expect(current.outerHTML).toBe(previous.outerHTML) |
| 67 | + }) |
| 68 | + |
| 69 | + it('forwards refs and native props through a tooltip and suppresses disabled clicks', () => { |
| 70 | + vi.useFakeTimers() |
| 71 | + const ref = createRef<HTMLButtonElement>() |
| 72 | + const onClick = vi.fn() |
| 73 | + const onKeyDown = vi.fn() |
| 74 | + const action = (disabled: boolean) => ( |
| 75 | + <Tooltip.Root> |
| 76 | + <Tooltip.Trigger asChild> |
| 77 | + <OverlayActionButton |
| 78 | + ref={ref} |
| 79 | + type='button' |
| 80 | + aria-label='Copy' |
| 81 | + data-action='copy' |
| 82 | + disabled={disabled} |
| 83 | + onClick={onClick} |
| 84 | + onKeyDown={onKeyDown} |
| 85 | + /> |
| 86 | + </Tooltip.Trigger> |
| 87 | + <Tooltip.Content>Copy output</Tooltip.Content> |
| 88 | + </Tooltip.Root> |
| 89 | + ) |
| 90 | + const view = mount(action(false)) |
| 91 | + const button = view.querySelector('button') |
| 92 | + if (!button) throw new Error('Button did not render') |
| 93 | + expect(view.querySelectorAll('button')).toHaveLength(1) |
| 94 | + expect(ref.current).toBe(button) |
| 95 | + expect(button.type).toBe('button') |
| 96 | + expect(button.dataset.action).toBe('copy') |
| 97 | + expect(button.getAttribute('aria-label')).toBe('Copy') |
| 98 | + act(() => |
| 99 | + button.dispatchEvent( |
| 100 | + new MouseEvent('pointerover', { bubbles: true, clientX: 200, clientY: 200 }) |
| 101 | + ) |
| 102 | + ) |
| 103 | + expect(document.querySelector('[role="tooltip"]')?.textContent).toBe('Copy output') |
| 104 | + act(() => button.focus()) |
| 105 | + expect(document.activeElement).toBe(button) |
| 106 | + const keyEvent = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }) |
| 107 | + act(() => button.dispatchEvent(keyEvent)) |
| 108 | + expect(onKeyDown).toHaveBeenCalledTimes(1) |
| 109 | + expect(onKeyDown.mock.calls[0][0].nativeEvent).toBe(keyEvent) |
| 110 | + act(() => button.click()) |
| 111 | + expect(onClick).toHaveBeenCalledTimes(1) |
| 112 | + act(() => root?.render(action(true))) |
| 113 | + expect(button.disabled).toBe(true) |
| 114 | + act(() => button.click()) |
| 115 | + expect(onClick).toHaveBeenCalledTimes(1) |
| 116 | + }) |
| 117 | +}) |
0 commit comments