Skip to content

Commit 232f2dc

Browse files
committed
chore(tests): use custom editor for Mathematics spec
Also introduce `testEditor` test helper. Signed-off-by: Max <max@nextcloud.com>
1 parent 749773c commit 232f2dc

3 files changed

Lines changed: 50 additions & 34 deletions

File tree

src/tests/nodes/Mathematics.spec.js

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@
44
*/
55

66
import { getExtensionField } from '@tiptap/core'
7-
import { test as baseTest } from 'vitest'
8-
import { createRichEditor } from '../../EditorFactory.ts'
9-
import { createMarkdownSerializer } from '../../extensions/Markdown.js'
107
import markdownit from '../../markdownit/index.js'
118
import { MathBlock, MathInline } from '../../nodes/Mathematics.js'
12-
import { markdownThroughEditor } from '../testHelpers/markdown.js'
13-
14-
const test = baseTest.extend({
15-
// eslint-disable-next-line no-empty-pattern
16-
editor: async ({}, use) => {
17-
const editor = createRichEditor()
18-
await use(editor)
19-
editor.destroy()
20-
},
21-
})
9+
import testEditor from '../testHelpers/testEditor.ts'
10+
11+
const test = testEditor.override('extensions', [MathBlock, MathInline])
2212

2313
describe('Mathematics nodes', () => {
2414
describe('Node structure', () => {
@@ -38,38 +28,38 @@ describe('Mathematics nodes', () => {
3828
})
3929

4030
describe('Markdown roundtrip - Inline math', () => {
41-
test('simple inline formula', () => {
31+
test('simple inline formula', ({ markdownThroughEditor }) => {
4232
expect(markdownThroughEditor('$E=mc^2$')).toBe('$E=mc^2$')
4333
})
4434

45-
test('inline formula with complex LaTeX', () => {
35+
test('inline formula with complex LaTeX', ({ markdownThroughEditor }) => {
4636
expect(markdownThroughEditor('$\\sum_{i=1}^n i$')).toBe('$\\sum_{i=1}^n i$')
4737
})
4838

49-
test('inline formula with fractions', () => {
39+
test('inline formula with fractions', ({ markdownThroughEditor }) => {
5040
expect(markdownThroughEditor('$\\frac{a}{b}$')).toBe('$\\frac{a}{b}$')
5141
})
5242

53-
test('multiple inline formulas', () => {
43+
test('multiple inline formulas', ({ markdownThroughEditor }) => {
5444
expect(markdownThroughEditor('$a$ and $b$')).toBe('$a$ and $b$')
5545
})
5646

57-
test('inline formula in text', () => {
47+
test('inline formula in text', ({ markdownThroughEditor }) => {
5848
expect(markdownThroughEditor('The formula $E=mc^2$ is famous.')).toBe('The formula $E=mc^2$ is famous.')
5949
})
6050
})
6151

6252
describe('Markdown roundtrip - Block math', () => {
63-
test('simple block formula', () => {
53+
test('simple block formula', ({ markdownThroughEditor }) => {
6454
expect(markdownThroughEditor('$$\nE=mc^2\n$$')).toBe('$$\nE=mc^2\n$$')
6555
})
6656

67-
test('block formula with complex LaTeX', () => {
57+
test('block formula with complex LaTeX', ({ markdownThroughEditor }) => {
6858
const input = '$$\n\\sum_{i=1}^n i = \\frac{n(n+1)}{2}\n$$'
6959
expect(markdownThroughEditor(input)).toBe(input)
7060
})
7161

72-
test('block formula without newlines', () => {
62+
test('block formula without newlines', ({ markdownThroughEditor }) => {
7363
// markdown-it-katex accepts this format too
7464
expect(markdownThroughEditor('$$E=mc^2$$')).toBe('$$\nE=mc^2\n$$')
7565
})
@@ -152,23 +142,15 @@ describe('Mathematics nodes', () => {
152142
})
153143

154144
describe('Serialization to markdown', () => {
155-
test('serializes inline math node', ({ editor }) => {
145+
test('serializes inline math node', ({ editor, serializeMarkdown }) => {
156146
editor.commands.insertInlineMath({ latex: 'E=mc^2' })
157147
editor.commands.insertContent(' some more text.')
158-
159-
const serializer = createMarkdownSerializer(editor.schema)
160-
const markdown = serializer.serialize(editor.state.doc)
161-
162-
expect(markdown).toBe('$E=mc^2$ some more text.')
148+
expect(serializeMarkdown()).toBe('$E=mc^2$ some more text.')
163149
})
164150

165-
test('serializes block math node', ({ editor }) => {
151+
test('serializes block math node', ({ editor, serializeMarkdown }) => {
166152
editor.commands.insertBlockMath({ latex: 'E=mc^2' })
167-
168-
const serializer = createMarkdownSerializer(editor.schema)
169-
const markdown = serializer.serialize(editor.state.doc)
170-
171-
expect(markdown).toBe('$$\nE=mc^2\n$$')
153+
expect(serializeMarkdown()).toBe('$$\nE=mc^2\n$$')
172154
})
173155
})
174156
})

src/tests/testHelpers/createCustomEditor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Content, Extensions } from '@tiptap/core'
88
import { Editor } from '@tiptap/core'
99
import { Document } from '@tiptap/extension-document'
1010
import { Text } from '@tiptap/extension-text'
11+
import Markdown from '../../extensions/Markdown.js'
1112
import Paragraph from '../../nodes/Paragraph.js'
1213

1314
/**
@@ -21,6 +22,6 @@ export default function createCustomEditor(
2122
): Editor {
2223
return new Editor({
2324
content,
24-
extensions: [Document, Paragraph, Text, ...extensions],
25+
extensions: [Markdown, Document, Paragraph, Text, ...extensions],
2526
})
2627
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { Editor, Extensions } from '@tiptap/core'
2+
3+
import { test as baseTest } from 'vitest'
4+
import { createMarkdownSerializer } from '../../extensions/Markdown.js'
5+
import markdownit from '../../markdownit/index.js'
6+
import createCustomEditor from './createCustomEditor.js'
7+
8+
export default baseTest.extend<{
9+
editor: Editor
10+
extensions: Extensions
11+
markdownThroughEditor: (input: string) => string
12+
serializeMarkdown: () => string
13+
}>({
14+
editor: async ({ extensions }, use) => {
15+
const editor = createCustomEditor('', extensions)
16+
await use(editor)
17+
editor.destroy()
18+
},
19+
extensions: [],
20+
markdownThroughEditor: async ({ editor, serializeMarkdown }, use) => {
21+
use((markdown: string) => {
22+
const content = markdownit.render(markdown)
23+
editor.commands.setContent(content)
24+
return serializeMarkdown()
25+
})
26+
},
27+
serializeMarkdown: async ({ editor }, use) => {
28+
use(() => {
29+
const serializer = createMarkdownSerializer(editor.schema)
30+
return serializer.serialize(editor.state.doc)
31+
})
32+
},
33+
})

0 commit comments

Comments
 (0)