Skip to content

Commit 1936a4b

Browse files
committed
feat(click): can click by trigger click or mouseup/mousedown events
1 parent f3b2e48 commit 1936a4b

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/api/NonoRobot.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ export interface NonoRobot {
2828
* A mouseclick DOM UI event.
2929
* @param params - The targeted DOM objects, a css selector, or an EventTargetInit object or a mouse event data object
3030
* @param count - The number of clicks to perform (one click if not specified)
31+
* @param usingClick - If false, mouseDown and mouseUp events are used. If true, the classical click event is used
3132
* @returns the robot (itself)
3233
*/
33-
click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count?: number): this;
34+
click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count?: number, usingClick?: boolean): this;
3435

3536
/**
3637
* A dblclick DOM UI event.
3738
* @param params - The targeted DOM objects, a css selector, or an EventTargetInit object or a mouse event data object
39+
* @param usingDblClick - If false, mouseDown and mouseUp events are used. If true, the classical dblclick event is used
3840
* @returns the robot (itself)
3941
*/
40-
dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit)): this;
42+
dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit), usingDblClick?: boolean): this;
4143

4244
/**
4345
* An auxclick DOM UI event.

src/internal/NonoRobotImpl.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,28 @@ export class NonoRobotImpl implements NonoRobot {
244244
return params as TouchEventInit;
245245
}
246246

247-
public click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1): this {
248-
for (let i = 0; i < count; i++) {
249-
this.processMouseEvent("click", this.processPotentialCssSelector(params));
247+
public click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1, usingClick = true): this {
248+
if(usingClick) {
249+
for (let i = 0; i < count; i++) {
250+
this.processMouseEvent("click", this.processPotentialCssSelector(params));
251+
}
252+
}else {
253+
for (let i = 0; i < count; i++) {
254+
this.processMouseEvent("mousedown", this.processPotentialCssSelector(params));
255+
this.processMouseEvent("mouseup", this.processPotentialCssSelector(params));
256+
}
250257
}
251258
return this;
252259
}
253260

254-
public dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit)): this {
255-
return this.processMouseEvent("dblclick", this.processPotentialCssSelector(params));
261+
public dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit), usingDblClick = true): this {
262+
if (usingDblClick) {
263+
this.processMouseEvent("dblclick", this.processPotentialCssSelector(params));
264+
}else {
265+
this.click(params, 2, usingDblClick);
266+
}
267+
268+
return this;
256269
}
257270

258271
public auxclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1): this {

test/internal/NonoRobotImpl.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ describe("robot with default event target", () => {
2121
describe("with mouse events", () => {
2222
let handler: () => void;
2323
let handler2: () => void;
24+
let downHandler: () => void;
25+
let upHandler: () => void;
2426

2527
beforeEach(() => {
2628
handler = jest.fn();
2729
handler2 = jest.fn();
30+
downHandler = jest.fn();
31+
upHandler = jest.fn();
2832
div.addEventListener("click", handler);
2933
div2.addEventListener("click", handler2);
34+
div.addEventListener("mousedown", downHandler);
35+
div.addEventListener("mouseup", upHandler);
3036
});
3137

3238
test("single click works", () => {
@@ -38,6 +44,20 @@ describe("robot with default event target", () => {
3844
}));
3945
});
4046

47+
test("single click with mousedown mouseup works", () => {
48+
robot.click({"button": 3}, 1, false);
49+
50+
expect(handler).not.toHaveBeenCalled();
51+
expect(downHandler).toHaveBeenCalledTimes(1);
52+
expect(upHandler).toHaveBeenCalledTimes(1);
53+
expect(downHandler).toHaveBeenNthCalledWith(1, expect.objectContaining({
54+
"button": 3
55+
}));
56+
expect(upHandler).toHaveBeenNthCalledWith(1, expect.objectContaining({
57+
"button": 3
58+
}));
59+
});
60+
4161
test("two clicks works", () => {
4262
robot
4363
.click({"button": 2})

0 commit comments

Comments
 (0)