wangEditor/test/unit/text/tab-to-space.test.ts

162 lines
4.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @description editor.text event-hooks tab-to-space test
* @author luochao
*/
import tabHandler from '../../../src/text/event-hooks/tab-to-space'
import $ from '../../../src/utils/dom-core'
import createEditor, { selector } from '../../helpers/create-editor'
import mockCommand from '../../helpers/command-mock'
import Editor from '../../../src/editor'
describe('editor.text event-hooks tab-to-space test', () => {
let editor: Editor
beforeEach(() => {
editor = createEditor(document, selector())
})
test('能绑定一个处理 tab 的函数', () => {
const fn: Function[] = []
tabHandler(editor, fn)
expect(fn.length).toBe(1)
})
test('能绑定一个处理 tab 的函数,如果不支持 insertHTML 指令,则不执行后续的插入操作', () => {
mockCommand(document)
const fn: Function[] = []
jest.spyOn(editor.cmd, 'queryCommandSupported').mockImplementation(() => false)
tabHandler(editor, fn)
fn.forEach(fn => {
fn()
})
expect(document.execCommand).not.toBeCalled()
})
test('能绑定一个处理 tab 的函数,如果没有选区内容,则不执行后续的插入操作', () => {
mockCommand(document)
const fn: Function[] = []
jest.spyOn(editor.cmd, 'queryCommandSupported').mockImplementation(() => true)
jest.spyOn(editor.selection, 'getSelectionContainerElem').mockImplementation(
() => undefined
)
tabHandler(editor, fn)
fn.forEach(fn => {
fn()
})
expect(document.execCommand).not.toBeCalled()
})
test('能绑定一个处理 tab 的函数如果有选区内容并且是正常的HTML元素则插入空格', () => {
mockCommand(document)
const fn: Function[] = []
jest.spyOn(editor.cmd, 'queryCommandSupported').mockImplementation(() => true)
const container = $('<p><br></p>')
container.append($('<p>123</p>'))
jest.spyOn(editor.selection, 'getSelectionContainerElem').mockImplementation(
() => container
)
tabHandler(editor, fn)
fn.forEach(fn => {
fn()
})
expect(document.execCommand).toBeCalledWith('insertHTML', false, '&nbsp;&nbsp;&nbsp;&nbsp;')
})
describe('当选区内容父元素为codeprehljs或者选区元素为code的情况则插入特殊的空格', () => {
mockCommand(document)
let editor: ReturnType<typeof createEditor>
let fn: Function[] = []
beforeEach(() => {
editor = createEditor(document, selector())
tabHandler(editor, fn)
jest.spyOn(editor.cmd, 'queryCommandSupported').mockImplementation(() => true)
})
// mock getSelectionContainerElem return value
const mockGetSelectionContainerElem = (tagString: string, isChild = true) => {
const container = $(tagString)
container.append($('<p>123</p>'))
jest.spyOn(editor.selection, 'getSelectionContainerElem').mockImplementation(() =>
isChild ? container.children()! : container
)
}
test('选区元素是 CODE', () => {
mockGetSelectionContainerElem('<code></code>', false)
fn.forEach(fn => {
fn()
})
expect(document.execCommand).toBeCalledWith(
'insertHTML',
false,
editor.config.languageTab
)
})
test('选区元素父元素是 CODE', () => {
mockGetSelectionContainerElem('<code></code>')
fn.forEach(fn => {
fn()
})
expect(document.execCommand).toBeCalledWith(
'insertHTML',
false,
editor.config.languageTab
)
})
test('选区元素父元素是 PRE', () => {
mockGetSelectionContainerElem('<pre></pre>')
fn.forEach(fn => {
fn()
})
expect(document.execCommand).toBeCalledWith(
'insertHTML',
false,
editor.config.languageTab
)
})
test('选区元素父元素是 hljs', () => {
mockGetSelectionContainerElem('<hljs></hljs>')
fn.forEach(fn => {
fn()
})
expect(document.execCommand).toBeCalledWith(
'insertHTML',
false,
editor.config.languageTab
)
})
})
})