Skip to content

event.currentTarget not populated when fireevent.change happens for an input.  #73

Closed
@kelly-tock

Description

@kelly-tock
  • dom-testing-library version: 2.9.1
  • react version:
  • node version:
  • npm (or yarn) version:

Relevant code or config:

import React, { Component } from 'react';

export class Input extends Component {

  constructor(props) {
    super();

    const {
      value
    } = props;

    this.state = {
      value: value || ''
    };
  }

  handleChange = (evt) => {
    const {
      onChange
    } = this.props;

    this.setState(prevState => ({
      value: evt.target.value
    }));

    if (onChange) {
      onChange( evt.target.value);
    }

  }

  render() {
    const {
      value
    } = this.state;

    return (
      <input onChange={this.handleChange} value={value} data-testid="input" />
    )
  }
}



// test

test('will call onChange prop if changes occur', async () => {
	const spy = jest.fn();
  const {getByText, getByTestId, container} = render(<Input value="test" onChange={spy} />);

  expect(getByTestId('input')).toHaveAttribute('value', 'test');

  const input = getByTestId('input');

  input.value = 'testing';

  fireEvent.change(
    input
  );

	expect(getByTestId('input')).toHaveAttribute('value', 'testing');
	expect(spy).toHaveBeenCalled();
  // expect(container.firstChild).toMatchSnapshot();
});

What you did:

writing a test for an input.

What happened:

component was using event.currentTarget.value, but got an error in the test because currentTarget was not defined.

Reproduction:

change the code above to use current target instead.

Problem description:

Suggested solution:

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