我正在尝试使用包含组件呈现的 Ag Grid 的 Enzyme/Jest 进行测试。我已经目视检查了它,并且组件呈现。此外,当我不触发 await ensureGridApiHasBeenSet(ref);
时,测试通过。
这让我觉得我在 ensureGridApiHasBeenSet() 中有错误,但我想不通。
这是我的 SampleTable.test.js 的全部
import React from "react";
import SampleTable from "./../src/components/Samples/SampleTable";
import { AgGridReact } from "ag-grid-react";
import { mount } from "enzyme";
import { act } from "@testing-library/react";
import Enzyme from "enzyme";
import Adapter from "@wojtekmaj/enzyme-adapter-react-17";
Enzyme.configure({ adapter: new Adapter() });
let component = null;
let agGridReact = null;
const ensureGridApiHasBeenSet = async (componentRef) => {
await act(async () => {
await new Promise(function (resolve, reject) {
(function waitForGridReady() {
if (componentRef.current.getApi()) {
if (componentRef.current.getApi().getRowNode(8)) {
return resolve();
}
}
setTimeout(waitForGridReady, 10);
})();
});
});
};
beforeEach(async () => {
const ref = React.createRef();
component = mount(<SampleTable />);
agGridReact = component.find(AgGridReact).instance();
await ensureGridApiHasBeenSet(ref);
});
afterEach(() => {
component.unmount();
agGridReact = null;
});
it("stateful component returns a valid component instance", () => {
expect(agGridReact).toBeTruthy();
});
或者,我是否得到了误报并且没有使用 expect(agGridReact).toBeTruthy()
正确测试我的组件?
感谢您的帮助!