1- import expect from 'expect'
2- import React from 'react'
3- import { render , unmountComponentAtNode } from 'react-dom'
1+ import expect from 'expect' ;
2+ import React from 'react' ;
3+ import { render , unmountComponentAtNode } from 'react-dom' ;
44import Enzyme , { mount , shallow } from 'enzyme' ;
55import Adapter from 'enzyme-adapter-react-16' ;
66
7- import JsonPathEditor from 'src/'
7+ import JsonPathEditor from 'src/' ;
88
99Enzyme . configure ( { adapter : new Adapter ( ) } ) ;
1010
11- import { setCaretPosition , getInputSelection , insertAtCursor } from " ../src/components/getInputSelection" ;
11+ import { setCaretPosition , getInputSelection , insertAtCursor } from ' ../src/components/getInputSelection' ;
1212
1313describe ( 'input selection helpers' , ( ) => {
1414 it ( 'should return and set expected positions' , ( ) => {
15- const wrapper = mount ( < JsonPathEditor value = 'a' /> )
15+ const wrapper = mount ( < JsonPathEditor value = 'a' /> ) ;
1616 let input = wrapper . instance ( ) . inputRef . current ;
1717 expect ( getInputSelection ( input ) ) . toEqual ( { start : 1 , end : 1 } ) ;
1818 setCaretPosition ( input , 0 ) ;
19- } )
19+ } ) ;
20+
21+ it ( 'should not set carret when element is null' , ( ) => {
22+ const wrapper = mount ( < JsonPathEditor value = 'a' /> ) ;
23+ let input = wrapper . instance ( ) . inputRef . current ;
24+ setCaretPosition ( null , 0 ) ;
25+ expect ( getInputSelection ( input ) ) . toEqual ( { start : 1 , end : 1 } ) ;
26+ } ) ;
2027
2128 it ( 'should set carret at 0' , ( ) => {
22- const wrapper = mount ( < JsonPathEditor /> )
29+ const wrapper = mount ( < JsonPathEditor /> ) ;
2330 let input = wrapper . instance ( ) . inputRef . current ;
2431 setCaretPosition ( input , 0 ) ;
2532 expect ( getInputSelection ( input ) ) . toEqual ( { start : 0 , end : 0 } ) ;
26- } )
33+ } ) ;
2734
2835 it ( 'should set carret at 0' , ( ) => {
29- const wrapper = mount ( < JsonPathEditor /> )
36+ const wrapper = mount ( < JsonPathEditor /> ) ;
3037 let input = wrapper . instance ( ) . inputRef . current ;
3138 setCaretPosition ( input , 0 ) ;
3239 expect ( getInputSelection ( input ) ) . toEqual ( { start : 0 , end : 0 } ) ;
33- } )
40+ } ) ;
3441
3542 it ( 'should set carret at 5' , ( ) => {
3643 const spyMove = expect . createSpy ( ) ;
3744 const spySelect = expect . createSpy ( ) ;
3845 const spy = expect . createSpy ( ) . andCall ( function ( ) {
39- return { move : spyMove , select : spySelect }
40- } ) ;
46+ return { move : spyMove , select : spySelect } ;
47+ } ) ;
4148 setCaretPosition ( { createTextRange : spy } , 5 ) ;
4249 expect ( spy ) . toHaveBeenCalledWith ( ) ;
4350 expect ( spyMove ) . toHaveBeenCalledWith ( 'character' , 5 ) ;
4451 expect ( spySelect ) . toHaveBeenCalledWith ( ) ;
45- } )
52+ } ) ;
4653
4754 it ( 'should return text at position' , ( ) => {
48- const wrapper = mount ( < JsonPathEditor value = 'a' /> )
55+ const wrapper = mount ( < JsonPathEditor value = 'a' /> ) ;
4956 let input = wrapper . instance ( ) . inputRef . current ;
5057 expect ( getInputSelection ( input ) ) . toEqual ( { start : 1 , end : 1 } ) ;
5158 const result = insertAtCursor ( input , 'b' ) ;
5259 expect ( result ) . toEqual ( 'ab' ) ;
53- } )
54- } )
60+ } ) ;
61+
62+
63+ it ( 'should getInputSelection for old browser and return 0 when no range' , ( ) => {
64+ const mockedRange = undefined ;
65+ document . selection = { createRange : expect . createSpy ( )
66+ . andReturn ( mockedRange ) } ;
67+ const mockElement = { selectionStart : undefined , value :'hello' } ;
68+ const position = getInputSelection ( mockElement ) ;
69+ expect ( position ) . toEqual ( { start : 0 , end : 0 } ) ;
70+ } ) ;
71+
72+ it ( 'should getInputSelection for old browser and return 0' , ( ) => {
73+ const mockedTextRange = {
74+ moveToBookmark : expect . createSpy ( ) ,
75+ collapse : expect . createSpy ( ) ,
76+ compareEndPoints : expect . createSpy ( ) . andReturn ( 2 ) ,
77+ moveStart : expect . createSpy ( ) . andReturn ( 0 ) ,
78+ moveEnd : expect . createSpy ( )
79+ } ;
80+ const mockElement = { selectionStart : undefined , value :'hello' , createTextRange : expect . createSpy ( ) . andReturn ( mockedTextRange ) } ;
81+ const mockedRange = { parentElement : expect . createSpy ( ) . andReturn ( mockElement ) , getBookmark : expect . createSpy ( ) } ;
82+ document . selection = { createRange : expect . createSpy ( )
83+ . andReturn ( mockedRange ) } ;
84+ const position = getInputSelection ( mockElement ) ;
85+ expect ( position ) . toEqual ( { start : mockElement . value . length , end : mockElement . value . length } ) ;
86+ } ) ;
87+
88+ it ( 'should getInputSelection for old browser and return 0 when managing range' , ( ) => {
89+ const mockedTextRange = {
90+ moveToBookmark : expect . createSpy ( ) ,
91+ collapse : expect . createSpy ( ) ,
92+ compareEndPoints : expect . createSpy ( ) . andReturn ( - 2 ) ,
93+ moveStart : expect . createSpy ( ) . andReturn ( 0 ) ,
94+ moveEnd : expect . createSpy ( ) . andReturn ( 0 )
95+ } ;
96+ const mockElement = { selectionStart : undefined , value :'hello' , createTextRange : expect . createSpy ( ) . andReturn ( mockedTextRange ) } ;
97+ const mockedRange = { parentElement : expect . createSpy ( ) . andReturn ( mockElement ) , getBookmark : expect . createSpy ( ) } ;
98+ document . selection = { createRange : expect . createSpy ( ) . andReturn ( mockedRange ) } ;
99+ const position = getInputSelection ( mockElement ) ;
100+ expect ( position ) . toEqual ( { start : 0 , end : 0 } ) ;
101+ } ) ;
102+
103+ it ( 'should getInputSelection for old browser and return 0,len when managing selected range' , ( ) => {
104+ let isFirstCall = true ;
105+ const mockedTextRange = {
106+ moveToBookmark : expect . createSpy ( ) ,
107+ collapse : expect . createSpy ( ) ,
108+ compareEndPoints : expect . createSpy ( ) . andCall ( ( ) => {
109+ if ( isFirstCall ) {
110+ isFirstCall = false ;
111+ return - 2 ;
112+ } else {
113+ return 2 ;
114+ }
115+ } ) ,
116+ moveStart : expect . createSpy ( ) . andReturn ( 0 ) ,
117+ moveEnd : expect . createSpy ( ) . andReturn ( 0 )
118+ } ;
119+ const mockElement = { selectionStart : undefined , value :'hello' , createTextRange : expect . createSpy ( ) . andReturn ( mockedTextRange ) } ;
120+ const mockedRange = { parentElement : expect . createSpy ( ) . andReturn ( mockElement ) , getBookmark : expect . createSpy ( ) } ;
121+ document . selection = { createRange : expect . createSpy ( ) . andReturn ( mockedRange ) } ;
122+ const position = getInputSelection ( mockElement ) ;
123+ expect ( position ) . toEqual ( { start : 0 , end : mockElement . value . length } ) ;
124+ } ) ;
125+ } ) ;
0 commit comments