Skip to content

Commit

Permalink
feat: add touchMoveDelay option to realSwipe (#621)
Browse files Browse the repository at this point in the history
* chore(docs): fix a few typos and issues in realSwipe docs

* feat(realSwipe): add `touchMoveDelay` option

closes #620
  • Loading branch information
alirezamirian authored and dmtrKovalenko committed Feb 16, 2024
1 parent 450539c commit 4786db2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ cy.realSwipe(direction, options);
Options:

- `Optional` **length**: undefined \| number **`default`** 10
- `Optional` **step**: undefined \| number **`default`** 10
- `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
20 changes: 17 additions & 3 deletions src/commands/realSwipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@ export interface RealSwipeOptions {
*/
y?: number;
/** Length of swipe (in pixels)
* @default 50
* @default 10
* @example
* cy.get(".drawer").realSwipe("toLeft", { length: 50 })
*/
length?: number;
/**
* Swipe step (how often new touch move will be generated). Less more precise
* ! Must be less than options.length
* Swipe step (how often new touch move will be generated).
* Must be less than or equal options.length
* @default 10
* 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 4786db2

Please sign in to comment.