I'm working on a test code for my React project. What I want to check is 'if the input type is number, it disallow the text insertion'. Below is the test code I wrote.
it("disallow a text input", () => {
const input = screen.getByLabelText(label0); // it has 0 as initial value
const dummyText = "imyourfather";
// typing a text won't change the value of input
userEvent.type(input, dummyText);
screen.debug();
expect(input).toHaveValue(0); // FAILED -> return null
});
What I've expected here is to get the initial value, 0
, as a result of toHaveValue
. But it actually returns null
.
You might think 'yes, you tried to insert a text to a number-typed input, that's why it returns null', but the funny thing is that debug()
and console.log(input.value)
return 0
as I expected.
<!-- the result of debug() -->
<div
class="ui input"
>
<input
id="form-field-number01"
required=""
type="number"
value="0"
/>
</div>
And also if you manually try to insert a text value to a number-typed input on browser, it actually shows the initial value, 0. Anyone knows why toHaveValue
and debug
are showing different results?