Files
upstream-hocuspocus/tests/transformer/TiptapTransformer.ts
upstream-mirror e0c588ca1c
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
2026-09-15 17:21:35 +05:00

74 lines
1.6 KiB
TypeScript

import test from 'ava'
import { TiptapTransformer } from '@hocuspocus/transformer'
test('transforms JSON to Y.Doc', async t => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
},
],
},
],
}
const ydoc = TiptapTransformer.toYdoc(json, 'content')
t.is(
ydoc.getXmlFragment('content').toJSON(),
'<paragraph>Example Text</paragraph>',
)
})
test('writes to the correct Y.Doc field', async t => {
const json = {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Example Text',
},
],
},
],
}
const ydoc = TiptapTransformer.toYdoc(json, 'mySuperCustomField')
t.is(
ydoc.getXmlFragment('mySuperCustomField').toJSON(),
'<paragraph>Example Text</paragraph>',
)
})
test('throws a helpful error when the document is empty', async t => {
const invalidJson = null
const error = t.throws(() => {
TiptapTransformer.toYdoc(invalidJson, 'content')
}, { instanceOf: Error })
t.truthy(error?.message.includes('ProseMirror-compatible JSON'))
})
test('throws a helpful error when the document is invalid', async t => {
const invalidJson = {
type: 'invalidType',
content: [],
}
const error = t.throws(() => {
TiptapTransformer.toYdoc(invalidJson, 'content')
}, { instanceOf: Error })
t.truthy(error?.message.includes('Unknown node type: invalidType'))
})