New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing state on decorated components #98
Comments
Hey @CosticaPuntaru, the shallow renderer is intentionally limited to operating on only the root component so as to make the test more isolated. In the case of decorators or "wrapped" components like this, the wrapped component is not what we want to test. Internally at Airbnb, we use a pattern like the following: class MyComponent extends React.Component {
...
}
export default connect(MyComponent); // default export. used in your app.
export { MyComponent as PureMyComponent}; // pure component. used in tests This will work fine with redux's Does that help at all? |
i know that you can do that, but:
|
@CosticaPuntaru if you want to test the full flux lifecycle for a component, you will likely need to use |
Closing for now. I don't see any clear path to making this better than it already is. Please reopen if you have suggestions. |
@lelandrichardson How do you guys test Redux connected components? |
@montogeek i am also looking for a way to test connected components |
@montogeek @vickenliu I'd follow @lelandrichardson's initial advice of exporting the pure component alongside the connected component. Then you just have to mock the props that redux would provide. |
@vickenliu I am following @aweary advice :) If you need to test it with the store, write E2E tests :) |
Full example can be found here: https://github.com/fshowalter/redux/tree/ccf1416de48ae81c823d7e58b8d3f9f0fa0df5fb/examples/async (@connect or the connect() HOC is the same) If you look at /containers/App.js, you'll see that both class App is exported, as export default connect(). This is also described on http://redux.js.org/docs/recipes/WritingTests.html under 'Connected components' |
hello , how can i get state of decorated component by DragDropContext ? mount getState() return null |
Is this still the preferred method? I was able to solve this with @lelandrichardson's advice. Thanks. |
Is there a way to achieve the dual-export of a pure component and connected component using the |
@joncursi At this point, you no longer need to export the pure component - use |
@joncursi Do you had the answer about your question already ? I also encounter this issue. |
@ljharb Let's say I want to get the component that is inside of the HOC/decorator. Is there a way that I can pass props into that component? If I use dive() I get the wrapper and if I use .instance() I can get the component, but I would like to get the component function that I can pass props into. update: oh I can just use .type from the wrapper |
@ronghuang0 in that case, you'd need to expose it directly so you could test it directly. |
I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use const wrapper = mount(shallow(<MyComponent />).get(0)) Works like a charm :) |
using enzyme you can only setState on root component, but many decorators wraps your component, in this manner you cannot set the state of your component,
here is an example using redux, but is the same for other decorators:
isn't there a way to create something like getReactComponentOf(selector) that can support setState?
The text was updated successfully, but these errors were encountered: