From 01dca9d081682fcdce7e92f60f070c28cb129cdf Mon Sep 17 00:00:00 2001
From: cycleccc <2991205548@qq.com>
Date: Sat, 17 Aug 2024 14:11:04 +0800
Subject: [PATCH 1/5] test(justify): add more tests
add more justify tests and delete unuse code
---
.../__tests__/justify/menus.test.ts | 13 ++++++++--
.../src/modules/justify/menu/BaseMenu.ts | 24 -------------------
2 files changed, 11 insertions(+), 26 deletions(-)
diff --git a/packages/basic-modules/__tests__/justify/menus.test.ts b/packages/basic-modules/__tests__/justify/menus.test.ts
index 8101df1fc..e007baf5f 100644
--- a/packages/basic-modules/__tests__/justify/menus.test.ts
+++ b/packages/basic-modules/__tests__/justify/menus.test.ts
@@ -29,7 +29,13 @@ describe('justify menus', () => {
startLocation = null
})
- // getValue getActive 不需要测试
+ it('get value', () => {
+ expect(centerMenu.getValue(editor)).toBe('')
+ })
+
+ it('is active', () => {
+ expect(centerMenu.isActive(editor)).toBeFalsy()
+ })
it('is disabled', () => {
editor.deselect()
@@ -40,7 +46,10 @@ describe('justify menus', () => {
editor.insertNode({ type: 'pre', children: [{ type: 'code', children: [{ text: 'var' }] }] })
expect(centerMenu.isDisabled(editor)).toBeTruthy()
- // Transforms.removeNodes(editor, { mode: 'highest' }) // 移除 pre/code
+ Transforms.removeNodes(editor, { mode: 'highest' })
+
+ editor.insertNode({ type: 'divider', children: [{ text: '' }] })
+ expect(centerMenu.isDisabled(editor)).toBeTruthy()
})
it('exec', () => {
diff --git a/packages/basic-modules/src/modules/justify/menu/BaseMenu.ts b/packages/basic-modules/src/modules/justify/menu/BaseMenu.ts
index decb8279c..1be8e0f5a 100644
--- a/packages/basic-modules/src/modules/justify/menu/BaseMenu.ts
+++ b/packages/basic-modules/src/modules/justify/menu/BaseMenu.ts
@@ -21,30 +21,6 @@ abstract class BaseMenu implements IButtonMenu {
return false
}
- /**
- * 获取 node 节点
- * @param editor editor
- */
- protected getMatchNode(editor: IDomEditor): Node | null {
- const [nodeEntry] = Editor.nodes(editor, {
- match: n => {
- const type = DomEditor.getNodeType(n)
-
- // 只可用于 p blockquote header
- if (type === 'paragraph') return true
- if (type === 'blockquote') return true
- if (type.startsWith('header')) return true
-
- return false
- },
- universal: true,
- mode: 'highest', // 匹配最高层级
- })
-
- if (nodeEntry == null) return null
- return nodeEntry[0]
- }
-
isDisabled(editor: IDomEditor): boolean {
if (editor.selection == null) return true
From fb2cc6fdb067b65a908c4ab379fd0a31942ce4b8 Mon Sep 17 00:00:00 2001
From: cycleccc <2991205548@qq.com>
Date: Sat, 17 Aug 2024 14:59:51 +0800
Subject: [PATCH 2/5] test(enter): add enter menu tests
---
.../__tests__/common/enter-menu.test.ts | 53 +++++++++++++++++++
1 file changed, 53 insertions(+)
create mode 100644 packages/basic-modules/__tests__/common/enter-menu.test.ts
diff --git a/packages/basic-modules/__tests__/common/enter-menu.test.ts b/packages/basic-modules/__tests__/common/enter-menu.test.ts
new file mode 100644
index 000000000..c54662405
--- /dev/null
+++ b/packages/basic-modules/__tests__/common/enter-menu.test.ts
@@ -0,0 +1,53 @@
+/**
+ * @description enter menu test
+ * @author cycleccc
+ */
+
+import { Editor, Transforms } from 'slate'
+import createEditor from '../../../../tests/utils/create-editor'
+import EnterMenu from '../../src/modules/common/menu/EnterMenu'
+
+describe('enter menu', () => {
+ const menu = new EnterMenu()
+ let editor: any
+ let startLocation: any
+
+ beforeEach(() => {
+ editor = createEditor()
+ startLocation = Editor.start(editor, [])
+ })
+
+ afterEach(() => {
+ editor = null
+ startLocation = null
+ })
+
+ it('get value', () => {
+ expect(menu.getValue(editor)).toBe('')
+ })
+
+ it('is active', () => {
+ expect(menu.isActive(editor)).toBeFalsy()
+ })
+
+ it('is disabled', () => {
+ editor.deselect()
+ expect(menu.isDisabled(editor)).toBeTruthy()
+ editor.select(startLocation)
+ expect(menu.isDisabled(editor)).toBeFalsy()
+ editor.insertText('hello')
+ editor.select([])
+ expect(menu.isDisabled(editor)).toBeTruthy()
+ })
+
+ it('exec', () => {
+ editor.deselect()
+ expect(menu.exec(editor, '')).toBeUndefined()
+ editor.select(startLocation)
+ menu.exec(editor, '')
+ expect(editor.children).toStrictEqual([
+ { children: [{ text: '' }], type: 'paragraph' },
+ { children: [{ text: '' }], type: 'paragraph' },
+ ])
+ })
+})
From 53bee8d23672a95101ffc354de2b296a6e6b87be Mon Sep 17 00:00:00 2001
From: cycleccc <2991205548@qq.com>
Date: Sat, 17 Aug 2024 15:12:00 +0800
Subject: [PATCH 3/5] test(utils): add more dom tests
---
.../basic-modules/__tests__/utils/dom.test.ts | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 packages/basic-modules/__tests__/utils/dom.test.ts
diff --git a/packages/basic-modules/__tests__/utils/dom.test.ts b/packages/basic-modules/__tests__/utils/dom.test.ts
new file mode 100644
index 000000000..3e11b0377
--- /dev/null
+++ b/packages/basic-modules/__tests__/utils/dom.test.ts
@@ -0,0 +1,17 @@
+/**
+ * @description utils dom
+ * @author cycleccc
+ */
+
+import { getTagName } from '../../src/utils/dom'
+import { Dom7Array } from 'dom7'
+
+describe('redo menu', () => {
+ it('get tag name', () => {
+ // 模拟一个空的 Dom7Array 对象
+ const emptyElem: Dom7Array = [] as unknown as Dom7Array
+
+ const result = getTagName(emptyElem)
+ expect(result).toBe('') // 验证返回的是空字符串
+ })
+})
From 7b5fec7992ba9a3a8fc90b09bd5547f38e58b9b5 Mon Sep 17 00:00:00 2001
From: cycleccc <2991205548@qq.com>
Date: Sat, 17 Aug 2024 15:54:57 +0800
Subject: [PATCH 4/5] test(code highlight): add more tests
---
.../code-highlight/__tests__/decorate.test.ts | 15 +++++++++++++++
.../__tests__/elem-to-html.test.ts | 6 ++++--
.../__tests__/select-lang-menu.test.ts | 16 ++++++++++++++++
packages/code-highlight/src/utils/vdom.ts | 15 +--------------
4 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/packages/code-highlight/__tests__/decorate.test.ts b/packages/code-highlight/__tests__/decorate.test.ts
index f9f02c149..a34b4efc7 100644
--- a/packages/code-highlight/__tests__/decorate.test.ts
+++ b/packages/code-highlight/__tests__/decorate.test.ts
@@ -7,6 +7,7 @@ import { IDomEditor } from '@wangeditor-next/core'
import createEditor from '../../../tests/utils/create-editor'
import codeHighLightDecorate from '../src/decorate/index'
import { content, textNode, textNodePath } from './content'
+import { getPrismTokenLength } from '../src/vendor/prism'
describe('code-highlight decorate', () => {
let editor: IDomEditor | null = null
@@ -29,4 +30,18 @@ describe('code-highlight decorate', () => {
const ranges = codeHighLightDecorate([textNode, textNodePath])
expect(ranges.length).toBe(4) // 把 textNode 内容拆分为 4 段
})
+
+ it('getPrismTokenLength', () => {
+ const token = {
+ type: 'example',
+ content: [
+ 'hello', // length 5
+ { type: 'nested', content: 'world' }, // length 5
+ { type: 'nested', content: ['foo', { type: 'deepNested', content: 'bar' }] }, // length 3 + 3 = 6
+ ],
+ }
+
+ const result = getPrismTokenLength(token)
+ expect(result).toBe(16) // 'hello' (5) + 'world' (5) + 'foo' (3) + 'bar' (3) = 16
+ })
})
diff --git a/packages/code-highlight/__tests__/elem-to-html.test.ts b/packages/code-highlight/__tests__/elem-to-html.test.ts
index c164eb876..0ac576ab9 100644
--- a/packages/code-highlight/__tests__/elem-to-html.test.ts
+++ b/packages/code-highlight/__tests__/elem-to-html.test.ts
@@ -6,7 +6,7 @@
import { IDomEditor } from '@wangeditor-next/core'
import createEditor from '../../../tests/utils/create-editor'
import { codeToHtmlConf } from '../src/module/elem-to-html'
-import { content, codeNode, language } from './content'
+import { content, codeNode, preNode, language } from './content'
describe('code-highlight elem to html', () => {
let editor: IDomEditor | null = null
@@ -30,7 +30,9 @@ describe('code-highlight elem to html', () => {
if (editor == null) throw new Error('editor is null')
const text = 'var n = 100;'
- const html = codeToHtmlConf.elemToHtml(codeNode, text)
+ let html = codeToHtmlConf.elemToHtml(codeNode, text)
expect(html).toBe(`${text}
`)
+ html = codeToHtmlConf.elemToHtml(preNode, text)
+ expect(html).toBe(`${text}
`)
})
})
diff --git a/packages/code-highlight/__tests__/select-lang-menu.test.ts b/packages/code-highlight/__tests__/select-lang-menu.test.ts
index 81f1b2e6a..fdf1dfd18 100644
--- a/packages/code-highlight/__tests__/select-lang-menu.test.ts
+++ b/packages/code-highlight/__tests__/select-lang-menu.test.ts
@@ -103,4 +103,20 @@ describe('code-highlight select lang menu', () => {
done()
})
})
+
+ it('menu exec (without lang)', done => {
+ if (editor == null || menu == null) throw new Error('editor or menu is null')
+
+ // select codeNode
+ editor.select(codeLocation)
+ menu.exec(editor, 'hello') // change lang
+
+ setTimeout(() => {
+ if (editor == null || menu == null) return
+
+ editor.select(codeLocation)
+ expect(menu.getValue(editor)).toBe('')
+ done()
+ })
+ })
})
diff --git a/packages/code-highlight/src/utils/vdom.ts b/packages/code-highlight/src/utils/vdom.ts
index 3a6cecad0..2be81d2c9 100644
--- a/packages/code-highlight/src/utils/vdom.ts
+++ b/packages/code-highlight/src/utils/vdom.ts
@@ -3,7 +3,7 @@
* @author wangfupeng
*/
-import { VNode, VNodeStyle } from 'snabbdom'
+import { VNode } from 'snabbdom'
/**
* 给 vnode 添加 className
@@ -17,16 +17,3 @@ export function addVnodeClassName(vnode: VNode, className: string) {
Object.assign(data.props, { className })
}
-
-/**
- * 给 vnode 添加样式
- * @param vnode vnode
- * @param newStyle { key: val }
- */
-export function addVnodeStyle(vnode: VNode, newStyle: VNodeStyle) {
- if (vnode.data == null) vnode.data = {}
- const data = vnode.data
- if (data.style == null) data.style = {}
-
- Object.assign(data.style, newStyle)
-}
From e37a39194f6bf1e81d4b7345ef73fde23c2d739b Mon Sep 17 00:00:00 2001
From: cycleccc <2991205548@qq.com>
Date: Sat, 17 Aug 2024 17:21:20 +0800
Subject: [PATCH 5/5] test(editor): add more tests
---
packages/editor/__tests__/create.test.ts | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/packages/editor/__tests__/create.test.ts b/packages/editor/__tests__/create.test.ts
index 8f6240ea9..411bf2335 100644
--- a/packages/editor/__tests__/create.test.ts
+++ b/packages/editor/__tests__/create.test.ts
@@ -5,6 +5,8 @@
import { createEditor, createToolbar } from '../../../packages/editor/src/index'
import { ICreateEditorOption, ICreateToolbarOption } from '../../../packages/editor/src/create'
+import { DOMElement } from '../../../packages/editor/src/utils/dom'
+import Boot from '../../../packages/editor/src/Boot'
function customCreateEditor(config: Partial = {}) {
const editorContainer = document.createElement('div')
@@ -29,7 +31,7 @@ function customCreateToolbar(config: Partial = {}) {
// create toolbar
const toolbar = createToolbar({
editor,
- selector: toolbarContainer,
+ selector: toolbarContainer as DOMElement,
...config,
})
@@ -37,6 +39,22 @@ function customCreateToolbar(config: Partial = {}) {
}
describe('create editor and toolbar', () => {
+ test('create editor selector undefind', () => {
+ const editor = customCreateEditor()
+ expect(() => {
+ createToolbar({
+ editor,
+ selector: undefined as any,
+ })
+ }).toThrow(`Cannot find 'selector' when create toolbar`)
+ })
+
+ test('test new Boot and registerModule', () => {
+ expect(() => {
+ new Boot()
+ }).toThrow('不能实例化\nCan not construct a instance')
+ })
+
test('create editor with default mode', () => {
const editor = customCreateEditor()