我正在尝试使用@testing-library/react 测试使用useAsync挂钩的组件。
如果我在 TestAPI 模块上使用 jest.mock,然后是 getTest.mockResolvedValueOnce(testArray); 在 getTest 函数上,我希望模拟能够正确返回测试值。
测试 :
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import TestPanel from "./TestPanel";
import { getTest } from "./TestAPI";
jest.mock("./TestAPI");
it("renders cards correctly", async () => {
const testArray = ["hi there"];
getTest.mockResolvedValueOnce(testArray);
render(<TestPanel />);
expect(getTest).toHaveBeenCalledTimes(1);
expect(getTest).toHaveBeenCalledWith();
await waitFor(() => expect(screen.getByText("hi there")).toBeInTheDocument());
});
要测试的组件:
import React from "react";
import { useAsync } from "react-async-hook";
import { getTest } from "./TestAPI";
export default function TestPanel() {
const { result: elems } = useAsync(getTest, []);
return (
<div>
{elems &&
elems.map((elem) => {
return <div>{elem}</div>;
})}
</div>
);
}
API调用:
import React from "react";
import axios from "axios";
export async function getTest(): Promise {
const response = await axios.get("/someservice");
return response.data || [];
}
但是,如果我运行测试,则会引发异常,说明“elems.map”未定义。
仔细观察,elems 似乎是一个承诺。
有什么我做错了吗?