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.
9 import {$createTableSelection} from '@lexical/table';
21 import {createTestEditor} from 'lexical/__tests__/utils';
23 describe('table selection', () => {
24 let originalText: TextNode;
25 let parsedParagraph: ParagraphNode;
26 let parsedRoot: RootNode;
27 let parsedText: TextNode;
28 let paragraphKey: string;
30 let parsedEditorState: EditorState;
31 let root: HTMLDivElement;
32 let container: HTMLDivElement | null = null;
33 let editor: LexicalEditor | null = null;
36 container = document.createElement('div');
37 root = document.createElement('div');
38 root.setAttribute('contenteditable', 'true');
39 document.body.appendChild(container);
46 function init(onError?: () => void) {
47 editor = createTestEditor({
49 onError: onError || jest.fn(),
52 bold: 'editor-text-bold',
53 italic: 'editor-text-italic',
54 underline: 'editor-text-underline',
59 editor.setRootElement(root);
62 async function update(fn: () => void) {
65 return Promise.resolve().then();
68 beforeEach(async () => {
72 const paragraph = $createParagraphNode();
73 originalText = $createTextNode('Hello world');
74 const selection = $createTableSelection();
76 originalText.getKey(),
77 originalText.getKey(),
78 originalText.getKey(),
80 $setSelection(selection);
81 paragraph.append(originalText);
82 $getRoot().append(paragraph);
85 const stringifiedEditorState = JSON.stringify(
86 editor!.getEditorState().toJSON(),
89 parsedEditorState = editor!.parseEditorState(stringifiedEditorState);
90 parsedEditorState.read(() => {
91 parsedRoot = $getRoot();
92 parsedParagraph = parsedRoot.getFirstChild()!;
93 paragraphKey = parsedParagraph.getKey();
94 parsedText = parsedParagraph.getFirstChild()!;
95 textKey = parsedText.getKey();
99 it('Parses the nodes of a stringified editor state', async () => {
100 expect(parsedRoot).toEqual({
103 __first: paragraphKey,
105 __last: paragraphKey,
113 expect(parsedParagraph).toEqual({
129 expect(parsedText).toEqual({
135 __parent: paragraphKey,
138 __text: 'Hello world',
143 it('Parses the text content of the editor state', async () => {
144 expect(parsedEditorState.read(() => $getRoot().__cachedText)).toBe(null);
145 expect(parsedEditorState.read(() => $getRoot().getTextContent())).toBe(