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

View File

@ -1,10 +1,23 @@
export function createKeyEvent(element, key, ctrlKey) { import {
var e = document.createEvent('Events') || new document.defaultView.CustomEvent('keyEvent'); isString,
assign
} from 'min-dash';
e.key = key; /**
e.keyCode = key; * Create a fake key event for testing purposes.
e.which = key; *
e.ctrlKey = ctrlKey; * @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 || {});
}