The jQuery UI source library contains a jquery.simulate.js file for simulating mouse and keyboard events. What is the benefit of using that simulate function instead of using jQuery event methods when testing jQuery UI stuff?
The jquery.simulate.js
file is used only to simulate events - it doesn't provide a full event management system like jQuery does. This means, it creates a fake event object, and then sends this event to the DOM. This can be compared to jQuery's .trigger()
method, which does a similar thing.
However, there are many properties on an event object that can be modified, including whether it should bubble or not, what the source element was, whether any special keys were pressed, the exact x and y locations the mouse was clicked, and so on. jQuery hides this from us when we call .trigger()
, by setting only the necessary properties to some default values. jquery.simulate.js
on the other hand is designed for testing (notice that the file is located under "jquery-ui/tests/"). For testing purposes, you'd want more fine-grained control over the event object to better mimic real user interaction.
Long story short, unless you're doing testing, most likely you won't need jquery.simulate.js
at all. Even if you want some of the functionalities it provides, be warned that the file isn't officially part of the jquery-ui library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With