1
- import { initializeUnitTest } from "lexical/__tests__/utils" ;
2
- import { SerializedLinkNode } from "@lexical/link" ;
1
+ import {
2
+ createTestContext ,
3
+ dispatchKeydownEventForNode , expectEditorStateJSONPropToEqual ,
4
+ expectNodeShapeToMatch
5
+ } from "lexical/__tests__/utils" ;
3
6
import {
4
7
$getRoot ,
5
8
ParagraphNode ,
6
- SerializedParagraphNode ,
7
- SerializedTextNode ,
8
9
TextNode
9
10
} from "lexical" ;
10
11
import { registerAutoLinks } from "../auto-links" ;
11
12
12
13
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 ( 34 , 34 ) ;
28
- } ) ;
14
+ test ( 'space after link in text' , async ( ) => {
15
+ const { editor} = createTestContext ( ) ;
16
+ registerAutoLinks ( editor ) ;
17
+ let pNode ! : ParagraphNode ;
18
+
19
+ editor . updateAndCommit ( ( ) => {
20
+ pNode = new ParagraphNode ( ) ;
21
+ const text = new TextNode ( 'Some https://example.com?test=true text' ) ;
22
+ pNode . append ( text ) ;
23
+ $getRoot ( ) . append ( pNode ) ;
24
+
25
+ text . select ( 34 , 34 ) ;
26
+ } ) ;
29
27
30
- editor . commitUpdates ( ) ;
28
+ dispatchKeydownEventForNode ( pNode , editor , ' ' ) ;
31
29
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 ) ;
30
+ expectEditorStateJSONPropToEqual ( editor , '0.1.url' , 'https://example.com?test=true' ) ;
31
+ expectEditorStateJSONPropToEqual ( editor , '0.1.0.text' , 'https://example.com?test=true' ) ;
32
+ } ) ;
40
33
41
- editor . commitUpdates ( ) ;
34
+ test ( 'space after link at end of line' , async ( ) => {
35
+ const { editor} = createTestContext ( ) ;
36
+ registerAutoLinks ( editor ) ;
37
+ let pNode ! : ParagraphNode ;
42
38
43
- const paragraph = editor ! . getEditorState ( ) . toJSON ( ) . root
44
- . children [ 0 ] as SerializedParagraphNode ;
45
- expect ( paragraph . children [ 1 ] . type ) . toBe ( 'link' ) ;
39
+ editor . updateAndCommit ( ( ) => {
40
+ pNode = new ParagraphNode ( ) ;
41
+ const text = new TextNode ( 'Some https://example.com?test=true' ) ;
42
+ pNode . append ( text ) ;
43
+ $getRoot ( ) . append ( pNode ) ;
46
44
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' ) ;
45
+ text . selectEnd ( ) ;
51
46
} ) ;
52
47
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 ) ;
48
+ dispatchKeydownEventForNode ( pNode , editor , ' ' ) ;
64
49
65
- text . select ( 34 , 34 ) ;
66
- } ) ;
50
+ expectNodeShapeToMatch ( editor , [ { type : 'paragraph' , children : [
51
+ { text : 'Some ' } ,
52
+ { type : 'link' , children : [ { text : 'https://example.com?test=true' } ] }
53
+ ] } ] ) ;
54
+ expectEditorStateJSONPropToEqual ( editor , '0.1.url' , 'https://example.com?test=true' ) ;
55
+ } ) ;
67
56
68
- editor . commitUpdates ( ) ;
57
+ test ( 'enter after link in text' , async ( ) => {
58
+ const { editor} = createTestContext ( ) ;
59
+ registerAutoLinks ( editor ) ;
60
+ let pNode ! : ParagraphNode ;
69
61
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 ) ;
62
+ editor . updateAndCommit ( ( ) => {
63
+ pNode = new ParagraphNode ( ) ;
64
+ const text = new TextNode ( 'Some https://example.com?test=true text' ) ;
65
+ pNode . append ( text ) ;
66
+ $getRoot ( ) . append ( pNode ) ;
78
67
79
- editor . commitUpdates ( ) ;
68
+ text . select ( 34 , 34 ) ;
69
+ } ) ;
80
70
81
- const paragraph = editor ! . getEditorState ( ) . toJSON ( ) . root
82
- . children [ 0 ] as SerializedParagraphNode ;
83
- expect ( paragraph . children [ 1 ] . type ) . toBe ( 'link' ) ;
71
+ dispatchKeydownEventForNode ( pNode , editor , 'Enter' ) ;
84
72
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
- } ) ;
73
+ expectEditorStateJSONPropToEqual ( editor , '0.1.url' , 'https://example.com?test=true' ) ;
74
+ expectEditorStateJSONPropToEqual ( editor , '0.1.0.text' , 'https://example.com?test=true' ) ;
90
75
} ) ;
91
76
} ) ;
0 commit comments