jest-dom icon indicating copy to clipboard operation
jest-dom copied to clipboard

`.toHaveValue` expect string values for input type range

Open Okazari opened this issue 4 years ago • 3 comments

  • @testing-library/jest-dom version: 5.11.9
  • node version: 12.16.0
  • npm (or yarn) version: 1.22.5

Relevant code or config:


const MyComponent = () => <input data-test="input" type="range" value={5} onChange={() => {}} />

  it('should work', () => {
    render(<MyComponent />);
    let inputRange = screen.getByTestId('input');
    expect(inputRange).toHaveValue(5);
  });

Hey there ! I just stumble upon a strange case were testing value for a input type range expect values to be string. I find it quite odd since those input value are always numbers.

Reproduction:

Here is a minimap repro in a codesandbox https://codesandbox.io/s/react-testing-library-demo-forked-pve4d?file=/src/tests/hello.js

Problem description:

  • .toHaveValue expect string values for input type range

It force the test to use strings instead of number. It is annoying when dealing with const that are numbers.

Suggested solution:

  • .toHaveValue could expect number values for input type range

Okazari avatar May 10 '21 12:05 Okazari

I may open a PR for that if you can point me where the job is done for input type="number" :+1:

Okazari avatar May 10 '21 13:05 Okazari

Thanks for your bug report. Indeed, this should work the same as with <input type="number" />. Here's where we treat numeric inputs differently: https://github.com/testing-library/jest-dom/blob/637529e3892611092671f30c135587c130fbf92d/src/utils.js#L191-L192

gnapse avatar May 11 '21 14:05 gnapse