From e2e5cf322a5ab6c9010ab3a8c85da60929e26607 Mon Sep 17 00:00:00 2001 From: MiraGeowerkstatt Date: Wed, 14 Aug 2024 09:04:18 +0200 Subject: [PATCH] Improve tests --- .../cypress/e2e/editor/boreholeTable.cy.js | 144 ++++++------------ src/client/cypress/e2e/editor/bulkedit.cy.js | 16 +- .../cypress/e2e/editor/copyBorehole.cy.js | 3 +- src/client/cypress/e2e/editor/location.cy.js | 3 +- src/client/cypress/e2e/filters/filter.cy.js | 12 +- .../cypress/e2e/helpers/dataGridHelpers.js | 29 ++++ src/client/cypress/e2e/viewer/viewer.cy.js | 7 +- 7 files changed, 96 insertions(+), 118 deletions(-) create mode 100644 src/client/cypress/e2e/helpers/dataGridHelpers.js diff --git a/src/client/cypress/e2e/editor/boreholeTable.cy.js b/src/client/cypress/e2e/editor/boreholeTable.cy.js index 408b1ca1a..664f362dd 100644 --- a/src/client/cypress/e2e/editor/boreholeTable.cy.js +++ b/src/client/cypress/e2e/editor/boreholeTable.cy.js @@ -1,129 +1,83 @@ import { loginAsAdmin, loginAsEditor, returnToOverview } from "../helpers/testHelpers.js"; +import { + showTableAndWaitForData, + verifiyPaginationText, + verifyRowContains, + waitForTableData, +} from "../helpers/dataGridHelpers"; + +function sortBy(headerTextContent) { + cy.get(".MuiDataGrid-columnHeader").contains(headerTextContent).click(); + cy.wait("@edit_list"); + cy.get(".loading-indicator").should("not.exist"); +} describe("Borehole editor table tests", () => { it("Boreholes are displayed in correct order with admin login", () => { loginAsAdmin(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); - - cy.wait("@edit_list"); - cy.get("div[id=map]").should("be.visible"); - - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".loading-indicator").should("not.exist"); + showTableAndWaitForData(); // default soring by name ascending - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("Aaliyah Casper").should("exist"); - }); - cy.get(".MuiDataGrid-row") - .eq(1) - .within(() => { - cy.contains("Aaliyah Lynch").should("exist"); - }); - cy.get(".MuiDataGrid-row") - .eq(2) - .within(() => { - cy.contains("Aaron Bartell").should("exist"); - }); + verifyRowContains("Aaliyah Casper", 0); + verifyRowContains("Aaliyah Lynch", 1); + verifyRowContains("Aaron Bartell", 2); // sort by Name descending - cy.get(".MuiDataGrid-columnHeader").contains("Name").click(); - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".loading-indicator").should("not.exist"); - - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("Zena Rath").should("exist"); - }); - cy.get(".MuiDataGrid-row") - .eq(1) - .within(() => { - cy.contains("Zena Mraz").should("exist"); - }); - cy.get(".MuiDataGrid-row") - .eq(2) - .within(() => { - cy.contains("Zena Halvorson").should("exist"); - }); - - // sort by borehole length desc - cy.get(".MuiDataGrid-columnHeader").contains("Borehole length").click(); - cy.get(".MuiDataGrid-columnHeader").contains("Borehole length").click(); - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".loading-indicator").should("not.exist"); - - cy.wait("@edit_list"); - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("1998.07").should("exist"); - }); - cy.get(".MuiDataGrid-row") - .eq(1) - .within(() => { - cy.contains("1997.79").should("exist"); - }); + sortBy("Name"); + verifyRowContains("Zena Rath", 0); + verifyRowContains("Zena Mraz", 1); + verifyRowContains("Zena Halvorson", 2); + + // sort by borehole length descending + sortBy("Borehole length"); + sortBy("Borehole length"); + verifyRowContains("1998.07", 0); + verifyRowContains("1997.79", 1); + verifyRowContains("1995.5", 2); // sort by reference elevation - cy.get(".MuiDataGrid-columnHeader").contains("Reference elevation").click(); - - cy.wait("@edit_list"); - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("3.36").should("exist"); - }); + sortBy("Reference elevation"); + verifyRowContains("3.36", 0); + verifyRowContains("4.26", 1); + verifyRowContains("4.58", 2); + + // sort by borehole type + sortBy("Borehole type"); + verifyRowContains("borehole", 0); + verifyRowContains("borehole", 1); + verifyRowContains("borehole", 2); }); it("preserves column sorting and active page when navigating", () => { loginAsEditor(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); + showTableAndWaitForData(); // sort by name descending - cy.get(".MuiDataGrid-columnHeaderTitle").contains("Name").click(); - cy.wait("@edit_list"); - - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("Zena Rath").should("exist"); - }); + sortBy("Name"); + verifyRowContains("Zena Rath", 0); // navigate to page 4 cy.get('[aria-label="next page"]').scrollIntoView().click(); - cy.wait("@edit_list"); + waitForTableData(); cy.get('[aria-label="next page"]').scrollIntoView().click(); - cy.wait("@edit_list"); + waitForTableData(); cy.get('[aria-label="next page"]').scrollIntoView().click(); - cy.wait("@edit_list"); + waitForTableData(); cy.get('[aria-label="next page"]').scrollIntoView().click(); - cy.wait("@edit_list"); + waitForTableData(); // verify current page is 4 - cy.get(".MuiTablePagination-displayedRows").should("have.text", "401 - 500 of 1627"); - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("Nichole VonRueden").should("exist").click(); - }); - cy.wait("@edit_list"); + verifiyPaginationText("401 - 500 of 1627"); + verifyRowContains("Nichole VonRueden", 0); // return to list returnToOverview(); // verify current page is still 4 - cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); - cy.get(".MuiTablePagination-displayedRows").should("have.text", "401 - 500 of 1627"); - cy.get(".MuiDataGrid-row") - .eq(0) - .within(() => { - cy.contains("Nichole VonRueden").should("exist").click(); - }); + showTableAndWaitForData(); + verifiyPaginationText("401 - 500 of 1627"); + verifyRowContains("Nichole VonRueden", 0); }); }); diff --git a/src/client/cypress/e2e/editor/bulkedit.cy.js b/src/client/cypress/e2e/editor/bulkedit.cy.js index 88e3e2bd5..e2019f50a 100644 --- a/src/client/cypress/e2e/editor/bulkedit.cy.js +++ b/src/client/cypress/e2e/editor/bulkedit.cy.js @@ -1,13 +1,11 @@ import { createBorehole, loginAsAdmin, startBoreholeEditing, stopBoreholeEditing } from "../helpers/testHelpers"; import adminUser from "../../fixtures/adminUser.json"; +import { showTableAndWaitForData } from "../helpers/dataGridHelpers"; beforeEach(() => { loginAsAdmin(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); - cy.get(".loading-indicator").should("not.exist"); + showTableAndWaitForData(); }); describe("Test the borehole bulk edit feature.", () => { @@ -49,10 +47,7 @@ describe("Test the borehole bulk edit feature.", () => { body: JSON.stringify(adminUser2Workgroups), }).as("adminUser2Workgroups"); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); - cy.get(".loading-indicator").should("not.exist"); + showTableAndWaitForData(); cy.get(".MuiDataGrid-columnHeaderCheckbox .MuiCheckbox-root").find('input[type="checkbox"]').check({ force: true }); cy.contains("button", "Bulk editing").click({ force: true }); @@ -74,7 +69,7 @@ describe("Test the borehole bulk edit feature.", () => { loginAsAdmin(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); + showTableAndWaitForData(); cy.wait("@borehole"); // select the boreholes for bulk edit @@ -133,8 +128,7 @@ describe("Test the borehole bulk edit feature.", () => { cy.visit(`/${id}/borehole`); startBoreholeEditing(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); + showTableAndWaitForData(); cy.contains(".MuiDataGrid-row", "AAA_JUNIORSOUFFLE") .find('.MuiCheckbox-root input[type="checkbox"]') .should("be.disabled"); diff --git a/src/client/cypress/e2e/editor/copyBorehole.cy.js b/src/client/cypress/e2e/editor/copyBorehole.cy.js index aabc42166..ad8741288 100644 --- a/src/client/cypress/e2e/editor/copyBorehole.cy.js +++ b/src/client/cypress/e2e/editor/copyBorehole.cy.js @@ -1,4 +1,5 @@ import { createBorehole, handlePrompt, loginAsAdmin, startBoreholeEditing } from "../helpers/testHelpers"; +import { showTableAndWaitForData } from "../helpers/dataGridHelpers"; describe("Test copying of boreholes", () => { it("copies a borehole", () => { @@ -6,7 +7,7 @@ describe("Test copying of boreholes", () => { loginAsAdmin(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); + showTableAndWaitForData(); cy.get(".MuiDataGrid-checkboxInput").eq(1).scrollIntoView().click(); cy.contains("button", "Create a copy").click(); diff --git a/src/client/cypress/e2e/editor/location.cy.js b/src/client/cypress/e2e/editor/location.cy.js index 9f437d224..7d5fac12e 100644 --- a/src/client/cypress/e2e/editor/location.cy.js +++ b/src/client/cypress/e2e/editor/location.cy.js @@ -1,4 +1,5 @@ import { newEditableBorehole, returnToOverview, stopBoreholeEditing } from "../helpers/testHelpers"; +import { showTableAndWaitForData } from "../helpers/dataGridHelpers"; describe("Tests for 'Location' edit page.", () => { it("creates and deletes a borehole.", () => { @@ -11,7 +12,7 @@ describe("Tests for 'Location' edit page.", () => { // stop editing stopBoreholeEditing(); returnToOverview(); - cy.get('[data-cy="showTableButton"]').click(); + showTableAndWaitForData(); // search the newly created borehole and delete it cy.get('[data-cy="borehole-table"]').within(() => { diff --git a/src/client/cypress/e2e/filters/filter.cy.js b/src/client/cypress/e2e/filters/filter.cy.js index 4e8dadb26..fb9da740f 100644 --- a/src/client/cypress/e2e/filters/filter.cy.js +++ b/src/client/cypress/e2e/filters/filter.cy.js @@ -1,4 +1,5 @@ import { loginAsAdmin } from "../helpers/testHelpers.js"; +import { verifiyPaginationText } from "../helpers/dataGridHelpers"; describe("Search filter tests", () => { it("has search filters", () => { @@ -90,7 +91,7 @@ describe("Search filter tests", () => { // check content of table cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiTablePagination-displayedRows").should("have.text", "1 - 100 of 329"); // when testing with cypress locally use electron browser, otherwise text might be displayed as "1-100 of 329" + verifiyPaginationText("1 - 100 of 329"); // when testing with cypress locally use electron browser, otherwise text might be displayed as "1-100 of 329" }); it("filters boreholes by color and uscs3", () => { @@ -116,7 +117,7 @@ describe("Search filter tests", () => { cy.wait("@edit_list"); cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiTablePagination-displayedRows").should("have.text", "1 - 100 of 229"); + verifiyPaginationText("1 - 100 of 229"); let uscs3Dropdown = cy.contains("label", "USCS 3").next(); uscs3Dropdown.scrollIntoView().click({ force: true }); @@ -134,7 +135,7 @@ describe("Search filter tests", () => { cy.wait("@edit_list"); // check content of table - cy.get(".MuiTablePagination-displayedRows").should("have.text", "1 - 39 of 39"); + verifiyPaginationText("1 - 39 of 39"); cy.get(".MuiDataGrid-row").contains("Darion Rowe").should("exist"); }); @@ -151,7 +152,7 @@ describe("Search filter tests", () => { filterByOriginalLithology(); cy.wait("@edit_list"); cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiTablePagination-displayedRows").should("have.text", "1 - 21 of 21"); + verifiyPaginationText("1 - 21 of 21"); }); it("filters boreholes by creation date", () => { loginAsAdmin(); @@ -179,8 +180,9 @@ describe("Search filter tests", () => { // check content of table cy.get('[data-cy="showTableButton"]').click(); - cy.get(".MuiTablePagination-displayedRows").should("have.text", "1 - 3 of 3"); + verifiyPaginationText("1 - 3 of 3"); }); + it("filters boreholes by workgroup", () => { loginAsAdmin(); cy.visit("/"); diff --git a/src/client/cypress/e2e/helpers/dataGridHelpers.js b/src/client/cypress/e2e/helpers/dataGridHelpers.js new file mode 100644 index 000000000..7c2cb6adf --- /dev/null +++ b/src/client/cypress/e2e/helpers/dataGridHelpers.js @@ -0,0 +1,29 @@ +export const verifiyPaginationText = text => { + cy.get(".MuiTablePagination-displayedRows").should("have.text", text); +}; + +export const sortBy = headerTextContent => { + cy.get(".MuiDataGrid-columnHeader").contains(headerTextContent).click(); + cy.wait("@edit_list"); + cy.get(".loading-indicator").should("not.exist"); +}; + +export const verifyRowContains = (rowContent, rowIndex) => { + cy.get(".MuiDataGrid-row") + .eq(rowIndex) + .within(() => { + cy.contains(rowContent).should("exist"); + }); +}; + +export const waitForTableData = () => { + cy.wait("@edit_list"); + cy.get(".MuiDataGrid-root").should("be.visible"); + cy.get(".loading-indicator").should("not.exist"); + cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); +}; + +export const showTableAndWaitForData = () => { + cy.get('[data-cy="showTableButton"]').click(); + waitForTableData(); +}; diff --git a/src/client/cypress/e2e/viewer/viewer.cy.js b/src/client/cypress/e2e/viewer/viewer.cy.js index a01f12ab3..1bbd41317 100644 --- a/src/client/cypress/e2e/viewer/viewer.cy.js +++ b/src/client/cypress/e2e/viewer/viewer.cy.js @@ -1,15 +1,12 @@ import { loginAsAdmin, loginAsViewer } from "../helpers/testHelpers"; +import { showTableAndWaitForData } from "../helpers/dataGridHelpers"; describe("Viewer tests", () => { it("Assures viewer cannot add, edit or delete boreholes", () => { loginAsViewer(); cy.visit("/"); - cy.get('[data-cy="showTableButton"]').click(); - cy.wait("@edit_list"); - cy.get("div[id=map]").should("be.visible"); - cy.get(".MuiDataGrid-root").should("be.visible"); - cy.get(".MuiDataGrid-row").should("have.length.greaterThan", 0); + showTableAndWaitForData(); cy.get('[data-cy="import-borehole-button"]').should("have.class", "Mui-disabled"); cy.get('[data-cy="new-borehole-button"]').should("have.class", "Mui-disabled");