Skip to content

Commit

Permalink
feat(click): can click by trigger click or mouseup/mousedown events
Browse files Browse the repository at this point in the history
  • Loading branch information
arnobl committed Dec 3, 2024
1 parent f3b2e48 commit 1936a4b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/api/NonoRobot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ export interface NonoRobot {
* A mouseclick DOM UI event.
* @param params - The targeted DOM objects, a css selector, or an EventTargetInit object or a mouse event data object
* @param count - The number of clicks to perform (one click if not specified)
* @param usingClick - If false, mouseDown and mouseUp events are used. If true, the classical click event is used
* @returns the robot (itself)
*/
click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count?: number): this;
click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count?: number, usingClick?: boolean): this;

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

/**
* An auxclick DOM UI event.
Expand Down
23 changes: 18 additions & 5 deletions src/internal/NonoRobotImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,28 @@ export class NonoRobotImpl implements NonoRobot {
return params as TouchEventInit;
}

public click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1): this {
for (let i = 0; i < count; i++) {
this.processMouseEvent("click", this.processPotentialCssSelector(params));
public click(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1, usingClick = true): this {
if(usingClick) {
for (let i = 0; i < count; i++) {
this.processMouseEvent("click", this.processPotentialCssSelector(params));
}
}else {
for (let i = 0; i < count; i++) {
this.processMouseEvent("mousedown", this.processPotentialCssSelector(params));
this.processMouseEvent("mouseup", this.processPotentialCssSelector(params));
}
}
return this;
}

public dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit)): this {
return this.processMouseEvent("dblclick", this.processPotentialCssSelector(params));
public dblclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit), usingDblClick = true): this {
if (usingDblClick) {
this.processMouseEvent("dblclick", this.processPotentialCssSelector(params));
}else {
this.click(params, 2, usingDblClick);
}

return this;
}

public auxclick(params?: EventTarget | string | (EventTargetInit & MouseEventInit), count: number = 1): this {
Expand Down
20 changes: 20 additions & 0 deletions test/internal/NonoRobotImpl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ describe("robot with default event target", () => {
describe("with mouse events", () => {
let handler: () => void;
let handler2: () => void;
let downHandler: () => void;
let upHandler: () => void;

beforeEach(() => {
handler = jest.fn();
handler2 = jest.fn();
downHandler = jest.fn();
upHandler = jest.fn();
div.addEventListener("click", handler);
div2.addEventListener("click", handler2);
div.addEventListener("mousedown", downHandler);
div.addEventListener("mouseup", upHandler);
});

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

test("single click with mousedown mouseup works", () => {
robot.click({"button": 3}, 1, false);

expect(handler).not.toHaveBeenCalled();
expect(downHandler).toHaveBeenCalledTimes(1);
expect(upHandler).toHaveBeenCalledTimes(1);
expect(downHandler).toHaveBeenNthCalledWith(1, expect.objectContaining({
"button": 3
}));
expect(upHandler).toHaveBeenNthCalledWith(1, expect.objectContaining({
"button": 3
}));
});

test("two clicks works", () => {
robot
.click({"button": 2})
Expand Down

0 comments on commit 1936a4b

Please sign in to comment.