test(keyboard): use standard KeyEvents util

This commit is contained in:
Nico Rehwaldt 2018-11-21 09:38:11 +01:00
parent 9240971ce7
commit b410eed96f
2 changed files with 30 additions and 24 deletions

View File

@ -5,8 +5,6 @@ import {
inject
} from 'test/TestHelper';
import TestContainer from 'mocha-test-container-support';
import { forEach } from 'min-dash';
import coreModule from 'lib/core';
@ -20,7 +18,9 @@ import keyboardModule from 'lib/features/keyboard';
import modelingModule from 'lib/features/modeling';
import labelEditingModule from 'lib/features/label-editing';
var createKeyEvent = require('../../../util/KeyEvents').createKeyEvent;
import {
createKeyEvent
} from 'test/util/KeyEvents';
describe('features/keyboard', function() {
@ -45,13 +45,6 @@ describe('features/keyboard', function() {
describe('bpmn keyboard bindings', function() {
var container;
beforeEach(function() {
container = TestContainer.get(this);
});
it('should include triggers inside editorActions', inject(function(editorActions) {
// given
var expectedActions = [
@ -82,7 +75,7 @@ describe('features/keyboard', function() {
sinon.spy(globalConnect, 'toggle');
// given
var e = createKeyEvent(container, key, false);
var e = createKeyEvent(key);
// when
keyboard._keyHandler(e);
@ -101,7 +94,7 @@ describe('features/keyboard', function() {
sinon.spy(lassoTool, 'activateSelection');
// given
var e = createKeyEvent(container, key, false);
var e = createKeyEvent(key);
// when
keyboard._keyHandler(e);
@ -120,7 +113,7 @@ describe('features/keyboard', function() {
sinon.spy(spaceTool, 'activateSelection');
// given
var e = createKeyEvent(container, key, false);
var e = createKeyEvent(key);
// when
keyboard._keyHandler(e);
@ -143,7 +136,7 @@ describe('features/keyboard', function() {
selection.select(task);
var e = createKeyEvent(container, key, false);
var e = createKeyEvent(key);
// when
keyboard._keyHandler(e);
@ -161,7 +154,7 @@ describe('features/keyboard', function() {
inject(function(canvas, keyboard, selection, elementRegistry) {
// given
var e = createKeyEvent(container, key, true);
var e = createKeyEvent(key, { ctrlKey: true });
var allElements = elementRegistry.getAll(),
rootElement = canvas.getRootElement();
@ -187,7 +180,7 @@ describe('features/keyboard', function() {
sinon.spy(searchPad, 'toggle');
// given
var e = createKeyEvent(container, key, true);
var e = createKeyEvent(key, { ctrlKey: true });
// when
keyboard._keyHandler(e);

View File

@ -1,10 +1,23 @@
export function createKeyEvent(element, key, ctrlKey) {
var e = document.createEvent('Events') || new document.defaultView.CustomEvent('keyEvent');
import {
isString,
assign
} from 'min-dash';
e.key = key;
e.keyCode = key;
e.which = key;
e.ctrlKey = ctrlKey;
/**
* Create a fake key event for testing purposes.
*
* @param {String|Number} key the key or keyCode/charCode
* @param {Object} [attrs]
*
* @return {Event}
*/
export function createKeyEvent(key, attrs) {
var event = document.createEvent('Events') || new document.defaultView.CustomEvent('keyEvent');
return e;
}
// init and mark as bubbles / cancelable
event.initEvent('keydown', false, true);
var keyAttrs = isString(key) ? { key: key } : { keyCode: key, which: key };
return assign(event, keyAttrs, attrs || {});
}