forked from SpringRoll/SpringRoll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.spec.js
More file actions
136 lines (109 loc) · 3.24 KB
/
Controller.spec.js
File metadata and controls
136 lines (109 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Controller } from './Controller';
import { newEvent } from '../debug';
import Sinon from 'sinon';
const ieKeysMap = {
'down':'downArrow',
'up': 'upArrow',
'left': 'leftArrow',
'right': 'rightArrow'
};
describe('controller', () => {
it('Should accept a array of buttons', () => {
const controller = new Controller([
{
key: 'Enter',
down: console.log('key down')
},
{ key: 'w', down: console.log('key down') },
{ key: 'a', down: console.log('key down') },
{ key: 's', down: console.log('key down') },
{ key: 'd', down: console.log('key down') },
{ key: ' ', down: console.log('key down') }
]);
expect(controller.keys.length).to.equal(6);
});
it('Should call functions on key press', () => {
const callback = Sinon.fake();
const controller = new Controller([
{ key: 'enter', down: callback }
]);
const event = newEvent('keydown');
event.key = 'Enter';
window.dispatchEvent(event);
controller.update();
expect(callback.callCount).to.equal(1);
});
it('Should not be case-sensitive', () => {
const callback = Sinon.fake();
const controller = new Controller([
{ key: 'EnTeR', down: callback}
]);
const event = newEvent('keydown');
event.key = 'Enter';
window.dispatchEvent(event);
controller.update();
expect(callback.callCount).to.equal(1);
});
it('Should not call functions when key is not pressed', () => {
const callback = Sinon.fake();
const controller = new Controller([
{ key: 'Enter', down: callback }
]);
const eventDown = newEvent('keydown');
eventDown.key = 'Enter';
window.dispatchEvent(eventDown);
const eventUp = newEvent('keyup');
eventUp.key = 'Enter';
window.dispatchEvent(eventUp);
controller.update();
expect(callback.callCount).to.equal(0);
});
it('Should call up function on blur if the key was down', done => {
const controller = new Controller([
{
key: 'Enter',
up: function() {
done();
}.bind(this)
}
]);
const eventDown = newEvent('keydown');
eventDown.key = 'Enter';
window.dispatchEvent(eventDown);
const blurEvent = newEvent('blur');
window.dispatchEvent(blurEvent);
controller.update();
});
it('Should not call up function on blur if the key was not down', done => {
const controller = new Controller([
{
key: 'Enter',
up: function() {
done(new Error());
}.bind(this)
}
]);
const blurEvent = newEvent('blur');
window.dispatchEvent(blurEvent);
controller.update();
controller.update();
setTimeout(() => {
done();
}, 10);
});
// Check all IE keys are mapped to Chrome/Firefox
for (const keyName in ieKeysMap) {
const mappedName = ieKeysMap[keyName];
it(`should map ${keyName} to ${mappedName} event (needed for IE)`, function() {
const callback = Sinon.fake();
const controller = new Controller([
{ key: keyName, down: callback}
]);
const event = newEvent('keydown');
event.key = mappedName;
window.dispatchEvent(event);
controller.update();
expect(callback.callCount).to.equal(1);
});
}
});