-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommands.ts
60 lines (51 loc) · 1.92 KB
/
commands.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Cypress.Commands.add("getByTestId", { prevSubject: "optional" }, (subject, id) => {
if (subject) {
return cy.wrap(subject).find(`[data-testid="${id}"]`);
}
return cy.get(`[data-testid="${id}"]`);
});
Cypress.Commands.add("waitIfNecessary", (url, func) => {
const alias = "interception#" + Math.random().toString(36).substring(2, 8);
cy.intercept(url, { times: 1 }, () => {}).as(alias);
func();
cy.get("@" + alias).then((interception) => {
if (!interception) {
fetch(url);
}
});
cy.wait("@" + alias);
});
Cypress.Commands.add("goToWeek", (action) => {
cy.waitIfNecessary("/api/schedule", () => {
cy.getByTestId(`${action}-week-btn`).click();
});
});
declare global {
namespace Cypress {
interface Chainable {
/**
* Custom command to select DOM element by data-testid attribute.
* @example cy.getTestId('greeting')
*/
getByTestId(value: string): Chainable<JQuery<HTMLElement>>;
/**
* Custom command to enable waiting for potential api requests caused by some function.
* If the api request occurs, it is awaited.
* Otherwise, the request called manually, to clear the interception.
* @param url the url pattern for the application api request
* @param func the function to be called, which might trigger an api request
* @example
* cy.waitIfNecessary("/api/schedule", () => {
* cy.getByTestId(`${action}-week-btn`).click();
* })
*/
waitIfNecessary(url: string, func: () => void): void;
/**
* Shorthand to enable interaction with the week navigation buttons.
* @param action the button to be clicked
*/
goToWeek(action: "next" | "prev" | "today"): void;
}
}
}
export {};