Skip to content

Commit

Permalink
WIP: test link mark
Browse files Browse the repository at this point in the history
  • Loading branch information
max-nextcloud committed Apr 17, 2024
1 parent 95cc248 commit 10bfdeb
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions cypress/e2e/marks/Link.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* eslint-disable no-unused-expressions */
/**
* @copyright Copyright (c) 2024 Max <[email protected]>
*
* @author Max <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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 )
}

})

0 comments on commit 10bfdeb

Please sign in to comment.