Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed Mar 29, 2024
1 parent 2d39181 commit 95827a7
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
102 changes: 102 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {$generateHtmlFromNodes, $generateNodesFromDOM} from '@lexical/html';
import {useLexicalComposerContext} from '@lexical/react/src/LexicalComposerContext';
import {ContentEditable} from '@lexical/react/src/LexicalContentEditable';
import LexicalErrorBoundary from '@lexical/react/src/LexicalErrorBoundary';
Expand Down Expand Up @@ -2212,4 +2213,105 @@ describe('LexicalEditor tests', () => {

expect(ParagraphNode.importDOM).toHaveBeenCalledTimes(1);
});

describe('html config', () => {
it('should work correctly', async () => {
const onError = jest.fn();

const newEditor = createTestEditor({
html: {
export: new Map([
[TextNode, () => ({element: document.createElement('figure')})],
]),
import: {
figure: () => ({
conversion: () => ({node: $createTextNode('yolo')}),
priority: 4,
}),
},
},
onError: onError,
});

newEditor.setRootElement(container);

newEditor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
const text = $createTextNode();
root.append(paragraph);
paragraph.append(text);

const selection = $createNodeSelection();
selection.add(text.getKey());

const html = $generateHtmlFromNodes(newEditor, selection);
expect(html).toBe('<figure></figure>');

const parser = new DOMParser();
const dom = parser.parseFromString(html, 'text/html');
const node = $generateNodesFromDOM(newEditor, dom)[0];

expect(node).toEqual({
__detail: 0,
__format: 0,
__key: node.getKey(),
__mode: 0,
__next: null,
__parent: null,
__prev: null,
__style: '',
__text: 'yolo',
__type: 'text',
});
});

expect(onError).not.toHaveBeenCalled();
});

it('can utilize the methods of the target node in export callback', async () => {
const onError = jest.fn();

const newEditor = createTestEditor({
html: {
export: new Map([
[
TextNode,
(_, target: TextNode) => {
if (target.hasFormat('bold')) {
return {element: document.createElement('bar')};
}

return {element: document.createElement('foo')};
},
],
]),
},
onError: onError,
});

newEditor.setRootElement(container);

newEditor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
const text = $createTextNode();
root.append(paragraph);
paragraph.append(text);

const selection = $createNodeSelection();
selection.add(text.getKey());

const htmlFoo = $generateHtmlFromNodes(newEditor, selection);
expect(htmlFoo).toBe('<foo></foo>');

text.toggleFormat('bold');

const htmlBar = $generateHtmlFromNodes(newEditor, selection);
expect(htmlBar).toBe('<bar></bar>');
});

expect(onError).not.toHaveBeenCalled();
});
});
});
7 changes: 6 additions & 1 deletion packages/lexical/src/__tests__/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ import {createRef} from 'react';
import {createRoot} from 'react-dom/client';
import * as ReactTestUtils from 'react-dom/test-utils';

import {CreateEditorArgs, LexicalNodeReplacement} from '../../LexicalEditor';
import {
CreateEditorArgs,
HTMLConfig,
LexicalNodeReplacement,
} from '../../LexicalEditor';
import {resetRandomKey} from '../../LexicalUtils';

type TestEnv = {
Expand Down Expand Up @@ -519,6 +523,7 @@ export function createTestEditor(
onError?: (error: Error) => void;
disableEvents?: boolean;
readOnly?: boolean;
html?: HTMLConfig;
} = {},
): LexicalEditor {
const customNodes = config.nodes || [];
Expand Down

0 comments on commit 95827a7

Please sign in to comment.