forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.test.mjs
More file actions
137 lines (95 loc) · 3.99 KB
/
mouse.test.mjs
File metadata and controls
137 lines (95 loc) · 3.99 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
137
import { expect } from 'chai';
import { MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT } from '../../../src/platform/input/constants.js';
import { Mouse } from '../../../src/platform/input/mouse.js';
const buttons = [MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT];
// Mock the _getTargetCoords method, otherwise it returns null
Mouse.prototype._getTargetCoords = function (event) {
return { x: 0, y: 0 };
};
describe('Mouse', function () {
/** @type { Mouse } */
let mouse;
beforeEach(function () {
mouse = new Mouse(document.body);
});
afterEach(function () {
mouse.detach();
});
describe('#constructor', function () {
it('should create a new instance', function () {
expect(mouse).to.be.an.instanceOf(Mouse);
});
});
describe('#isPressed', function () {
it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.isPressed(button)).to.be.false;
}
});
it('should return true for a mouse button that is pressed', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);
expect(mouse.isPressed(button)).to.be.true;
const mouseUpEvent = new MouseEvent('mouseup', { button });
window.dispatchEvent(mouseUpEvent);
expect(mouse.isPressed(button)).to.be.false;
}
});
});
describe('#on', function () {
it('should handle mousedown events', (done) => {
mouse.on('mousedown', (event) => {
expect(event.button).to.equal(MOUSEBUTTON_LEFT);
expect(event.event).to.be.an.instanceOf(MouseEvent);
done();
});
const mouseDownEvent = new MouseEvent('mousedown', { button: 0 });
window.dispatchEvent(mouseDownEvent);
});
it('should handle mouseup events', (done) => {
mouse.on('mouseup', (event) => {
expect(event.button).to.equal(MOUSEBUTTON_LEFT);
expect(event.event).to.be.an.instanceOf(MouseEvent);
done();
});
const mouseUpEvent = new MouseEvent('mouseup', { button: 0 });
window.dispatchEvent(mouseUpEvent);
});
});
describe('#wasPressed', function () {
it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.wasPressed(button)).to.be.false;
}
});
it('should return true for a mouse button that was pressed', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);
expect(mouse.wasPressed(button)).to.be.true;
mouse.update();
expect(mouse.wasPressed(button)).to.be.false;
}
});
});
describe('#wasReleased', function () {
it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.wasReleased(button)).to.be.false;
}
});
it('should return true for a mouse button that was released', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);
mouse.update();
const mouseUpEvent = new MouseEvent('mouseup', { button });
window.dispatchEvent(mouseUpEvent);
expect(mouse.wasReleased(button)).to.be.true;
mouse.update();
expect(mouse.wasReleased(button)).to.be.false;
}
});
});
});