-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Deprecation of .simulate() #2173
Comments
The reason it's bad is because it's named "simulate" and it takes an event name, but it does not actually faithfully simulate anything. It's based on the historic implementation of the same method in the shallow renderer which suffers from the same problems. All it does is transform the event name to a prop name, and invoke the function in that prop. The only way a replacement would be worth considering is if it actually simulated the event - ie, simulating "change" produced a stream of events, including keypress/keydown/keyup/etc, input, change, blur, whatever made sense - and invoked all the expected handlers as a result. This is a very large project, however, and I don't think the knowledge even exists in a single package in the entire ecosystem yet to encapsulate it (it's only in browsers, and not something that could be referenced in this direction). A PR to make this clear in the docs would be welcomed. |
Awesome, I'll get started on a PR today. |
@ljharb not sure if you saw but I opened a PR on Monday. |
I saw; I’ll take a look as soon as i can - thanks! |
@ljharb with deprecation of If not, what would be an idiomatic way to test component's behavior in response to various DOM interactions? I am aware that you suggest directly accessing a callback functions passed as props to a component and calling them; but if we stick to the principle of testing public api of a component and not peeking inside, what will a test of a DOM-mounted component look like? |
@azangru i'm not convinced any convenience method is needed - if you want to invoke the You shouldn't be testing the browser or React itself - and anything related to events is doing just that. Shallow testing is all about peeking inside - you're supposed to test that. |
@ljharb I am sorry; I must have expressed myself poorly. Let me try again. As far as I understand, there are two testing strategies when testing React components. One is testing a component entirely in isolation. This is purely a unit test and is probably best achieved with shallow rendering. As you say, with shallow rendering you are supposed to peek inside a component, reach for props that you pass to children components, and use them to test the component's functionality. Since children components are not rendered anyway during a shallow test, that's the only practical way of testing callbacks passed to children components. Of course the drawbacks of shallow testing is that it relies too much on the implementation of React the library; so when React extended its api with hooks, shallow testing was not yet ready for that. Another strategy is to test the component as well as the whole tree of its children by fully rendering it to the DOM. This is where a unit test borders on (or perhaps becomes) an integration test. This strategy is evangelized by This second strategy can be achieved in enzyme with full DOM rendering, which would make it possible to use |
@azangru Separately, you can always find an HTML element on a |
@ljharb thank you for your explanation; I wasn’t aware how |
@azangru v4 decisions haven't been made since v4 is a long way off. In general, I want to keep as close parity as possible between mount and shallow's methods. I agree that the current functionality of mount's simulate might be worth keeping, but i'd probably remove it and add a |
`.simulate` will be depreciated in the future. Adding alternate methods to the cheatsheet will make it more complete. See enzymejs/enzyme#2173
Hello! Big fan of enzyme here.
I use Enzyme a lot for unit testing and I've noticed recently that people are consistently recommending people use instance prop functions versus using the simulate behavior provided by Enzyme because of the bad implementation.
Is there anyway we could get the docs updated with what the preferred solution is? It's not really clear as the docs stand right now.
Can we also add a note that there are plans to deprecate it Enzyme v4 too? I saw that in the comments and I was surprised by it. I'm more than happy to put in a PR if that is the preferred solution.
Cheers.
The text was updated successfully, but these errors were encountered: