Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(click): default force to .5 on mousePressed events #582

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cypress/e2e/click.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ describe("cy.realClick", () => {
cy.get(".navbar-brand").realClick({ button: "right" });
});

it("results in pointerdown events with the default pressure of 0.5", () => {
const handler = cy.stub();
cy.get(".action-btn")
.then(($button) => {
$button[0].addEventListener("pointerdown", (e) =>
handler("pointerdown", e.pressure),
);
$button[0].addEventListener("pointerup", (e) =>
handler("pointerup", e.pressure),
);
})
.realClick();
cy.wrap(handler).should("be.calledWith", "pointerdown", 0.5);
cy.wrap(handler).should("be.calledWith", "pointerup", 0);
});

it("allows for setting pressure of the pointerdown event", () => {
const handler = cy.stub();
cy.get(".action-btn")
.then(($button) => {
$button[0].addEventListener("pointerdown", (e) =>
handler("pointerdown", Math.round(e.pressure * 100) / 100),
);
$button[0].addEventListener("pointerup", (e) =>
handler("pointerup", Math.round(e.pressure * 100) / 100),
);
})
.realClick({ pressure: 0 })
.realClick({ pressure: 0.4 });
cy.wrap(handler)
.should("be.calledWith", "pointerdown", 0)
.should("be.calledWith", "pointerdown", 0.4)
.should("be.calledWith", "pointerup", 0);
});

describe("scroll behavior", () => {
function getScreenEdges() {
const cypressAppWindow =
Expand Down
27 changes: 27 additions & 0 deletions cypress/e2e/mouse.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ describe("cy.realMouseDown and cy.realMouseUp", () => {
.realMouseUp({ position: "bottomRight" });
});

it("results in pointerdown events with the default pressure of 0.5", () => {
const handler = cy.stub();
cy.get(".action-btn")
.then(($button) => {
$button[0].addEventListener("pointerdown", (e) =>
handler("pointerdown", e.pressure),
);
})
.realMouseDown();
cy.wrap(handler).should("be.calledWith", "pointerdown", 0.5);
});

it("allows for setting pressure of the pointerdown event", () => {
const handler = cy.stub();
cy.get(".action-btn")
.then(($button) => {
$button[0].addEventListener("pointerdown", (e) =>
handler("pointerdown", Math.round(e.pressure * 100) / 100),
);
})
.realMouseDown({ pressure: 0 })
.realMouseDown({ pressure: 0.4 });
cy.wrap(handler)
.should("be.calledWith", "pointerdown", 0)
.should("be.calledWith", "pointerdown", 0.4);
});

describe("options.button", () => {
it("should allow to press down mouse using middle button", (done) => {
cy.get(".action-btn")
Expand Down
9 changes: 9 additions & 0 deletions src/commands/mouseDown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export interface realMouseDownOptions {
* @example cy.realMouseDown({ shiftKey: true });
*/
shiftKey?: boolean;
/**
* The normalized pressure, which has a range of [0,1]. It affects the `pressure` property of the triggered
* pointerdown event.
*
* @type {number}
* @default {0.5}
*/
pressure?: number;
}

/** @ignore this, update documentation for this function at index.d.ts */
Expand Down Expand Up @@ -78,6 +86,7 @@ export async function realMouseDown(
pointerType: options.pointer ?? "mouse",
button: options.button ?? "left",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
force: options.pressure ?? 0.5,
});

log.snapshot("after").end();
Expand Down
10 changes: 10 additions & 0 deletions src/commands/realClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export interface RealClickOptions {
* @example cy.realClick({ shiftKey: true });
*/
shiftKey?: boolean;

/**
* The normalized pressure, which has a range of [0,1]. It affects the `pressure` property of the triggered
* pointerdown event.
*
* @type {number}
* @default {0.5}
*/
pressure?: number;
}

/** @ignore this, update documentation for this function at index.d.ts */
Expand Down Expand Up @@ -83,6 +92,7 @@ export async function realClick(
pointerType: options.pointer ?? "mouse",
button: options.button ?? "left",
modifiers: options.shiftKey ? keyToModifierBitMap.Shift : 0,
force: options.pressure ?? 0.5,
});

await fireCdpCommand("Input.dispatchMouseEvent", {
Expand Down