1 import {initializeUnitTest} from "lexical/__tests__/utils";
2 import {SerializedLinkNode} from "@lexical/link";
6 SerializedParagraphNode,
10 import {registerAutoLinks} from "../auto-links";
12 describe('Auto-link service tests', () => {
13 initializeUnitTest((testEnv) => {
15 test('space after link in text', async () => {
16 const {editor} = testEnv;
18 registerAutoLinks(editor);
19 let pNode!: ParagraphNode;
22 pNode = new ParagraphNode();
23 const text = new TextNode('Some https://example.com?test=true text');
25 $getRoot().append(pNode);
30 editor.commitUpdates();
32 const pDomEl = editor.getElementByKey(pNode.getKey());
33 const event = new KeyboardEvent('keydown', {
39 pDomEl?.dispatchEvent(event);
41 editor.commitUpdates();
43 const paragraph = editor!.getEditorState().toJSON().root
44 .children[0] as SerializedParagraphNode;
45 expect(paragraph.children[1].type).toBe('link');
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');
53 test('enter after link in text', async () => {
54 const {editor} = testEnv;
56 registerAutoLinks(editor);
57 let pNode!: ParagraphNode;
60 pNode = new ParagraphNode();
61 const text = new TextNode('Some https://example.com?test=true text');
63 $getRoot().append(pNode);
68 editor.commitUpdates();
70 const pDomEl = editor.getElementByKey(pNode.getKey());
71 const event = new KeyboardEvent('keydown', {
77 pDomEl?.dispatchEvent(event);
79 editor.commitUpdates();
81 const paragraph = editor!.getEditorState().toJSON().root
82 .children[0] as SerializedParagraphNode;
83 expect(paragraph.children[1].type).toBe('link');
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');