// MyComponent.jsx
const MyComponent = (props) => {
const { fetchSomeData } = props;
useEffect(()=> {
fetchSomeData();
}, []);
return (
// Some other components here
)
};
// MyComponent.react.test.jsx
...
describe('MyComponent', () => {
test('useEffect', () => {
const props = {
fetchSomeData: jest.fn(),
};
const wrapper = shallow(<MyComponent {...props} />);
// THIS DOES NOT WORK, HOW CAN I FIX IT?
expect(props.fetchSomeData).toHaveBeenCalled();
});
});
Run Code Online (Sandbox Code Playgroud)
运行测试时,我得到:
expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called, but it was not called.
Run Code Online (Sandbox Code Playgroud)
期望失败,因为shallow没有调用 useEffect。由于其他问题,我无法使用 mount,需要找到一种使用shallow.
酶的浅渲染不支持useEffect。正如ljharb所提到的,它在路线图上(参见“v16.8+:Hooks”列)将在下一版本的 Enzyme 中修复
当前设置无法满足您的要求。然而,很多人都在为此苦苦挣扎。
我已经通过以下方式解决/解决了这个问题:
这是基于React 文档中的Mock Modules 的关于如何模拟模块的摘要。
联系人.js
import React from "react";
import Map from "./map";
function Contact(props) {
return (
<div>
<p>
Contact us via foo@bar.com
</p>
<Map center={props.center} />
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
联系.test.js
import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import { act } from "react-dom/test-utils";
import Contact from "./contact";
import MockedMap from "./map";
jest.mock("./map", () => {
return function DummyMap(props) {
return (
<p>A dummy map.</p>
);
};
});
it("should render contact information", () => {
const center = { lat: 0, long: 0 };
act(() => {
render(
<Contact
name="Joni Baez"
email="test@example.com"
site="http://test.com"
center={center}
/>,
container
);
});
});
Run Code Online (Sandbox Code Playgroud)
有用的资源:
| 归档时间: |
|
| 查看次数: |
24584 次 |
| 最近记录: |