From 79b734484753485ac90243699172c166133d7e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 4 Mar 2020 10:21:48 +0100 Subject: [PATCH] add more tests --- cypress/integration/basic.spec.js | 63 ++++++++++++++++++++-- package.json | 2 +- packages/tiptap-core/src/Editor.ts | 17 +++++- packages/tiptap-core/src/commands/focus.ts | 2 +- 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/cypress/integration/basic.spec.js b/cypress/integration/basic.spec.js index 8743c21f..36ee7630 100644 --- a/cypress/integration/basic.spec.js +++ b/cypress/integration/basic.spec.js @@ -1,15 +1,68 @@ -context('Basic', () => { +context('basic', () => { beforeEach(() => { cy.visit('/tests/basic') }) + describe('export', () => { + it('should return html', () => { + cy.get('.ProseMirror').window().then(window => { + const { editor } = window + const html = editor.html() + + expect(html).to.equal('

foo

') + }) + }) + + it('should return json', () => { + cy.get('.ProseMirror').window().then(window => { + const { editor } = window + const json = editor.json() + + expect(json).to.deep.equal({ + type: 'doc', + content: [ + { + type: 'paragraph', + content: [ + { + type: 'text', + text: 'foo' + } + ] + } + ] + }) + }) + }) + }) + describe('insertText', () => { it('should prepend text', () => { - cy.get('.ProseMirror').should('contain', 'foo') + cy.get('.ProseMirror').window().then(window => { + const { editor } = window - cy.window().then(win => { - win.editor.insertText('bar') - cy.get('.ProseMirror').should('contain', 'barfoo') + editor.insertText('bar') + cy.get('.ProseMirror p:first').should('contain', 'barfoo') + //.contains('barfoo') + // .should('contain', 'barfoo') + }) + }) + + it('should append text', () => { + cy.get('.ProseMirror').window().then(window => { + const { editor } = window + + editor.focus('end').insertText('bar') + cy.get('.ProseMirror p:first').should('contain', 'foobar') + }) + }) + + it('should append html', () => { + cy.get('.ProseMirror').window().then(window => { + const { editor } = window + + editor.focus('end').insertHTML('

bar

') + cy.get('.ProseMirror p:first').should('contain', 'foobar') }) }) }) diff --git a/package.json b/package.json index 2864a898..a6f2ab95 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build:docs": "gridsome build", "build:packages": "yarn clean:packages && lerna exec --parallel -- microbundle --raw --compress", "clean:packages": "rm -rf ./packages/*/dist", - "test:debug": "cypress start", + "test:debug": "cypress open", "test": "cypress run", "startandtest": "start-server-and-test start http://localhost:3000 test" }, diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index f6d937ad..80ca7dd9 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -1,6 +1,6 @@ import {EditorState, Plugin} from "prosemirror-state" import {EditorView} from "prosemirror-view" -import {Schema, DOMParser} from "prosemirror-model" +import {Schema, DOMParser, DOMSerializer} from "prosemirror-model" // @ts-ignore import {schema} from "prosemirror-schema-basic" // @ts-ignore @@ -133,5 +133,20 @@ export class Editor { // @ts-ignore return this[name](...args) } + + public json() { + return this.state.doc.toJSON() + } + + public html() { + const div = document.createElement('div') + const fragment = DOMSerializer + .fromSchema(this.schema) + .serializeFragment(this.state.doc.content) + + div.appendChild(fragment) + + return div.innerHTML + } } diff --git a/packages/tiptap-core/src/commands/focus.ts b/packages/tiptap-core/src/commands/focus.ts index 7c3de881..4d2c085a 100644 --- a/packages/tiptap-core/src/commands/focus.ts +++ b/packages/tiptap-core/src/commands/focus.ts @@ -33,7 +33,7 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe return { from: size, - to: size, + to: size - 1, // TODO: -1 only for nodes with content } }