-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests specifically for React static rendering (#6029)
- Loading branch information
1 parent
56994fe
commit 4c995e4
Showing
5 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
164 changes: 164 additions & 0 deletions
164
tests/cypress/integration/static-renderer/react-string.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/// <reference types="cypress" /> | ||
|
||
import { prettyDOM, render } from '@testing-library/react' | ||
import Bold from '@tiptap/extension-bold' | ||
import Document from '@tiptap/extension-document' | ||
import Paragraph from '@tiptap/extension-paragraph' | ||
import Text from '@tiptap/extension-text' | ||
import { Mark, Node } from '@tiptap/pm/model' | ||
import { renderToReactElement } from '@tiptap/static-renderer/pm/react' | ||
import React from 'react' | ||
|
||
describe('static render json to react elements (with prosemirror)', () => { | ||
it('generates a React element from JSON without an editor instance', () => { | ||
const json = { | ||
type: 'doc', | ||
content: [ | ||
{ | ||
type: 'paragraph', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Example Text', | ||
marks: [ | ||
{ | ||
type: 'bold', | ||
attrs: {}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
attrs: {}, | ||
} | ||
|
||
const view = render( | ||
renderToReactElement({ | ||
content: json, | ||
extensions: [Document, Paragraph, Text, Bold], | ||
}), | ||
) | ||
const html = prettyDOM(view.container, undefined, { highlight: false }) | ||
|
||
expect(html).to.eq(`<div> | ||
<p> | ||
<strong> | ||
Example Text | ||
</strong> | ||
</p> | ||
</div>`) | ||
}) | ||
|
||
it('supports custom mapping for nodes & marks', () => { | ||
const json = { | ||
type: 'doc', | ||
content: [ | ||
{ | ||
type: 'paragraph', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Example Text', | ||
marks: [ | ||
{ | ||
type: 'bold', | ||
attrs: {}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
attrs: {}, | ||
} | ||
|
||
const view = render( | ||
renderToReactElement({ | ||
content: json, | ||
extensions: [Document, Paragraph, Text, Bold], | ||
options: { | ||
nodeMapping: { | ||
doc: ({ children }) => { | ||
return React.createElement('doc', {}, children) | ||
}, | ||
}, | ||
markMapping: { | ||
bold: ({ children }) => { | ||
return React.createElement('b', {}, children) | ||
}, | ||
}, | ||
}, | ||
}), | ||
) | ||
const html = prettyDOM(view.container, undefined, { highlight: false }) | ||
|
||
expect(html).to.eq(`<div> | ||
<doc> | ||
<p> | ||
<b> | ||
Example Text | ||
</b> | ||
</p> | ||
</doc> | ||
</div>`) | ||
}) | ||
|
||
it('gives access to a prosemirror node or mark instance', () => { | ||
const json = { | ||
type: 'doc', | ||
content: [ | ||
{ | ||
type: 'paragraph', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Example Text', | ||
marks: [ | ||
{ | ||
type: 'bold', | ||
attrs: {}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
attrs: {}, | ||
} | ||
|
||
const view = render( | ||
renderToReactElement({ | ||
content: json, | ||
extensions: [Document, Paragraph, Text, Bold], | ||
options: { | ||
nodeMapping: { | ||
doc: ({ children, node }) => { | ||
expect(node.type.name).to.eq('doc') | ||
expect(node).to.be.instanceOf(Node) | ||
return React.createElement('doc', {}, children) | ||
}, | ||
}, | ||
markMapping: { | ||
bold: ({ children, mark }) => { | ||
expect(mark.type.name).to.eq('bold') | ||
expect(mark).to.be.instanceOf(Mark) | ||
return React.createElement('b', {}, children) | ||
}, | ||
}, | ||
}, | ||
}), | ||
) | ||
const html = prettyDOM(view.container, undefined, { highlight: false }) | ||
|
||
expect(html).to.eq(`<div> | ||
<doc> | ||
<p> | ||
<b> | ||
Example Text | ||
</b> | ||
</p> | ||
</doc> | ||
</div>`) | ||
}) | ||
}) |