import Document from '@tiptap/extension-document' import Image from '@tiptap/extension-image' import Link from '@tiptap/extension-link' import Paragraph from '@tiptap/extension-paragraph' import Text from '@tiptap/extension-text' import { Color, TextStyle } from '@tiptap/extension-text-style' import StarterKit from '@tiptap/starter-kit' import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test' import { Editor } from '../Editor.js' describe('insertContent', () => { let editor: Editor afterEach(() => { editor?.destroy() }) it('inserts plain text object {type: "text", text: "world"}', () => { editor = new Editor({ extensions: [Document, Paragraph, Text], content: '
hello
', }) // Place cursor at the end of "hello" (position 6) editor.commands.setTextSelection(6) const result = editor.commands.insertContent({ type: 'text', text: 'world' }) expect(result).toBe(true) expect(editor.getHTML()).toBe('helloworld
') }) }) describe('insertContent command', () => { let editor: Editor beforeEach(() => { const element = document.createElement('div') document.body.appendChild(element) editor = new Editor({ element, extensions: [Image, Color, TextStyle, Link, StarterKit], content: '', }) }) afterEach(() => { editor.destroy() }) it('inserts html content correctly', () => { editor.commands.insertContent( 'Hello World
This is a paragraph
with a break.
And this is some additional string content.
', ) expect(editor.getHTML()).toContain( 'Hello World
This is a paragraph
with a break.
And this is some additional string content.
', ) }) it('keeps spaces inbetween tags in html content', () => { editor.commands.insertContent('Hello World
') expect(editor.getHTML()).toContain('Hello World
') }) it('keeps empty spaces', () => { editor.commands.insertContent(' ') expect(editor.getHTML()).toContain('') }) it('inserts text content correctly', () => { editor.commands.insertContent(`Hello World This is content with a new line. Is this working? Lets see if multiple new lines are inserted correctly And more lines`) expect(editor.getText()).toContain( 'Hello World\nThis is content with a new line. Is this working?\n\nLets see if multiple new lines are inserted correctly', ) }) it('keeps newlines in pre tag', () => { editor.commands.insertContent('
foo\nbar')
expect(editor.getHTML()).toContain('foo\nbar')
})
it('keeps newlines and tabs', () => {
editor.commands.insertContent('Hello\n\tworld\n\t\thow\n\t\t\tnice.\ntest\tOK
') expect(editor.getHTML()).toContain('Hello\n\tworld\n\t\thow\n\t\t\tnice.\ntest\tOK
') }) it('keeps newlines and tabs between html fragments', () => { editor.commands.insertContent('Hello World
') expect(editor.getHTML()).toContain('Hello World
') }) it('allows inserting nothing', () => { editor.commands.insertContent('') expect(editor.getHTML()).toBeDefined() }) it('allows inserting a partial HTML tag', () => { editor.commands.insertContent('foo') expect(editor.getHTML()).toContain('
foo
') }) it('allows inserting an incomplete HTML tag', () => { editor.commands.insertContent('foofoo<p
') }) it('allows inserting a list', () => { editor.commands.insertContent('ABC
123
Hello\n World\n
\n', { parseOptions: { preserveWhitespace: false }, }) expect(editor.getHTML()).toContain('Hello World
') }) it('splits content when image is inserted inbetween text', () => { editor.commands.insertContent('HelloWorld
') editor.commands.setTextSelection(6) editor.commands.insertContent('Hello
World
', ) }) it('does not split content when image is inserted at beginning of text', () => { editor.commands.insertContent('HelloWorld
') editor.commands.setTextSelection(1) editor.commands.insertContent('HelloWorld
', ) }) it('respects editor.options.parseOptions if defined to be `false`', () => { editor.options.parseOptions = { preserveWhitespace: false } editor.commands.insertContent('\nHello\n World\n
\n') expect(editor.getHTML()).toContain('Hello World
') }) it('respects editor.options.parseOptions if defined to be `full`', () => { editor.options.parseOptions = { preserveWhitespace: 'full' } editor.commands.insertContent('\nHello\n World\n
\n') expect(editor.getHTML()).toContain('Hello\n World
') }) it('respects editor.options.parseOptions if defined to be `true`', () => { editor.options.parseOptions = { preserveWhitespace: true } editor.commands.insertContent('Hello\n World\n
') expect(editor.getHTML()).toContain('Hello
World
This is an italic text
') }) })