// 图片链接弹窗(docs/31 更正):打开就带出图片地址,包裹链接在第二字段。 import { describe, it, expect, vi } from 'vitest' import { createRoot } from 'react-dom/client' import { act } from 'react-dom/test-utils' import { ImageLinkModal } from '../src/components/Modals' function setInput(input: HTMLInputElement, value: string) { const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set setter?.call(input, value) input.dispatchEvent(new Event('input', { bubbles: true })) } describe('ImageLinkModal', () => { it('预填图片地址与已有包裹链接;改地址后提交回传两者', async () => { const container = document.createElement('div') document.body.appendChild(container) const root = createRoot(container) const onSubmit = vi.fn() await act(async () => { root.render( undefined} />) }) const inputs = container.querySelectorAll('.modal-input') expect(inputs.length).toBe(2) expect(inputs[0].value).toBe('doc.assets/a.png') expect(inputs[1].value).toBe('https://x.dev') await act(async () => { setInput(inputs[0], 'https://cdn.example.com/a.png') }) const ok = [...container.querySelectorAll('button')].find((b) => b.textContent === '确定')! await act(async () => { ok.dispatchEvent(new MouseEvent('click', { bubbles: true })) }) expect(onSubmit).toHaveBeenCalledWith({ src: 'https://cdn.example.com/a.png', linkHref: 'https://x.dev' }) root.unmount(); container.remove() }) it('无包裹链接时第二字段为空', async () => { const container = document.createElement('div') document.body.appendChild(container) const root = createRoot(container) await act(async () => { root.render( undefined} onCancel={() => undefined} />) }) const inputs = container.querySelectorAll('.modal-input') expect(inputs[0].value).toBe('https://img.example.com/b.png') expect(inputs[1].value).toBe('') root.unmount(); container.remove() }) })