@@ -70,6 +70,49 @@ describe('#applyChangeToValue', () => {
7070 )
7171 } )
7272
73+ it ( 'should correctly add characters with cursor in the middle at the end, beginning, and in the middle of text' , ( ) => {
74+ let changed = 'S[start]' + plainText
75+ let result = applyChangeToValue (
76+ value ,
77+ changed ,
78+ {
79+ selectionStartBefore : 0 ,
80+ selectionEndBefore : 0 ,
81+ selectionEndAfter : 1 ,
82+ } ,
83+ config
84+ )
85+ expect ( result ) . toEqual ( 'S[start]' + value )
86+
87+ changed = plainText + 'E[end]'
88+ result = applyChangeToValue (
89+ value ,
90+ changed ,
91+ {
92+ selectionStartBefore : plainText . length ,
93+ selectionEndBefore : plainText . length ,
94+ selectionEndAfter : plainText . length + 1 ,
95+ } ,
96+ config
97+ )
98+ expect ( result ) . toEqual ( value + 'E[end]' )
99+
100+ changed = "Hi John Doe, \n\nlet's M[mid]add [email protected] to this conversation..." 101+ result = applyChangeToValue (
102+ value ,
103+ changed ,
104+ {
105+ selectionStartBefore : 21 ,
106+ selectionEndBefore : 21 ,
107+ selectionEndAfter : 22 ,
108+ } ,
109+ config
110+ )
111+ expect ( result ) . toEqual (
112+ "Hi @[John Doe](user:johndoe), \n\nlet's M[mid]add @[[email protected] ](email:[email protected] ) to this conversation..." 113+ )
114+ } )
115+
73116 it ( 'should correctly delete single characters and ranges of selected text' , ( ) => {
74117 // delete "i"
75118 let changed =
@@ -133,6 +176,20 @@ describe('#applyChangeToValue', () => {
133176 config
134177 )
135178 expect ( result ) . toEqual ( value . replace ( 'add' , 'remove' ) )
179+
180+ // replace range with cursor in the middle, eg. remove|[remove]
181+ changed = plainText . replace ( 'add' , 'remove[remove]' )
182+ result = applyChangeToValue (
183+ value ,
184+ changed ,
185+ {
186+ selectionStartBefore : plainText . indexOf ( 'add' ) ,
187+ selectionEndBefore : plainText . indexOf ( 'add' ) + 'add' . length ,
188+ selectionEndAfter : plainText . indexOf ( 'add' ) + 'remove' . length ,
189+ } ,
190+ config
191+ )
192+ expect ( result ) . toEqual ( value . replace ( 'add' , 'remove[remove]' ) )
136193 } )
137194
138195 it ( 'should remove mentions markup contained in deleted text ranges' , ( ) => {
@@ -257,7 +314,7 @@ describe('#applyChangeToValue', () => {
257314 } )
258315
259316 it ( 'should correctly handle text auto-correction' , ( ) => {
260- const result = applyChangeToValue (
317+ let result = applyChangeToValue (
261318 'ill' ,
262319 "I'll" ,
263320 {
@@ -268,5 +325,20 @@ describe('#applyChangeToValue', () => {
268325 config
269326 )
270327 expect ( result ) . toEqual ( "I'll" )
328+
329+ // case like
330+ // input queue: s -> a -> d
331+ // IME queue(| is the cursor position): s|[sa] -> s'a|[sa'a] -> sad|[sad]
332+ result = applyChangeToValue (
333+ "s'a[sa'a]" ,
334+ "sad[sad]" ,
335+ {
336+ selectionStartBefore : 2 ,
337+ selectionEndBefore : 2 ,
338+ selectionEndAfter : 3 ,
339+ } ,
340+ config
341+ )
342+ expect ( result ) . toEqual ( "sad[sad]" )
271343 } )
272344} )
0 commit comments