-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28da392
commit 47c84cd
Showing
7 changed files
with
146 additions
and
40 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
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
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
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
66 changes: 66 additions & 0 deletions
66
packages/plugins/paragraph/src/plugin/ParagraphPlugin.test.tsx
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,66 @@ | ||
import React from 'react'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { Paragraph } from './'; | ||
import { renderYooptaEditor } from 'test-utils'; | ||
|
||
const plugins = [Paragraph]; | ||
|
||
describe('Paragraph Plugin', () => { | ||
it('renders correctly with basic props', () => { | ||
const { container } = renderYooptaEditor({ plugins }); | ||
|
||
const paragraph = screen.getByTestId('paragraph'); | ||
expect(paragraph).toBeInTheDocument(); | ||
expect(paragraph.tagName).toBe('P'); | ||
expect(paragraph.textContent).toBe('Test content'); | ||
}); | ||
|
||
it('applies custom classes and attributes', () => { | ||
const customProps = { | ||
className: 'custom-class', | ||
style: { textAlign: 'center' }, | ||
attributes: { 'data-testid': 'paragraph' }, | ||
}; | ||
render(<Paragraph editor={editor} {...customProps} children="Test content" />); | ||
const paragraph = screen.getByTestId('paragraph'); | ||
expect(paragraph).toHaveClass('custom-class'); | ||
expect(paragraph).toHaveStyle({ textAlign: 'center' }); | ||
}); | ||
|
||
it('handles extendRender prop', () => { | ||
const extendedRender = vi.fn(({ children }) => <div>{children}</div>); | ||
render( | ||
<Paragraph | ||
editor={editor} | ||
extendRender={extendedRender} | ||
children="Extended content" | ||
attributes={{ 'data-testid': 'extended' }} | ||
/>, | ||
); | ||
expect(extendedRender).toHaveBeenCalled(); | ||
const extendedElement = screen.getByTestId('extended'); | ||
expect(extendedElement.textContent).toBe('Extended content'); | ||
}); | ||
|
||
it('serializes to HTML correctly', () => { | ||
const element = { type: 'paragraph', children: [{ text: 'Hello world' }] }; | ||
const text = 'Hello world'; | ||
const serializedOutput = Paragraph.parsers.html.serialize(element, text); | ||
expect(serializedOutput).toBe('<p>Hello world</p>'); | ||
}); | ||
|
||
it('deserializes from HTML correctly', () => { | ||
document.body.innerHTML = `<p>Hello world</p>`; | ||
const pElement = document.querySelector('p'); | ||
const deserializedOutput = Paragraph.parsers.html.deserialize.nodeNames.includes(pElement.nodeName); | ||
expect(deserializedOutput).toBeTruthy(); | ||
}); | ||
|
||
it('serializes to Markdown correctly', () => { | ||
const element = { type: 'paragraph', children: [{ text: 'Hello world' }] }; | ||
const text = 'Hello world'; | ||
const serializedMarkdown = Paragraph.parsers.markdown.serialize(element, text); | ||
expect(serializedMarkdown).toBe('Hello world\n'); | ||
}); | ||
}); |
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,63 @@ | ||
import { render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { createYooptaEditor } from '../packages/core/editor/src/editor'; | ||
import { YooptaPlugin } from '../packages/core/editor/src/plugins'; | ||
import { YooptaEditor, YooptaEditorProps } from '../packages/core/editor/src/YooptaEditor'; | ||
|
||
export const InlinePlugin = new YooptaPlugin({ | ||
type: 'Inline', | ||
elements: { | ||
inline: { | ||
render: (props) => <span {...props.attributes}>{props.children}</span>, | ||
props: { nodeType: 'inline' }, | ||
}, | ||
}, | ||
}); | ||
|
||
export const DefaultParagraph = new YooptaPlugin({ | ||
type: 'Paragraph', | ||
elements: { | ||
paragraph: { | ||
render: (props) => <p {...props.attributes}>{props.children}</p>, | ||
props: { nodeType: 'block' }, | ||
}, | ||
}, | ||
}); | ||
|
||
export const BlockPluginWithProps = new YooptaPlugin({ | ||
type: 'Block', | ||
elements: { | ||
block: { | ||
render: (props) => <div {...props.attributes}>{props.children}</div>, | ||
props: { nodeType: 'block', checked: false }, | ||
}, | ||
}, | ||
}); | ||
|
||
export const TEST_PLUGIN_LIST = [InlinePlugin, DefaultParagraph, BlockPluginWithProps]; | ||
|
||
export function renderYooptaEditor(props: Partial<YooptaEditorProps> = {}) { | ||
const editor = createYooptaEditor(); | ||
|
||
return render( | ||
<YooptaEditor {...props} editor={props.editor || editor} plugins={props.plugins || TEST_PLUGIN_LIST} />, | ||
); | ||
} | ||
|
||
const data = { | ||
'aafd7597-1e9a-4c80-ab6c-88786595d72a': { | ||
'aafd7597-1e9a-4c80-ab6c-88786595d72a': { | ||
id: 'aafd7597-1e9a-4c80-ab6c-88786595d72a', | ||
meta: { depth: 0, order: 0 }, | ||
type: 'Paragraph', | ||
value: [ | ||
{ | ||
id: '3aff9e2c-5fee-43ff-b426-1e4fee8b303c', | ||
type: 'paragraph', | ||
props: { nodeType: 'block' }, | ||
children: [{ text: '' }], | ||
}, | ||
], | ||
}, | ||
}, | ||
}; |
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