Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/tests/nodes/Preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { test as baseTest } from 'vitest'
import Preview from './../../nodes/Preview.js'
import Markdown from './../../extensions/Markdown.js'
import Link from './../../marks/Link.js'
Expand All @@ -13,33 +14,39 @@ import {
} from '../testHelpers/markdown.js'
import createCustomEditor from '../testHelpers/createCustomEditor.ts'

const test = baseTest.extend({
editor: async ({ task: _ }, use) => {
const editor = createCustomEditor('', [Markdown, Preview, Link])
await use(editor)
editor.destroy()
}
})

describe('Preview extension', () => {
it('exposes toMarkdown function', () => {
test('exposes toMarkdown function', () => {
const toMarkdown = getExtensionField(Preview, 'toMarkdown', Preview)
expect(typeof toMarkdown).toEqual('function')
})

it('exposes the toMarkdown function in the prosemirror schema', () => {
const editor = createEditorWithPreview()
test('exposes the toMarkdown function in the prosemirror schema', ({ editor }) => {
const preview = editor.schema.nodes.preview
expect(preview.spec.toMarkdown).toBeDefined()
})

it('markdown syntax is preserved through editor', () => {
test('markdown syntax is preserved through editor', () => {
const markdown = '[link](https://nextcloud.com (preview))'
expect(markdownThroughEditor(markdown)).toBe(markdown)
})

it('serializes HTML to markdown', () => {
test('serializes HTML to markdown', () => {
const markdown = '[link](https://nextcloud.com (preview))'
const link = '<a href="https://nextcloud.com" title="preview">link</a>'
expect(markdownThroughEditorHtml(link))
.toBe(markdown)
})

it('detects links', () => {
test('detects links', ({ editor }) => {
const link = '<a href="https://nextcloud.com" title="preview">link</a>'
const editor = createEditorWithPreview()
editor.commands.setContent(`${link}<p>hello></p>`)
const node = editor.state.doc.content.firstChild
expect(node.type.name).toBe('preview')
Expand All @@ -49,4 +56,3 @@ describe('Preview extension', () => {

})

const createEditorWithPreview = () => createCustomEditor('', [Markdown, Preview, Link])
43 changes: 23 additions & 20 deletions src/tests/nodes/Table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { test as baseTest } from 'vitest'
import { createRichEditor } from '../../EditorFactory.js'
import { createMarkdownSerializer } from '../../extensions/Markdown.js'

Expand All @@ -19,16 +20,24 @@ import handbookOut from '../fixtures/tables/handbook/handbook.out.html?raw'

import { br, table, td, th, thead, tr, expectDocument } from '../testHelpers/builders.js'

const test = baseTest.extend({
editor: async ({ task: _ }, use) => {
const editor = createRichEditor()
await use(editor)
editor.destroy()
}
})

describe('Table', () => {
it('Markdown-IT renders tables', () => {
test('Markdown-IT renders tables', () => {
const rendered = markdownit.render(input)
expect(rendered).toBe(output)
})

test('Load into editor', () => {
const tiptap = editorWithContent(markdownit.render(input))
test('Load into editor', ({ editor }) => {
editor.commands.setContent(markdownit.render(input))

expectDocument(tiptap.state.doc,
expectDocument(editor.state.doc,
table(
thead(
th({ textAlign: 'center' }, 'heading'),
Expand All @@ -44,10 +53,10 @@ describe('Table', () => {
)
})

test('load html table with other structure', () => {
const tiptap = editorWithContent(otherStructure.replace(/\n\s*/g, ''))
test('load html table with other structure', ({ editor }) => {
editor.commands.setContent(otherStructure.replace(/\n\s*/g, ''))

expectDocument(tiptap.state.doc,
expectDocument(editor.state.doc,
table(
thead(
th({ textAlign: 'center' }, 'heading'),
Expand All @@ -63,25 +72,19 @@ describe('Table', () => {
)
})

test('handle html table from handbook', () => {
const tiptap = editorWithContent(handbook.replace(/\n\s*/g, ''))
expect(formatHTML(tiptap.getHTML())).toBe(formatHTML(handbookOut))
test('handle html table from handbook', ({ editor }) => {
editor.commands.setContent(handbook.replace(/\n\s*/g, ''))
expect(formatHTML(editor.getHTML())).toBe(formatHTML(handbookOut))
})

test('serialize from editor', () => {
const tiptap = editorWithContent(markdownit.render(input))
const serializer = createMarkdownSerializer(tiptap.schema)
test('serialize from editor', ({ editor }) => {
editor.commands.setContent(markdownit.render(input))
const serializer = createMarkdownSerializer(editor.schema)

expect(serializer.serialize(tiptap.state.doc)).toBe(input)
expect(serializer.serialize(editor.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$/, '')
}
4 changes: 3 additions & 1 deletion src/tests/tiptap.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const renderedHTML = (markdown) => {
const editor = createRichEditor()
editor.commands.setContent(markdownit.render(markdown))
// Remove TrailingNode
return editor.getHTML().replace(/<p><\/p>$/, '')
const res = editor.getHTML().replace(/<p><\/p>$/, '')
editor.destroy()
return res
}

describe('TipTap', () => {
Expand Down
Loading