tests with jest and enzyme for datepickers onChange event doesnt work
See original GitHub issueWhen 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:
- Created 5 years ago
- Reactions:6
- Comments:12
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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