simulate.mustache revision 5d3dc0444c51f18e44c016a89b16a8951529518c
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai<div class="intro">
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai When creating automated tests for your application or modules, you need
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai to be able to mock user events. The DOM supports creating native events
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai that behave essentially the same as user generated events, though
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai without the associated browser default behaviors (e.g. following a link
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai on click).
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai The `event-simulate` module adds the `Y.Event.simulate` method for
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai working with raw DOM nodes, but for most cases, the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai `node-event-simulate` module is the right choice, since it allows you
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai to call the `simulate` method directly from the `Node`.
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai<h2>Simulating Mouse Events</h2>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai There are seven mouse events that can be simulated:
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`click`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`dblclick`</li>
bb25c06cca41ca78e5fb87fbb8e81d55beb18c95jg <li>`mousedown`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`mouseup`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`mouseover`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`mouseout`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <li>`mousemove`</li>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Each event is fired by calling <code>simulate()</code> and passing in two
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai arguments: the type of event to fire and an optional object specifying
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai additional information for the event. To simulate a click on the document's
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai body element, for example, the following code can be used:
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllaiYUI().use('node-event-simulate', function(Y) {
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Y.one("body").simulate("click");
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai This code simulates a click with all of the default properties on the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <code>event</code> object. To specify additional information, such as the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Shift key being down, the second argument must be used and the exact DOM
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai name for the event property specified (there is browser-normalizing logic
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai that translates these into browser-specific properties when necessary):
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllaiY.one("body").simulate("click", { shiftKey: true });
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai In this updated example, a click event is fired on the document's body
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai while simulating that the Shift key is down.
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai The extra properties to specify vary depending on the event being simulated
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai and are limited to this list:
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <dt>`detail`</dt>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Indicates the number of times a button was clicked (DOM-compliant
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai browsers only).
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <dt>`screenX`, `screenY`</dt>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Coordinates of the mouse event in relation to the entire screen
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai (DOM-compliant browsers only).
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <dt>`clientX`, `clientY`</dt>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai Coordinates of the mouse event in relation to the browser client
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai <dt>`ctrlKey`, `altKey`, `shiftKey`, `metaKey`</dt>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai The state of the Ctrl, Alt, Shift, and Meta keys, respectively
var node = Y.one("#myDiv");
node.simulate("click", { altKey: true});
node.simulate("dblclick", { ctrlKey: true });
node.simulate("mousedown", { clientX: 100, clientY: 100 });
node.simulate("mouseup", { clientX: 100, clientY: 100 });
node.simulate("mousemove", { clientX: 200, clientY: 200 });
var node = Y.one("#myDiv");
node.simulate("keydown", { keyCode: 97 });
node.simulate("keyup", { keyCode: 97 });
node.simulate("keypress", { charCode: 97 });
var node = Y.one("#myInput");
node.simulate("change");
node.simulate("select");
href="../event-custom/index.html">custom events</a>, instead.
The <a href="synths.html">Synthetic event system</a> doesn't yet support