Skip to content

Commit 9eb923b

Browse files
authored
Refactor cypress tests (#23)
1 parent e5a3efe commit 9eb923b

File tree

7 files changed

+65
-76
lines changed

7 files changed

+65
-76
lines changed

cypress/integration/issue-list.spec.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,32 @@ import mockIssues1 from "../fixtures/issues-page-1.json";
22
import mockIssues2 from "../fixtures/issues-page-2.json";
33
import mockIssues3 from "../fixtures/issues-page-3.json";
44

5-
describe("Project List", () => {
5+
describe("Issue List", () => {
66
beforeEach(() => {
7+
// setup request mocks
78
cy.intercept("GET", "https://prolog-api.profy.dev/project", {
89
fixture: "projects.json",
9-
});
10+
}).as("getProjects");
1011
cy.intercept("GET", "https://prolog-api.profy.dev/issue?page=1", {
1112
fixture: "issues-page-1.json",
12-
});
13+
}).as("getIssues");
1314
cy.intercept("GET", "https://prolog-api.profy.dev/issue?page=2", {
1415
fixture: "issues-page-2.json",
1516
});
1617
cy.intercept("GET", "https://prolog-api.profy.dev/issue?page=3", {
1718
fixture: "issues-page-3.json",
1819
});
20+
21+
// open issues page
1922
cy.visit("http://localhost:3000/issues");
23+
24+
// wait for request to resolve
25+
cy.wait("@getProjects");
26+
cy.wait("@getIssues");
27+
28+
// set button aliases
29+
cy.get("button", { timeout: 10000 }).contains("Previous").as("prev-button");
30+
cy.get("button").contains("Next").as("next-button");
2031
});
2132

2233
context("desktop resolution", () => {
@@ -25,8 +36,8 @@ describe("Project List", () => {
2536
});
2637

2738
it("renders the issues", () => {
28-
cy.findByRole("main")
29-
.findAllByRole("row")
39+
cy.get("main")
40+
.find("tr")
3041
.each(($el, index) => {
3142
// skip the header row
3243
if (index === 0) return;
@@ -43,38 +54,29 @@ describe("Project List", () => {
4354
it("paginates the data", () => {
4455
// test first page
4556
cy.contains("Page 1 of 3");
46-
cy.findByRole("button", { name: "Previous" }).should(
47-
"have.attr",
48-
"disabled"
49-
);
57+
cy.get("@prev-button").should("have.attr", "disabled");
5058

5159
// test navigation to second page
52-
cy.findByRole("button", { name: "Next" }).click();
53-
cy.findByRole("button", { name: "Previous" }).should(
54-
"not.have.attr",
55-
"disabled"
56-
);
60+
cy.get("@next-button").click();
61+
cy.get("@prev-button").should("not.have.attr", "disabled");
5762
cy.contains("Page 2 of 3");
5863
cy.get("tbody tr:first").contains(mockIssues2.items[0].message);
5964

6065
// test navigation to third and last page
61-
cy.findByRole("button", { name: "Next" }).click();
62-
cy.findByRole("button", { name: "Next" }).should("have.attr", "disabled");
66+
cy.get("@next-button").click();
67+
cy.get("@next-button").should("have.attr", "disabled");
6368
cy.contains("Page 3 of 3");
6469
cy.get("tbody tr:first").contains(mockIssues3.items[0].message);
6570

6671
// test navigation back to second page
67-
cy.findByRole("button", { name: "Previous" }).click();
68-
cy.findByRole("button", { name: "Next" }).should(
69-
"not.have.attr",
70-
"disabled"
71-
);
72+
cy.get("@prev-button").click();
73+
cy.get("@next-button").should("not.have.attr", "disabled");
7274
cy.contains("Page 2 of 3");
7375
cy.get("tbody tr:first").contains(mockIssues2.items[0].message);
7476
});
7577

76-
it("perists page after reload", () => {
77-
cy.findByRole("button", { name: "Next" }).click();
78+
it("persists page after reload", () => {
79+
cy.get("@next-button").click();
7880
cy.contains("Page 2 of 3");
7981

8082
cy.reload();

cypress/integration/navigation.spec.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ describe("Sidebar Navigation", () => {
99
});
1010

1111
it("links are working", () => {
12-
cy.findByRole("navigation").findByText("Issues").click();
12+
// check that issues link leads to the right page
13+
cy.get("nav").contains("Issues").click();
1314
cy.url().should("eq", "http://localhost:3000/issues");
1415

15-
cy.findByRole("navigation").findByText("Projects").click();
16+
// check that projects link leads to the right page
17+
cy.get("nav").contains("Projects").click();
1618
cy.url().should("eq", "http://localhost:3000/");
1719
});
1820

1921
it("is collapsible", () => {
20-
cy.findByText("Collapse").click();
21-
cy.findByRole("navigation")
22-
.findAllByRole("link")
23-
.should("have.length", 5);
24-
cy.findByRole("navigation").findByText("Issues").should("not.exist");
22+
// collapse navigation
23+
cy.get("nav").contains("Collapse").click();
24+
25+
// check that links still exist
26+
cy.get("nav").find("a").should("have.length", 5);
27+
28+
// check that text is not rendered
29+
cy.get("nav").contains("Issues").should("not.exist");
2530
});
2631
});
2732

@@ -32,6 +37,7 @@ describe("Sidebar Navigation", () => {
3237

3338
function isInViewport(el: string) {
3439
cy.get(el).then(($el) => {
40+
// navigation should cover the whole screen
3541
const rect = $el[0].getBoundingClientRect();
3642
expect(rect.right).to.be.equal(rect.width);
3743
expect(rect.left).to.be.equal(0);
@@ -40,29 +46,34 @@ describe("Sidebar Navigation", () => {
4046

4147
function isNotInViewport(el: string) {
4248
cy.get(el).then(($el) => {
49+
// naviation should be outside of the screen
4350
const rect = $el[0].getBoundingClientRect();
4451
expect(rect.left).to.be.equal(-rect.width);
4552
expect(rect.right).to.be.equal(0);
4653
});
4754
}
4855

4956
it("toggles sidebar navigation by clicking the menu icon", () => {
57+
// wait for animation to finish
5058
cy.wait(500);
5159
isNotInViewport("nav");
5260

53-
cy.findByAltText("open menu").click();
61+
// open mobile navigation
62+
cy.get("img[alt='open menu']").click();
5463

64+
// wait for animation to finish
5565
cy.wait(500);
5666
isInViewport("nav");
5767

58-
cy.findByRole("navigation")
59-
.findAllByRole("link")
60-
.should("have.length", 5);
68+
// check that all links are rendered
69+
cy.get("nav").find("a").should("have.length", 5);
6170

62-
cy.findByText("Support").should("exist");
63-
cy.findByText("Collapse").should("not.be.visible");
71+
// Support button should be rendered but Collapse button not
72+
cy.get("nav").contains("Support").should("exist");
73+
cy.get("nav").contains("Collapse").should("not.be.visible");
6474

65-
cy.findByAltText("close menu").click();
75+
// close mobile navigation and check that it disappears
76+
cy.get("img[alt='close menu']").click();
6677
cy.wait(500);
6778
isNotInViewport("nav");
6879
});

cypress/integration/project-list.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import mockProjects from "../fixtures/projects.json";
33

44
describe("Project List", () => {
55
beforeEach(() => {
6+
// setup request mock
67
cy.intercept("GET", "https://prolog-api.profy.dev/project", {
78
fixture: "projects.json",
8-
});
9+
}).as("getProjects");
10+
11+
// open projects page
912
cy.visit("http://localhost:3000");
13+
14+
// wait for request to resolve
15+
cy.wait("@getProjects");
1016
});
1117

1218
context("desktop resolution", () => {
@@ -17,17 +23,17 @@ describe("Project List", () => {
1723
it("renders the projects", () => {
1824
const languageNames = ["React", "Node.js", "Python"];
1925

20-
cy.findByRole("main")
21-
.findAllByRole("listitem")
26+
// get all project cards
27+
cy.get("main")
28+
.find("li")
2229
.each(($el, index) => {
30+
// check that project data is rendered
2331
cy.wrap($el).contains(mockProjects[index].name);
2432
cy.wrap($el).contains(languageNames[index]);
2533
cy.wrap($el).contains(mockProjects[index].numIssues);
2634
cy.wrap($el).contains(mockProjects[index].numEvents24h);
2735
cy.wrap($el).contains(capitalize(mockProjects[index].status));
28-
cy.wrap($el)
29-
.findByRole("link")
30-
.should("have.attr", "href", "/issues");
36+
cy.wrap($el).find("a").should("have.attr", "href", "/issues");
3137
});
3238
});
3339
});

cypress/support/commands.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,3 @@
2323
//
2424
// -- This will overwrite an existing command --
2525
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
26-
import "@testing-library/cypress/add-commands";

cypress/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"isolatedModules": false,
66
// be explicit about types included
77
// to avoid clashing with Jest types
8-
"types": ["cypress", "@testing-library/cypress"]
8+
"types": ["cypress"]
99
},
1010
"include": ["../node_modules/cypress", "./**/*.ts"]
1111
}

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"@storybook/addon-links": "^6.4.19",
3434
"@storybook/react": "^6.4.19",
3535
"@storybook/testing-library": "^0.0.9",
36-
"@testing-library/cypress": "^8.0.2",
3736
"@types/jest": "^27.4.1",
3837
"@types/node": "17.0.21",
3938
"@types/react": "17.0.39",

0 commit comments

Comments
 (0)