How to make snapshot tests work
See original GitHub issueI’m using Jest for testing and I try to make snapshot tests of components rendering a DayPickerRangeController (although I guess this issue would be the same for other date pickers).
Say I want to make a snapshot test of a simple component wrapping DayPickerRangeController; used for setting nice defaults for instance, in my example numberOfMonths, startDateOffset and endDateOffset.
import React from "react";
import moment from "moment";
import PropTypes from "prop-types";
import momentPropTypes from "react-moment-proptypes";
import "react-dates/initialize";
import { START_DATE, END_DATE } from "react-dates/constants";
import { DayPickerRangeController } from "react-dates";
const DayPickerRangeControllerWrapper = ({
startDate,
endDate,
focusedInput,
onDatesChange,
onFocusChange
}) => (
<DayPickerRangeController
startDate={startDate}
endDate={endDate}
onDatesChange={onDatesChange}
focusedInput={focusedInput}
onFocusChange={onFocusChange}
numberOfMonths={1}
startDateOffset={day => day.clone().add(1, "day")}
endDateOffset={day => day.clone().subtract(1, "day")}
/>
);
DayPickerRangeControllerWrapper.propTypes = {
startDate: momentPropTypes.momentObj,
endDate: momentPropTypes.momentObj,
focusedInput: PropTypes.oneOf([START_DATE, END_DATE]),
onDatesChange: PropTypes.func.isRequired,
onFocusChange: PropTypes.func.isRequired
};
DayPickerRangeControllerWrapper.defaultProps = {
startDate: moment("2018-01-01"),
endDate: moment("2018-01-07"),
focusedInput: START_DATE
};
export default DayPickerRangeControllerWrapper;
I then try to snapshot test it
import React from "react";
import renderer from "react-test-renderer";
import DayPickerRangeControllerWrapper from "./DayPickerRangeControllerWrapper";
describe("DayPickerRangeControllerWrapper", () => {
it("renders correctly", () => {
const tree = renderer
.create(
<DayPickerRangeControllerWrapper
onDatesChange={() => {}}
onFocusChange={() => {}}
onClearDates={() => {}}
/>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
I get the following error
TypeError: Cannot read property '__consolidated_events_handlers__' of null
at addEventListener (node_modules/consolidated-events/lib/index.js:22:14)
at CalendarMonthGrid.componentDidMount (node_modules/react-dates/lib/components/CalendarMonthGrid.js:215:77)
This seems to be somehow related to https://github.com/airbnb/react-dates/issues/528, but I haven’t found a way to properly (and simply) mock ReactDOM or CalendarMonthGrid (that doesn’t seem like a good way to go though).
I could always unit test my wrapper component, but I would like to be able to snapshot test it as well.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
I think I found the right way to do it. What I am really trying to test here is my
DayPickerRangeControllerWrapperwhere I have some custom logic (pretty simple in my example); I don’t want to testreact-dates’sDayPickerRangeController, as it’s not my concern.So I need to mock
DayPickerRangeController, which is a named export of thereact-datesmodule (mock modules documentation by Jest).react-datesmockSnapshot
It works with the resulting snapshot
@nicolasrouanne
You could mock out the DayPickerRangeController using Jest: