Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed May 22, 2024
1 parent 8ef020f commit 997620c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ describe('Input ref', () => {
it('selectionXXX should pass', () => {
const onChange = jest.fn();
const { container } = render(<Input onChange={onChange} />);
const spySetSelectionRange = jest.spyOn(
container.querySelector('input')!,
'setSelectionRange',
);

const inputEl = container.querySelector('input')!;
fireEvent.change(inputEl, { target: { value: 'test' } });
Expand All @@ -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', () => {
Expand Down

0 comments on commit 997620c

Please sign in to comment.