Reactjs 笑话:如何期望Redux操作已被调度?
我尝试的是确保一个Redux操作(作为Reactjs 笑话:如何期望Redux操作已被调度?,reactjs,redux,jestjs,enzyme,Reactjs,Redux,Jestjs,Enzyme,我尝试的是确保一个Redux操作(作为prop传递)已被调度 我正试图通过jest监视该操作,但一直失败 下面是测试的简化版本 let props; let wrapped; describe("Sample", () => { beforeEach(() => { props = { setResults: jest.fn() // This is the action I'm trying to spy on. }; wrapped
prop
传递)已被调度
我正试图通过jest
监视该操作,但一直失败
下面是测试的简化版本
let props;
let wrapped;
describe("Sample", () => {
beforeEach(() => {
props = {
setResults: jest.fn() // This is the action I'm trying to spy on.
};
wrapped = mount(
<Provider store={createStore(reducers, {})}>
<Sample {...props} />
</Provider>
);
});
afterEach(() => {
wrapped.unmount();
});
it("can setResults", () => {
wrapped.find("input").simulate("change", { target: { value: "example" } });
wrapped.update();
wrapped.find("button").simulate("click");
// setResults is supposed to have been called!
expect(props.setResults).toHaveBeenCalled();
});
});
我在操作前后放置了两个console.log()
,这确认调用了handleEvent
函数本身。尽管如此,expect(props.setResults).tohavebeencall()
在测试中失败
当我把setResults
放在最后一个console.log()
中时,我看到
What is in setResults? results => dispatch((0, _actions.setResults)(results))
这让我确信,Redux的行为不能以这种方式被嘲笑(或被窥视)
该操作与react redux
连接
const mapDispatchToProps = dispatch => ({
setResults: results => dispatch(setResults(results))
});
或者,我在上面的代码中遗漏了什么
如有任何建议,将不胜感激。(目标是确保已调用操作。)除非在提供程序中包装组件时有一些我不知道的警告,否则这将起作用:
const setResults = jest.fn();
在每次回调之前:
beforeEach(() => {
wrapped = mount(
<Provider store={createStore(reducers, {})}>
<Sample setResults={setResults} />
</Provider>
);
除非在提供程序中包装组件时有一些我不知道的警告,否则这将起作用:
const setResults = jest.fn();
在每次回调之前:
beforeEach(() => {
wrapped = mount(
<Provider store={createStore(reducers, {})}>
<Sample setResults={setResults} />
</Provider>
);