import Mention from '@tiptap/extension-mention' import StarterKit from '@tiptap/starter-kit' import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test' import { Editor } from '../Editor.js' describe('setContent command', () => { let editor: Editor beforeEach(() => { const element = document.createElement('div') document.body.appendChild(element) editor = new Editor({ element, extensions: [StarterKit, Mention], content: '', }) }) afterEach(() => { editor.destroy() }) it('inserts raw text content', () => { editor.commands.setContent('Hello World.') expect(editor.getHTML()).toContain('
Hello World.
') }) it('inserts raw JSON content', () => { editor.commands.setContent({ type: 'paragraph', content: [{ type: 'text', text: 'Hello World.' }], }) expect(editor.getHTML()).toContain('Hello World.
') }) it('inserts a Prosemirror Node as content', () => { editor.commands.setContent( editor.schema.node('paragraph', null, editor.schema.text('Hello World.')), ) expect(editor.getHTML()).toContain('Hello World.
') }) it('inserts a Prosemirror Fragment as content', () => { editor.commands.setContent( editor.schema.node( 'doc', null, editor.schema.node('paragraph', null, editor.schema.text('Hello World.')), ).content, ) expect(editor.getHTML()).toContain('Hello World.
') }) it('respects the emitUpdate option', () => { let updateCount = 0 const callback = () => { updateCount += 1 } editor.on('update', callback) editor.commands.setContent('Hello World.', { emitUpdate: true }) const first = updateCount updateCount = 0 editor.commands.setContent('Hello World again.', { emitUpdate: false }) const second = updateCount editor.off('update', callback) expect(first).toBe(1) expect(second).toBe(0) }) it('inserts more complex html content', () => { editor.commands.setContent( 'This is a paragraph.
List Item A
List Item B
Subchild
This is a paragraph.
List Item A
List Item B
Subchild
Hello\n\tworld\n\t\thow\n\t\t\tnice.
') expect(editor.getHTML()).toContain('Hello world how nice.
') }) it('keeps newlines and tabs when preserveWhitespace = full', () => { editor.commands.setContent('Hello\n\tworld\n\t\thow\n\t\t\tnice.
', { parseOptions: { preserveWhitespace: 'full' }, }) expect(editor.getHTML()).toContain('Hello\n\tworld\n\t\thow\n\t\t\tnice.
') }) it('overwrites existing content', () => { editor.commands.setContent('Initial Content
') expect(editor.getHTML()).toContain('Initial Content
') editor.commands.setContent('Overwritten Content
') expect(editor.getHTML()).toContain('Overwritten Content
') editor.commands.setContent('Content without tags') expect(editor.getHTML()).toContain('Content without tags
') }) it('inserts mentions', () => { editor.commands.setContent( '@John Doe
', ) expect(editor.getHTML()).toContain( '@John Doe', ) }) it('removes newlines and tabs between html fragments', () => { editor.commands.setContent('Hello World
') expect(editor.getHTML()).toContain('Hello World
') }) it('keeps newlines and tabs between html fragments when preserveWhitespace = full', () => { editor.commands.setContent('Hello World
', { parseOptions: { preserveWhitespace: 'full' }, }) expect(editor.getHTML()).toContain( '\n\t
Hello World
', ) }) it('allows inserting nothing', () => { editor.commands.setContent('') expect(editor.getHTML()).toBeDefined() }) it('allows inserting nothing when preserveWhitespace = full', () => { editor.commands.setContent('', { parseOptions: { preserveWhitespace: 'full' } }) expect(editor.getHTML()).toBeDefined() }) it('allows inserting a partial HTML tag', () => { editor.commands.setContent('foo') expect(editor.getHTML()).toContain('
foo
') }) it('allows inserting a partial HTML tag when preserveWhitespace = full', () => { editor.commands.setContent('foo', { parseOptions: { preserveWhitespace: 'full' } }) expect(editor.getHTML()).toContain('
foo
') }) it('removes an incomplete HTML tag', () => { editor.commands.setContent('foofoo
') }) it('allows inserting an incomplete HTML tag when preserveWhitespace = full', () => { editor.commands.setContent('foofoo<p
') }) it('allows inserting a list', () => { editor.commands.setContent('ABC
123
ABC
123
Hello\n World\n
\n', { parseOptions: { preserveWhitespace: false }, }) expect(editor.getHTML()).toContain('Hello World
') }) })