Can't seem to get the value of an input using react-testing library, but for some reason one can set a value via fireEvent. nameInput is an actual input element
test('verify name validation works', () => {
const { getByPlaceholderText, getByTestId, debug } = render(<Home/>)
const passForm = getByTestId('form')
const nameInput = getByPlaceholderText('Name');
fireEvent.change(nameInput, { target: { value: 'TestName' }});
debug(nameInput.value) // error
})
Update
I have to assert as HTMLInputElement to work as ts inferring it as a generic HTMLElement
expect((nameInput as HTMLInputElement).value)
Instead of using type assertion, you can use generic:
const nameInput = getByPlaceholderText<HTMLInputElement>('Name');
expect(nameInput.value).toBe('Nice!')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With