snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
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
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
This commit is contained in:
73
tests/transformer/TiptapTransformer.ts
Normal file
73
tests/transformer/TiptapTransformer.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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'))
|
||||
})
|
||||
Reference in New Issue
Block a user