admin/cypress/e2e/discount-codes-fixed.cy.ts

252 lines
8.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/// <reference types="../support" />
describe("Discount Codes Management - Fixed", () => {
beforeEach(() => {
cy.login();
cy.visit("/discount-codes");
cy.waitForLoading();
});
it("should display discount codes list page", () => {
cy.contains("مدیریت کدهای تخفیف").should("be.visible");
cy.getByTestId("create-discount-button").should("be.visible");
});
it("should navigate to create discount code page", () => {
cy.getByTestId("create-discount-button").click();
cy.url().should("include", "/discount-codes/create");
cy.contains("ایجاد کد تخفیف").should("be.visible");
});
it("should create a basic percentage discount code", () => {
cy.getByTestId("create-discount-button").click();
// Fill basic information using data-testid
cy.getByTestId("discount-code-input").type("SAVE20");
cy.getByTestId("discount-name-input").type("تخفیف ۲۰ درصدی");
cy.getByTestId("discount-description-textarea").type(
"تخفیف ۲۰ درصدی برای کل خرید"
);
// Set discount settings using data-testid
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("20");
// Set other required fields
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select("invoice");
// Submit form
cy.getByTestId("submit-discount-button").click();
// Verify creation (might need to mock API response)
cy.url().should("include", "/discount-codes");
});
it("should validate required fields properly", () => {
cy.getByTestId("create-discount-button").click();
// Submit button should be disabled initially
cy.getByTestId("submit-discount-button").should("be.disabled");
// Fill only code field
cy.getByTestId("discount-code-input").type("TEST");
cy.getByTestId("submit-discount-button").should("be.disabled");
// Fill name field
cy.getByTestId("discount-name-input").type("Test Name");
cy.getByTestId("submit-discount-button").should("be.disabled");
// Fill all required fields
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("10");
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select("invoice");
// Now submit button should be enabled
cy.getByTestId("submit-discount-button").should("not.be.disabled");
});
it("should validate code length constraints", () => {
cy.getByTestId("create-discount-button").click();
// Test code too short
cy.getByTestId("discount-code-input").type("AB");
cy.getByTestId("discount-name-input").type("Test");
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("10");
// Check for validation error
cy.get(".text-red-600").should("contain", "کد باید حداقل ۳ کاراکتر باشد");
// Clear and test code too long
cy.getByTestId("discount-code-input").clear().type("A".repeat(51));
cy.get(".text-red-600").should(
"contain",
"کد نباید بیشتر از ۵۰ کاراکتر باشد"
);
});
it("should create different discount types", () => {
const discountTypes = [
{ type: "percentage", value: "25", level: "invoice" },
{ type: "fixed", value: "50000", level: "invoice" },
{ type: "fee_percentage", value: "5", level: "product_fee" },
];
discountTypes.forEach((discount, index) => {
// Navigate to create page before each iteration
cy.visit("/discount-codes");
cy.waitForLoading();
cy.getByTestId("create-discount-button").click();
cy.getByTestId("discount-code-input").type(
`TEST${index}${discount.type.toUpperCase()}`
);
cy.getByTestId("discount-name-input").type(`Test ${discount.type}`);
cy.getByTestId("discount-type-select").select(discount.type);
cy.getByTestId("discount-value-input").type(discount.value);
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select(
discount.level
);
cy.getByTestId("submit-discount-button").click();
cy.url().should("include", "/discount-codes");
});
});
it("should handle form cancellation", () => {
cy.getByTestId("create-discount-button").click();
// Fill some data
cy.getByTestId("discount-code-input").type("CANCELTEST");
cy.getByTestId("discount-name-input").type("Cancel Test");
// Click cancel button
cy.getByTestId("cancel-discount-button").click();
// Should return to list page
cy.url().should("include", "/discount-codes");
cy.url().should("not.include", "/create");
});
it("should show empty state when no results found", () => {
// Search for non-existent code
cy.get('input[placeholder*="جستجو"]').type("NONEXISTENTCODE123");
cy.wait(500);
// Check for empty state
cy.contains("هیچ کد تخفیفی یافت نشد").should("be.visible");
});
it("should navigate back properly", () => {
cy.getByTestId("create-discount-button").click();
// Wait for form to load completely
cy.getByTestId("discount-code-input").should("be.visible");
// Click cancel button
cy.getByTestId("cancel-discount-button").click();
// Should return to list page
cy.url().should("include", "/discount-codes");
cy.url().should("not.include", "/create");
});
// Test with API mocking
it("should handle API errors gracefully", () => {
// Mock API error
cy.intercept("POST", "**/discount/**", {
statusCode: 400,
body: { message: "کد تخفیف تکراری است" },
}).as("createError");
cy.getByTestId("create-discount-button").click();
cy.getByTestId("discount-code-input").type("ERRORTEST");
cy.getByTestId("discount-name-input").type("Error Test");
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("10");
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select("invoice");
cy.getByTestId("submit-discount-button").click();
cy.wait("@createError");
// Error message should appear
cy.contains("خطا در ایجاد کد تخفیف").should("be.visible");
});
it("should handle loading states", () => {
// Mock slow API response
cy.intercept("POST", "**/discount/**", {
delay: 2000,
statusCode: 201,
body: { id: 1, code: "TEST", name: "Test" },
}).as("createSlow");
cy.getByTestId("create-discount-button").click();
cy.getByTestId("discount-code-input").type("LOADTEST");
cy.getByTestId("discount-name-input").type("Load Test");
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("10");
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select("invoice");
cy.getByTestId("submit-discount-button").click();
// Check loading state
cy.getByTestId("submit-discount-button").should("be.disabled");
cy.wait("@createSlow");
});
// Test mobile responsiveness
it("should work on mobile viewport", () => {
cy.viewport("iphone-6");
cy.getByTestId("create-discount-button").should("be.visible");
cy.getByTestId("create-discount-button").click();
cy.contains("ایجاد کد تخفیف").should("be.visible");
// Form should be usable on mobile
cy.getByTestId("discount-code-input").type("MOBILETEST");
cy.getByTestId("discount-name-input").type("Mobile Test");
cy.getByTestId("discount-type-select").select("percentage");
cy.getByTestId("discount-value-input").type("10");
cy.getByTestId("discount-status-select").select("active");
cy.getByTestId("discount-application-level-select").select("invoice");
// Scroll to submit button to make it visible
cy.getByTestId("submit-discount-button").scrollIntoView();
cy.getByTestId("submit-discount-button").should("be.visible");
});
// Test accessibility
it("should be accessible", () => {
cy.getByTestId("create-discount-button").click();
// Check for proper labels
cy.get("label").should("have.length.greaterThan", 5);
// Check for required field indicators
cy.getByTestId("discount-code-input").should(
"have.attr",
"aria-required",
"true"
);
cy.getByTestId("discount-name-input").should(
"have.attr",
"aria-required",
"true"
);
// Check for proper form structure
cy.get("form").should("exist");
cy.get(".bg-gradient-to-r").should("have.length.greaterThan", 3);
});
});