question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tests with jest and enzyme for datepickers onChange event doesnt work

See original GitHub issue

When I try to test Datepicker onChange behaviour with this test unit:

import React from 'react';
import {mount} from 'enzyme';
import toJson from 'enzyme-to-json';
import DatePicker from 'bundles/DatePicker/DatePicker';

describe('DatePicker', () => {
    test('datepicker calls the onChange Event', done => {
        const DateComponent = mount(<DatePicker />),
            dateInput = DateComponent.find("input[type='text']");

       DateComponent.instance().handleChange = jest.fn();

        dateInput.simulate('change', {target: { value: "2018-01-04" }});
        done();

        expect(DateComponent.instance().handleChange.mock.calls.length).toBe(1);
    });
});

Expected behavior

Expected value to be (using ===):
      1

Actual behavior

Received:
      0

jsx file:

import PropTypes from 'prop-types';
import React from 'react';
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";

// registerLocale("en-GB", enGB);

import "./DatePicker.scss";

class Dpicker extends React.Component {
  static propTypes = {
    date: PropTypes.string
  };

  constructor(props) {
    super(props);

    let {date, placeholder, labelDate} = props;

    date = this.props.date ? new Date(date) : new Date();

    this.state = {
      date: date,
      labelDate: labelDate,
      placeholder: placeholder
    };
  }

  handleChange = date => {
    this.setState({
      date: date
    });
  };

  render() {
    return (
      <div className="row">
        <div className="col-md-12">
          <div className="datepicker-single-date">
            <label htmlFor="date">{this.state.labelDate}</label>
            <DatePicker
              selected={this.state.date}
              onChange={this.handleChange}
              autoComplete={"off"}
              dateFormat="yyyy-MM-dd"
              maxDate={new Date()}
              name="date"
              id="date"
              required={true}
              placeholder={this.state.placeholder}/>
          </div>
        </div>
      </div>
    );
  }
}

export default Dpicker;

Thanks in advance

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:12

github_iconTop GitHub Comments

5reactions
JRasmusBmcommented, Jul 22, 2019

Same issue with react-testing-library. Focusing, blurring, clicking all doesn’t work. It does change the value in the input field, but when testing the result does not bubble to the onChange event.

1reaction
pikitgbcommented, Apr 11, 2020

In order to test successfully with 100% cover use this => https://codesandbox.io/s/bitter-cache-mthww?file=/src/DateRangeFilter.spec.js:519-534

the idea is to mock with jest the onChange method

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-testing: unable to detect DatePicker field to initiate change
I found a solution that work perfectly fine for me. describe('Date Picker Test', () => { let wrapper; beforeEach(() => { wrapper =...
Read more >
Unit Testing in React: Full Guide on Jest and Enzyme Testing
Test events :​​ Check the onChange event, for that mock onChange callback => render date input component => then simulate change event with...
Read more >
How to Test React Component With Jest and Enzyme, Part 2
Test events: 4.1. Check the onChange event, for that mock onChange callback => render date input component => then simulate change event with ......
Read more >
Jest trigger input change - Caritas Castellaneta
Before we talk about Enzyme and Jest, we should define a few terms: Test runner, ... The onchange event is not working in...
Read more >
Testing React Apps - Jest
Setup without Create React App​. If you have an existing application you'll need to install a few packages to make everything work well...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found