feat(tests, components): enhance Cypress commands and UI elements for improved testing and accessibility

- Added new Cypress commands for mocking admin-related API interactions, including list, create, update, delete, and change status functionalities.
- Introduced data-cy attributes across various components (e.g., Breadcrumbs, Forms, Tables) to improve testability and accessibility.
- Updated existing components to support new data-cy attributes for better integration with Cypress tests.
This commit is contained in:
AmirReza Jamali
2026-04-25 13:25:23 +03:30
parent 2a4f3e7e4b
commit eab516c7f3
24 changed files with 814 additions and 23 deletions
+74
View File
@@ -0,0 +1,74 @@
/// <reference types="cypress" />
export {};
Cypress.env("AUTO_LOGIN", false);
const AUTH_COOKIE_KEYS = {
accessToken: "TENDER_PANEL_token",
refreshToken: "TENDER_PANEL_refresh_token",
user: "TENDER_PANEL_user",
} as const;
const seedValidAuthCookies = () => {
cy.setCookie(AUTH_COOKIE_KEYS.accessToken, "valid-e2e-token");
cy.setCookie(AUTH_COOKIE_KEYS.refreshToken, "valid-e2e-refresh-token");
cy.setCookie(
AUTH_COOKIE_KEYS.user,
encodeURIComponent(
JSON.stringify({
id: "507f1f77bcf86cd799439011",
username: "e2e-admin",
email: "e2e-admin@example.com",
role: "admin",
}),
),
);
};
describe("conditional layout auth routing", () => {
after(() => {
Cypress.env("AUTO_LOGIN", true);
});
beforeEach(() => {
cy.clearCookies();
cy.clearLocalStorage();
});
it("shows dashboard page when user has a valid token", () => {
seedValidAuthCookies();
cy.visit("/");
cy.location("pathname").should("eq", "/");
cy.contains("Opportunity Lens").should("be.visible");
cy.contains("Dashboard").should("be.visible");
});
it("redirects unauthenticated user to login page for protected route", () => {
cy.visit("/admins");
cy.location("pathname").should("eq", "/auth/sign-in");
cy.contains("Welcome back").should("be.visible");
cy.get('input[name="username"]').should("be.visible");
});
it("redirects authenticated user away from login page to dashboard", () => {
seedValidAuthCookies();
cy.visit("/auth/sign-in");
cy.location("pathname").should("eq", "/");
cy.contains("Opportunity Lens").should("be.visible");
});
it("redirects to login when token exists but user cookie is invalid", () => {
cy.setCookie(AUTH_COOKIE_KEYS.accessToken, "invalid-session-token");
cy.setCookie(AUTH_COOKIE_KEYS.user, "{invalid-json");
cy.visit("/admins");
cy.location("pathname").should("eq", "/auth/sign-in");
cy.contains("Welcome back").should("be.visible");
});
});
+1
View File
@@ -106,4 +106,5 @@ describe("sign-in page", () => {
.should("exist");
cy.get('button[type="submit"]').should("not.contain", "Sign in");
});
});
@@ -0,0 +1,53 @@
/// <reference types="cypress" />
export {};
Cypress.env("AUTO_LOGIN", false);
describe("sign-in theme persistence", () => {
after(() => {
Cypress.env("AUTO_LOGIN", true);
});
beforeEach(() => {
cy.clearCookies();
cy.clearLocalStorage();
cy.intercept("POST", "**/api/proxy/profile/login*", (req) => {
req.reply({
delay: 2500,
statusCode: 200,
body: {
success: true,
message: "ok",
data: {
access_token: "e2e-access-token",
refresh_token: "e2e-refresh-token",
expires_at: Date.now() + 60_000,
user: {
id: "507f1f77bcf86cd799439011",
username: "tester",
email: "tester@example.com",
role: "admin",
},
},
},
});
}).as("loginRequest");
cy.visit("/auth/sign-in");
cy.location("pathname").should("eq", "/auth/sign-in");
});
it("keeps selected theme after user logs in and lands on dashboard", () => {
cy.contains("button", "Switch to dark mode").click();
cy.get("html").should("have.class", "dark");
cy.get('input[name="username"]').type("tester");
cy.get('input[name="password"]').type("Secret123!");
cy.contains('button[type="submit"]', "Sign in").click();
cy.wait("@loginRequest");
cy.location("pathname").should("eq", "/");
cy.get("html").should("have.class", "dark");
});
});