Skip to content

Commit

Permalink
Add e2e test for starting material
Browse files Browse the repository at this point in the history
Add e2e test for starting material

Add e2e test for starting material

Add e2e test for starting-material
  • Loading branch information
BenjaminCharmes committed Sep 26, 2024
1 parent b448266 commit fcd512b
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 1 deletion.
128 changes: 128 additions & 0 deletions webapp/cypress/e2e/startingMaterial.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const API_URL = Cypress.config("apiUrl");
console.log(API_URL);

let item_ids = ["test_sm1", "test_sm2", "test_sm3", "123startingMaterial", "test_sm3_copy"];

before(() => {
cy.visit("/starting-materials");
cy.removeAllTestSamples(item_ids);
cy.visit("/starting-materials").then(() => {
cy.get("[data-testid='starting-material-table'] > tbody > tr").should("have.length", 0);
});
});

after(() => {
cy.visit("/starting-materials");
cy.removeAllTestSamples(item_ids);
cy.visit("/starting-materials").then(() => {
cy.get("[data-testid='starting-material-table'] > tbody > tr").should("have.length", 0);
});
});

describe("Starting material table page", () => {
beforeEach(() => {
cy.visit("/starting-materials");
});

it("Loads the Starting material page without any errors", () => {
cy.findByText("About").should("exist");
cy.findByText("Inventory").should("exist");
cy.findByText("Add a starting material").should("exist");
});

it("Adds some valid starting material entries", () => {
cy.createStartingMaterial("test_sm1", "a scientific starting material");
cy.verifyStartingMaterial("test_sm1", "a scientific starting material");

cy.createStartingMaterial("test_sm2");
cy.verifyStartingMaterial("test_sm2");

cy.createStartingMaterial("test_sm3", "my inst", "1990-01-07T00:00:00");
cy.verifyStartingMaterial("test_sm3", "my inst", "1990-01-07T00:00:00");

cy.createStartingMaterial("123startingMaterial", null, "2005-04-03T00:00:00");
cy.verifyStartingMaterial("123startingMaterial", null, "2005-04-03T00:00:00");
});

it("Checks if one of the starting material is in the database", () => {
cy.request({ url: `${API_URL}/get-item-data/test_sm3`, failOnStatusCode: true })
.its("body")
.then((body) => {
expect(body).to.have.property("item_id", "test_sm3");
expect(body.item_data).to.have.property("item_id", "test_sm3");
expect(body.item_data).to.have.property("name", "my inst");
expect(body.item_data).to.have.property("date", "1990-01-07T00:00:00");
});
});

it("Attempts to Add a starting material with the same name", () => {
cy.findByText("Add a starting material").click();
cy.get('[data-testid="create-item-form"]').within(() => {
cy.findByLabelText("ID:").type("test_sm3");
cy.findByText("Submit").click();
});

cy.contains("already in use").should("exist");
cy.get(".form-error a").contains("test_sm3");

cy.get("[data-testid=create-item-form]").contains("Submit").should("be.disabled");
});

it("Deletes a starting material", function () {
cy.deleteStartingMaterial(["test_sm2"]);
cy.contains("test_sm2").should("not.exist");

cy.request({ url: `${API_URL}/get-item-data/test_sm2`, failOnStatusCode: false }).then(
(resp) => {
expect(resp.status).to.be.gte(400).lt(500);
},
);
});

it("copies a starting material entry", () => {
cy.findByText("Add a starting material").click();

cy.get('[data-testid="create-item-form"]').within(() => {
cy.findByLabelText("ID:").type("test_sm3_copy");
cy.findByLabelText("(Optional) Copy from existing starting material:").type("test_sm3");
cy.findByLabelText("(Optional) Copy from existing starting material:")
.contains(".vs__dropdown-menu .badge", "test_sm3")
.click();

cy.findByLabelText("Name:"); //("COPY OF my inst")

cy.findByText("Submit").click();
});

cy.verifyStartingMaterial("test_sm3_copy", "COPY OF my inst", null);
});
});

describe("Starting material edit page", () => {
beforeEach(() => {
cy.visit("/starting-materials/");
});

it("Checks the equipment edit page", () => {
cy.findByText("test_sm3").click();
cy.wait(1000);
cy.go("back");
cy.verifyStartingMaterial("test_sm3", "my inst", "1990-01-07T00:00:00");
});

it("modifies some data in a starting material", () => {
cy.findByText("test_sm3").click();
cy.get('[id="item-description"]').type("this is a description of testB.");
cy.findByLabelText("Date acquired").type("2000-01-01");
cy.findByLabelText("Location").type("room 404");

cy.findByText("Add a block").click();
cy.findByText("Comment").click();

cy.get(".datablock-content div").first().type("a comment is added here.");

cy.get(".fa-save").click();
cy.visit("/starting-materials");
cy.verifyStartingMaterial("test_sm3", "my inst", "2000-01-01T00:00", "room 404");
});
});
61 changes: 61 additions & 0 deletions webapp/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,64 @@ Cypress.Commands.add("removeAllTestSamples", (item_ids, check_sample_table) => {
});
}
});

Cypress.Commands.add(
"createStartingMaterial",
(item_id, name = null, date = null, generate_id_automatically = false) => {
cy.findByText("Add a starting material").click();
cy.get('[data-testid="create-item-form"]').within(() => {
cy.findByText("Add new item").should("exist");
cy.findByLabelText("ID:").type(item_id);
if (name) {
cy.findByLabelText("Name:").type(name);
}
if (date) {
cy.findByLabelText("Date Created:").type(date);
}
if (generate_id_automatically) {
cy.findByLabelText("generate automatically").click();
}
cy.findByText("Submit").click();
});
},
);

Cypress.Commands.add("verifyStartingMaterial", (item_id, name = null, date = null) => {
cy.get("[data-testid=starting-material-table]")
.contains(item_id)
.parents("tr")
.within(() => {
if (date) {
cy.contains(date.slice(0, 8));
} else {
cy.contains(TODAY.split("T")[0]);
}
if (name) {
cy.contains(name);
}
});
});

Cypress.Commands.add("deleteStartingMaterial", (items_id) => {
cy.log("search for and delete: " + items_id);
items_id.forEach((item_id) => {
cy.get("[data-testid=starting-material-table]")
.contains(new RegExp("^" + item_id + "$", "g"))
.parents("tr")
.find("input[type='checkbox']")
.click();
});

cy.get("[data-testid=delete-selected-button]").click();

cy.on("window:confirm", (text) => {
expect(text).to.contains(items_id);
return true;
});

items_id.forEach((item_id) => {
cy.get("[data-testid=starting-material-table]")
.contains(new RegExp("^" + item_id + "$", "g"))
.should("not.exist");
});
});
2 changes: 1 addition & 1 deletion webapp/src/components/StartingMaterialInformation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
</div>

<label class="mr-2">Description</label>
<TinyMceInline v-model="ItemDescription"></TinyMceInline>
<TinyMceInline id="item-description" v-model="ItemDescription"></TinyMceInline>

<TableOfContents
class="mb-3"
Expand Down

0 comments on commit fcd512b

Please sign in to comment.