Skip to content

Commit 95827a7

Browse files
committed
add test
1 parent 2d39181 commit 95827a7

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88

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

22132214
expect(ParagraphNode.importDOM).toHaveBeenCalledTimes(1);
22142215
});
2216+
2217+
describe('html config', () => {
2218+
it('should work correctly', async () => {
2219+
const onError = jest.fn();
2220+
2221+
const newEditor = createTestEditor({
2222+
html: {
2223+
export: new Map([
2224+
[TextNode, () => ({element: document.createElement('figure')})],
2225+
]),
2226+
import: {
2227+
figure: () => ({
2228+
conversion: () => ({node: $createTextNode('yolo')}),
2229+
priority: 4,
2230+
}),
2231+
},
2232+
},
2233+
onError: onError,
2234+
});
2235+
2236+
newEditor.setRootElement(container);
2237+
2238+
newEditor.update(() => {
2239+
const root = $getRoot();
2240+
const paragraph = $createParagraphNode();
2241+
const text = $createTextNode();
2242+
root.append(paragraph);
2243+
paragraph.append(text);
2244+
2245+
const selection = $createNodeSelection();
2246+
selection.add(text.getKey());
2247+
2248+
const html = $generateHtmlFromNodes(newEditor, selection);
2249+
expect(html).toBe('<figure></figure>');
2250+
2251+
const parser = new DOMParser();
2252+
const dom = parser.parseFromString(html, 'text/html');
2253+
const node = $generateNodesFromDOM(newEditor, dom)[0];
2254+
2255+
expect(node).toEqual({
2256+
__detail: 0,
2257+
__format: 0,
2258+
__key: node.getKey(),
2259+
__mode: 0,
2260+
__next: null,
2261+
__parent: null,
2262+
__prev: null,
2263+
__style: '',
2264+
__text: 'yolo',
2265+
__type: 'text',
2266+
});
2267+
});
2268+
2269+
expect(onError).not.toHaveBeenCalled();
2270+
});
2271+
2272+
it('can utilize the methods of the target node in export callback', async () => {
2273+
const onError = jest.fn();
2274+
2275+
const newEditor = createTestEditor({
2276+
html: {
2277+
export: new Map([
2278+
[
2279+
TextNode,
2280+
(_, target: TextNode) => {
2281+
if (target.hasFormat('bold')) {
2282+
return {element: document.createElement('bar')};
2283+
}
2284+
2285+
return {element: document.createElement('foo')};
2286+
},
2287+
],
2288+
]),
2289+
},
2290+
onError: onError,
2291+
});
2292+
2293+
newEditor.setRootElement(container);
2294+
2295+
newEditor.update(() => {
2296+
const root = $getRoot();
2297+
const paragraph = $createParagraphNode();
2298+
const text = $createTextNode();
2299+
root.append(paragraph);
2300+
paragraph.append(text);
2301+
2302+
const selection = $createNodeSelection();
2303+
selection.add(text.getKey());
2304+
2305+
const htmlFoo = $generateHtmlFromNodes(newEditor, selection);
2306+
expect(htmlFoo).toBe('<foo></foo>');
2307+
2308+
text.toggleFormat('bold');
2309+
2310+
const htmlBar = $generateHtmlFromNodes(newEditor, selection);
2311+
expect(htmlBar).toBe('<bar></bar>');
2312+
});
2313+
2314+
expect(onError).not.toHaveBeenCalled();
2315+
});
2316+
});
22152317
});

packages/lexical/src/__tests__/utils/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ import {createRef} from 'react';
4747
import {createRoot} from 'react-dom/client';
4848
import * as ReactTestUtils from 'react-dom/test-utils';
4949

50-
import {CreateEditorArgs, LexicalNodeReplacement} from '../../LexicalEditor';
50+
import {
51+
CreateEditorArgs,
52+
HTMLConfig,
53+
LexicalNodeReplacement,
54+
} from '../../LexicalEditor';
5155
import {resetRandomKey} from '../../LexicalUtils';
5256

5357
type TestEnv = {
@@ -519,6 +523,7 @@ export function createTestEditor(
519523
onError?: (error: Error) => void;
520524
disableEvents?: boolean;
521525
readOnly?: boolean;
526+
html?: HTMLConfig;
522527
} = {},
523528
): LexicalEditor {
524529
const customNodes = config.nodes || [];

0 commit comments

Comments
 (0)