Skip to content

Commit

Permalink
WIP Cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippluca committed Jan 15, 2025
1 parent d00e062 commit 7a0d7f8
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 19 deletions.
157 changes: 138 additions & 19 deletions src/Geopilot.Frontend/cypress/e2e/delivery.cy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadWithoutAuth, loginAsUploader } from "./helpers/appHelpers.js";
import { clickCancel } from "./helpers/buttonHelpers.js";
import { evaluateSelect, hasError, isDisabled, setInput, setSelect, toggleCheckbox } from "./helpers/formHelpers.js";
import { evaluateSelect, getFormField, hasError, isDisabled, setInput, setSelect, toggleCheckbox } from "./helpers/formHelpers.js";

const fileNameExists = (filePath, success) => {
const fileName = filePath.split("/").pop();
Expand Down Expand Up @@ -124,14 +124,23 @@ const mockMandates = () => {
{
id: 1,
name: "Handmade Soft Cheese",
evaluatePrecursorDelivery: "notEvaluated",
evaluatePartial: "notEvaluated",
evaluateComment: "notEvaluated",
},
{
id: 5,
name: "Licensed Frozen Towels",
evaluatePrecursorDelivery: "optional",
evaluatePartial: "notEvaluated",
evaluateComment: "optional",
},
{
id: 9,
name: "Unbranded Wooden Pants",
evaluatePrecursorDelivery: "required",
evaluatePartial: "required",
evaluateComment: "required",
},
],
delay: 500,
Expand Down Expand Up @@ -240,9 +249,10 @@ describe("Delivery tests", () => {
stepIsActive("submit", false); // Should not be active if validation has errors
});

it("can submit a delivery", () => {
it("can cancel at any step", () => {
mockValidationSuccess();
mockMandates();
mockUploadSuccess();
cy.intercept({ url: "/api/v1/delivery?mandateId=*", method: "GET" }).as("precursors");
cy.intercept({ url: "/api/v1/delivery", method: "POST" }, req => {
req.reply({
Expand All @@ -256,6 +266,7 @@ describe("Delivery tests", () => {
}).as("submit");

loginAsUploader();

// All steps are visible
cy.get('[data-cy="upload-step"]').should("exist");
cy.get('[data-cy="validate-step"]').should("exist");
Expand All @@ -264,19 +275,14 @@ describe("Delivery tests", () => {
stepIsActive("upload");

// All file types are allowed
addFile("deliveryFiles/invalid-type.png", true);
addFile("deliveryFiles/ilimodels_not_conform.xml", true);
stepHasError("upload", false);

// Selected file can be removed
cy.get('[data-cy="file-remove-button"]').click();
cy.contains("invalid-type.png").should("not.exist");
stepIsActive("upload");

// Can upload a file once the terms of use are accepted and the file is valid
cy.get('[data-cy="upload-button"]').should("be.disabled");
addFile("deliveryFiles/ilimodels_not_conform.xml", true);
cy.get('[data-cy="upload-button"]').should("be.disabled");
toggleCheckbox("acceptTermsOfUse");
cy.get('[data-cy="upload-button"]').should("not.be.disabled");
uploadFile();

// Can cancel the upload which will reset the form
Expand All @@ -299,6 +305,22 @@ describe("Delivery tests", () => {
resetDelivery("validate");
cy.get('[data-cy="upload-step"]').contains("ilimodels_not_conform.xml").should("not.exist");

// Validation starts automatically after a file is uploaded
addFile("deliveryFiles/ilimodels_not_conform.xml", true);
cy.get('[data-cy="upload-button"]').should("not.be.disabled");
uploadFile();
cy.wait("@upload");
stepIsLoading("upload", false);
stepIsCompleted("upload");
cy.get('[data-cy="upload-step"]').contains("ilimodels_not_conform.xml");
stepIsActive("validate");
stepIsLoading("validate");
cy.get('[data-cy="validate-step"]').contains("The file is currently being validated with ilicheck...");

// Validation can be cancelled
resetDelivery("validate");
cy.get('[data-cy="upload-step"]').contains("ilimodels_not_conform.xml").should("not.exist");

// Submit is active if validation is successful
addFile("deliveryFiles/ilimodels_valid.xml", true);
uploadFile();
Expand All @@ -317,6 +339,55 @@ describe("Delivery tests", () => {

// Submit can be cancelled, which will return to step 1 with a reset form
resetDelivery("submit");
});

it("can upload any filetype", () => {
mockValidationSuccess();

loginAsUploader();
stepIsActive("upload");

// All file types are allowed
addFile("deliveryFiles/invalid-type.png", true);
stepHasError("upload", false);
uploadFile();

stepIsCompleted("upload");
});

it("checks if terms of use are accepted", () => {
loginAsUploader();
mockUploadSuccess();
addFile("deliveryFiles/ilimodels_valid.xml", true);

// Can upload a file once the terms of use are accepted and the file is valid
cy.get('[data-cy="upload-button"]').should("be.disabled");
addFile("deliveryFiles/ilimodels_not_conform.xml", true);
cy.get('[data-cy="upload-button"]').should("be.disabled");
toggleCheckbox("acceptTermsOfUse");
cy.get('[data-cy="upload-button"]').should("not.be.disabled");
uploadFile();

cy.wait("@upload");
stepIsCompleted("upload");
});

it("can submit delivery", () => {
mockValidationSuccess();
mockMandates();
cy.intercept({ url: "/api/v1/delivery?mandateId=*", method: "GET" }).as("precursors");
cy.intercept({ url: "/api/v1/delivery", method: "POST" }, req => {
req.reply({
statusCode: 201,
body: {
id: 43,
jobId: "d49ba857-5db5-45a0-b838-9d41cc7d8d64",
},
delay: 500,
});
}).as("submit");

loginAsUploader();

// Add delivery with minimal form
addFile("deliveryFiles/ilimodels_valid.xml", true);
Expand All @@ -330,18 +401,69 @@ describe("Delivery tests", () => {

cy.get('[data-cy="createDelivery-button"]').should("be.disabled");
isDisabled("mandate", false);
isDisabled("precursor", true);

getFormField("precursor").should("not.exist");
getFormField("isPartial").should("not.exist");
getFormField("comment").should("not.exis");

// NotEvaluated mandate does not show input fields;
setSelect("mandate", 1, 3);
cy.wait("@precursors");
isDisabled("precursor", false);
getFormField("precursor").should("not.exist");
getFormField("isPartial").should("not.exist");
getFormField("comment").should("not.exis");
cy.get('[data-cy="createDelivery-button"]').should("be.enabled");
setSelect("precursor", 1);
setSelect("mandate", 2);

// Optional mandate does show optional input fields
setSelect("mandate", 2, 3);
cy.wait("@precursors");
getFormField("precursor").should("exist");
getFormField("isPartial").should("not.exist");
getFormField("comment").should("exis");
setSelect("precursor", 0); // reset
setInput("comment", "");
evaluateSelect("precursor", "");
setSelect("precursor", 2);
setSelect("precursor", 0);
hasError("precursor", false);
hasError("isPartial", false);
hasError("comment", false);
cy.get('[data-cy="createDelivery-button"]').should("be.enabled");

// Required mandate does show all input fields
setSelect("mandate", 3, 3);
cy.wait("@precursors");
getFormField("precursor").should("exist");
getFormField("isPartial").should("exist");
getFormField("comment").should("exis");
cy.get('[data-cy="createDelivery-button"]').should("be.disabled");
hasError("precursor", false);
hasError("isPartial", false);
hasError("comment", false);

// Touch fields
getFormField("precursor").focus();
getFromField("comment").focus();
hasError("precursor", false);
hasError("comment", false);

// Set valid values
setSelect("precursor", 1);
toggleCheckbox("isPartial");
setInput("comment", "This is a test comment.");
hasError("precursor", false);
hasError("isPartial", false);
hasError("comment", false);
cy.get('[data-cy="createDelivery-button"]').should("be.enabled");

// Change of mandate clears inputs & errors
setSelect("mandate", 1, 3);
setSelect("mandate", 3, 3);
getFormField("precursor").contains("");
getFormField("comment").contains("");
cy.get('[data-cy="createDelivery-button"]').should("be.disabled");

// Submit minimal delivery
setSelect("mandate", 1, 3);
cy.wait("@precursors");
cy.get('[data-cy="createDelivery-button"]').should("be.enabled");
cy.get('[data-cy="createDelivery-button"]').click();
stepIsLoading("submit");
Expand All @@ -364,7 +486,7 @@ describe("Delivery tests", () => {
stepIsCompleted("done", false);
cy.get('[data-cy="upload-button"]').should("be.disabled");

// Add delivery with completed form
// Add delivery with previously completed form
addFile("deliveryFiles/ilimodels_valid.xml", true);
uploadFile();
cy.wait("@upload");
Expand All @@ -376,9 +498,6 @@ describe("Delivery tests", () => {

setSelect("mandate", 1);
cy.wait("@precursors");
setSelect("precursor", 1);
toggleCheckbox("isPartial");
setInput("comment", "This is a test comment.");
cy.get('[data-cy="createDelivery-button"]').click();
stepIsLoading("submit");
cy.wait("@submit");
Expand Down
11 changes: 11 additions & 0 deletions src/Geopilot.Frontend/cypress/e2e/helpers/formHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const isDisabled = (fieldName, isDisabled = true, parent) => {
}
};

/**
* Gets a from element.
* @param {any} fieldName The name of the form element.
* @param {any} parent (optional) The parent of the form element.
* @returns
*/
export const getFormField = (fieldName, parent) => {
const selector = createBaseSelector(parent) + `[data-cy^="${fieldName}-form"]`;
return cy.get(selector);
};

/**
* Sets the value for an input form element.
* @param {string} fieldName The name of the input field.
Expand Down

0 comments on commit 7a0d7f8

Please sign in to comment.