- 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.
20 lines
3.6 KiB
TypeScript
20 lines
3.6 KiB
TypeScript
import { fireEvent,render,screen,waitFor } from '@testing-library/react';
|
|
import { toast } from 'react-hot-toast';
|
|
import { beforeEach,describe,expect,it,vi } from 'vitest';
|
|
import api from '../services/api';
|
|
import FinancialSettingsPage from './FinancialSettingsPage';
|
|
vi.mock('../services/api', () => ({ default: { get: vi.fn(), patch: vi.fn(), put: vi.fn(), post: vi.fn() } }));
|
|
vi.mock('react-hot-toast', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
|
|
const submit = () => fireEvent.submit(document.querySelector('form')!);
|
|
describe('FinancialSettingsPage', () => {
|
|
|
|
beforeEach(() => { vi.resetAllMocks(); vi.mocked(api.get).mockImplementation(async (url) => ({ data: url === '/settings/financial' ? { data: { taxPercentage: '9', charityDonationOptions: ['10000', '50000'], walletWithdrawalEnabled: false } } : url === '/admin/settings' ? { success: true, data: { CHARITY_ROUND_STEP: '20000', REFILL_REWARD_PERCENT: '7' } } : { success: true, data: { roundingStep: '1000', roundingMode: 'DOWN', defaultRetailMarginPercent: '20', defaultWholesaleMarginPercent: '10' } } })); vi.mocked(api.patch).mockResolvedValue({ data: {} }); vi.mocked(api.put).mockResolvedValue({ data: { message: 'Pricing saved' } }); });
|
|
const load = async () => { render(<FinancialSettingsPage />); await screen.findByDisplayValue('9'); };
|
|
it('saves numeric financial/pricing values and string admin settings', async () => { await load(); fireEvent.change(screen.getByDisplayValue('9'), { target: { value: '12' } }); submit(); await waitFor(() => expect(toast.success).toHaveBeenCalledWith('Pricing saved')); expect(api.patch).toHaveBeenCalledWith('/settings/financial', expect.objectContaining({ taxPercentage: 12, charityDonationOptions: [10000, 50000] })); expect(api.put).toHaveBeenCalledWith('/admin/settings', expect.objectContaining({ TAX_PERCENTAGE: '12', CHARITY_ROUND_STEP: '20000', REFILL_REWARD_PERCENT: '7' })); expect(api.put).toHaveBeenCalledWith('/admin/pricing/settings', expect.objectContaining({ roundingStep: 1000, roundingMode: 'DOWN', defaultRetailMarginPercent: 20, applyToAllProducts: true })); });
|
|
it('rejects out-of-range VAT through native validation', async () => { await load(); const vat = screen.getByDisplayValue('9') as HTMLInputElement; fireEvent.change(vat, { target: { value: '101' } }); expect(vat.checkValidity()).toBe(false); fireEvent.click(document.querySelector('button[type=submit]')!); expect(api.patch).not.toHaveBeenCalled(); });
|
|
it('adds unique positive donations in sorted order', async () => { await load(); const input = screen.getByPlaceholderText('مبلغ جدید (مثال: ۱۰۰,۰۰۰)'); const add = input.parentElement!.parentElement!.querySelector('button')!; for (const amount of ['0', '10000', '20000']) { fireEvent.change(input, { target: { value: amount } }); fireEvent.click(add); } submit(); await waitFor(() => expect(api.patch).toHaveBeenCalledWith('/settings/financial', expect.objectContaining({ charityDonationOptions: [10000, 20000, 50000] }))); });
|
|
it('reports partial save rejection without false success and can retry', async () => { await load(); vi.mocked(api.patch).mockRejectedValueOnce(new Error('offline')); submit(); await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در بروزرسانی تنظیمات مالی')); expect(toast.success).not.toHaveBeenCalled(); submit(); await waitFor(() => expect(toast.success).toHaveBeenCalled()); });
|
|
it('loads defaults after failure without updating products', async () => { vi.mocked(api.get).mockRejectedValue(new Error('offline')); render(<FinancialSettingsPage />); await screen.findByDisplayValue('10'); expect(api.put).not.toHaveBeenCalled(); });
|
|
|
|
});
|