41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
// 源码模式(CodeMirror 6)冒烟:能挂载、锚点 API 可用、改动经 onChange 回传。
|
||
import { describe, it, expect, afterEach } from 'vitest'
|
||
import { createRoot, type Root } from 'react-dom/client'
|
||
import { act } from 'react-dom/test-utils'
|
||
import SourceEditor, { type SourceEditorApi } from '../src/components/SourceEditor'
|
||
|
||
let root: Root | null = null
|
||
afterEach(() => {
|
||
act(() => root?.unmount())
|
||
root = null
|
||
document.body.innerHTML = ''
|
||
})
|
||
|
||
describe('SourceEditor', () => {
|
||
it('挂载 CodeMirror 并提供锚点/跳行 API', async () => {
|
||
const md = '# 标题\n\n正文段落。\n'
|
||
let api: SourceEditorApi | null = null
|
||
const changes: string[] = []
|
||
const container = document.createElement('div')
|
||
document.body.appendChild(container)
|
||
root = createRoot(container)
|
||
await act(async () => {
|
||
root!.render(
|
||
<SourceEditor
|
||
tabId="t1"
|
||
initialText={md}
|
||
initialCursorOffset={md.indexOf('正文')}
|
||
dark={false}
|
||
onChange={(t) => changes.push(t)}
|
||
onReady={(a) => { api = a }}
|
||
/>,
|
||
)
|
||
})
|
||
expect(container.querySelector('.cm-editor')).not.toBeNull()
|
||
expect(api).not.toBeNull()
|
||
expect(api!.getAnchor()).toEqual({ blockText: '正文段落。', offsetInBlock: 0 })
|
||
act(() => api!.jumpToLine(0))
|
||
expect(api!.getAnchor()?.blockText).toBe('标题')
|
||
})
|
||
})
|