@@ -4,23 +4,36 @@ test('EditorMarkdown', () => {
4
4
const textarea = document . createElement ( 'textarea' ) ;
5
5
initTextareaMarkdown ( textarea ) ;
6
6
7
- const testInput = ( value , expected ) => {
8
- textarea . value = value ;
9
- textarea . setSelectionRange ( value . length , value . length ) ;
7
+ type ValueWithCursor = string | {
8
+ value : string ;
9
+ pos : number ;
10
+ }
11
+ const testInput = ( input : ValueWithCursor , result : ValueWithCursor ) => {
12
+ const intputValue = typeof input === 'string' ? input : input . value ;
13
+ const inputPos = typeof input === 'string' ? intputValue . length : input . pos ;
14
+ textarea . value = intputValue ;
15
+ textarea . setSelectionRange ( inputPos , inputPos ) ;
16
+
10
17
const e = new KeyboardEvent ( 'keydown' , { key : 'Enter' , cancelable : true } ) ;
11
18
textarea . dispatchEvent ( e ) ;
12
- if ( ! e . defaultPrevented ) textarea . value += '\n' ;
13
- expect ( textarea . value ) . toEqual ( expected ) ;
19
+ if ( ! e . defaultPrevented ) textarea . value += '\n' ; // simulate default behavior
20
+
21
+ const expectedValue = typeof result === 'string' ? result : result . value ;
22
+ const expectedPos = typeof result === 'string' ? expectedValue . length : result . pos ;
23
+ expect ( textarea . value ) . toEqual ( expectedValue ) ;
24
+ expect ( textarea . selectionStart ) . toEqual ( expectedPos ) ;
14
25
} ;
15
26
16
27
testInput ( '-' , '-\n' ) ;
17
28
testInput ( '1.' , '1.\n' ) ;
18
29
19
30
testInput ( '- ' , '' ) ;
20
31
testInput ( '1. ' , '' ) ;
32
+ testInput ( { value : '1. \n2. ' , pos : 3 } , { value : '\n2. ' , pos : 0 } ) ;
21
33
22
34
testInput ( '- x' , '- x\n- ' ) ;
23
35
testInput ( '1. foo' , '1. foo\n2. ' ) ;
36
+ testInput ( { value : '1. a\n2. b\n3. c' , pos : 4 } , { value : '1. a\n2. \n2. b\n3. c' , pos : 8 } ) ;
24
37
testInput ( '- [ ]' , '- [ ]\n- ' ) ;
25
38
testInput ( '- [ ] foo' , '- [ ] foo\n- [ ] ' ) ;
26
39
testInput ( '* [x] foo' , '* [x] foo\n* [ ] ' ) ;
0 commit comments