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
74 lines
1.6 KiB
TypeScript
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'))
|
|
})
|