61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
// 阶段门禁(架构 §7):阶段 0 的往返稳定性套件在正式工程上必须继续全绿。
|
||
import { describe, it, expect, afterEach } from 'vitest'
|
||
import type { Editor } from '@milkdown/kit/core'
|
||
import { createMarkdownEditor, getMarkdown, callUndo } from '../src/editor/editor'
|
||
|
||
const SAMPLE = `# 标题
|
||
|
||
包含**粗体**与任务:
|
||
|
||
- [ ] 待办
|
||
- [x] 完成
|
||
|
||
| 列一 | 列二 |
|
||
| :--- | ---: |
|
||
| 甲 | 1 |
|
||
|
||
\`\`\`ts
|
||
const n = 1;
|
||
\`\`\`
|
||
`
|
||
|
||
let editors: Editor[] = []
|
||
afterEach(async () => {
|
||
for (const e of editors) { try { await e.destroy() } catch { /* ignore */ } }
|
||
editors = []
|
||
document.body.innerHTML = ''
|
||
})
|
||
|
||
async function load(md: string): Promise<Editor> {
|
||
const root = document.createElement('div')
|
||
document.body.appendChild(root)
|
||
const editor = createMarkdownEditor(root, md)
|
||
editors.push(editor)
|
||
await editor.create()
|
||
return editor
|
||
}
|
||
|
||
describe('编辑器回归(阶段 0 套件)', () => {
|
||
it('往返保真且第二轮逐字稳定', async () => {
|
||
const once = getMarkdown(await load(SAMPLE))
|
||
expect(once).toContain('**粗体**')
|
||
expect(once).toMatch(/-\s\[ \]\s待办/)
|
||
expect(once).toMatch(/-\s\[[xX]\]\s完成/)
|
||
expect(once).toContain('甲')
|
||
const twice = getMarkdown(await load(once))
|
||
expect(twice).toBe(once)
|
||
})
|
||
|
||
it('撤销命令仍可用', async () => {
|
||
const editor = await load('原文\n')
|
||
const { editorViewCtx } = await import('@milkdown/kit/core')
|
||
editor.action((ctx) => {
|
||
const view = ctx.get(editorViewCtx)
|
||
view.dispatch(view.state.tr.insertText('X', view.state.doc.content.size))
|
||
})
|
||
expect(getMarkdown(editor)).toContain('X')
|
||
callUndo(editor)
|
||
expect(getMarkdown(editor)).not.toContain('X')
|
||
})
|
||
})
|