Skip to content

Commit

Permalink
Add cy Test for FormattedItemName
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminCharmes committed Sep 5, 2024
1 parent 715e6a5 commit 6aab27b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 73 deletions.
115 changes: 115 additions & 0 deletions webapp/cypress/component/FormattedItemNameTest.cy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import FormattedItemName from "@/components/FormattedItemName.vue";
import { itemTypes } from "@/resources.js";

function hexToRgb(hex) {
const bigint = parseInt(hex.replace("#", ""), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return `rgb(${r}, ${g}, ${b})`;
}

describe("FormattedItemName.vue", () => {
const item_id = "Test";
const itemType = ["samples", "cells", "starting_materials", "equipment"];

it("should display item details and apply correct color based on item type", () => {
itemType.forEach((type) => {
cy.mount(FormattedItemName, {
props: {
item_id,
itemType: type,
},
}).then(() => {
cy.contains(item_id).should("exist");

const expectedColorHex = itemTypes[type]?.lightColor || "LightGrey";
const expectedColorRgb = hexToRgb(expectedColorHex);

cy.get(".formatted-item-name").should("have.css", "background-color", expectedColorRgb);
});
});
});

it("should truncate the name when it exceeds maxLength", () => {
const longName = "This is a very long name for testing";
const maxLength = 10;

cy.mount(FormattedItemName, {
props: {
item_id: "Test",
itemType: "samples",
name: longName,
maxLength: maxLength,
},
});

cy.contains("This is a ...").should("exist");
});

it("should display full name when it's shorter than maxLength", () => {
const shortName = "Short name";

cy.mount(FormattedItemName, {
props: {
item_id: "Test",
itemType: "samples",
name: shortName,
},
});

cy.contains(shortName).should("exist");
});

it("should emit 'itemIdClicked' event when item_id is clicked", () => {
const item_id = "Test";

cy.window().then((win) => {
cy.spy(win, "open").as("windowOpen");
});

cy.mount(FormattedItemName, {
props: {
item_id,
itemType: "samples",
enableClick: true,
},
});

cy.get(".formatted-item-name").click();

cy.get("@windowOpen").should("have.been.calledWith", `/edit/${item_id}`, "_blank");
});

it("should emit 'itemIdClicked' event when clicked with Ctrl or Meta key", () => {
const item_id = "Test";

cy.window().then((win) => {
cy.spy(win, "open").as("windowOpen");
});

cy.mount(FormattedItemName, {
props: {
item_id,
itemType: "samples",
enableModifiedClick: true,
},
});

cy.get(".formatted-item-name").click({ ctrlKey: true });

cy.get("@windowOpen").should("have.been.calledWith", `/edit/${item_id}`, "_blank");
});

it("should display chemical formula when chemform is provided", () => {
cy.mount(FormattedItemName, {
props: {
item_id: "Test",
itemType: "samples",
chemform: "H2O",
},
});

cy.contains("[ H2O ]").should("exist");
});
});
2 changes: 1 addition & 1 deletion webapp/cypress/component/StyledBlockHelpTest.cy.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StyledBlockHelp from "@/components/StyledBlockHelp.vue";

describe("StyledBlockHelp Component", () => {
describe("StyledBlockHelp", () => {
const blockInfo = {
name: "Sample Block",
description: "This is a sample block description.",
Expand Down
2 changes: 1 addition & 1 deletion webapp/cypress/component/StyledBlockInfoTest.cy.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import StyledBlockInfo from "@/components/StyledBlockInfo.vue";

describe("StyledBlockInfo Component", () => {
describe("StyledBlockInfo", () => {
const blockInfo = {
attributes: {
name: "Example Block",
Expand Down
71 changes: 0 additions & 71 deletions webapp/src/components/AdminNavbar.vue

This file was deleted.

0 comments on commit 6aab27b

Please sign in to comment.