Files
apes-sidebar/tests/toggle-switch.spec.tsx
T
Menghuan1918 daf0329666 refactor(ui): 外壳与残余件接入统一组件层(B4)
- Switch.tsx 删除,唯一消费者 native/guide-order.tsx 改用 ui 的 ToggleSwitch
  (native checkbox → button role="switch",受控写回契约不变:下一次值仍由 prop 推导)。
- tests/switch.spec.tsx 改名 tests/toggle-switch.spec.tsx,断言改 aria-checked;
  native-guide 两处开关查询同步改选择器。
- sidebar/dock-fallback.tsx 与 split-pane.tsx 判断不接入:前者只有定位/让位逻辑
  与 BottomDockToggle 包壳,后者是纯布局与 pointer 拖拽手柄,统一层没有语义对应件。
- 收敛文档补 B4 实施记录,并把映射表中这两行改为「判断不接入」及理由。
2026-11-25 01:05:35 +08:00

44 lines
1.7 KiB
TypeScript

// @vitest-environment jsdom
/**
* The shared toggle switch (ui/controls `ToggleSwitch`): a button with
* `role="switch"` whose next value derives from the controlled PROP. The
* parent's write is asynchronous, so a switch turned off must be able to turn
* back on even when the controlled value is reset before React's handler runs
* (the failure the old custom checkbox switch was built to avoid).
*/
import { useState } from 'react'
import { describe, expect, it } from 'vitest'
import { act } from 'react-dom/test-utils'
import { ToggleSwitch } from '../src/client/ui/index.ts'
import { renderRoot, setupReactAct } from './test-utils.ts'
setupReactAct()
function Controlled({ initial = true, onChange }: { initial?: boolean; onChange?: (value: boolean) => void }) {
const [on, setOn] = useState(initial)
return (
<ToggleSwitch
aria-label="test switch"
checked={on}
onChange={(next) => { setOn(next); onChange?.(next) }}
/>
)
}
describe('ToggleSwitch', () => {
it('turns off and back on even when the parent write is asynchronous', () => {
const changes: boolean[] = []
const mounted = renderRoot(<Controlled onChange={value => changes.push(value)} />)
const toggle = mounted.container.querySelector('[role="switch"]') as HTMLElement
expect(toggle.getAttribute('aria-checked')).toBe('true')
act(() => { toggle.click() })
expect(toggle.getAttribute('aria-checked')).toBe('false')
act(() => { toggle.click() })
expect(toggle.getAttribute('aria-checked')).toBe('true')
act(() => { toggle.click() })
expect(toggle.getAttribute('aria-checked')).toBe('false')
expect(changes).toEqual([false, true, false])
mounted.unmount()
})
})