From 490c37e518200e2fbe2abbd3eb38fde4e5231079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Wed, 22 May 2024 15:00:42 +0800 Subject: [PATCH] fix: Proxy of setSelectionRange (#70) * fix: rc-input change event * test: add test case --- src/utils/commonUtils.ts | 4 ++++ tests/index.test.tsx | 8 ++++++++ 2 files changed, 12 insertions(+) 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', () => {