From 6a5ea819520c8a8e2b6c5c13b12be6c61b4ce2e8 Mon Sep 17 00:00:00 2001 From: Nick the Sick Date: Fri, 22 Nov 2024 14:24:51 +0100 Subject: [PATCH] test: add test for marks on nodes --- tests/y-prosemirror.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/y-prosemirror.test.js b/tests/y-prosemirror.test.js index 730818c..b5af84c 100644 --- a/tests/y-prosemirror.test.js +++ b/tests/y-prosemirror.test.js @@ -23,7 +23,7 @@ import { findWrapping } from 'prosemirror-transform' import { schema as complexSchema } from './complexSchema.js' import * as promise from 'lib0/promise' -const schema = /** @type {any} */ (basicSchema.schema) +const schema = /** @type {import('prosemirror-model').Schema} */ (basicSchema.schema) /** * Verify that update events in plugins are only fired once. @@ -452,6 +452,30 @@ export const testAddToHistoryIgnore = (_tc) => { ) } +/** + * @param {t.TestCase} tc + */ +export const testMarksOnNodes = (_tc) => { + const view = createNewProsemirrorView(new Y.Doc()) + view.dispatch( + view.state.tr.insert( + 0, + /** @type {any} */ (schema.node('image', { src: 'http://example.com', alt: null, title: null }, [], [schema.mark('link', { href: 'https://yjs.dev', title: null })])) + ) + ) + // convert to JSON and back because the ProseMirror schema messes with the constructors + const stateJSON = JSON.parse(JSON.stringify(view.state.doc.toJSON())) + // test if transforming back and forth from Yjs doc works + const backandforth = JSON.parse(JSON.stringify(yDocToProsemirrorJSON( + prosemirrorJSONToYDoc(/** @type {any} */ (schema), stateJSON) + ))) + // Add the missing alt and title attributes, these are defaults for the image mark + backandforth.content[0].content[0].attrs.alt = null + backandforth.content[0].content[0].attrs.title = null + + t.compare(stateJSON, backandforth, 'marks on nodes are preserved') +} + const createNewProsemirrorViewWithSchema = (y, schema, undoManager = false) => { const view = new EditorView(null, { // @ts-ignore