Skip to content

onChange event not triggering function #175

Closed
@alexwendte

Description

@alexwendte
  • react-testing-library version: 5.0.1
  • react version: 16.5.0
  • node version: 9.11.1 or 8.11.1 (tested to see if it made a difference)
  • npm (or yarn) version: 5.6.0

Relevant code or config:

// CostInput.js
import React from 'react';

const CostInput = ({ handleChange }) => (
  <div>
    <label htmlFor="cost">Item Cost</label>
    <input type="string" id="cost" onChange={handleChange} />
  </div>
);
export default CostInput;

// CostInput.test.js
import React from "react";
import { render, cleanup, fireEvent } from "react-testing-library";
import CostInput from "./CostInput";

afterEach(cleanup);
it("handleChange function called", () => {
  const spy = jest.fn();
  const { getByLabelText } = render(<CostInput handleChange={spy} />);
  let input = getByLabelText("Item Cost");
  expect(input.value).toBe("");
  fireEvent.change(input, { target: { value: "23" } });
  expect(spy).toHaveBeenCalledTimes(1);
});

What you did:

I wrote a test to check that the handleChange function would be called. I also ran the same code on CodeSandbox at https://codesandbox.io/s/jvw4499xzw.

What happened:

The test fails on my local environment which is a CRA application that only has those 2 files in it.
It works on the Code Sandbox without a problem.

Reproduction:

The working Code Sandbox can be found here: https://codesandbox.io/s/jvw4499xzw
The broken repo is found here: https://github.com/themostcolm/react-testing-bug
If possible, please create a repository that reproduces the issue with the
minimal amount of code possible.

Problem description:

It is making my app untestable.

Suggested solution:

I honestly have no idea, I'm sorry. I tried using waitForElement, but it didn't make a difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions