I have recently upgraded my React project to ant design v4 and all the tests that use a Select, AutoComplete or Tooltip are broken. Basically when clicking the components, the modal or select options are not present in JSDOM. This used to work fine in v3.
Can somebody show me how to test antd v4 with react testing library ?
Example:
My Component:
import React from "react";
import "./styles.css";
import { Select } from "antd";
const { Option } = Select;
function handleChange(value) {
console.log(`selected ${value}`);
}
export default function App() {
return (
<div className="App" style={{ marginTop: "40px" }}>
<Select
defaultValue="lucy"
style={{ width: 120 }}
onChange={handleChange}
>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
<Option value="disabled" disabled>
Disabled
</Option>
<Option value="Yiminghe">yiminghe</Option>
</Select>
</div>
);
}
My test
import "@testing-library/jest-dom/extend-expect";
import React from "react";
import { render, fireEvent, prettyDOM } from "@testing-library/react";
import App from "./App";
test("App Test", () => {
const { queryAllByText, getByText, container } = render(<App />);
expect(queryAllByText("Lucy").length).toBe(1);
expect(queryAllByText("Jack").length).toBe(0);
fireEvent.click(getByText("Lucy"));
console.log(prettyDOM(container));
// This line fails although I would expect the dropdown to be open and all the options visible
expect(queryAllByText("Jack").length).toBe(1);
});
Here is a link to a codesandbox that reproduces the issue. (As mentioned, that code used to work in v3).
https://codesandbox.io/s/staging-shape-0xkrl?file=/src/App.test.js:0-494