gpt4 book ai didi

reactjs - 测试 redux 的 ConnectedComponent

转载 作者:行者123 更新时间:2023-12-03 14:17:59 25 4
gpt4 key购买 nike

我一直在尝试测试 redux 的 ConnectedComponent。但我却无法与之相处。当我将 store 作为 store prop 传递给 ConnectedComponent 时,我收到此错误:

Invariant Violation: Passing redux store in props has been removed and does not do anything. To use a custom Redux store for specific components, create a custom React context with React.createContext(), and pass the context object to React-Redux's Provider and specific components like: . You may also pass a {context : MyContext} option to connect

我的测试:

const store = storeFactory({ success: true });
test('renders component without error', () => {
const wrapper = shallow(<Input store={store} />);

expect(wrapper.props()).toBe({});
});

我使用创建商店的辅助函数:

export const storeFactory = initialState => {
return createStore(rootReducer, initialState);
};

我使用这个版本的软件包:

"react-redux": "^6.0.0",
"redux": "^4.0.1",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1",
"enzyme-redux": "^0.2.1",
"jest": "^23.6.0",
"jest-enzyme": "^7.0.1"

我不知道如何测试 ConnectedComponent。如果您对这种情况有任何解决方案或建议,请与我分享))

最佳答案

是的,在 React-Redux v6 中,we removed the ability to pass store as a prop to connected components ,因为内部架构的变化。

老实说,浅层渲染连接的组件对我来说似乎毫无意义,因为您真正测试的是 connect 调用您的 mapStatemapDispatch 并将组合结果传递给您自己的组件。我们已经对 React-Redux 进行了测试来验证该行为:)

但是,似乎确实有人想要这样做。我们有an open issue to discuss how we can help handle the "shallow+connected" use case 。 API 的具体更改尚未公布,但请密切关注该问题。

关于reactjs - 测试 redux 的 ConnectedComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54275430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com