Skip to content

Commit

Permalink
chore: cleaner dispatching of fake events
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto committed Apr 27, 2017
1 parent 1a88980 commit b4ef15f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
28 changes: 12 additions & 16 deletions src/lib/core/testing/dispatch-events.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import {
createFakeEvent,
createKeyboardEvent,
createMouseEvent
} from './event-objects';
import {createFakeEvent, createKeyboardEvent, createMouseEvent} from './event-objects';

/** Shorthand to dispatch a fake event on a specified node. */
export function dispatchFakeEvent(node: Node | Window, type: string) {
let event = createFakeEvent(type);
/** Utility to dispatch any event on a Node. */
export function dispatchEvent(node: Node | Window, event: Event): Event {
node.dispatchEvent(event);
return event;
}

/** Shorthand to dispatch a fake event on a specified node. */
export function dispatchFakeEvent(node: Node | Window, type: string): Event {
return dispatchEvent(node, createFakeEvent(type));
}

/** Shorthand to dispatch a keyboard event with a specified key code. */
export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number) {
let event = createKeyboardEvent(type, keyCode);
node.dispatchEvent(event);
return event;
export function dispatchKeyboardEvent(node: Node, type: string, keyCode: number): KeyboardEvent {
return dispatchEvent(node, createKeyboardEvent(type, keyCode)) as KeyboardEvent;
}

/** Shorthand to dispatch a mouse event on the specified coordinates. */
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0) {
let event = createMouseEvent(type, x, y);
node.dispatchEvent(event);
return event;
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0): MouseEvent {
return dispatchEvent(node, createMouseEvent(type, x, y)) as MouseEvent;
}
2 changes: 1 addition & 1 deletion src/lib/core/testing/event-objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function createKeyboardEvent(type: string, keyCode: number) {
// See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
Object.defineProperty(event, 'keyCode', { get: () => keyCode });

// IE won't set `defaultPrevented` on syntetic events so we need to do it manually.
// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
event.preventDefault = function() {
Object.defineProperty(event, 'defaultPrevented', { get: () => true });
return originalPreventDefault.apply(this, arguments);
Expand Down

0 comments on commit b4ef15f

Please sign in to comment.