import { Editor } from '@tiptap/core' import Document from '@tiptap/extension-document' import Paragraph from '@tiptap/extension-paragraph' import Text from '@tiptap/extension-text' import { describe, expect, it } from 'vite-plus/test' import { TableHeader } from '../header/table-header.js' import { TableRow } from '../row/table-row.js' import { Table } from '../table/table.js' import { TableCell } from './table-cell.js' describe('extension table cell', () => { const editorElClass = 'tiptap' let editor: Editor | null = null const createEditorEl = () => { const editorEl = document.createElement('div') editorEl.classList.add(editorElClass) document.body.appendChild(editorEl) return editorEl } const getEditorEl = () => document.querySelector(`.${editorElClass}`) it('should start with a Table', () => { const content = '
FirstnameLastnameAge
JillSmith50
EveJackson94
JohnDoe80
' editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table.configure({ resizable: true, }), ], content, }) expect(editor.getHTML()).toContain('Jackson') editor?.destroy() getEditorEl()?.remove() }) it('should parse a single colWidth', () => { const content = '
NameDescription
Cyndi LauperSingerSongwriterActress
Marie CurieScientistChemistPhysicist
Indira GandhiPrime ministerPolitician
' editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table.configure({ resizable: true, }), ], content, }) // @ts-expect-error content is not guaranteed to be this shape expect(editor.getJSON().content[0].content[0].content[0].attrs.colwidth[0]).toBe(200) editor?.destroy() getEditorEl()?.remove() }) it('should parse multiple colWidths', () => { const content = '
NameDescription
Cyndi LauperSingerSongwriterActress
Marie CurieScientistChemistPhysicist
Indira GandhiPrime ministerPolitician
' editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table.configure({ resizable: true, }), ], content, }) // @ts-expect-error content is not guaranteed to be this shape expect(editor.getJSON().content[0].content[0].content[1].attrs.colwidth).toEqual([150, 100]) editor?.destroy() getEditorEl()?.remove() }) it('should parse the colgroup col width for the first column', () => { const content = '
helloworld
' editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table.configure({ resizable: true, }), ], content, }) const firstRow = editor.getJSON().content?.[0].content?.[0] expect(firstRow?.content?.[0].attrs?.colwidth).toEqual([64]) expect(firstRow?.content?.[1].attrs?.colwidth).toBeNull() editor?.destroy() getEditorEl()?.remove() }) it('should not serialize the default colspan and rowspan', () => { const content = '
Cell
' editor = new Editor({ element: createEditorEl(), extensions: [Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table], content, }) const html = editor.getHTML() expect(html).toContain('

Cell

') expect(html).not.toContain('colspan="1"') expect(html).not.toContain('rowspan="1"') editor?.destroy() getEditorEl()?.remove() }) it('should not render configured spans for a cell with default spans', () => { editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell.configure({ HTMLAttributes: { colspan: 2, rowspan: 2 } }), TableHeader, TableRow, Table, ], content: '
Cell
', }) expect(editor.getHTML()).toContain('

Cell

') expect(editor.view.dom.querySelector('td')?.hasAttribute('colspan')).toBe(false) expect(editor.view.dom.querySelector('td')?.hasAttribute('rowspan')).toBe(false) editor.destroy() getEditorEl()?.remove() }) it('should keep colspan and rowspan when a cell actually spans', () => { const content = '
Cell
' editor = new Editor({ element: createEditorEl(), extensions: [Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table], content, }) const html = editor.getHTML() expect(html).toContain('colspan="2"') expect(html).toContain('rowspan="3"') editor?.destroy() getEditorEl()?.remove() }) it('should prefer the colwidth attribute over the colgroup col width', () => { const content = '
helloworld
' editor = new Editor({ element: createEditorEl(), extensions: [ Document, Text, Paragraph, TableCell, TableHeader, TableRow, Table.configure({ resizable: true, }), ], content, }) // @ts-expect-error content is not guaranteed to be this shape expect(editor.getJSON().content[0].content[0].content[0].attrs.colwidth).toEqual([200]) editor?.destroy() getEditorEl()?.remove() }) })