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.
52 lines
1.4 KiB
52 lines
1.4 KiB
import { describe, expect, test } from 'bun:test'
|
|
import { initializePostBodyMarkdownEditorVditor } from './post-body-markdown-editor-vditor-init'
|
|
|
|
function createDeferred<T>() {
|
|
let resolve!: (value: T) => void
|
|
let reject!: (reason?: unknown) => void
|
|
const promise = new Promise<T>((res, rej) => {
|
|
resolve = res
|
|
reject = rej
|
|
})
|
|
return { promise, resolve, reject }
|
|
}
|
|
|
|
describe('PostBodyMarkdownEditor Vditor init helper', () => {
|
|
test('卸载标记已置位时,import resolve 后不触发 mount', async () => {
|
|
const deferred = createDeferred<{ default: new () => unknown }>()
|
|
let unmounted = false
|
|
let readyCalls = 0
|
|
|
|
const task = initializePostBodyMarkdownEditorVditor({
|
|
importVditor: () => deferred.promise,
|
|
isUnmounted: () => unmounted,
|
|
onReady: () => {
|
|
readyCalls += 1
|
|
},
|
|
})
|
|
|
|
unmounted = true
|
|
deferred.resolve({ default: class MockVditor {} })
|
|
await task
|
|
|
|
expect(readyCalls).toBe(0)
|
|
})
|
|
|
|
test('import reject 时不抛出并走错误处理回调', async () => {
|
|
const error = new Error('mock import failed')
|
|
const handled: unknown[] = []
|
|
|
|
await initializePostBodyMarkdownEditorVditor({
|
|
importVditor: async () => {
|
|
throw error
|
|
},
|
|
isUnmounted: () => false,
|
|
onReady: () => {
|
|
throw new Error('should not call onReady')
|
|
},
|
|
onError: (err) => handled.push(err),
|
|
})
|
|
|
|
expect(handled).toEqual([error])
|
|
})
|
|
})
|
|
|