Skip to content

Commit

Permalink
test: maintain cursor position on controlled React input
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche committed Apr 27, 2021
1 parent 1560b29 commit 99fab5a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/__tests__/react/keyboard.tsx
@@ -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)
})

0 comments on commit 99fab5a

Please sign in to comment.