Skip to content

Commit 3ac2339

Browse files
authored
Add cypress-pipe, add custom command to retry clicks (#415)
1 parent 4117466 commit 3ac2339

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

cypress/e2e/course/restricted-courses.cy.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,18 @@ describe("whitelisted courses", () => {
9393
cy.contains(".csm-btn", /cs61a/i).should("be.visible");
9494

9595
// view unrestricted courses; should show nothing
96-
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).click();
96+
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).clickUntil($el => {
97+
expect($el).to.have.class("active");
98+
});
9799
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).should("have.class", "active");
98100
cy.contains(".csm-btn", /cs61a/i).should("not.exist");
99101

100102
// go to cs61a sections
101-
cy.contains(".course-menu-sidebar-tab", /restricted/i).click();
103+
cy.contains(".course-menu-sidebar-tab", /restricted/i).clickUntil($el => {
104+
expect($el).to.have.class("active");
105+
});
102106
cy.contains(".course-menu-sidebar-tab", /restricted/i).should("have.class", "active");
107+
103108
cy.contains(".csm-btn", /cs61a/i).click();
104109
cy.get(".section-card").should("have.length", 1).should("be.visible");
105110

@@ -143,13 +148,17 @@ describe("whitelisted courses", () => {
143148
cy.contains(".csm-btn", /cs70/i).should("not.exist");
144149

145150
// view unrestricted courses; should show cs70, but not cs61a
146-
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).click();
151+
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).clickUntil($el => {
152+
expect($el).to.have.class("active");
153+
});
147154
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).should("have.class", "active");
148155
cy.contains(".csm-btn", /cs61a/i).should("not.exist");
149156
cy.contains(".csm-btn", /cs70/i).should("be.visible");
150157

151158
// go to cs61a sections
152-
cy.contains(".course-menu-sidebar-tab", /restricted/i).click();
159+
cy.contains(".course-menu-sidebar-tab", /restricted/i).clickUntil($el => {
160+
expect($el).to.have.class("active");
161+
});
153162
cy.contains(".course-menu-sidebar-tab", /restricted/i).should("have.class", "active");
154163
cy.contains(".csm-btn", /cs61a/i).click();
155164
cy.get(".section-card").should("have.length", 1).should("be.visible");
@@ -225,15 +234,19 @@ describe("whitelisted courses", () => {
225234
cy.contains(".enrollment-container .enrollment-course", /cs61a/i).should("be.visible");
226235

227236
// view unrestricted courses; should show cs70, but not cs61a
228-
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).click();
237+
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).clickUntil($el => {
238+
expect($el).to.have.class("active");
239+
});
229240
cy.contains(".course-menu-sidebar-tab", /unrestricted/i).should("have.class", "active");
230241
cy.contains(".csm-btn", /cs61a/i).should("not.exist");
231242
cy.contains(".csm-btn", /cs70/i).should("be.visible");
232243
// should not show any enrollment times
233244
cy.get(".enrollment-container").should("not.exist");
234245

235246
// go to cs61a sections
236-
cy.contains(".course-menu-sidebar-tab", /restricted/i).click();
247+
cy.contains(".course-menu-sidebar-tab", /restricted/i).clickUntil($el => {
248+
expect($el).to.have.class("active");
249+
});
237250
cy.contains(".course-menu-sidebar-tab", /restricted/i).should("have.class", "active");
238251
cy.contains(".csm-btn", /cs61a/i).click();
239252
cy.get(".section-card").should("have.length", 1).should("be.visible");

cypress/support/commands.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,14 @@ Cypress.Commands.add("_exec", command => {
7272
}
7373
});
7474
});
75+
76+
/**
77+
* Chained click until a condition is satisfied;
78+
* doesn't use the Cypress .click(), and instead uses the jQuery .trigger("click").
79+
*
80+
* Should guard this with a visibility check to ensure that the element is actually clickable.
81+
*/
82+
Cypress.Commands.add("clickUntil", { prevSubject: true }, (subject: JQuery<HTMLElement>, condition) => {
83+
const click = ($el: JQuery<HTMLElement>) => $el.trigger("click");
84+
return cy.wrap(subject).pipe(click).should(condition);
85+
});

cypress/support/e2e.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// https://on.cypress.io/configuration
1414
// ***********************************************************
1515

16+
// add pipe command
17+
import "cypress-pipe";
18+
1619
// Import commands.js using ES2015 syntax:
17-
import './commands'
20+
import "./commands";
1821

1922
// Alternatively you can use CommonJS syntax:
2023
// require('./commands')

cypress/support/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ declare namespace Cypress {
1414
setupDB(script_name: string, func_name: string, options?: SetupDBOptions): Chainable<void>;
1515
initDB(): Chainable<void>;
1616
_exec(command: string): Chainable<void>;
17+
clickUntil(
18+
condition: ($el: JQuery<HTMLElement>) => void | Chainable<JQuery<HTMLElement>>
19+
): Chainable<JQuery<HTMLElement>>;
1720
}
1821
}

cypress/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
"allowJs": true,
44
"baseUrl": "../node_modules",
5-
"types": ["cypress"],
5+
"types": ["cypress", "cypress-pipe"],
66
"target": "es2019"
77
},
88
"include": ["**/*.*"]

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"@testing-library/react": "^13.4.0",
3939
"@types/js-cookie": "^3.0.2",
4040
"@types/lodash": "^4.14.175",
41+
"@types/random-words": "^1.1.2",
4142
"@types/react-router-dom": "^5.3.3",
4243
"@typescript-eslint/eslint-plugin": "^5.43.0",
4344
"@typescript-eslint/parser": "^5.43.0",
4445
"babel-jest": "^28.1.1",
45-
"@types/random-words": "^1.1.2",
4646
"babel-loader": "^8.2.2",
4747
"babel-plugin-dynamic-import-node": "^2.3.3",
4848
"babel-plugin-lodash": "^3.3.4",
@@ -51,6 +51,7 @@
5151
"css-loader": "^6.2.0",
5252
"csso-cli": "^3.0.0",
5353
"cypress": "^12.2.0",
54+
"cypress-pipe": "^2.0.0",
5455
"eslint": "^8.27.0",
5556
"eslint-config-prettier": "^7.2.0",
5657
"eslint-plugin-cypress": "^2.12.1",

0 commit comments

Comments
 (0)