Skip to content

Commit 7b5fec7

Browse files
committed
test(code highlight): add more tests
1 parent 53bee8d commit 7b5fec7

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

packages/code-highlight/__tests__/decorate.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IDomEditor } from '@wangeditor-next/core'
77
import createEditor from '../../../tests/utils/create-editor'
88
import codeHighLightDecorate from '../src/decorate/index'
99
import { content, textNode, textNodePath } from './content'
10+
import { getPrismTokenLength } from '../src/vendor/prism'
1011

1112
describe('code-highlight decorate', () => {
1213
let editor: IDomEditor | null = null
@@ -29,4 +30,18 @@ describe('code-highlight decorate', () => {
2930
const ranges = codeHighLightDecorate([textNode, textNodePath])
3031
expect(ranges.length).toBe(4) // 把 textNode 内容拆分为 4 段
3132
})
33+
34+
it('getPrismTokenLength', () => {
35+
const token = {
36+
type: 'example',
37+
content: [
38+
'hello', // length 5
39+
{ type: 'nested', content: 'world' }, // length 5
40+
{ type: 'nested', content: ['foo', { type: 'deepNested', content: 'bar' }] }, // length 3 + 3 = 6
41+
],
42+
}
43+
44+
const result = getPrismTokenLength(token)
45+
expect(result).toBe(16) // 'hello' (5) + 'world' (5) + 'foo' (3) + 'bar' (3) = 16
46+
})
3247
})

packages/code-highlight/__tests__/elem-to-html.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { IDomEditor } from '@wangeditor-next/core'
77
import createEditor from '../../../tests/utils/create-editor'
88
import { codeToHtmlConf } from '../src/module/elem-to-html'
9-
import { content, codeNode, language } from './content'
9+
import { content, codeNode, preNode, language } from './content'
1010

1111
describe('code-highlight elem to html', () => {
1212
let editor: IDomEditor | null = null
@@ -30,7 +30,9 @@ describe('code-highlight elem to html', () => {
3030

3131
if (editor == null) throw new Error('editor is null')
3232
const text = 'var n = 100;'
33-
const html = codeToHtmlConf.elemToHtml(codeNode, text)
33+
let html = codeToHtmlConf.elemToHtml(codeNode, text)
3434
expect(html).toBe(`<code class="language-${language}">${text}</code>`)
35+
html = codeToHtmlConf.elemToHtml(preNode, text)
36+
expect(html).toBe(`<code >${text}</code>`)
3537
})
3638
})

packages/code-highlight/__tests__/select-lang-menu.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ describe('code-highlight select lang menu', () => {
103103
done()
104104
})
105105
})
106+
107+
it('menu exec (without lang)', done => {
108+
if (editor == null || menu == null) throw new Error('editor or menu is null')
109+
110+
// select codeNode
111+
editor.select(codeLocation)
112+
menu.exec(editor, 'hello') // change lang
113+
114+
setTimeout(() => {
115+
if (editor == null || menu == null) return
116+
117+
editor.select(codeLocation)
118+
expect(menu.getValue(editor)).toBe('')
119+
done()
120+
})
121+
})
106122
})

packages/code-highlight/src/utils/vdom.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author wangfupeng
44
*/
55

6-
import { VNode, VNodeStyle } from 'snabbdom'
6+
import { VNode } from 'snabbdom'
77

88
/**
99
* 给 vnode 添加 className
@@ -17,16 +17,3 @@ export function addVnodeClassName(vnode: VNode, className: string) {
1717

1818
Object.assign(data.props, { className })
1919
}
20-
21-
/**
22-
* 给 vnode 添加样式
23-
* @param vnode vnode
24-
* @param newStyle { key: val }
25-
*/
26-
export function addVnodeStyle(vnode: VNode, newStyle: VNodeStyle) {
27-
if (vnode.data == null) vnode.data = {}
28-
const data = vnode.data
29-
if (data.style == null) data.style = {}
30-
31-
Object.assign(data.style, newStyle)
32-
}

0 commit comments

Comments
 (0)