import React from 'react' ; import KeyCode from 'rc-util/lib/KeyCode' ; import { mount , ReactWrapper } from './util/wrapper' ; import InputNumber from '../src' ; describe ( 'InputNumber.Cursor' , ( ) => { function cursorInput ( wrapper , pos ? ) { const input = ( wrapper . findInput ( ) . instance ( ) as any ) as HTMLInputElement ; if ( pos !== undefined ) { input . setSelectionRange ( pos , pos ) ; } return input . selectionStart ; } function changeOnPos ( wrapper , changeValue , cursorPos , which ? ) { wrapper . focusInput ( ) ; wrapper . findInput ( ) . simulate ( 'keyDown' , { which } ) ; ( wrapper . findInput ( ) . instance ( ) as any ) . value = changeValue ; cursorInput ( wrapper , cursorPos ) ; wrapper . findInput ( ) . simulate ( 'change' , { target : { value : changeValue } } ) ; wrapper . findInput ( ) . simulate ( 'keyUp' , { which } ) ; } it ( 'DELETE (not backspace)' , ( ) => { const wrapper = mount ( < InputNumber defaultValue = { 12 } / > ) ; changeOnPos ( wrapper , '12' , 1 , KeyCode . DELETE ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 1 ) ; } ) ; describe ( 'pre-pend string' , ( ) => { it ( 'quick typing' , ( ) => { const wrapper = mount ( < InputNumber defaultValue = "$ " formatter = { ( val ) => `$ ${ val }` } />); wrapper . focusInput ( ) ; cursorInput ( wrapper , 0 ) ; changeOnPos ( wrapper , '9$ ' , 1 , KeyCode . NUM_ONE ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 3 ) ; } ) ; describe ( '[LEGACY]' , ( ) => { const setUpCursorTest = ( initValue , prependValue ) => { const Demo = ( ) => { const [ value , setValue ] = React . useState ( initValue ) ; return ( < InputNumber < string > stringMode value = { value } onChange = { ( newValue ) => { setValue ( newValue ) ; } } formatter = { ( val ) => `$ ${ val }` . replace ( /\B(?=(\d{3})+(?!\d))/g , ',' ) } parser = { ( val ) => val . replace ( /\$\s?|(,*)/g , '' ) } />); } ; const wrapper = mount ( < Demo />); wrapper . focusInput ( ) ; for ( let i = 0 ; i < prependValue . length ; i += 1 ) { wrapper . findInput ( ) . simulate ( 'keyDown' , { which : KeyCode . ONE } ) ; } const finalValue = prependValue + initValue ; ( wrapper . findInput ( ) . instance ( ) as any ) . value = finalValue ; cursorInput ( wrapper , prependValue . length ) ; wrapper . findInput ( ) . simulate ( 'change' , { target : { value : finalValue } } ) ; return wrapper ; } ; it ( 'should fix caret position on case 1' , ( ) => { const wrapper = setUpCursorTest ( '' , '1' ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 3 ) ; } ) ; it ( 'should fix caret position on case 2' , ( ) => { const wrapper = setUpCursorTest ( '' , '111' ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 5 ) ; } ) ; it ( 'should fix caret position on case 3' , ( ) => { const wrapper = setUpCursorTest ( '1' , '11' ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 4 ) ; } ) ; it ( 'should fix caret position on case 4' , ( ) => { const wrapper = setUpCursorTest ( '456' , '123' ) ; expect ( cursorInput ( wrapper ) ) . toEqual ( 6 ) ; } ) ; } ) ; } ) ; } ) ;