gongxiangp2/lib/__tests__/searchable-user-select.test.tsx
2026-07-30 14:39:18 +08:00

55 lines
1.6 KiB
TypeScript

/** @jest-environment jsdom */
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import SearchableUserSelect from '@/components/SearchableUserSelect'
describe('SearchableUserSelect', () => {
const users = [
{ id: 'user-1', username: 'moli', displayName: '魔礼红' },
{ id: 'user-2', username: 'wuxie', displayName: '无邪' },
]
test('支持拼音搜索、键盘选择与标准 combobox 语义', async () => {
const onChange = jest.fn()
const user = userEvent.setup()
render(
<SearchableUserSelect
users={users}
value=""
onChange={onChange}
/>,
)
const combobox = screen.getByRole('combobox')
expect(combobox).toHaveAttribute('aria-expanded', 'false')
await user.click(combobox)
await user.type(combobox, 'molihong')
expect(combobox).toHaveAttribute('aria-expanded', 'true')
expect(screen.getByRole('option', { name: /魔礼红/ })).toBeInTheDocument()
expect(screen.queryByRole('option', { name: /无邪/ })).not.toBeInTheDocument()
await user.keyboard('{Enter}')
expect(onChange).toHaveBeenCalledWith('user-1', '魔礼红')
})
test('加载期间保留已选人员并禁用交互', () => {
render(
<SearchableUserSelect
users={users}
value="user-2"
onChange={jest.fn()}
loading
/>,
)
const combobox = screen.getByRole('combobox')
expect(combobox).toBeDisabled()
expect(combobox).toHaveValue('无邪 (wuxie)')
expect(combobox).toHaveAttribute('aria-busy', 'true')
})
})