I am using react testing library to create my test.
import React, {useState} from 'react';
const Input = () => {
const [state, setState] = useState(0);
const onChange = (e) => {
setState(e.target.value)
};
return (
<div>
<h1>{state}</h1>
<input placeholder='type' type="number" value={state} onChange={onChange}/>
</div>
);
};
export default Input;
My test:
import Input from "./Input";
import { render, fireEvent } from '@testing-library/react'
describe('test if render', ()=> {
test('check render text', ()=> {
const { getByPlaceholderText, getByRole } = render(<Input />);
const input = getByPlaceholderText('type');
const h1 = getByRole('heading', {level: 1});
fireEvent.change(input, { target: { value: "123" } });
expect(h1).toHaveTextContent('123')
});
test('check not render text', ()=> {
const { getByPlaceholderText, getByRole } = render(<Input />);
const input = getByPlaceholderText('type');
const h1 = getByRole('heading', {level: 1});
fireEvent.change(input, { target: { value: "" } });
expect(h1).toHaveTextContent('')
})
});
The tests now are passed, but why? I just crated an input with type: number, not text, so i expect the test not to be passed? How to check an input with type number?