1
1
import {
2
- createTestContext ,
2
+ createTestContext , destroyFromContext ,
3
3
dispatchKeydownEventForNode ,
4
4
dispatchKeydownEventForSelectedNode ,
5
- initializeUnitTest
6
5
} from "lexical/__tests__/utils" ;
7
6
import {
8
7
$createParagraphNode , $createTextNode ,
9
- $getRoot , LexicalNode ,
10
- ParagraphNode ,
8
+ $getRoot , $getSelection , LexicalEditor , LexicalNode ,
9
+ ParagraphNode , TextNode ,
11
10
} from "lexical" ;
12
11
import { $createDetailsNode , DetailsNode } from "@lexical/rich-text/LexicalDetailsNode" ;
13
12
import { registerKeyboardHandling } from "../keyboard-handling" ;
14
13
import { registerRichText } from "@lexical/rich-text" ;
14
+ import { EditorUiContext } from "../../ui/framework/core" ;
15
+ import { $createListItemNode , $createListNode , ListItemNode , ListNode } from "@lexical/list" ;
15
16
16
17
describe ( 'Keyboard-handling service tests' , ( ) => {
17
- initializeUnitTest ( ( testEnv ) => {
18
18
19
- test ( 'Details: down key on last lines creates new sibling node' , ( ) => {
20
- const { editor} = testEnv ;
19
+ let context ! : EditorUiContext ;
20
+ let editor ! : LexicalEditor ;
21
21
22
- registerRichText ( editor ) ;
23
- registerKeyboardHandling ( createTestContext ( testEnv ) ) ;
22
+ beforeEach ( ( ) => {
23
+ context = createTestContext ( ) ;
24
+ editor = context . editor ;
25
+ registerRichText ( editor ) ;
26
+ registerKeyboardHandling ( context ) ;
27
+ } ) ;
24
28
25
- let lastRootChild ! : LexicalNode | null ;
26
- let detailsPara ! : ParagraphNode ;
29
+ afterEach ( ( ) => {
30
+ destroyFromContext ( context ) ;
31
+ } ) ;
27
32
28
- editor . updateAndCommit ( ( ) => {
29
- const root = $getRoot ( )
30
- const details = $createDetailsNode ( ) ;
31
- detailsPara = $createParagraphNode ( ) ;
32
- details . append ( detailsPara ) ;
33
- $getRoot ( ) . append ( details ) ;
34
- detailsPara . select ( ) ;
33
+ test ( 'Details: down key on last lines creates new sibling node' , ( ) => {
34
+ let lastRootChild ! : LexicalNode | null ;
35
+ let detailsPara ! : ParagraphNode ;
35
36
36
- lastRootChild = root . getLastChild ( ) ;
37
- } ) ;
37
+ editor . updateAndCommit ( ( ) => {
38
+ const root = $getRoot ( )
39
+ const details = $createDetailsNode ( ) ;
40
+ detailsPara = $createParagraphNode ( ) ;
41
+ details . append ( detailsPara ) ;
42
+ $getRoot ( ) . append ( details ) ;
43
+ detailsPara . select ( ) ;
38
44
39
- expect ( lastRootChild ) . toBeInstanceOf ( DetailsNode ) ;
45
+ lastRootChild = root . getLastChild ( ) ;
46
+ } ) ;
40
47
41
- dispatchKeydownEventForNode ( detailsPara , editor , 'ArrowDown' ) ;
42
- editor . commitUpdates ( ) ;
48
+ expect ( lastRootChild ) . toBeInstanceOf ( DetailsNode ) ;
43
49
44
- editor . getEditorState ( ) . read ( ( ) => {
45
- lastRootChild = $getRoot ( ) . getLastChild ( ) ;
46
- } ) ;
50
+ dispatchKeydownEventForNode ( detailsPara , editor , 'ArrowDown' ) ;
51
+ editor . commitUpdates ( ) ;
47
52
48
- expect ( lastRootChild ) . toBeInstanceOf ( ParagraphNode ) ;
53
+ editor . getEditorState ( ) . read ( ( ) => {
54
+ lastRootChild = $getRoot ( ) . getLastChild ( ) ;
49
55
} ) ;
50
56
51
- test ( 'Details: enter on last empy block creates new sibling node' , ( ) => {
52
- const { editor} = testEnv ;
57
+ expect ( lastRootChild ) . toBeInstanceOf ( ParagraphNode ) ;
58
+ } ) ;
59
+
60
+ test ( 'Details: enter on last empty block creates new sibling node' , ( ) => {
61
+ registerRichText ( editor ) ;
62
+
63
+ let lastRootChild ! : LexicalNode | null ;
64
+ let detailsPara ! : ParagraphNode ;
53
65
54
- registerRichText ( editor ) ;
55
- registerKeyboardHandling ( createTestContext ( testEnv ) ) ;
66
+ editor . updateAndCommit ( ( ) => {
67
+ const root = $getRoot ( )
68
+ const details = $createDetailsNode ( ) ;
69
+ const text = $createTextNode ( 'Hello!' ) ;
70
+ detailsPara = $createParagraphNode ( ) ;
71
+ detailsPara . append ( text ) ;
72
+ details . append ( detailsPara ) ;
73
+ $getRoot ( ) . append ( details ) ;
74
+ text . selectEnd ( ) ;
56
75
57
- let lastRootChild ! : LexicalNode | null ;
58
- let detailsPara ! : ParagraphNode ;
76
+ lastRootChild = root . getLastChild ( ) ;
77
+ } ) ;
78
+
79
+ expect ( lastRootChild ) . toBeInstanceOf ( DetailsNode ) ;
80
+
81
+ dispatchKeydownEventForNode ( detailsPara , editor , 'Enter' ) ;
82
+ editor . commitUpdates ( ) ;
59
83
60
- editor . updateAndCommit ( ( ) => {
61
- const root = $getRoot ( )
62
- const details = $createDetailsNode ( ) ;
63
- const text = $createTextNode ( 'Hello!' ) ;
64
- detailsPara = $createParagraphNode ( ) ;
65
- detailsPara . append ( text ) ;
66
- details . append ( detailsPara ) ;
67
- $getRoot ( ) . append ( details ) ;
68
- text . selectEnd ( ) ;
84
+ dispatchKeydownEventForSelectedNode ( editor , 'Enter' ) ;
85
+ editor . commitUpdates ( ) ;
69
86
70
- lastRootChild = root . getLastChild ( ) ;
71
- } ) ;
87
+ let detailsChildren ! : LexicalNode [ ] ;
88
+ let lastDetailsText ! : string ;
72
89
73
- expect ( lastRootChild ) . toBeInstanceOf ( DetailsNode ) ;
90
+ editor . getEditorState ( ) . read ( ( ) => {
91
+ detailsChildren = ( lastRootChild as DetailsNode ) . getChildren ( ) ;
92
+ lastRootChild = $getRoot ( ) . getLastChild ( ) ;
93
+ lastDetailsText = detailsChildren [ 0 ] . getTextContent ( ) ;
94
+ } ) ;
74
95
75
- dispatchKeydownEventForNode ( detailsPara , editor , 'Enter' ) ;
76
- editor . commitUpdates ( ) ;
96
+ expect ( lastRootChild ) . toBeInstanceOf ( ParagraphNode ) ;
97
+ expect ( detailsChildren ) . toHaveLength ( 1 ) ;
98
+ expect ( lastDetailsText ) . toBe ( 'Hello!' ) ;
99
+ } ) ;
77
100
78
- dispatchKeydownEventForSelectedNode ( editor , 'Enter' ) ;
79
- editor . commitUpdates ( ) ;
101
+ test ( 'Lists: tab on empty list item insets item' , ( ) => {
80
102
81
- let detailsChildren ! : LexicalNode [ ] ;
82
- let lastDetailsText ! : string ;
103
+ let list ! : ListNode ;
104
+ let listItemB ! : ListItemNode ;
83
105
84
- editor . getEditorState ( ) . read ( ( ) => {
85
- detailsChildren = ( lastRootChild as DetailsNode ) . getChildren ( ) ;
86
- lastRootChild = $getRoot ( ) . getLastChild ( ) ;
87
- lastDetailsText = detailsChildren [ 0 ] . getTextContent ( ) ;
88
- } ) ;
106
+ editor . updateAndCommit ( ( ) => {
107
+ const root = $getRoot ( ) ;
108
+ list = $createListNode ( 'bullet' ) ;
109
+ const listItemA = $createListItemNode ( ) ;
110
+ listItemA . append ( $createTextNode ( 'Hello!' ) ) ;
111
+ listItemB = $createListItemNode ( ) ;
112
+ list . append ( listItemA , listItemB ) ;
113
+ root . append ( list ) ;
114
+ listItemB . selectStart ( ) ;
115
+ } ) ;
89
116
90
- expect ( lastRootChild ) . toBeInstanceOf ( ParagraphNode ) ;
91
- expect ( detailsChildren ) . toHaveLength ( 1 ) ;
92
- expect ( lastDetailsText ) . toBe ( 'Hello!' ) ;
117
+ dispatchKeydownEventForNode ( listItemB , editor , 'Tab' ) ;
118
+ editor . commitUpdates ( ) ;
119
+
120
+ editor . getEditorState ( ) . read ( ( ) => {
121
+ const list = $getRoot ( ) . getChildren ( ) [ 0 ] as ListNode ;
122
+ const listChild = list . getChildren ( ) [ 0 ] as ListItemNode ;
123
+ const children = listChild . getChildren ( ) ;
124
+ expect ( children ) . toHaveLength ( 2 ) ;
125
+ expect ( children [ 0 ] ) . toBeInstanceOf ( TextNode ) ;
126
+ expect ( children [ 0 ] . getTextContent ( ) ) . toBe ( 'Hello!' ) ;
127
+ expect ( children [ 1 ] ) . toBeInstanceOf ( ListNode ) ;
128
+
129
+ const innerList = children [ 1 ] as ListNode ;
130
+ const selectedNode = $getSelection ( ) ?. getNodes ( ) [ 0 ] ;
131
+ expect ( selectedNode ) . toBeInstanceOf ( ListItemNode ) ;
132
+ expect ( selectedNode ?. getKey ( ) ) . toBe ( innerList . getChildren ( ) [ 0 ] . getKey ( ) ) ;
93
133
} ) ;
94
134
} ) ;
95
135
} ) ;
0 commit comments