mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-03 15:41:06 -04:00
Expand event helpers
Extends triggerEvent, allowing the creation of different types of event, and to change the data on its object. Also fakes the positional data browsers add to the event object. Also adds helpers for simulating: - all the events for a mouse click - the events invovled in moving the selection in a radio group
This commit is contained in:
@@ -1,12 +1,104 @@
|
||||
const triggerEvent = (el, evtType) => {
|
||||
const evt = new Event(evtType, {
|
||||
const triggerEvent = (el, evtType, options) => {
|
||||
const eventInit = {
|
||||
bubbles: true,
|
||||
cancelable: true
|
||||
});
|
||||
};
|
||||
let setPositionData = () => {
|
||||
const browserUI = {
|
||||
leftFrameBorder: 0,
|
||||
topHeight: 100
|
||||
};
|
||||
const cursorOffset = {
|
||||
x: 5,
|
||||
y: 5
|
||||
};
|
||||
const elBoundingBox = el.getBoundingClientRect();
|
||||
|
||||
if (!eventInit.clientX) { eventInit.clientX = elBoundingBox.left + cursorOffset.x; }
|
||||
if (!eventInit.clientY) { eventInit.clientY = elBoundingBox.top + cursorOffset.y; }
|
||||
if (!eventInit.pageX) { eventInit.pageX = elBoundingBox.left + cursorOffset.x; }
|
||||
if (!eventInit.pageY) { eventInit.pageY = elBoundingBox.top + cursorOffset.y; }
|
||||
if (!eventInit.screenX) { eventInit.screenX = eventInit.clientX + browserUI.leftFrameBorder; }
|
||||
if (!eventInit.screenY) { eventInit.screenY = eventInit.clientY + browserUI.topHeight; }
|
||||
if (!eventInit.offsetX) { eventInit.offsetX = cursorOffset.x; }
|
||||
if (!eventInit.offsetY) { eventInit.offsetY = cursorOffset.y; }
|
||||
};
|
||||
let Instance;
|
||||
|
||||
// mixin any specified event properties with the defaults
|
||||
if (options && ('eventInit' in options)) {
|
||||
Object.assign(eventInit, options.eventInit);
|
||||
}
|
||||
|
||||
// use event interface if specified
|
||||
if (options && ('interface' in options)) {
|
||||
Instance = options.interface;
|
||||
} else {
|
||||
|
||||
// otherwise, derive from the event type
|
||||
switch (evtType) {
|
||||
case 'click':
|
||||
// click events are part of the MouseEvent interface
|
||||
Instance = window.MouseEvent;
|
||||
break;
|
||||
case 'mousedown':
|
||||
Instance = window.MouseEvent;
|
||||
break;
|
||||
case 'mouseup':
|
||||
Instance = window.MouseEvent;
|
||||
break;
|
||||
case 'keydown':
|
||||
Instance = window.KeyboardEvent;
|
||||
break;
|
||||
case 'keyup':
|
||||
Instance = window.KeyboardEvent;
|
||||
break;
|
||||
default:
|
||||
Instance = Event;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (evtType === 'click') {
|
||||
// hack for click events to simulate details of pointer interaction
|
||||
setPositionData();
|
||||
}
|
||||
|
||||
const evt = new Instance(evtType, eventInit);
|
||||
|
||||
el.dispatchEvent(evt);
|
||||
};
|
||||
|
||||
function clickElementWithMouse (el) {
|
||||
triggerEvent(el, 'mousedown');
|
||||
triggerEvent(el, 'mouseup');
|
||||
triggerEvent(el, 'click');
|
||||
};
|
||||
|
||||
function moveSelectionToRadio (el, options) {
|
||||
// movement within a radio group with arrow keys fires no keyboard events
|
||||
|
||||
// click event fired from option radio being activated
|
||||
triggerEvent(el, 'click', {
|
||||
eventInit: { pageX: 0 }
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
function activateRadioWithSpace (el) {
|
||||
|
||||
// simulate events for space key press to confirm selection
|
||||
// event for space key press
|
||||
triggerEvent(el, 'keydown', {
|
||||
eventInit: { which: 32 }
|
||||
});
|
||||
// click event fired from option radio being activated
|
||||
triggerEvent(el, 'click', {
|
||||
eventInit: { pageX: 0 }
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
class ElementQuery {
|
||||
constructor (el) {
|
||||
this.el = el;
|
||||
@@ -141,5 +233,8 @@ const element = function (el) {
|
||||
};
|
||||
|
||||
exports.triggerEvent = triggerEvent;
|
||||
exports.clickElementWithMouse = clickElementWithMouse;
|
||||
exports.moveSelectionToRadio = moveSelectionToRadio;
|
||||
exports.activateRadioWithSpace = activateRadioWithSpace;
|
||||
exports.element = element;
|
||||
exports.WindowMock = WindowMock;
|
||||
|
||||
Reference in New Issue
Block a user