-
-
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
How do I simulate drag and drop in enzyme testcase? #1357
Comments
In general, I'd discourage any use of |
Hi @ljharb I noticed your Owner badge after reading your comment discouraging the use of |
It's my personal opinion, and isn't documented in the repo anywhere. Basically, it's that the name "simulate" implies that it's actually simulating how events in the browser work; however, events don't bubble the same way, and simulating "change" doesn't fire all the key/focus/blur events leading up to it, etc. I think a much simpler and cleaner approach is to explicitly extract the prop function, and invoke it manually. |
That's a fair point. I've been working working with I do think that it should be mentioned in the docs that calling the function explicitly is a good alternative as |
Sounds like a good PR :-D @saudar82 I'm going to close this; I think explicitly invoking props is the proper solution here, since simulate doesn't actually simulate things faithfully. |
Hi both, nothing personal but I'd have to disagree with you. When you are testing, you are generally testing a specific bit of functionality, not the whole thing, in fact most of the time you specifically don't want to test the whole thing in one test. Using simulate, yes you aren't getting the surrounding events, but I think this is right. If you were to simulate a "click" event, why should you get mouse down and mouse up events? What if you are testing touch screen? You wouldn't want them in that case and by making a simulate click do those events you are starting to make big assumptions about what is actually needed. If you want to properly unit test something then:
Personally, I don't think invoking props is the proper solution either, how often can a user do that? Never. If you aren't testing an API I think you should be testing it how the user would be using it or as close as is feasible to do so. Otherwise you begin to make assumptions about how the browser would call that method and then all you are testing is how you think a browser would call your code, not how it actually would (of course, assuming that the If you want more than one event to happen in a particular order then I'd recommend making some test helper functions to alleviate any repetitiveness. For example; I'm not trying to cause any offence here and I hope that I have not got the wrong idea about what you guys are saying, but I wanted to offer a different opinion and perspective on this subject. |
@Anupheaus the problem is that Your tests don't need to test that react and the DOM are working properly - that's for React, and browsers, to test. Your tests should be testing your code. In other words, I think we're somewhat agreeing - a proper simulate would be most useful - but since one doesn't exist, the current broken one should be avoided. |
@ljharb Ah sorry, from the issue comments above it implies that it is only "simulating" the surrounding events that is not working, not I agree that you don't need to test react and the DOM and I wasn't suggesting that. I also agree that your tests primary purpose should only be to test your own code. I guess it just doesn't sit right with me that you are exposing a prop purely for testing purposes. My advice would be; rather than expose a prop purely for testing, do this in your test instead: const component = enzyme.mount(<Component />);
const div = component.find('div');
simulateEvent(div, 'mousedown', { clientX: 10, clientY: 10 }); where simulateEvent is the following method in a test helpers file somewhere: function simulateEvent(component, eventName, eventData) {
const eventType = (() => {
if (['mousedown', 'mousemove', 'mouseup'].includes(eventName)) { return 'MouseEvent'; }
})();
const event = new window[eventType](eventName, eventData);
component.getDOMNode().dispatchEvent(event);
} Of course, the simulateEvent method makes a number of assumptions and is definitely not complete but I've only just created it and I only needed the events I've added so far (but it can easily be enhanced with more events). |
@Anupheaus we didn't realize |
Does this mean libs like |
It means their testing strategy might be unique, but consumers of those libs are already testable as-is, they just have to explicitly invoke props as needed. |
Hm, my particular problem with |
|
Oh cool! Thanks for helping out a noob! |
I need to know the parameter names, but a bit of greybox testing is ok i guess. |
Hm, I still need to figure out how to access the
|
|
Works, thanks a lot! |
@fabb How did you resolve this issue? Can you provide solution for this? |
@Raj002 exactly like @ljharb suggested:
|
Should we then mark |
Yes, see #2173. |
... without '.simulate()' event, as seen [here](enzymejs/enzyme#1357 (comment)) that it will be discontinued. - Lift the state of elements from SortableCategorySelector to parent; - Configures lint to not conflict with 'pretier/react';
This required a different approach than using Enzme's simulate() function which is apparently not the most useful: enzymejs/enzyme#1357
I am trying to create a test for simulating a dragging of an image into a div. I am clueless on how to achieve this. Any help is much appreciated.
I am trying to simulate the drop event, not sure how to do as 2 elements are involved in it.
The text was updated successfully, but these errors were encountered: