I have two ant design date pickers in a component. I am trying to change and test the value of date picker but not able to find the calendar input in the test environment dom.
Date Pickers Component
import { DatePicker, Form } from "antd";
import React, { Component } from "react";
import moment from "moment";
class DatePickers extends Component {
constructor(props) {
super(props);
this.state = {
startDate: moment().subtract(1, "month"),
startDateError: "",
endDate: moment(),
endDateError: "",
};
}
onStartDateChange = (date) => {
this.setState({
startDate: date,
startDateError: date ? "" : "Please select start date",
});
};
onEndDateChange = (date) => {
this.setState({
endDate: date,
endDateError: date ? "" : "Please select end date",
});
};
render() {
const { endDate, endDateError, startDate, startDateError } = this.state;
return (
<React.Fragment>
<Form.Item
validateStatus={startDateError ? "error" : ""}
help={startDateError}
htmlFor="startDate"
label="Date From"
>
<DatePicker
id="startDate"
placeholder="Select Start Date"
allowClear={false}
onChange={this.onStartDateChange}
defaultPickerValue={startDate}
defaultValue={startDate}
format="DD-MM-YYYY"
/>
</Form.Item>
<Form.Item
validateStatus={endDateError ? "error" : ""}
help={endDateError}
htmlFor="endDate"
label="To"
>
<DatePicker
id="endDate"
placeholder="Select End Date"
allowClear={false}
onChange={this.onEndDateChange}
defaultPickerValue={endDate}
defaultValue={endDate}
format="DD-MM-YYYY"
/>
</Form.Item>
</React.Fragment>
);
}
}
export default DatePickers;
Test case using @testing-library/react
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
test("date input value validation", async () => {
const { container } = render(<DatePickers />);
const startDateNode = screen.getByPlaceholderText(/select start date/i);
const endDateNode = screen.getByPlaceholderText(/select end date/i);
userEvent.click(startDateNode);
const calendarInput = container.querySelector("ant-calendar-input");
await userEvent.type(calendarInput, "");
// assert
expect(screen.getByText("Please select by start date")).toBeInTheDocument();
});
The test case fails and throws an error
console.error node_modules/jest-environment-jsdom-fourteen/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: navigation (except hash changes)
Versions:
"react" : "16.10.1",
"antd": "3.25.1",
"@testing-library/jest-dom": "5.7.0",
"@testing-library/react": "10.0.4",
"@testing-library/user-event": "10.1.2"
Is there any solution to assert this?
fireEvent.mouseDown()
andfireEvent.change()
were important. I had been trying to useuserEvent.click()
anduserEvent.type()
to no avail. Maybe that's helpful to someone. – Nemesis