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

332 lines
12 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.

describe("Discount Codes Management", () => {
beforeEach(() => {
cy.login();
cy.visit("/discount-codes");
cy.waitForLoading();
});
it("should display discount codes list page", () => {
cy.contains("مدیریت کدهای تخفیف").should("be.visible");
cy.contains("ایجاد و مدیریت کدهای تخفیف").should("be.visible");
cy.get('[title="کد تخفیف جدید"]').should("be.visible");
});
it("should navigate to create discount code page", () => {
cy.get('[title="کد تخفیف جدید"]').click();
cy.url().should("include", "/discount-codes/create");
cy.contains("ایجاد کد تخفیف").should("be.visible");
cy.contains("ایجاد و مدیریت کدهای تخفیف برای فروشگاه").should("be.visible");
});
it("should create a percentage discount code", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Fill basic information
cy.get('input[name="code"]').type("SAVE20");
cy.get('input[name="name"]').type("تخفیف ۲۰ درصدی");
cy.get('textarea[name="description"]').type("تخفیف ۲۰ درصدی برای کل خرید");
// Set discount settings
cy.get('select[name="type"]').select("percentage");
cy.get('input[name="value"]').type("20");
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
// Set limits
cy.get('input[name="min_purchase_amount"]').type("100000");
cy.get('input[name="max_discount_amount"]').type("50000");
cy.get('input[name="usage_limit"]').type("1000");
cy.get('input[name="user_usage_limit"]').type("1");
// Set date range
cy.get('input[name="valid_from"]').type("2024-01-01T00:00");
cy.get('input[name="valid_to"]').type("2024-12-31T23:59");
// Set user restrictions
cy.get('select[name="user_restrictions.user_group"]').select("loyal");
// Set meta information
cy.get('input[name="meta.campaign"]').type("summer_sale");
cy.get('input[name="meta.category"]').type("general");
// Submit form
cy.get('button[type="submit"]').click();
// Verify creation
cy.url().should("include", "/discount-codes");
cy.contains("کد تخفیف با موفقیت ایجاد شد").should("be.visible");
cy.contains("SAVE20").should("be.visible");
});
it("should create a fixed amount discount code", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Fill basic information
cy.get('input[name="code"]').type("FIXED50000");
cy.get('input[name="name"]').type("تخفیف ۵۰ هزار تومانی");
cy.get('textarea[name="description"]').type(
"تخفیف مبلغ ثابت ۵۰ هزار تومان"
);
// Set discount settings
cy.get('select[name="type"]').select("fixed");
cy.get('input[name="value"]').type("50000");
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
// Set single use
cy.get('input[name="single_use"]').check();
// Set user restrictions for new users only
cy.get('input[name="user_restrictions.new_users_only"]').check();
// Submit form
cy.get('button[type="submit"]').click();
// Verify creation
cy.url().should("include", "/discount-codes");
cy.contains("کد تخفیف با موفقیت ایجاد شد").should("be.visible");
cy.contains("FIXED50000").should("be.visible");
});
it("should create a fee percentage discount code", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Fill basic information
cy.get('input[name="code"]').type("FEEREDUCTION10");
cy.get('input[name="name"]').type("کاهش کارمزد ۱۰ درصدی");
// Set discount settings
cy.get('select[name="type"]').select("fee_percentage");
cy.get('input[name="value"]').type("10");
cy.get('select[name="application_level"]').select("product_fee");
// Submit form
cy.get('button[type="submit"]').click();
// Verify creation
cy.url().should("include", "/discount-codes");
cy.contains("کد تخفیف با موفقیت ایجاد شد").should("be.visible");
cy.contains("FEEREDUCTION10").should("be.visible");
});
it("should validate required fields", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Try to submit without required fields
cy.get('button[type="submit"]').should("be.disabled");
// Fill only code field
cy.get('input[name="code"]').type("TEST");
cy.get('button[type="submit"]').should("be.disabled");
// Fill name field
cy.get('input[name="name"]').type("Test");
cy.get('button[type="submit"]').should("be.disabled");
// Fill all required fields
cy.get('select[name="type"]').select("percentage");
cy.get('input[name="value"]').type("10");
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
// Now submit button should be enabled
cy.get('button[type="submit"]').should("not.be.disabled");
});
it("should validate code length", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Test code too short
cy.get('input[name="code"]').type("AB");
cy.get('input[name="name"]').type("Test");
cy.get('select[name="type"]').select("percentage");
cy.get('input[name="value"]').type("10");
cy.contains("کد باید حداقل ۳ کاراکتر باشد").should("be.visible");
// Clear and test code too long
cy.get('input[name="code"]').clear().type("A".repeat(51));
cy.contains("کد نباید بیشتر از ۵۰ کاراکتر باشد").should("be.visible");
});
it("should validate percentage value range", () => {
cy.get('[title="کد تخفیف جدید"]').click();
cy.get('input[name="code"]').type("TESTPERCENTAGE");
cy.get('input[name="name"]').type("Test Percentage");
cy.get('select[name="type"]').select("percentage");
// Test value too high for percentage (should warn in UI for >100)
cy.get('input[name="value"]').type("150");
// The form should still accept it but backend will validate
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
});
it("should search and filter discount codes", () => {
// Assuming we have some discount codes in the list
cy.get('input[placeholder="جستجو بر اساس کد..."]').type("SAVE");
cy.wait(500); // Wait for search to filter
// Test status filter
cy.get("select").contains("همه وضعیت‌ها").parent().select("active");
cy.wait(500);
// Clear filters
cy.contains("پاک کردن فیلترها").click();
cy.get('input[placeholder="جستجو بر اساس کد..."]').should("have.value", "");
});
it("should edit existing discount code", () => {
// Assuming we have discount codes in the list
cy.get("table tbody tr")
.first()
.within(() => {
cy.get('[title="ویرایش"]').click();
});
cy.url().should("include", "/discount-codes/");
cy.url().should("include", "/edit");
cy.contains("ویرایش کد تخفیف").should("be.visible");
// Modify the discount code
cy.get('input[name="name"]').clear().type("کد تخفیف ویرایش شده");
cy.get('textarea[name="description"]').clear().type("توضیحات ویرایش شده");
// Submit changes
cy.get('button[type="submit"]').click();
// Verify update
cy.url().should("include", "/discount-codes");
cy.contains("کد تخفیف با موفقیت به‌روزرسانی شد").should("be.visible");
cy.contains("کد تخفیف ویرایش شده").should("be.visible");
});
it("should delete discount code", () => {
// Create a test discount code first
cy.get('[title="کد تخفیف جدید"]').click();
cy.get('input[name="code"]').type("TESTDELETE");
cy.get('input[name="name"]').type("Test Delete");
cy.get('select[name="type"]').select("percentage");
cy.get('input[name="value"]').type("10");
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
cy.get('button[type="submit"]').click();
// Wait for creation
cy.contains("کد تخفیف با موفقیت ایجاد شد").should("be.visible");
// Find and delete the test discount code
cy.contains("TESTDELETE")
.parent()
.parent()
.within(() => {
cy.get('[title="حذف"]').click();
});
// Confirm deletion in modal
cy.contains("آیا از حذف این کد تخفیف اطمینان دارید؟").should("be.visible");
cy.contains("button", "حذف").click();
// Verify deletion
cy.contains("کد تخفیف با موفقیت حذف شد").should("be.visible");
cy.contains("TESTDELETE").should("not.exist");
});
it("should display proper status badges", () => {
// Check if status badges are displayed with correct colors
cy.get("table tbody tr").each(($row) => {
cy.wrap($row).within(() => {
cy.get("span")
.contains(/فعال|غیرفعال/)
.should("exist");
});
});
});
it("should show empty state when no discount codes exist", () => {
// This test assumes a clean state or uses a filter that returns no results
cy.get('input[placeholder="جستجو بر اساس کد..."]').type("NONEXISTENTCODE");
cy.wait(500);
cy.contains("هیچ کد تخفیفی یافت نشد").should("be.visible");
cy.contains("برای شروع یک کد تخفیف ایجاد کنید").should("be.visible");
});
it("should cancel discount code creation", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Fill some data
cy.get('input[name="code"]').type("CANCELTEST");
cy.get('input[name="name"]').type("Cancel Test");
// Click cancel
cy.contains("button", "انصراف").click();
// Should return to list page
cy.url().should("include", "/discount-codes");
cy.url().should("not.include", "/create");
});
it("should handle user restrictions properly", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Fill basic fields
cy.get('input[name="code"]').type("USERRESTRICT");
cy.get('input[name="name"]').type("User Restriction Test");
cy.get('select[name="type"]').select("percentage");
cy.get('input[name="value"]').type("15");
cy.get('select[name="status"]').select("active");
cy.get('select[name="application_level"]').select("invoice");
// Set user restrictions
cy.get('select[name="user_restrictions.user_group"]').select("new");
cy.get('input[name="user_restrictions.min_purchase_count"]').type("0");
cy.get('input[name="user_restrictions.max_purchase_count"]').type("5");
cy.get('input[name="user_restrictions.referrer_user_id"]').type("123");
// Check warning about mutually exclusive options
cy.get('input[name="user_restrictions.new_users_only"]').check();
cy.get('input[name="user_restrictions.loyal_users_only"]').check();
// Should show warning
cy.contains(
"new_users_only و loyal_users_only نمی‌توانند همزمان فعال باشند"
).should("be.visible");
cy.get('button[type="submit"]').click();
cy.url().should("include", "/discount-codes");
cy.contains("کد تخفیف با موفقیت ایجاد شد").should("be.visible");
});
it("should validate form sections are properly organized", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Verify all sections exist
cy.contains("اطلاعات اصلی کد تخفیف").should("be.visible");
cy.contains("تنظیمات تخفیف").should("be.visible");
cy.contains("بازه زمانی اعتبار").should("be.visible");
cy.contains("محدودیت‌های کاربری").should("be.visible");
cy.contains("اطلاعات تکمیلی").should("be.visible");
// Verify form has proper styling
cy.get(".bg-gradient-to-r").should("have.length.greaterThan", 3);
cy.get(".rounded-xl").should("have.length.greaterThan", 3);
});
it("should handle back navigation properly", () => {
cy.get('[title="کد تخفیف جدید"]').click();
// Click back button
cy.contains("بازگشت").click();
// Should return to list page
cy.url().should("include", "/discount-codes");
cy.url().should("not.include", "/create");
cy.contains("مدیریت کدهای تخفیف").should("be.visible");
});
});