]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/core/__tests__/unit/LexicalEditorState.test.ts
38ecf03bce2b87e4ddb146a73cb991a0e00fb824
[bookstack] / resources / js / wysiwyg / lexical / core / __tests__ / unit / LexicalEditorState.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 {
10   $createParagraphNode,
11   $createTextNode,
12   $getEditor,
13   $getRoot,
14   ParagraphNode,
15   TextNode,
16 } from 'lexical';
17
18 import {EditorState} from '../../LexicalEditorState';
19 import {$createRootNode, RootNode} from '../../nodes/LexicalRootNode';
20 import {initializeUnitTest} from '../utils';
21
22 describe('LexicalEditorState tests', () => {
23   initializeUnitTest((testEnv) => {
24     test('constructor', async () => {
25       const root = $createRootNode();
26       const nodeMap = new Map([['root', root]]);
27
28       const editorState = new EditorState(nodeMap);
29       expect(editorState._nodeMap).toBe(nodeMap);
30       expect(editorState._selection).toBe(null);
31     });
32
33     test('read()', async () => {
34       const {editor} = testEnv;
35
36       await editor.update(() => {
37         const paragraph = $createParagraphNode();
38         const text = $createTextNode('foo');
39         paragraph.append(text);
40         $getRoot().append(paragraph);
41       });
42
43       let root!: RootNode;
44       let paragraph!: ParagraphNode;
45       let text!: TextNode;
46
47       editor.getEditorState().read(() => {
48         root = $getRoot();
49         paragraph = root.getFirstChild()!;
50         text = paragraph.getFirstChild()!;
51       });
52
53       expect(root).toEqual({
54         __cachedText: 'foo',
55         __dir: null,
56         __first: '1',
57         __format: 0,
58         __indent: 0,
59         __key: 'root',
60         __last: '1',
61         __next: null,
62         __parent: null,
63         __prev: null,
64         __size: 1,
65         __style: '',
66         __type: 'root',
67       });
68       expect(paragraph).toEqual({
69         __dir: null,
70         __first: '2',
71         __format: 0,
72         __indent: 0,
73         __key: '1',
74         __last: '2',
75         __next: null,
76         __parent: 'root',
77         __prev: null,
78         __size: 1,
79         __style: '',
80         __textFormat: 0,
81         __textStyle: '',
82         __type: 'paragraph',
83       });
84       expect(text).toEqual({
85         __detail: 0,
86         __format: 0,
87         __key: '2',
88         __mode: 0,
89         __next: null,
90         __parent: '1',
91         __prev: null,
92         __style: '',
93         __text: 'foo',
94         __type: 'text',
95       });
96       expect(() => editor.getEditorState().read(() => $getEditor())).toThrow(
97         /Unable to find an active editor/,
98       );
99       expect(
100         editor.getEditorState().read(() => $getEditor(), {editor: editor}),
101       ).toBe(editor);
102     });
103
104     test('toJSON()', async () => {
105       const {editor} = testEnv;
106
107       await editor.update(() => {
108         const paragraph = $createParagraphNode();
109         const text = $createTextNode('Hello world');
110         text.select(6, 11);
111         paragraph.append(text);
112         $getRoot().append(paragraph);
113       });
114
115       expect(JSON.stringify(editor.getEditorState().toJSON())).toEqual(
116         `{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"Hello world","type":"text","version":1}],"direction":null,"format":"","indent":0,"type":"paragraph","version":1,"textFormat":0,"textStyle":""}],"direction":null,"format":"","indent":0,"type":"root","version":1}}`,
117       );
118     });
119
120     test('ensure garbage collection works as expected', async () => {
121       const {editor} = testEnv;
122
123       await editor.update(() => {
124         const paragraph = $createParagraphNode();
125         const text = $createTextNode('foo');
126         paragraph.append(text);
127         $getRoot().append(paragraph);
128       });
129       // Remove the first node, which should cause a GC for everything
130
131       await editor.update(() => {
132         $getRoot().getFirstChild()!.remove();
133       });
134
135       expect(editor.getEditorState()._nodeMap).toEqual(
136         new Map([
137           [
138             'root',
139             {
140               __cachedText: '',
141               __dir: null,
142               __first: null,
143               __format: 0,
144               __indent: 0,
145               __key: 'root',
146               __last: null,
147               __next: null,
148               __parent: null,
149               __prev: null,
150               __size: 0,
151               __style: '',
152               __type: 'root',
153             },
154           ],
155         ]),
156       );
157     });
158   });
159 });