@@ -202,3 +202,45 @@ test('track changes to value and selection per setRangeText', () => {
202202 expect ( getUIValue ( element ) ) . toBe ( 'aYcd' )
203203 expect ( getUISelection ( element ) ) . toHaveProperty ( 'focusOffset' , 1 )
204204} )
205+
206+ test ( 'circumvent setters/methods for UI changes' , ( ) => {
207+ const { element} = render < HTMLInputElement > ( `<input/>` , { focus : false } )
208+
209+ const prototypeDescr = Object . getOwnPropertyDescriptors < HTMLInputElement > (
210+ Object . getPrototypeOf ( element ) as HTMLInputElement ,
211+ )
212+ const valueSpy = jest . fn ( prototypeDescr . value . set )
213+ const setSelectionRangeSpy = jest . fn ( prototypeDescr . setSelectionRange . value )
214+
215+ Object . defineProperties ( element , {
216+ value : {
217+ get : ( ) => {
218+ throw new Error ( )
219+ } ,
220+ ...prototypeDescr . value ,
221+ set : valueSpy ,
222+ } ,
223+ setSelectionRange : {
224+ ...prototypeDescr . setSelectionRange ,
225+ value : setSelectionRangeSpy ,
226+ } ,
227+ } )
228+
229+ prepare ( element )
230+ element . focus ( )
231+
232+ setUIValue ( element , 'abcd' )
233+ setUISelection ( element , { focusOffset : 3 } )
234+
235+ expect ( element ) . toHaveValue ( 'abcd' )
236+ expect ( element ) . toHaveProperty ( 'selectionStart' , 3 )
237+ expect ( valueSpy ) . not . toBeCalled ( )
238+ expect ( setSelectionRangeSpy ) . not . toBeCalled ( )
239+
240+ element . value = 'efgh'
241+ element . setSelectionRange ( 1 , 2 )
242+ expect ( element ) . toHaveValue ( 'efgh' )
243+ expect ( element ) . toHaveProperty ( 'selectionStart' , 1 )
244+ expect ( valueSpy ) . toBeCalledWith ( 'efgh' )
245+ expect ( setSelectionRangeSpy ) . toBeCalledWith ( 1 , 2 )
246+ } )
0 commit comments