]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/table/__tests__/unit/LexicalTableSelection.test.ts
1548216cf1aefbfd555d95f15fb6d64ff1ca5777
[bookstack] / resources / js / wysiwyg / lexical / table / __tests__ / unit / LexicalTableSelection.test.ts
1 /**
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8
9 import {$createTableSelection} from '@lexical/table';
10 import {
11   $createParagraphNode,
12   $createTextNode,
13   $getRoot,
14   $setSelection,
15   EditorState,
16   type LexicalEditor,
17   ParagraphNode,
18   RootNode,
19   TextNode,
20 } from 'lexical';
21 import {createTestEditor} from 'lexical/__tests__/utils';
22
23 describe('table selection', () => {
24   let originalText: TextNode;
25   let parsedParagraph: ParagraphNode;
26   let parsedRoot: RootNode;
27   let parsedText: TextNode;
28   let paragraphKey: string;
29   let textKey: string;
30   let parsedEditorState: EditorState;
31   let root: HTMLDivElement;
32   let container: HTMLDivElement | null = null;
33   let editor: LexicalEditor | null = null;
34
35   beforeEach(() => {
36     container = document.createElement('div');
37     root = document.createElement('div');
38     root.setAttribute('contenteditable', 'true');
39     document.body.appendChild(container);
40   });
41
42   afterEach(() => {
43     container?.remove();
44   });
45
46   function init(onError?: () => void) {
47     editor = createTestEditor({
48       nodes: [],
49       onError: onError || jest.fn(),
50       theme: {
51         text: {
52           bold: 'editor-text-bold',
53           italic: 'editor-text-italic',
54           underline: 'editor-text-underline',
55         },
56       },
57     })
58
59     editor.setRootElement(root);
60   }
61
62   async function update(fn: () => void) {
63     editor!.update(fn);
64
65     return Promise.resolve().then();
66   }
67
68   beforeEach(async () => {
69     init();
70
71     await update(() => {
72       const paragraph = $createParagraphNode();
73       originalText = $createTextNode('Hello world');
74       const selection = $createTableSelection();
75       selection.set(
76         originalText.getKey(),
77         originalText.getKey(),
78         originalText.getKey(),
79       );
80       $setSelection(selection);
81       paragraph.append(originalText);
82       $getRoot().append(paragraph);
83     });
84
85     const stringifiedEditorState = JSON.stringify(
86       editor!.getEditorState().toJSON(),
87     );
88
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();
96     });
97   });
98
99   it('Parses the nodes of a stringified editor state', async () => {
100     expect(parsedRoot).toEqual({
101       __cachedText: null,
102       __dir: null,
103       __first: paragraphKey,
104       __key: 'root',
105       __last: paragraphKey,
106       __next: null,
107       __parent: null,
108       __prev: null,
109       __size: 1,
110       __style: '',
111       __type: 'root',
112     });
113     expect(parsedParagraph).toEqual({
114       __alignment: "",
115       __dir: null,
116       __first: textKey,
117       __id: '',
118       __inset: 0,
119       __key: paragraphKey,
120       __last: textKey,
121       __next: null,
122       __parent: 'root',
123       __prev: null,
124       __size: 1,
125       __style: '',
126       __textStyle: '',
127       __type: 'paragraph',
128     });
129     expect(parsedText).toEqual({
130       __detail: 0,
131       __format: 0,
132       __key: textKey,
133       __mode: 0,
134       __next: null,
135       __parent: paragraphKey,
136       __prev: null,
137       __style: '',
138       __text: 'Hello world',
139       __type: 'text',
140     });
141   });
142
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(
146       'Hello world',
147     );
148   });
149 });