如何使用Jest和Enzyme测试此功能?
addProducts = id => {
toast.success("Product added", {
position: toast.POSITION.TOP_RIGHT
});
history.push(`/product/${id}`);
};我正在使用这段代码,但这还不够……
it("Must return added addProduct with success message", () => {
const componente = shallow(<AddProduct />);
const spy = jest.spyOn(wrapper.instance(), "addProducts");
expect(spy).toBeTruthy();
});发布于 2019-12-31 10:10:45
如果你正在进行我们的单元测试,你可以在这里测试两件事:
1)使用正确的参数调用toast.success(),即'Product added'和对象{ position: toast.POSITION.TOP_RIGHT }
2)调用history.push(),且history.push调用正确。
对于以上两种情况,您都必须对它们调用jest.spyOn(),然后检查它们是否被调用过一次。
expect(toastSpy).toBeCalledTimes(1);
expect(historyPushSpy).toBeCalledTimes(1);此外,您还需要断言上面的mock是正确调用的。
const toastSpyCall = toastSpy.mock.calls[0][0];
// expect(...).toBe(....)https://stackoverflow.com/questions/59538108
复制相似问题