Skip to content

Commit bff07e1

Browse files
committed
test(editor): test keyboard using actual DOM events
1 parent d16c2b4 commit bff07e1

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

packages/form-js-editor/test/spec/features/keyboard/FormEditorKeyboardBindings.spec.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ import modelingModule from 'src/features/modeling';
1010

1111
import { createKeyEvent } from 'diagram-js/test/util/KeyEvents';
1212

13-
import {
14-
KEYS_REDO,
15-
KEYS_UNDO
16-
} from 'diagram-js/lib/features/keyboard/KeyboardBindings';
17-
1813
import schema from '../../form.json';
1914

15+
var KEYS_REDO = [ 'y', 'Y', 89 ];
16+
var KEYS_UNDO = [ 'z', 'Z', 90 ];
17+
2018

2119
describe('features/editor-actions', function() {
2220

@@ -32,22 +30,27 @@ describe('features/editor-actions', function() {
3230
getFormEditor().destroy();
3331
});
3432

33+
function triggerEvent(key, attrs, target) {
34+
return getFormEditor().invoke(function(config) {
35+
target = target || config.renderer.container;
36+
37+
return target.dispatchEvent(createKeyEvent(key, attrs));
38+
});
39+
}
3540

3641
KEYS_UNDO.forEach((key) => {
3742

38-
it('should undo', inject(function(keyboard, editorActions) {
43+
it('should undo', inject(function(config, keyboard, editorActions) {
3944

4045
// given
4146
const undoSpy = sinon.spy(editorActions, 'trigger');
4247

43-
const event = createKeyEvent(key, {
48+
// when
49+
triggerEvent(key, {
4450
ctrlKey: true,
4551
shiftKey: false
4652
});
4753

48-
// when
49-
keyboard._keyHandler(event);
50-
5154
// then
5255
expect(undoSpy).to.have.been.calledWith('undo');
5356
}));
@@ -62,45 +65,45 @@ describe('features/editor-actions', function() {
6265
// given
6366
const redoSpy = sinon.spy(editorActions, 'trigger');
6467

65-
const event = createKeyEvent(key, {
68+
// when
69+
triggerEvent(key, {
6670
ctrlKey: true,
6771
shiftKey: false
6872
});
6973

70-
// when
71-
keyboard._keyHandler(event);
72-
7374
// then
7475
expect(redoSpy).to.have.been.calledWith('redo');
7576
}));
7677

7778
});
7879

7980

80-
it('should undo/redo when focused on input', inject(function(formEditor, keyboard, editorActions) {
81+
it('should undo/redo when focused on input', inject(
82+
function(formEditor, keyboard, editorActions) {
8183

82-
// given
83-
const spy = sinon.spy(editorActions, 'trigger');
84+
// given
85+
const spy = sinon.spy(editorActions, 'trigger');
8486

85-
const container = formEditor._container;
86-
const inputField = document.createElement('input');
87+
const container = formEditor._container;
88+
const inputEl = document.createElement('input');
8789

88-
container.appendChild(inputField);
90+
container.appendChild(inputEl);
8991

90-
// when
91-
// select all
92-
keyboard._keyHandler({ key: 'a', ctrlKey: true, target: inputField });
92+
// when
93+
// select all
94+
triggerEvent('a', { ctrlKey: true }, inputEl);
9395

94-
// then
95-
expect(spy).to.not.have.been.called;
96+
// then
97+
expect(spy).to.not.have.been.called;
9698

97-
// when
98-
// undo/redo
99-
keyboard._keyHandler({ key: 'z', ctrlKey: true, target: inputField, preventDefault: () => {} });
100-
keyboard._keyHandler({ key: 'y', ctrlKey: true, target: inputField, preventDefault: () => {} });
99+
// when
100+
// undo/redo
101+
triggerEvent('z', { ctrlKey: true }, inputEl);
102+
triggerEvent('y', { ctrlKey: true }, inputEl);
101103

102-
// then
103-
expect(spy).to.have.been.called.calledTwice;
104-
}));
104+
// then
105+
expect(spy).to.have.been.called.calledTwice;
106+
}
107+
));
105108

106109
});

0 commit comments

Comments
 (0)