diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index ffc0d51..d17ee30 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -38,6 +38,10 @@ function cloneEvent< currentTarget.selectionEnd = target.selectionEnd; } + currentTarget.setSelectionRange = (...args) => { + target.setSelectionRange(...args); + }; + return newEvent; } diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 0c12ad5..a9c1e72 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -353,6 +353,10 @@ describe('Input ref', () => { it('selectionXXX should pass', () => { const onChange = jest.fn(); const { container } = render(); + const spySetSelectionRange = jest.spyOn( + container.querySelector('input')!, + 'setSelectionRange', + ); const inputEl = container.querySelector('input')!; fireEvent.change(inputEl, { target: { value: 'test' } }); @@ -361,6 +365,10 @@ describe('Input ref', () => { const event = onChange.mock.calls[0][0]; expect(event.target.selectionStart).toBe(4); expect(event.target.selectionEnd).toBe(4); + + // Call `setSelectionRange` + event.target.setSelectionRange(1, 2); + expect(spySetSelectionRange).toHaveBeenCalledWith(1, 2); }); it('email type not support selection', () => {