Skip to content

Commit

Permalink
feat(realSwipe): add touchMoveDelay option
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezamirian authored and dmtrKovalenko committed Feb 16, 2024
1 parent 56d41d7 commit 49dda19
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ Options:
- `Optional` **x**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **y**: undefined \| number ([more about coordinates](#coordinates))
- `Optional` **touchPosition**: "topLeft" | "top" | "topRight" | "left" | "center" | "right" | "bottomLeft" | "bottom" | "bottomRight"
- `Optional` **touchMoveDelay**: undefined \| number **`default`** 0

## cy.realMouseDown

Expand Down
11 changes: 11 additions & 0 deletions cypress/e2e/swipe.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ describe("cy.realSwipe", () => {
});
});
});

describe("touchMoveDelay", () => {
it("adds a delay between touch start and touch move events", () => {
cy.visit("./cypress/fixtures/swipe-touch-move-delay");
cy.get("[role=gridcell]").contains("14").realSwipe("toRight", {
touchMoveDelay: 300,
length: 70,
});
cy.get("[role=gridcell][aria-selected=true]").should("have.length", 3);
});
});
2 changes: 2 additions & 0 deletions cypress/fixtures/swipe-touch-move-delay/index.f3612ef8.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions cypress/fixtures/swipe-touch-move-delay/index.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/commands/realPress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fireCdpCommand } from "../fireCdpCommand";
import { keyCodeDefinitions } from "../keyCodeDefinitions";
import { keyToModifierBitMap } from "../keyToModifierBitMap";
import { wait } from "../utils";

export interface RealPressOptions {
/**
Expand Down Expand Up @@ -78,7 +79,7 @@ export async function realPress(
});
}

await new Promise((res) => setTimeout(res, options.pressDelay ?? 25));
await wait(options.pressDelay ?? 25);
}

await Promise.all(
Expand Down
14 changes: 14 additions & 0 deletions src/commands/realSwipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export interface RealSwipeOptions {
* cy.get(".drawer").realSwipe("toLeft", { step: 5 })
*/
step?: number;

/**
* Delay between touchStart and touchMove events (ms)
* @default 0
* cy.get(".drawer").realSwipe("toLeft", { touchMoveDelay: 300 })
*/
touchMoveDelay?: number;
}

async function forEachSwipePosition(
Expand Down Expand Up @@ -117,6 +124,12 @@ export async function realSwipe(
touchPoints: [startPosition],
});

if (options.touchMoveDelay) {
// cy.wait() can't be used here since we are in a command that returns a promise.
// Read more: https://on.cypress.io/returning-promise-and-commands-in-another-command
await wait(options.touchMoveDelay);
}

await forEachSwipePosition(
{
length,
Expand All @@ -140,3 +153,4 @@ export async function realSwipe(

return subject;
}
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
3 changes: 2 additions & 1 deletion src/commands/realType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { keyCodeDefinitions } from "../keyCodeDefinitions";
import { realPress } from "./realPress";
import { wait } from "../utils";

export interface RealTypeOptions {
/**
Expand Down Expand Up @@ -58,7 +59,7 @@ export async function realType(text: string, options: RealTypeOptions = {}) {
log: false,
});

await new Promise((res) => setTimeout(res, options.delay ?? 25));
await wait(options.delay ?? 25);
}

log?.snapshot("after").end();
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Returns a Promise that resolves after `ms` milliseconds.
* @param ms wait time in milliseconds.
*/
export const wait = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit 49dda19

Please sign in to comment.