50 lines
2.0 KiB
TypeScript
50 lines
2.0 KiB
TypeScript
// 编辑区右键菜单:条目随模式/选区变化,点击回传动作并关闭。
|
|
import { describe, it, expect, vi } from 'vitest'
|
|
import { createRoot } from 'react-dom/client'
|
|
import { act } from 'react-dom/test-utils'
|
|
import EditorContextMenu from '../src/components/EditorContextMenu'
|
|
|
|
async function renderMenu(props: Record<string, unknown> = {}) {
|
|
const container = document.createElement('div')
|
|
document.body.appendChild(container)
|
|
const root = createRoot(container)
|
|
const actions: string[] = []
|
|
let closed = 0
|
|
await act(async () => {
|
|
root.render(
|
|
<EditorContextMenu
|
|
x={10} y={10} hasSelection={false} sourceMode={false}
|
|
onAction={(a) => actions.push(a)}
|
|
onClose={() => { closed += 1 }}
|
|
{...props}
|
|
/>,
|
|
)
|
|
})
|
|
return { container, root, actions, closedCount: () => closed }
|
|
}
|
|
|
|
const btn = (c: HTMLElement, text: string) => [...c.querySelectorAll('button')].find((b) => b.textContent === text)
|
|
|
|
describe('编辑区右键菜单', () => {
|
|
it('无选区时剪切/复制禁用,点加粗回传 bold 并关闭', async () => {
|
|
const { container, root, actions, closedCount } = await renderMenu()
|
|
expect(btn(container, '剪切')?.disabled).toBe(true)
|
|
expect(btn(container, '复制')?.disabled).toBe(true)
|
|
expect(btn(container, '粘贴')?.disabled).toBe(false)
|
|
await act(async () => { btn(container, '加粗')?.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
|
|
expect(actions).toEqual(['bold'])
|
|
expect(closedCount()).toBe(1)
|
|
await act(async () => { root.unmount() })
|
|
container.remove()
|
|
})
|
|
|
|
it('源码模式只出编辑命令与「即时渲染」', async () => {
|
|
const { container, root } = await renderMenu({ sourceMode: true, hasSelection: true })
|
|
expect(btn(container, '加粗')).toBeUndefined()
|
|
expect(btn(container, '即时渲染')).toBeTruthy()
|
|
expect(btn(container, '剪切')?.disabled).toBe(false)
|
|
await act(async () => { root.unmount() })
|
|
container.remove()
|
|
})
|
|
})
|