You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.9 KiB
107 lines
2.9 KiB
import { describe, expect, test } from 'bun:test'
|
|
import { createPostBodyMarkdownEditorBridge } from './post-body-markdown-editor-vditor-bridge'
|
|
|
|
describe('PostBodyMarkdownEditor Vditor bridge', () => {
|
|
test('内容变化时触发 update:modelValue', () => {
|
|
let modelValue = 'hello'
|
|
const updates: string[] = []
|
|
let onInput: ((value: string) => void) | null = null
|
|
|
|
const bridge = createPostBodyMarkdownEditorBridge({
|
|
getModelValue: () => modelValue,
|
|
emitUpdate: (value) => updates.push(value),
|
|
createEditor: ({ onInput: input }) => {
|
|
onInput = input
|
|
return {
|
|
getValue: () => modelValue,
|
|
setValue: () => undefined,
|
|
destroy: () => undefined,
|
|
}
|
|
},
|
|
})
|
|
|
|
bridge.mount({} as HTMLElement)
|
|
onInput?.('hello world')
|
|
|
|
expect(updates).toEqual(['hello world'])
|
|
})
|
|
|
|
test('props 变化可同步到实例 setValue', () => {
|
|
let modelValue = 'a'
|
|
const setCalls: Array<{ value: string; render?: boolean }> = []
|
|
let editorValue = modelValue
|
|
|
|
const bridge = createPostBodyMarkdownEditorBridge({
|
|
getModelValue: () => modelValue,
|
|
emitUpdate: () => undefined,
|
|
createEditor: () => ({
|
|
getValue: () => editorValue,
|
|
setValue: (value, render) => {
|
|
setCalls.push({ value, render })
|
|
editorValue = value
|
|
},
|
|
destroy: () => undefined,
|
|
}),
|
|
})
|
|
|
|
bridge.mount({} as HTMLElement)
|
|
modelValue = 'b'
|
|
bridge.syncFromProps()
|
|
bridge.syncFromProps()
|
|
|
|
expect(setCalls).toEqual([{ value: 'b', render: true }])
|
|
})
|
|
|
|
test('setValue 抛错后可恢复同步状态并继续响应 input', () => {
|
|
let modelValue = 'a'
|
|
const updates: string[] = []
|
|
let onInput: ((value: string) => void) | null = null
|
|
let editorValue = modelValue
|
|
|
|
const bridge = createPostBodyMarkdownEditorBridge({
|
|
getModelValue: () => modelValue,
|
|
emitUpdate: (value) => updates.push(value),
|
|
createEditor: ({ onInput: input }) => {
|
|
onInput = input
|
|
return {
|
|
getValue: () => editorValue,
|
|
setValue: () => {
|
|
throw new Error('setValue failed')
|
|
},
|
|
destroy: () => undefined,
|
|
}
|
|
},
|
|
})
|
|
|
|
bridge.mount({} as HTMLElement)
|
|
modelValue = 'b'
|
|
expect(() => bridge.syncFromProps()).toThrow('setValue failed')
|
|
|
|
onInput?.('c')
|
|
|
|
expect(updates).toEqual(['c'])
|
|
})
|
|
|
|
test('卸载时销毁实例', () => {
|
|
let destroyed = 0
|
|
|
|
const bridge = createPostBodyMarkdownEditorBridge({
|
|
getModelValue: () => '',
|
|
emitUpdate: () => undefined,
|
|
createEditor: () => ({
|
|
getValue: () => '',
|
|
setValue: () => undefined,
|
|
destroy: () => {
|
|
destroyed += 1
|
|
},
|
|
}),
|
|
})
|
|
|
|
bridge.mount({} as HTMLElement)
|
|
bridge.unmount()
|
|
bridge.unmount()
|
|
|
|
expect(destroyed).toBe(1)
|
|
expect(bridge.getEditor()).toBeNull()
|
|
})
|
|
})
|
|
|