Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDS-5505] e2e test major projects refactor #2780

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/core-web.unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
run: yarn workspace @mds/core-web run http-server-spa build /index.html 3000 &

- name: Run cypress tests
run: yarn workspace @mds/core-web cypress run --spec "**/*.cy.ts,!**/majorprojects.cy.ts"
run: yarn workspace @mds/core-web cypress run
env:
CYPRESS_TEST_USER: ${{ secrets.CYPRESS_CORE_USER }}
CYPRESS_TEST_PASSWORD: ${{ secrets.CYPRESS_CORE_PASSWORD }}
Expand Down
150 changes: 63 additions & 87 deletions services/core-web/cypress/e2e/majorprojects.cy.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,74 @@
describe("Major Projects", () => {
beforeEach(() => {
cy.login();
beforeEach(() => {
cy.login();

cy.get('[data-cy="home-link-button-major-projects"]', { timeout: 5000 }).click({ force: true });
cy.get('[data-cy="home-link-button-major-projects"]', { timeout: 10000 }).click({ force: true });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one thing- is this running 2 separate tests, the first which uploads the file and the second which downloads? If so, would it be faster to run it as one test that uploads and then downloads, taking out the login and navigation in the middle? I know that unit-test thinking promotes making tests smaller and independent, but cypress recommends combining pieces of the process into a longer one to enhance performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combined the 2 tests into one to enhance performance.


// .eq(1) selects the second row (0-based index).
cy.get("[data-cy=major-projects-table-open-button]", { timeout: 5000 })
.eq(1)
.find("button")
.click({ force: true });
// .eq(1) selects the second row (0-based index).
cy.get("[data-cy=major-projects-table-open-button]", { timeout: 10000 })
.eq(1)
.find("button")
.click({ force: true });

cy.get('a[data-cy="project-description-view-link"]', { timeout: 5000 }).click();

// Wait for the edit button to be visible and click it
cy.get("#project-summary-submit").then(($button) => {
$button[0].click();
cy.get('a[data-cy="project-description-view-link"]', { timeout: 5000 }).click();
});
});

it("should upload a document successfully", () => {
const fileName = "dummy.pdf";

cy.fixture(fileName).then((fileContent) => {
cy.get('input[type="file"]').attachFile({
fileContent: fileContent,
fileName: fileName,
mimeType: "application/pdf",
});

// Mock the API call with a regular expression in the URL pattern
cy.intercept(
"POST",
/.*\/(api\/)?projects\/.*\/project-summaries\/.*\/documents\?mine_guid=.*$/,
(req) => {
req.reply((res) => {
res.send({
statusCode: 200,
delay: 1000,
});
});
}
).as("uploadComplete");

// Wait for the mocked PATCH request to complete
cy.wait("@uploadComplete");

// Make the PATCH request
cy.intercept("PATCH", /(?:\/document-manager)?\/documents\/.*$/, (req) => {
req.reply((res) => {
res.send({
statusCode: 200,
delay: 1000,
});
});
}).as("patchComplete");

// Wait for the mocked PATCH request to complete
cy.wait("@patchComplete");

// Make the GET request
cy.intercept("GET", /(?:\/api)?\/mines\/documents\/upload\/.*$/, (req) => {
req.reply((res) => {
res.send({
statusCode: 200,
delay: 1000,
});

it("should upload a document successfully", () => {

const fileName = 'dummy.pdf';

cy.get("#project-summary-submit").then(($button) => {
$button[0].click();
});
}).as("getComplete");

// Wait for the mocked GET request to complete
cy.wait("@getComplete");
cy.fixture(fileName).then((fileContent) => {
const apiUrlRegex = /.*\/(api\/)?projects\/.*\/project-summaries\/.*\/documents\?mine_guid=.*$/;

// Intercept the POST request and stub the response
cy.intercept('POST', apiUrlRegex, {
statusCode: 200,
body: { message: "file uploaded successfully" }, // Stubbed response
}).as('uploadRequest');

cy.get('input[type="file"]').attachFile({
fileContent: fileContent,
fileName: fileName,
mimeType: 'application/pdf',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did eslint not catch this file for formatting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to allow eslint to reformat. My frontend git tool had no verify checked all the while.

});

// Wait for the upload request to complete (simulated)
cy.wait('@uploadRequest').then((interception) => {
// Assert that the response body contains the expected message
expect(interception.response.body.message).to.equal("file uploaded successfully");
});
});

// Wait for the "Upload complete" text to appear within a maximum of 25 seconds.
cy.contains(".filepond--file-status-main", "Upload complete", { timeout: 25000 });
});
});

it("should download a document successfully", () => {
cy.get("[data-cy=menu-actions-button]")
.first()
.click({ force: true });

// Click the Download file button in the dropdown
cy.contains("button", "Download file", { timeout: 1000 })
.find("div")
.click({ force: true });

// Wait for the file to download
cy.url().then((url) => {
// Make an HTTP request to the URL
cy.request(url).then((response) => {
// Check the response status code
expect(response.status).to.eq(301);
});

it("should download a document successfully", () => {

cy.intercept("GET", "**/documents**", (req) => {
// Set the desired response properties
req.reply({
statusCode: 301,
body: "Mocked response data",
});
}).as("downloadRequest");

cy.get("[data-cy=menu-actions-button]")
.first()
.click({ force: true });

// Click the Download file button in the dropdown
cy.contains("button", "Download file", { timeout: 3000 })
.find("div")
.click({ force: true });

// Wait for the network request to complete
cy.wait("@downloadRequest").then((interception) => {
// Check that the request was made successfully
expect(interception.response.statusCode).to.equal(301);
});
});
});

});
Loading