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.
 
 
 
 
 

99 lines
2.9 KiB

import { describe, expect, test } from 'bun:test'
import {
buildPostBodyMarkdownEditorVditorOptions,
postBodyMarkdownEditorToolbarPresets,
} from './post-body-markdown-editor-vditor-config'
describe('PostBodyMarkdownEditor Vditor config', () => {
test('桌面端使用完整工具栏与即时渲染模式', () => {
const options = buildPostBodyMarkdownEditorVditorOptions({
value: 'hello',
isMobile: false,
onInput: () => undefined,
onUploadError: () => undefined,
})
expect(options.mode).toBe('ir')
expect(options.lang).toBe('zh_CN')
expect(options.cdn).toBe('/vditor')
expect(options.preview).toEqual({
mode: 'editor',
actions: [],
})
expect(options.toolbar).toEqual(postBodyMarkdownEditorToolbarPresets.desktop)
expect(postBodyMarkdownEditorToolbarPresets.desktop.includes('preview')).toBe(false)
expect(postBodyMarkdownEditorToolbarPresets.desktop.length).toBeGreaterThan(postBodyMarkdownEditorToolbarPresets.mobile.length)
})
test('移动端使用精简工具栏并将预览作为次级入口', () => {
const options = buildPostBodyMarkdownEditorVditorOptions({
value: 'hello',
isMobile: true,
onInput: () => undefined,
onUploadError: () => undefined,
})
expect(options.mode).toBe('ir')
expect(options.toolbar).toEqual(postBodyMarkdownEditorToolbarPresets.mobile)
expect(postBodyMarkdownEditorToolbarPresets.mobile).toEqual([
'bold',
'italic',
'headings',
'|',
'list',
'ordered-list',
'|',
'link',
'upload',
'code',
'|',
'preview',
])
})
test('上传配置会将服务端响应转换为 succMap 结构', async () => {
const options = buildPostBodyMarkdownEditorVditorOptions({
value: '',
isMobile: false,
onInput: () => undefined,
onUploadError: () => undefined,
}) as { upload?: { format?: (files: File[], responseText: string) => string } }
const files = [{ name: 'image.webp' }] as File[]
const result = options.upload?.format?.(files, JSON.stringify({
code: 0,
data: {
files: [{ url: '/public/upload/abc.webp' }],
},
}))
expect(result).toBe(JSON.stringify({
msg: '',
code: 0,
data: {
errFiles: [],
succMap: {
'image.webp': '/public/upload/abc.webp',
},
},
}))
})
test('上传配置解析失败时返回错误结构', () => {
const options = buildPostBodyMarkdownEditorVditorOptions({
value: '',
isMobile: false,
onInput: () => undefined,
onUploadError: () => undefined,
}) as { upload?: { format?: (files: File[], responseText: string) => string } }
const result = options.upload?.format?.([] as File[], 'invalid json')
expect(result).toBe(JSON.stringify({
msg: 'upload response parse failed',
code: 1,
data: {
errFiles: [],
succMap: {},
},
}))
})
})