- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
328 lines
10 KiB
TypeScript
328 lines
10 KiB
TypeScript
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 Transactions from './Transactions';
|
|
|
|
vi.mock('../services/api', () => ({
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock('react-hot-toast', () => ({
|
|
toast: {
|
|
success: vi.fn(),
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('Transactions Page Component', () => {
|
|
const mockTransactions = [
|
|
{
|
|
id: 'tx-1',
|
|
userId: 'usr-1',
|
|
orderId: 'ord-12345678',
|
|
amount: 1500000,
|
|
amountRials: 15000000,
|
|
gateway: 'zibal',
|
|
trackId: '123456789',
|
|
refNumber: '987654321',
|
|
cardNumber: '603799******1234',
|
|
status: 'VERIFIED',
|
|
type: 'ORDER',
|
|
createdAt: '2026-04-10T12:00:00Z',
|
|
user: {
|
|
id: 'usr-1',
|
|
firstName: 'رضا',
|
|
lastName: 'محمدی',
|
|
mobile: '09123456789',
|
|
},
|
|
order: {
|
|
id: 'ord-12345678',
|
|
orderNumber: 'CN-1001',
|
|
trackingNumber: 'CANINA-ORD-1001',
|
|
totalAmount: 1500000,
|
|
status: 'PROCESSING',
|
|
},
|
|
},
|
|
{
|
|
id: 'tx-2',
|
|
userId: 'usr-2',
|
|
orderId: null,
|
|
amount: 500000,
|
|
amountRials: 5000000,
|
|
gateway: 'wallet',
|
|
trackId: '555666777',
|
|
refNumber: null,
|
|
cardNumber: null,
|
|
status: 'PENDING',
|
|
type: 'WALLET_TOPUP',
|
|
createdAt: '2026-04-11T14:30:00Z',
|
|
user: {
|
|
id: 'usr-2',
|
|
firstName: 'مهدی',
|
|
lastName: 'کریمی',
|
|
mobile: '09198765432',
|
|
},
|
|
order: null,
|
|
},
|
|
];
|
|
|
|
const mockStats = {
|
|
totalCount: 45,
|
|
verifiedCount: 38,
|
|
pendingCount: 4,
|
|
failedCount: 3,
|
|
totalVolume: 85000000,
|
|
todayVolume: 12000000,
|
|
successRate: 84.4,
|
|
};
|
|
|
|
const mockHealth = {
|
|
gatewayName: 'زیبال (Zibal Payment Gateway)',
|
|
status: 'ONLINE',
|
|
latencyMs: 72,
|
|
checkedAt: '2026-04-11T14:40:00Z',
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(api.get).mockImplementation((url: string) => {
|
|
if (url.startsWith('/payment/admin/stats')) {
|
|
return Promise.resolve({ data: mockStats });
|
|
}
|
|
if (url.startsWith('/payment/admin/health')) {
|
|
return Promise.resolve({ data: mockHealth });
|
|
}
|
|
if (url.startsWith('/payment/admin/transactions')) {
|
|
return Promise.resolve({
|
|
data: {
|
|
success: true,
|
|
transactions: mockTransactions,
|
|
totalPages: 1,
|
|
totalCount: 2,
|
|
},
|
|
});
|
|
}
|
|
return Promise.reject(new Error('Unknown url: ' + url));
|
|
});
|
|
});
|
|
|
|
const renderTransactions = (initialEntries = ['/transactions']) => {
|
|
return render(
|
|
<MemoryRouter initialEntries={initialEntries}>
|
|
<Transactions />
|
|
</MemoryRouter>
|
|
);
|
|
};
|
|
|
|
it('renders transactions page with header, health banner, stats, and table', async () => {
|
|
renderTransactions();
|
|
|
|
expect(screen.getByText('لاگها و مدیریت تراکنشهای مالی')).toBeInTheDocument();
|
|
|
|
await waitFor(() => {
|
|
// Gateway health banner
|
|
expect(screen.getByText('زیبال (Zibal Payment Gateway)')).toBeInTheDocument();
|
|
expect(screen.getByText('Latency: 72ms')).toBeInTheDocument();
|
|
|
|
// Stats
|
|
expect(screen.getByText('حجم کل تراکنشهای موفق')).toBeInTheDocument();
|
|
expect(screen.getByText('پرداختهای موفق امروز')).toBeInTheDocument();
|
|
|
|
// Transactions rows
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
expect(screen.getByText('09123456789')).toBeInTheDocument();
|
|
expect(screen.getByText('CANINA-ORD-1001')).toBeInTheDocument();
|
|
expect(screen.getByText('زیبال (Zibal)')).toBeInTheDocument();
|
|
expect(screen.getByText('پرداخت تایید شده')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('مهدی کریمی')).toBeInTheDocument();
|
|
expect(screen.getByText('شارژ کیف پول')).toBeInTheDocument();
|
|
expect(screen.getByText('در انتظار تایید')).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it('filters transactions by search and status select', async () => {
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const searchInput = screen.getByPlaceholderText('جستجو بر اساس نام، موبایل، Track ID، شماره مرجع...');
|
|
fireEvent.change(searchInput, { target: { value: '09123456789' } });
|
|
|
|
await waitFor(() => {
|
|
expect(api.get).toHaveBeenCalledWith(expect.stringContaining('search=09123456789'));
|
|
});
|
|
|
|
const statusSelect = screen.getByDisplayValue('همه وضعیتها');
|
|
fireEvent.change(statusSelect, { target: { value: 'VERIFIED' } });
|
|
|
|
await waitFor(() => {
|
|
expect(api.get).toHaveBeenCalledWith(expect.stringContaining('status=VERIFIED'));
|
|
});
|
|
});
|
|
|
|
it('opens details modal when clicking details eye button and triggers live inquiry', async () => {
|
|
vi.mocked(api.post).mockResolvedValueOnce({
|
|
data: {
|
|
status: 1,
|
|
message: 'موفق',
|
|
paidAt: '2026-04-10T12:00:00Z',
|
|
},
|
|
});
|
|
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const detailBtns = screen.getAllByTitle('مشاهده جزئیات کامل و استعلام زنده');
|
|
fireEvent.click(detailBtns[0]);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/جزئیات تراکنش #123456789/)).toBeInTheDocument();
|
|
});
|
|
|
|
// In modal, there's button for live inquiry ("استعلام مجدد")
|
|
const inquiryBtn = screen.getByRole('button', { name: /استعلام مجدد/i });
|
|
fireEvent.click(inquiryBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith('/payment/admin/inquiry', {
|
|
trackId: '123456789',
|
|
});
|
|
expect(toast.success).toHaveBeenCalledWith('استعلام تراکنش با موفقیت دریافت شد');
|
|
});
|
|
});
|
|
|
|
it('triggers manual reconcile batch action', async () => {
|
|
vi.mocked(api.post).mockResolvedValueOnce({
|
|
data: {
|
|
verified: 2,
|
|
failed: 0,
|
|
},
|
|
});
|
|
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const reconcileBtn = screen.getByRole('button', { name: /انطباق خودکار تراکنشها/i });
|
|
fireEvent.click(reconcileBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith('/payment/admin/reconcile');
|
|
expect(toast.success).toHaveBeenCalledWith('انطباق انجام شد: 2 تایید شده، 0 ناموفق');
|
|
});
|
|
});
|
|
|
|
it('opens refund modal for verified transaction and executes refund to wallet', async () => {
|
|
vi.mocked(api.post).mockResolvedValueOnce({
|
|
data: { success: true },
|
|
});
|
|
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const refundBtn = screen.getByTitle('استرداد وجه تراکنش (کیف پول یا درگاه زیبال)');
|
|
fireEvent.click(refundBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/استرداد وجه تراکنش/)).toBeInTheDocument();
|
|
});
|
|
|
|
// Execute refund button in modal: "تایید و افزایش اعتبار کیف پول"
|
|
const executeBtn = screen.getByRole('button', { name: /تایید و افزایش اعتبار کیف پول/i });
|
|
fireEvent.click(executeBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith('/admin/users/usr-1/wallet-adjust', expect.objectContaining({
|
|
type: 'refund',
|
|
amount: 1500000,
|
|
}));
|
|
expect(toast.success).toHaveBeenCalledWith('مبلغ با موفقیت به کیف پول کاربر عودت داده شد');
|
|
});
|
|
});
|
|
|
|
it('opens digital receipt modal when clicking printer button', async () => {
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const receiptBtns = screen.getAllByTitle('مشاهده رسید دیجیتال تراکنش');
|
|
fireEvent.click(receiptBtns[0]);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/رسید تراکنش 123456789/)).toBeInTheDocument();
|
|
expect(screen.getByText('شماره تراکنش')).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
it('handles manual verification for pending transaction in details modal', async () => {
|
|
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
|
vi.mocked(api.post).mockResolvedValueOnce({
|
|
data: { message: 'تراکنش با موفقیت تایید شد' },
|
|
});
|
|
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('مهدی کریمی')).toBeInTheDocument();
|
|
});
|
|
|
|
const detailBtns = screen.getAllByTitle('مشاهده جزئیات کامل و استعلام زنده');
|
|
// Second transaction is PENDING
|
|
fireEvent.click(detailBtns[1]);
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText(/جزئیات تراکنش #555666777/)).toBeInTheDocument();
|
|
});
|
|
|
|
const verifyBtn = screen.getByRole('button', { name: /تایید دستی/i });
|
|
fireEvent.click(verifyBtn);
|
|
|
|
await waitFor(() => {
|
|
expect(api.post).toHaveBeenCalledWith('/payment/admin/manual-verify/tx-2', {
|
|
adminNote: 'تایید دستی فیش واریزی توسط مدیر سیستم',
|
|
});
|
|
expect(toast.success).toHaveBeenCalledWith('تراکنش با موفقیت تایید شد');
|
|
});
|
|
});
|
|
|
|
it('copies trackId to clipboard when copy button clicked', async () => {
|
|
Object.assign(navigator, {
|
|
clipboard: {
|
|
writeText: vi.fn().mockResolvedValue(undefined),
|
|
},
|
|
});
|
|
|
|
renderTransactions();
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('رضا محمدی')).toBeInTheDocument();
|
|
});
|
|
|
|
const firstRow = screen.getByText('رضا محمدی').closest('tr');
|
|
const copyBtns = firstRow?.querySelectorAll('button');
|
|
const trackCopyBtn = copyBtns?.[0];
|
|
if (trackCopyBtn) {
|
|
fireEvent.click(trackCopyBtn);
|
|
expect(toast.success).toHaveBeenCalledWith(expect.stringContaining('با موفقیت کپی شد'));
|
|
}
|
|
});
|
|
});
|