-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: maintain cursor position on controlled React input
- Loading branch information
1 parent
1560b29
commit 99fab5a
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { useState } from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from 'index' | ||
|
||
test('maintain cursor position on controlled input', () => { | ||
function Input({initialValue}: {initialValue: string}) { | ||
const [val, setVal] = useState(initialValue) | ||
|
||
return <input value={val} onChange={e => setVal(e.target.value)}/> | ||
} | ||
|
||
render(<Input initialValue="acd"/>) | ||
|
||
;screen.getByRole('textbox').focus() | ||
;(screen.getByRole('textbox') as HTMLInputElement).setSelectionRange(1,1) | ||
userEvent.keyboard('b') | ||
|
||
expect(screen.getByRole('textbox')).toHaveValue('abcd') | ||
expect(screen.getByRole('textbox')).toHaveProperty('selectionStart', 2) | ||
expect(screen.getByRole('textbox')).toHaveProperty('selectionEnd', 2) | ||
}) |