|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, useState } from 'react' |
| 3 | +import { IconSwitch } from '@sim/emcn' |
| 4 | +import { Code, List } from '@sim/emcn/icons' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const OPTIONS = [ |
| 9 | + { value: 'selector', label: 'Selector', icon: List }, |
| 10 | + { value: 'variable', label: 'Variable', icon: Code }, |
| 11 | +] as const |
| 12 | + |
| 13 | +interface HarnessProps { |
| 14 | + disabled?: boolean |
| 15 | + showTooltips?: boolean |
| 16 | + onValueChange: (value: string) => void |
| 17 | +} |
| 18 | + |
| 19 | +function Harness({ disabled, showTooltips, onValueChange }: HarnessProps) { |
| 20 | + const [value, setValue] = useState('selector') |
| 21 | + return ( |
| 22 | + <IconSwitch |
| 23 | + options={OPTIONS} |
| 24 | + value={value} |
| 25 | + onValueChange={(nextValue) => { |
| 26 | + setValue(nextValue) |
| 27 | + onValueChange(nextValue) |
| 28 | + }} |
| 29 | + disabled={disabled} |
| 30 | + showTooltips={showTooltips} |
| 31 | + aria-label='Input mode' |
| 32 | + /> |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +let root: Root | null = null |
| 37 | +let container: HTMLDivElement |
| 38 | + |
| 39 | +beforeEach(() => { |
| 40 | + vi.useFakeTimers() |
| 41 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 42 | + container = document.createElement('div') |
| 43 | + document.body.appendChild(container) |
| 44 | + root = createRoot(container) |
| 45 | +}) |
| 46 | + |
| 47 | +afterEach(() => { |
| 48 | + act(() => root?.unmount()) |
| 49 | + container.remove() |
| 50 | + root = null |
| 51 | + vi.useRealTimers() |
| 52 | +}) |
| 53 | + |
| 54 | +function mount(props: HarnessProps) { |
| 55 | + act(() => root?.render(<Harness {...props} />)) |
| 56 | + return { |
| 57 | + inputs: [...container.querySelectorAll('input')], |
| 58 | + labels: [...container.querySelectorAll('label')], |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +describe('IconSwitch', () => { |
| 63 | + it('selects either mode without toggling an already selected choice', () => { |
| 64 | + const onValueChange = vi.fn() |
| 65 | + const { inputs, labels } = mount({ onValueChange }) |
| 66 | + |
| 67 | + act(() => labels[1].click()) |
| 68 | + expect(inputs.map((input) => input.checked)).toEqual([false, true]) |
| 69 | + expect(onValueChange).toHaveBeenLastCalledWith('variable') |
| 70 | + |
| 71 | + act(() => labels[1].click()) |
| 72 | + expect(onValueChange).toHaveBeenCalledTimes(1) |
| 73 | + |
| 74 | + act(() => labels[0].click()) |
| 75 | + expect(inputs.map((input) => input.checked)).toEqual([true, false]) |
| 76 | + expect(onValueChange).toHaveBeenLastCalledWith('selector') |
| 77 | + }) |
| 78 | + |
| 79 | + it('prevents selection changes while disabled', () => { |
| 80 | + const onValueChange = vi.fn() |
| 81 | + const { inputs, labels } = mount({ disabled: true, onValueChange }) |
| 82 | + |
| 83 | + act(() => labels[1].click()) |
| 84 | + expect(onValueChange).not.toHaveBeenCalled() |
| 85 | + expect(inputs.every((input) => input.disabled)).toBe(true) |
| 86 | + expect(inputs.map((input) => input.checked)).toEqual([true, false]) |
| 87 | + }) |
| 88 | + |
| 89 | + it('shows each option label in its hover tooltip', () => { |
| 90 | + const { inputs } = mount({ showTooltips: true, onValueChange: vi.fn() }) |
| 91 | + |
| 92 | + for (const [index, option] of OPTIONS.entries()) { |
| 93 | + act(() => { |
| 94 | + inputs[index].dispatchEvent( |
| 95 | + new MouseEvent('pointerover', { bubbles: true, clientX: 200, clientY: 200 }) |
| 96 | + ) |
| 97 | + }) |
| 98 | + expect(document.querySelector('[role="tooltip"]')?.textContent).toBe(option.label) |
| 99 | + act(() => inputs[index].dispatchEvent(new MouseEvent('pointerout', { bubbles: true }))) |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + it('shows a tooltip on keyboard focus without changing selection', () => { |
| 104 | + const onValueChange = vi.fn() |
| 105 | + const { inputs } = mount({ showTooltips: true, onValueChange }) |
| 106 | + |
| 107 | + act(() => inputs[1].focus()) |
| 108 | + expect(document.querySelector('[role="tooltip"]')?.textContent).toBe('Variable') |
| 109 | + expect(onValueChange).not.toHaveBeenCalled() |
| 110 | + expect(inputs.map((input) => input.checked)).toEqual([true, false]) |
| 111 | + }) |
| 112 | +}) |
0 commit comments