import { fireEvent,render,screen,waitFor } from '@testing-library/react'; import { toast } from 'react-hot-toast'; import { MemoryRouter } from 'react-router-dom'; import { beforeEach,describe,expect,it,vi } from 'vitest'; import api from '../services/api'; import Coupons from './Coupons'; vi.mock('../services/api', () => ({ default: { get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), }, })); vi.mock('react-hot-toast', () => ({ toast: { success: vi.fn(), error: vi.fn(), }, })); describe('Coupons Page Component', () => { const mockCoupons = [ { id: 'cpn-1', code: 'WELCOME20', type: 'percent', value: 20, minCartValue: 200000, maxCartValue: 50000, maxUses: 100, usedCount: 25, expiresAt: '2026-12-31T23:59:59Z', isActive: true, targets: [ { targetType: 'ROLE', targetId: 'User_PetOwner', modifierType: 'override', modifierValue: 25, }, ], }, { id: 'cpn-2', code: 'FLAT50K', type: 'fixed', value: 50000, minCartValue: 500000, maxCartValue: null, maxUses: 50, usedCount: 50, expiresAt: '2026-06-30T23:59:59Z', isActive: false, targets: [], }, ]; beforeEach(() => { vi.clearAllMocks(); vi.mocked(api.get).mockResolvedValue({ data: { success: true, data: mockCoupons, meta: { lastPage: 1, total: 2 }, }, }); }); const renderCoupons = (initialEntries = ['/coupons']) => { return render( ); }; it('renders coupons table, headers, codes, and target badges', async () => { renderCoupons(); expect(screen.getByText(/تخفیف‌های پلیمورفیک/i)).toBeInTheDocument(); expect(screen.getByRole('button', { name: /کد تخفیف جدید/i })).toBeInTheDocument(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); expect(screen.getByText('20٪')).toBeInTheDocument(); expect(screen.getByText('1 هدف')).toBeInTheDocument(); expect(screen.getByText('FLAT50K')).toBeInTheDocument(); expect(screen.getByText(/50,000|۵۰,۰۰۰/)).toBeInTheDocument(); expect(screen.getByText('0 هدف')).toBeInTheDocument(); }); }); it('filters coupons by search term and requests API with debounce', async () => { renderCoupons(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); }); const searchInput = screen.getByPlaceholderText('جستجوی کد...'); fireEvent.change(searchInput, { target: { value: 'WELCOME' } }); await waitFor(() => { expect(api.get).toHaveBeenCalledWith('/admin/coupons', { params: expect.objectContaining({ search: 'WELCOME', page: 1, limit: 10, }), }); }, { timeout: 2000 }); }); it('toggles coupon active status via status button', async () => { vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true }, }); renderCoupons(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); }); const activeButtons = screen.getAllByRole('button', { name: /فعال/i }); expect(activeButtons.length).toBeGreaterThan(0); // Toggle active coupon to inactive fireEvent.click(activeButtons[0]); await waitFor(() => { expect(api.put).toHaveBeenCalledWith('/admin/coupons/cpn-1/toggle', { isActive: false, }); }); }); it('opens modal and creates a new percentage coupon', async () => { vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true }, }); renderCoupons(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); }); const createBtn = screen.getByRole('button', { name: /کد تخفیف جدید/i }); fireEvent.click(createBtn); expect(screen.getByText('ساخت تخفیف جدید')).toBeInTheDocument(); const labelCode = screen.getByText(/کد تخفیف \*/); const codeInput = labelCode.closest('.space-y-2')?.querySelector('input'); expect(codeInput).toBeTruthy(); if (codeInput) { fireEvent.change(codeInput, { target: { value: 'SUMMER30' } }); } const percentInput = screen.getByPlaceholderText('مثال: ۲۰'); fireEvent.change(percentInput, { target: { value: '30' } }); const submitBtn = screen.getByRole('button', { name: 'ثبت تخفیف' }); fireEvent.click(submitBtn); await waitFor(() => { expect(api.post).toHaveBeenCalledWith('/admin/coupons', expect.objectContaining({ code: 'SUMMER30', type: 'percent', value: 30, isActive: true, })); expect(toast.success).toHaveBeenCalledWith('کد تخفیف با موفقیت ایجاد شد'); }); }); it('opens modal in edit mode and updates existing coupon', async () => { vi.mocked(api.put).mockResolvedValueOnce({ data: { success: true }, }); renderCoupons(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); }); const firstRow = screen.getByText('WELCOME20').closest('tr'); const actionButtons = firstRow?.querySelectorAll('button'); // Button order in table row: toggle status (0), edit (1), trash (2) expect(actionButtons && actionButtons.length >= 3).toBe(true); if (actionButtons) { fireEvent.click(actionButtons[1]); } await waitFor(() => { expect(screen.getByText('ویرایش تخفیف')).toBeInTheDocument(); }); const labelCode = screen.getByText(/کد تخفیف \*/); const codeInput = labelCode.closest('.space-y-2')?.querySelector('input') as HTMLInputElement; expect(codeInput).toBeTruthy(); expect(codeInput.value).toBe('WELCOME20'); fireEvent.change(codeInput, { target: { value: 'WELCOME25' } }); const submitBtn = screen.getByRole('button', { name: 'ثبت تخفیف' }); fireEvent.click(submitBtn); await waitFor(() => { expect(api.put).toHaveBeenCalledWith('/admin/coupons/cpn-1', expect.objectContaining({ code: 'WELCOME25', type: 'percent', })); expect(toast.success).toHaveBeenCalledWith('کد تخفیف با موفقیت ویرایش شد'); }); }); it('opens confirmation modal and deletes coupon', async () => { vi.mocked(api.delete).mockResolvedValueOnce({ data: { success: true }, }); renderCoupons(); await waitFor(() => { expect(screen.getByText('WELCOME20')).toBeInTheDocument(); }); const firstRow = screen.getByText('WELCOME20').closest('tr'); const actionButtons = firstRow?.querySelectorAll('button'); expect(actionButtons && actionButtons.length >= 3).toBe(true); if (actionButtons) { fireEvent.click(actionButtons[2]); // delete button } await waitFor(() => { expect(screen.getByText('حذف کد تخفیف')).toBeInTheDocument(); expect(screen.getByText(/آیا از حذف این کد تخفیف مطمئن هستید؟/i)).toBeInTheDocument(); }); const confirmBtn = screen.getByRole('button', { name: 'تایید' }); fireEvent.click(confirmBtn); await waitFor(() => { expect(api.delete).toHaveBeenCalledWith('/admin/coupons/cpn-1'); expect(toast.success).toHaveBeenCalledWith('کد تخفیف با موفقیت حذف شد'); }); }); it('renders empty table message when no coupons found', async () => { vi.mocked(api.get).mockResolvedValueOnce({ data: { success: true, data: [], meta: { lastPage: 1 }, }, }); renderCoupons(); await waitFor(() => { expect(screen.getByText('هیچ کدی یافت نشد')).toBeInTheDocument(); }); }); });