]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/utils/__tests__/unit/LexicalRootHelpers.test.ts
369caaea4af569d69945bfb2ce5fb2bec935cf58
[bookstack] / resources / js / wysiwyg / lexical / utils / __tests__ / unit / LexicalRootHelpers.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   $isRootTextContentEmpty,
11   $isRootTextContentEmptyCurry,
12   $rootTextContent,
13 } from '@lexical/text';
14 import {$createParagraphNode, $createTextNode, $getRoot} from 'lexical';
15 import {initializeUnitTest} from 'lexical/__tests__/utils';
16
17 describe('LexicalRootHelpers tests', () => {
18   initializeUnitTest((testEnv) => {
19     it('textContent', async () => {
20       const editor = testEnv.editor;
21
22       expect(editor.getEditorState().read($rootTextContent)).toBe('');
23
24       await editor.update(() => {
25         const root = $getRoot();
26         const paragraph = $createParagraphNode();
27         const text = $createTextNode('foo');
28         root.append(paragraph);
29         paragraph.append(text);
30
31         expect($rootTextContent()).toBe('foo');
32       });
33
34       expect(editor.getEditorState().read($rootTextContent)).toBe('foo');
35     });
36
37     it('isBlank', async () => {
38       const editor = testEnv.editor;
39
40       expect(
41         editor
42           .getEditorState()
43           .read($isRootTextContentEmptyCurry(editor.isComposing())),
44       ).toBe(true);
45
46       await editor.update(() => {
47         const root = $getRoot();
48         const paragraph = $createParagraphNode();
49         const text = $createTextNode('foo');
50         root.append(paragraph);
51         paragraph.append(text);
52
53         expect($isRootTextContentEmpty(editor.isComposing())).toBe(false);
54       });
55
56       expect(
57         editor
58           .getEditorState()
59           .read($isRootTextContentEmptyCurry(editor.isComposing())),
60       ).toBe(false);
61     });
62   });
63 });