From 10bfdeb7afd55f120bbeb4b6aee39668026d4f29 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 17 Apr 2024 15:49:13 +0200 Subject: [PATCH] WIP: test link mark --- cypress/e2e/marks/Link.spec.js | 130 +++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 cypress/e2e/marks/Link.spec.js diff --git a/cypress/e2e/marks/Link.spec.js b/cypress/e2e/marks/Link.spec.js new file mode 100644 index 00000000000..c4904a29866 --- /dev/null +++ b/cypress/e2e/marks/Link.spec.js @@ -0,0 +1,130 @@ +/* eslint-disable no-unused-expressions */ +/** + * @copyright Copyright (c) 2024 Max + * + * @author Max + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +import Markdown from './../../../src/extensions/Markdown.js' +import { Italic, Link } from './../../../src/marks/index.js' +import { createCustomEditor } from './../../support/components.js' +import { loadMarkdown } from '../nodes/helpers.js' + +describe('Link marks', { retries: 0 }, () => { + + const editor = createCustomEditor({ + content: '', + extensions: [ + Markdown, + Link, + Italic, + ], + }) + + describe('insertOrSetLink command', { retries: 0 }, () => { + + it('is available in commands', () => { + expect(editor.commands).to.have.property('insertOrSetLink') + }) + + it('can run on normal paragraph', () => { + prepareEditor('hello\n', 3) + expect(editor.can().insertOrSetLink()).to.be.true + }) + + it('will insert a link in a normal paragraph', () => { + prepareEditor('hello\n', 3) + editor.commands.insertOrSetLink(false, { href: 'https://nextcloud.com' }) + expect(editor.can().insertOrSetLink()).to.be.true + }) + + it('convert a paragraph with a link', () => { + prepareEditor('[link text](https://nextcloud.com)\n') + editor.commands.setPreview() + expectLink() + }) + + it('convert the second a paragraph with a link', () => { + prepareEditor('hello\n\n[link text](https://nextcloud.com)\n') + editor.commands.setTextSelection(10) + editor.commands.setPreview() + expectLink() + }) + + it('convert a paragraph with a link and a space', () => { + prepareEditor('[link text](https://nextcloud.com)\n') + editor.commands.insertContentAt( + editor.state.doc.content.size - 1, + ' ', + { updateSelection: false }, + ) + editor.commands.setPreview() + expectLink() + }) + + it('results in a preview node with the href and text with link mark', () => { + prepareEditor( '[link text](https://nextcloud.com)\n', 3) + editor.commands.insertOrSetLink(true, { href: 'https://nextcloud.com/team' }) + expectLink() + }) + + it('cannot run twice', () => { + prepareEditor('[link text](https://nextcloud.com)\n') + editor.commands.setPreview() + expect(editor.can().setPreview()).to.be.false + }) + + }) + + /** + * Expect a link in the editor. + */ + function expectLink() { + expect(getParentNode().type.name).to.equal('paragraph') + expect(getParentNode().attrs.href).to.equal('https://nextcloud.com') + expect(getMark().attrs.href).to.equal('https://nextcloud.com') + } + + /** + * + */ + function getParentNode() { + const { state: { selection } } = editor + return selection.$head.parent + } + + /** + * + */ + function getMark() { + const { state: { selection } } = editor + console.info(selection.$head) + return selection.$head.nodeAfter.marks[0] + } + + /** + * + * @param input + */ + function prepareEditor(input, position = 1) { + loadMarkdown(editor, input) + editor.commands.setTextSelection( position ) + } + +})