]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/lexical/utils/__tests__/unit/LexicalRootHelpers.test.ts
Lexical: Finished conversion/update of test files
[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 {$createParagraphNode, $createTextNode, $getRoot} from 'lexical';
10 import {initializeUnitTest} from 'lexical/__tests__/utils';
11
12 export function $rootTextContent(): string {
13   const root = $getRoot();
14
15   return root.getTextContent();
16 }
17
18 export function $isRootTextContentEmpty(
19     isEditorComposing: boolean,
20     trim = true,
21 ): boolean {
22   if (isEditorComposing) {
23     return false;
24   }
25
26   let text = $rootTextContent();
27
28   if (trim) {
29     text = text.trim();
30   }
31
32   return text === '';
33 }
34
35 export function $isRootTextContentEmptyCurry(
36     isEditorComposing: boolean,
37     trim?: boolean,
38 ): () => boolean {
39   return () => $isRootTextContentEmpty(isEditorComposing, trim);
40 }
41
42 describe('LexicalRootHelpers tests', () => {
43   initializeUnitTest((testEnv) => {
44     it('textContent', async () => {
45       const editor = testEnv.editor;
46
47       expect(editor.getEditorState().read($rootTextContent)).toBe('');
48
49       await editor.update(() => {
50         const root = $getRoot();
51         const paragraph = $createParagraphNode();
52         const text = $createTextNode('foo');
53         root.append(paragraph);
54         paragraph.append(text);
55
56         expect($rootTextContent()).toBe('foo');
57       });
58
59       expect(editor.getEditorState().read($rootTextContent)).toBe('foo');
60     });
61
62     it('isBlank', async () => {
63       const editor = testEnv.editor;
64
65       expect(
66         editor
67           .getEditorState()
68           .read($isRootTextContentEmptyCurry(editor.isComposing())),
69       ).toBe(true);
70
71       await editor.update(() => {
72         const root = $getRoot();
73         const paragraph = $createParagraphNode();
74         const text = $createTextNode('foo');
75         root.append(paragraph);
76         paragraph.append(text);
77
78         expect($isRootTextContentEmpty(editor.isComposing())).toBe(false);
79       });
80
81       expect(
82         editor
83           .getEditorState()
84           .read($isRootTextContentEmptyCurry(editor.isComposing())),
85       ).toBe(false);
86     });
87   });
88 });