2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
17 import {initializeUnitTest} from '../../../__tests__/utils';
19 const editorConfig = Object.freeze({
22 paragraph: 'my-paragraph-class',
26 describe('LexicalParagraphNode tests', () => {
27 initializeUnitTest((testEnv) => {
28 test('ParagraphNode.constructor', async () => {
29 const {editor} = testEnv;
31 await editor.update(() => {
32 const paragraphNode = new ParagraphNode();
34 expect(paragraphNode.getType()).toBe('paragraph');
35 expect(paragraphNode.getTextContent()).toBe('');
37 expect(() => new ParagraphNode()).toThrow();
40 test('ParagraphNode.exportJSON() should return and object conforming to the expected schema', async () => {
41 const {editor} = testEnv;
43 await editor.update(() => {
44 const node = $createParagraphNode();
46 // If you broke this test, you changed the public interface of a
47 // serialized Lexical Core Node. Please ensure the correct adapter
48 // logic is in place in the corresponding importJSON method
49 // to accomodate these changes.
50 expect(node.exportJSON()).toStrictEqual({
63 test('ParagraphNode.createDOM()', async () => {
64 const {editor} = testEnv;
66 await editor.update(() => {
67 const paragraphNode = new ParagraphNode();
69 expect(paragraphNode.createDOM(editorConfig).outerHTML).toBe(
70 '<p class="my-paragraph-class"></p>',
73 paragraphNode.createDOM({
81 test('ParagraphNode.updateDOM()', async () => {
82 const {editor} = testEnv;
84 await editor.update(() => {
85 const paragraphNode = new ParagraphNode();
86 const domElement = paragraphNode.createDOM(editorConfig);
88 expect(domElement.outerHTML).toBe('<p class="my-paragraph-class"></p>');
90 const newParagraphNode = new ParagraphNode();
91 const result = newParagraphNode.updateDOM(
97 expect(result).toBe(false);
98 expect(domElement.outerHTML).toBe('<p class="my-paragraph-class"></p>');
102 test('ParagraphNode.insertNewAfter()', async () => {
103 const {editor} = testEnv;
104 let paragraphNode: ParagraphNode;
106 await editor.update(() => {
107 const root = $getRoot();
108 paragraphNode = new ParagraphNode();
109 root.append(paragraphNode);
112 expect(testEnv.outerHTML).toBe(
113 '<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><p><br></p></div>',
116 await editor.update(() => {
117 const selection = paragraphNode.select();
118 const result = paragraphNode.insertNewAfter(
119 selection as RangeSelection,
122 expect(result).toBeInstanceOf(ParagraphNode);
123 expect(result.getDirection()).toEqual(paragraphNode.getDirection());
124 expect(testEnv.outerHTML).toBe(
125 '<div contenteditable="true" style="user-select: text; white-space: pre-wrap; word-break: break-word;" data-lexical-editor="true"><p><br></p></div>',
130 test('id is supported', async () => {
131 const {editor} = testEnv;
132 let paragraphNode: ParagraphNode;
134 await editor.update(() => {
135 paragraphNode = new ParagraphNode();
136 paragraphNode.setId('testid')
137 $getRoot().append(paragraphNode);
140 expect(testEnv.innerHTML).toBe(
141 '<p id="testid"><br></p>',
145 test('$createParagraphNode()', async () => {
146 const {editor} = testEnv;
148 await editor.update(() => {
149 const paragraphNode = new ParagraphNode();
150 const createdParagraphNode = $createParagraphNode();
152 expect(paragraphNode.__type).toEqual(createdParagraphNode.__type);
153 expect(paragraphNode.__parent).toEqual(createdParagraphNode.__parent);
154 expect(paragraphNode.__key).not.toEqual(createdParagraphNode.__key);
158 test('$isParagraphNode()', async () => {
159 const {editor} = testEnv;
161 await editor.update(() => {
162 const paragraphNode = new ParagraphNode();
164 expect($isParagraphNode(paragraphNode)).toBe(true);