-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:cycleccc/wangEditor-next
- Loading branch information
Showing
4 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
packages/basic-modules/__tests__/format-painter/format-painter-menu.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* @description format painter menu test | ||
* @author CodePencil | ||
*/ | ||
|
||
import { Editor } from 'slate' | ||
import createEditor from '../../../../tests/utils/create-editor' | ||
import FormatPainter from '../../src/modules/format-painter/menu/FormatPainter' | ||
|
||
describe('format painter menu', () => { | ||
let editor: any | ||
let menu: any | ||
|
||
beforeEach(() => { | ||
editor = createEditor() | ||
menu = new FormatPainter() | ||
}) | ||
|
||
afterEach(() => { | ||
editor = null | ||
menu = null | ||
FormatPainter.attrs.isSelect = false | ||
FormatPainter.attrs.formatStyle = null | ||
}) | ||
|
||
it('get value', () => { | ||
expect(menu.getValue(editor)).toBe('') | ||
}) | ||
|
||
it('is active', () => { | ||
expect(menu.isActive(editor)).toBe(FormatPainter.attrs.isSelect) | ||
}) | ||
|
||
it('is disabled', () => { | ||
expect(menu.isDisabled(editor)).toBeFalsy() | ||
}) | ||
|
||
it('set format html', () => { | ||
editor.focus() | ||
editor.insertText('Hello World') | ||
|
||
// 选中文本 | ||
editor.select({ | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 5 }, | ||
}) | ||
|
||
editor.addMark('bold', true) | ||
editor.addMark('italic', true) | ||
|
||
// 选中有样式的文本后启用格式刷 | ||
menu.exec(editor) | ||
expect(FormatPainter.attrs.isSelect).toBeTruthy() | ||
|
||
// 启用了格式刷但是未选中文本 | ||
editor.deselect() | ||
menu.setFormatHtml(editor) | ||
expect(FormatPainter.attrs.isSelect).toBeTruthy() | ||
expect(FormatPainter.attrs.formatStyle).toEqual({ bold: true, italic: true }) | ||
|
||
// 启用了格式刷并选中文本 | ||
editor.select({ | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 5 }, | ||
}) | ||
menu.setFormatHtml(editor) | ||
expect(Editor.marks(editor)).toEqual({ bold: true, italic: true }) | ||
expect(FormatPainter.attrs.isSelect).toBeFalsy() | ||
expect(FormatPainter.attrs.formatStyle).toBeNull() | ||
}) | ||
|
||
it('exec', () => { | ||
editor.focus() | ||
|
||
editor.insertText('Hello World') | ||
|
||
// 取消选中文本 | ||
editor.deselect() | ||
menu.exec(editor) | ||
expect(FormatPainter.attrs.isSelect).toBeFalsy() | ||
expect(FormatPainter.attrs.formatStyle).toBeNull() | ||
|
||
// 选中文本 | ||
editor.select({ | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 5 }, | ||
}) | ||
|
||
menu.exec(editor) // 启用格式刷 | ||
expect(FormatPainter.attrs.isSelect).toBeTruthy() | ||
|
||
menu.exec(editor) // 取消格式刷 | ||
expect(FormatPainter.attrs.isSelect).toBeFalsy() | ||
|
||
// 选中文本 | ||
editor.select({ | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 5 }, | ||
}) | ||
|
||
// 选中有样式的文本后启用格式刷 | ||
editor.addMark('bold', true) | ||
editor.addMark('italic', true) | ||
|
||
menu.exec(editor) | ||
expect(FormatPainter.attrs.formatStyle).toEqual({ bold: true, italic: true }) | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
packages/basic-modules/__tests__/format-painter/helper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @description format painter helper test | ||
* @author CodePencil | ||
*/ | ||
|
||
import { Editor } from 'slate' | ||
import createEditor from '../../../../tests/utils/create-editor' | ||
import { clearAllMarks } from '../../src/modules/format-painter/helper' | ||
|
||
describe('format painter helper', () => { | ||
let editor: any | ||
let startLocation: any | ||
|
||
beforeEach(() => { | ||
editor = createEditor() | ||
startLocation = Editor.start(editor, []) | ||
}) | ||
|
||
afterEach(() => { | ||
editor = null | ||
startLocation = null | ||
}) | ||
|
||
it('clear all marks ', () => { | ||
editor.select(startLocation) | ||
|
||
editor.addMark('bold', true) | ||
editor.addMark('italic', true) | ||
|
||
expect(Editor.marks(editor)).toEqual({ bold: true, italic: true }) | ||
|
||
clearAllMarks(editor) | ||
|
||
expect(Editor.marks(editor)).toEqual({}) | ||
}) | ||
}) |
53 changes: 53 additions & 0 deletions
53
packages/basic-modules/__tests__/format-painter/plugin.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import createEditor from '../../../../tests/utils/create-editor' | ||
import withFormatPainter from '../../src/modules/format-painter/plugin' | ||
import FormatPainter from '../../src/modules/format-painter/menu/FormatPainter' | ||
|
||
describe('format painter plugin', () => { | ||
let editor: any | ||
|
||
beforeEach(() => { | ||
editor = withFormatPainter(createEditor()) | ||
|
||
jest.spyOn(document, 'addEventListener') | ||
jest.spyOn(document, 'removeEventListener') | ||
|
||
editor.focus() | ||
editor.insertText('Hello World') | ||
editor.select({ | ||
anchor: { path: [0, 0], offset: 0 }, | ||
focus: { path: [0, 0], offset: 5 }, | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('format painter change', () => { | ||
// 模拟 FormatPainter 处于活动状态 | ||
FormatPainter.attrs.isSelect = true | ||
|
||
// 监控静态方法 setFormatHtml | ||
const setFormatHtmlSpy = jest.spyOn(FormatPainter.prototype, 'setFormatHtml') | ||
|
||
// 模拟 onChange 的调用 | ||
editor.onChange() | ||
|
||
// 检查是否绑定了 mouseup 事件 | ||
expect(document.addEventListener).toHaveBeenCalledWith('mouseup', expect.any(Function)) | ||
|
||
// 创建并触发 mouseup 事件 | ||
const mouseUpEvent = new MouseEvent('mouseup', { | ||
bubbles: true, | ||
cancelable: true, | ||
}) | ||
|
||
document.dispatchEvent(mouseUpEvent) | ||
|
||
// 验证 setFormatHtml 是否被调用 | ||
expect(setFormatHtmlSpy).toHaveBeenCalledWith(editor) | ||
|
||
// 检查是否解绑了 mouseup 事件 | ||
expect(document.removeEventListener).toHaveBeenCalledWith('mouseup', expect.any(Function)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters