-
Notifications
You must be signed in to change notification settings - Fork 94
/
Table.spec.js
87 lines (73 loc) · 2.55 KB
/
Table.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { createRichEditor } from '../../EditorFactory.js'
import { createMarkdownSerializer } from '../../extensions/Markdown.js'
import markdownit from '../../markdownit/index.js'
// Eslint does not know about ?raw suffix it seems.
/* eslint-disable import/no-unresolved */
import input from '../fixtures/tables/basic/table.md?raw'
import output from '../fixtures/tables/basic/table.html?raw'
import otherStructure from '../fixtures/tables/basic/table.structure.html?raw'
import handbook from '../fixtures/tables/handbook/handbook.html?raw'
import handbookOut from '../fixtures/tables/handbook/handbook.out.html?raw'
/* eslint-enable import/no-unresolved */
import { br, table, td, th, thead, tr, expectDocument } from '../testHelpers/builders.js'
describe('Table', () => {
it('Markdown-IT renders tables', () => {
const rendered = markdownit.render(input)
expect(rendered).toBe(output)
})
test('Load into editor', () => {
const tiptap = editorWithContent(markdownit.render(input))
expectDocument(tiptap.state.doc,
table(
thead(
th({ textAlign: 'center' }, 'heading'),
th({ textAlign: 'right' }, 'heading 2'),
th('heading 3'),
),
tr(
td({ textAlign: 'center' }, 'center'),
td({ textAlign: 'right' }, 'right'),
td('left cell ', br({ syntax: 'html' }), 'with line break'),
),
),
)
})
test('load html table with other structure', () => {
const tiptap = editorWithContent(otherStructure.replace(/\n\s*/g, ''))
expectDocument(tiptap.state.doc,
table(
thead(
th({ textAlign: 'center' }, 'heading'),
th({ textAlign: 'right' }, 'heading 2'),
th('heading 3'),
),
tr(
td({ textAlign: 'center' }, 'center'),
td({ textAlign: 'right' }, 'right'),
td('left cell ', br({ syntax: ' ' }), 'with line break'),
),
),
)
})
test('handle html table from handbook', () => {
const tiptap = editorWithContent(handbook.replace(/\n\s*/g, ''))
expect(formatHTML(tiptap.getHTML())).toBe(formatHTML(handbookOut))
})
test('serialize from editor', () => {
const tiptap = editorWithContent(markdownit.render(input))
const serializer = createMarkdownSerializer(tiptap.schema)
expect(serializer.serialize(tiptap.state.doc)).toBe(input)
})
})
const editorWithContent = (content) => {
const editor = createRichEditor()
editor.commands.setContent(content)
return editor
}
const formatHTML = (html) => {
return html.replaceAll('><', '>\n<').replace(/\n$/, '')
}