From 997620cac8281307d5ea3019b53174020668be0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E6=9C=BA=E5=99=A8=E4=BA=BA?= Date: Wed, 22 May 2024 14:43:35 +0800 Subject: [PATCH] test: add test case --- src/utils/commonUtils.ts | 4 ++-- tests/index.test.tsx | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 66c51f8..d17ee30 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -38,8 +38,8 @@ function cloneEvent< currentTarget.selectionEnd = target.selectionEnd; } - currentTarget.setSelectionRange = (start: number, end: number) => { - target.setSelectionRange(start, end); + 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', () => {