]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/services/__tests__/auto-links.test.ts
Lexical: Added auto links on enter/space
[bookstack] / resources / js / wysiwyg / services / __tests__ / auto-links.test.ts
1 import {initializeUnitTest} from "lexical/__tests__/utils";
2 import {SerializedLinkNode} from "@lexical/link";
3 import {
4     $getRoot,
5     ParagraphNode,
6     SerializedParagraphNode,
7     SerializedTextNode,
8     TextNode
9 } from "lexical";
10 import {registerAutoLinks} from "../auto-links";
11
12 describe('Auto-link service tests', () => {
13     initializeUnitTest((testEnv) => {
14
15         test('space after link in text', async () => {
16             const {editor} = testEnv;
17
18             registerAutoLinks(editor);
19             let pNode!: ParagraphNode;
20
21             editor.update(() => {
22                 pNode = new ParagraphNode();
23                 const text = new TextNode('Some https://example.com?test=true text');
24                 pNode.append(text);
25                 $getRoot().append(pNode);
26
27                 text.select(35, 35);
28             });
29
30             editor.commitUpdates();
31
32             const pDomEl = editor.getElementByKey(pNode.getKey());
33             const event = new KeyboardEvent('keydown', {
34                 bubbles: true,
35                 cancelable: true,
36                 key: ' ',
37                 keyCode: 62,
38             });
39             pDomEl?.dispatchEvent(event);
40
41             editor.commitUpdates();
42
43             const paragraph = editor!.getEditorState().toJSON().root
44                 .children[0] as SerializedParagraphNode;
45             expect(paragraph.children[1].type).toBe('link');
46
47             const link = paragraph.children[1] as SerializedLinkNode;
48             expect(link.url).toBe('https://example.com?test=true');
49             const linkText = link.children[0] as SerializedTextNode;
50             expect(linkText.text).toBe('https://example.com?test=true');
51         });
52
53         test('enter after link in text', async () => {
54             const {editor} = testEnv;
55
56             registerAutoLinks(editor);
57             let pNode!: ParagraphNode;
58
59             editor.update(() => {
60                 pNode = new ParagraphNode();
61                 const text = new TextNode('Some https://example.com?test=true text');
62                 pNode.append(text);
63                 $getRoot().append(pNode);
64
65                 text.select(35, 35);
66             });
67
68             editor.commitUpdates();
69
70             const pDomEl = editor.getElementByKey(pNode.getKey());
71             const event = new KeyboardEvent('keydown', {
72                 bubbles: true,
73                 cancelable: true,
74                 key: 'Enter',
75                 keyCode: 66,
76             });
77             pDomEl?.dispatchEvent(event);
78
79             editor.commitUpdates();
80
81             const paragraph = editor!.getEditorState().toJSON().root
82                 .children[0] as SerializedParagraphNode;
83             expect(paragraph.children[1].type).toBe('link');
84
85             const link = paragraph.children[1] as SerializedLinkNode;
86             expect(link.url).toBe('https://example.com?test=true');
87             const linkText = link.children[0] as SerializedTextNode;
88             expect(linkText.text).toBe('https://example.com?test=true');
89         });
90     });
91 });