1
- import { sendMouse } from '../../browser/commands.mjs' ;
1
+ import { sendMouse , resetMouse } from '../../browser/commands.mjs' ;
2
2
import { expect } from '../chai.js' ;
3
3
4
4
function spyEvent ( ) {
@@ -14,8 +14,6 @@ function spyEvent() {
14
14
return callback ;
15
15
}
16
16
17
- let element , x , y ;
18
-
19
17
before ( ( ) => {
20
18
// The native context menu needs to be prevented from opening at least in WebKit
21
19
// where it doesn't get automatically closed that, in turn, blocks the next `mouseup` event.
@@ -24,145 +22,180 @@ before(() => {
24
22
} ) ;
25
23
} ) ;
26
24
27
- beforeEach ( ( ) => {
28
- element = document . createElement ( 'div' ) ;
29
- element . style . width = '100px' ;
30
- element . style . height = '100px' ;
31
- element . style . margin = '100px' ;
32
-
33
- x = 150 ; // Horizontal middle of the element.
34
- y = 150 ; // Vertical middle of the element.
35
-
36
- document . body . appendChild ( element ) ;
37
- } ) ;
25
+ describe ( 'sendMouse' , ( ) => {
26
+ let element , x , y ;
38
27
39
- afterEach ( ( ) => {
40
- element . remove ( ) ;
41
- } ) ;
28
+ beforeEach ( ( ) => {
29
+ element = document . createElement ( 'div' ) ;
30
+ element . style . width = '100px' ;
31
+ element . style . height = '100px' ;
32
+ element . style . margin = '100px' ;
42
33
43
- describe ( 'move' , ( ) => {
44
- let spy ;
34
+ x = 150 ; // Horizontal middle of the element.
35
+ y = 150 ; // Vertical middle of the element.
45
36
46
- beforeEach ( ( ) => {
47
- spy = spyEvent ( ) ;
48
- document . addEventListener ( 'mousemove' , spy ) ;
37
+ document . body . appendChild ( element ) ;
49
38
} ) ;
50
39
51
40
afterEach ( ( ) => {
52
- document . removeEventListener ( 'mousemove' , spy ) ;
41
+ element . remove ( ) ;
53
42
} ) ;
54
43
55
- it ( 'can move mouse to a position' , async ( ) => {
56
- await sendMouse ( { type : 'move' , position : [ x , y ] } ) ;
44
+ describe ( ' move' , ( ) => {
45
+ let spy ;
57
46
58
- expect ( spy . getLastEvent ( ) ) . to . include ( { x, y } ) ;
59
- } ) ;
60
- } ) ;
47
+ beforeEach ( ( ) => {
48
+ spy = spyEvent ( ) ;
49
+ document . addEventListener ( 'mousemove' , spy ) ;
50
+ } ) ;
61
51
62
- describe ( 'click' , ( ) => {
63
- let spy ;
52
+ afterEach ( ( ) => {
53
+ document . removeEventListener ( 'mousemove' , spy ) ;
54
+ } ) ;
64
55
65
- beforeEach ( async ( ) => {
66
- spy = spyEvent ( ) ;
67
- element . addEventListener ( 'mousedown' , spy ) ;
68
- element . addEventListener ( 'mouseup' , spy ) ;
56
+ it ( 'can move mouse to a position' , async ( ) => {
57
+ await sendMouse ( { type : 'move' , position : [ x , y ] } ) ;
69
58
70
- await sendMouse ( { type : 'move' , position : [ 0 , 0 ] } ) ;
59
+ expect ( spy . getLastEvent ( ) ) . to . include ( { x, y } ) ;
60
+ } ) ;
71
61
} ) ;
72
62
73
- it ( 'can click the left mouse button' , async ( ) => {
74
- await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'left' } ) ;
63
+ describe ( ' click' , ( ) => {
64
+ let spy ;
75
65
76
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
77
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x , y } ) ;
78
- expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup ', button : 0 , x , y } ) ;
79
- } ) ;
66
+ beforeEach ( async ( ) => {
67
+ spy = spyEvent ( ) ;
68
+ element . addEventListener ( 'mousedown ', spy ) ;
69
+ element . addEventListener ( 'mouseup' , spy ) ;
80
70
81
- it ( 'should click the left mouse button by default' , async ( ) => {
82
- await sendMouse ( { type : 'click' , position : [ x , y ] } ) ;
71
+ await sendMouse ( { type : 'move' , position : [ 0 , 0 ] } ) ;
72
+ } ) ;
83
73
84
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
85
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x, y } ) ;
86
- expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
87
- } ) ;
74
+ it ( 'can click the left mouse button' , async ( ) => {
75
+ await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'left' } ) ;
88
76
89
- it ( 'can click the middle mouse button' , async ( ) => {
90
- await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'middle' } ) ;
77
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
78
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x, y } ) ;
79
+ expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
80
+ } ) ;
91
81
92
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
93
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 1 , x, y } ) ;
94
- expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 1 , x, y } ) ;
95
- } ) ;
82
+ it ( 'should click the left mouse button by default' , async ( ) => {
83
+ await sendMouse ( { type : 'click' , position : [ x , y ] } ) ;
96
84
97
- it ( 'can click the right mouse button' , async ( ) => {
98
- await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'right' } ) ;
85
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
86
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x, y } ) ;
87
+ expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
88
+ } ) ;
99
89
100
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
101
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 2 , x, y } ) ;
102
- expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 2 , x, y } ) ;
103
- } ) ;
104
- } ) ;
90
+ it ( 'can click the middle mouse button' , async ( ) => {
91
+ await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'middle' } ) ;
105
92
106
- describe ( 'down and up' , ( ) => {
107
- let spy ;
93
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
94
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 1 , x, y } ) ;
95
+ expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 1 , x, y } ) ;
96
+ } ) ;
108
97
109
- beforeEach ( async ( ) => {
110
- spy = spyEvent ( ) ;
111
- element . addEventListener ( 'mousedown' , spy ) ;
112
- element . addEventListener ( 'mouseup' , spy ) ;
98
+ it ( 'can click the right mouse button' , async ( ) => {
99
+ await sendMouse ( { type : 'click' , position : [ x , y ] , button : 'right' } ) ;
113
100
114
- await sendMouse ( { type : 'move' , position : [ x , y ] } ) ;
101
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 2 ) ;
102
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 2 , x, y } ) ;
103
+ expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 2 , x, y } ) ;
104
+ } ) ;
115
105
} ) ;
116
106
117
- it ( 'can down and up the left mouse button' , async ( ) => {
118
- await sendMouse ( { type : 'down' , button : 'left' } ) ;
107
+ describe ( ' down and up' , ( ) => {
108
+ let spy ;
119
109
120
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
121
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x, y } ) ;
110
+ beforeEach ( async ( ) => {
111
+ spy = spyEvent ( ) ;
112
+ element . addEventListener ( 'mousedown' , spy ) ;
113
+ element . addEventListener ( 'mouseup' , spy ) ;
122
114
123
- spy . resetHistory ( ) ;
124
- await sendMouse ( { type : 'up' , button : 'left' } ) ;
115
+ await sendMouse ( { type : 'move' , position : [ x , y ] } ) ;
116
+ } ) ;
125
117
126
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
127
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
128
- } ) ;
118
+ it ( 'can down and up the left mouse button' , async ( ) => {
119
+ await sendMouse ( { type : 'down' , button : 'left' } ) ;
129
120
130
- it ( 'should down and up the left mouse button by default' , async ( ) => {
131
- await sendMouse ( { type : 'down' } ) ;
121
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
122
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 0 , x , y } ) ;
132
123
133
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
134
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown ' , button : 0 , x , y } ) ;
124
+ spy . resetHistory ( ) ;
125
+ await sendMouse ( { type : 'up ' , button : 'left' } ) ;
135
126
136
- spy . resetHistory ( ) ;
137
- await sendMouse ( { type : 'up' } ) ;
127
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
128
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
129
+ } ) ;
138
130
139
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
140
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
141
- } ) ;
131
+ it ( 'should down and up the left mouse button by default' , async ( ) => {
132
+ await sendMouse ( { type : 'down' } ) ;
142
133
143
- it ( 'can down and up the middle mouse button' , async ( ) => {
144
- await sendMouse ( { type : 'down ' , button : 'middle' } ) ;
134
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
135
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown ' , button : 0 , x , y } ) ;
145
136
146
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
147
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 1 , x , y } ) ;
137
+ spy . resetHistory ( ) ;
138
+ await sendMouse ( { type : 'up' } ) ;
148
139
149
- spy . resetHistory ( ) ;
150
- await sendMouse ( { type : 'up' , button : 'middle' } ) ;
140
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
141
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 0 , x, y } ) ;
142
+ } ) ;
143
+
144
+ it ( 'can down and up the middle mouse button' , async ( ) => {
145
+ await sendMouse ( { type : 'down' , button : 'middle' } ) ;
146
+
147
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
148
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 1 , x, y } ) ;
149
+
150
+ spy . resetHistory ( ) ;
151
+ await sendMouse ( { type : 'up' , button : 'middle' } ) ;
152
+
153
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
154
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 1 , x, y } ) ;
155
+ } ) ;
151
156
152
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
153
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 1 , x, y } ) ;
157
+ it ( 'can down and up the right mouse button' , async ( ) => {
158
+ await sendMouse ( { type : 'down' , button : 'right' } ) ;
159
+
160
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
161
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 2 , x, y } ) ;
162
+
163
+ spy . resetHistory ( ) ;
164
+ await sendMouse ( { type : 'up' , button : 'right' } ) ;
165
+
166
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
167
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 2 , x, y } ) ;
168
+ } ) ;
154
169
} ) ;
170
+ } ) ;
155
171
156
- it ( 'can down and up the right mouse button' , async ( ) => {
157
- await sendMouse ( { type : 'down' , button : 'right' } ) ;
172
+ describe ( 'resetMouse' , ( ) => {
173
+ let spy ;
158
174
159
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
160
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mousedown' , button : 2 , x, y } ) ;
175
+ beforeEach ( ( ) => {
176
+ spy = spyEvent ( ) ;
177
+ document . addEventListener ( 'mouseup' , spy ) ;
178
+ document . addEventListener ( 'mousemove' , spy ) ;
179
+ } ) ;
180
+
181
+ afterEach ( ( ) => {
182
+ document . addEventListener ( 'mouseup' , spy ) ;
183
+ document . removeEventListener ( 'mousemove' , spy ) ;
184
+ } ) ;
185
+
186
+ it ( 'can reset mouse' , async ( ) => {
187
+ await sendMouse ( { type : 'move' , position : [ 100 , 100 ] } ) ;
188
+ await sendMouse ( { type : 'down' , button : 'left' } ) ;
189
+ await sendMouse ( { type : 'down' , button : 'right' } ) ;
190
+ await sendMouse ( { type : 'down' , button : 'middle' } ) ;
161
191
162
192
spy . resetHistory ( ) ;
163
- await sendMouse ( { type : 'up' , button : 'right' } ) ;
193
+ await resetMouse ( ) ;
164
194
165
- expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 1 ) ;
166
- expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 2 , x, y } ) ;
195
+ expect ( spy . getEvents ( ) ) . to . have . lengthOf ( 4 ) ;
196
+ expect ( spy . getEvents ( ) [ 0 ] ) . to . include ( { type : 'mouseup' , button : 0 } ) ;
197
+ expect ( spy . getEvents ( ) [ 1 ] ) . to . include ( { type : 'mouseup' , button : 1 } ) ;
198
+ expect ( spy . getEvents ( ) [ 2 ] ) . to . include ( { type : 'mouseup' , button : 2 } ) ;
199
+ expect ( spy . getEvents ( ) [ 3 ] ) . to . include ( { type : 'mousemove' , x : 0 , y : 0 } ) ;
167
200
} ) ;
168
201
} ) ;
0 commit comments