Files
canina/frontend/admin-panel/src/pages/SmsSettingsPage.test.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

23 lines
4.3 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 SmsSettingsPage from './SmsSettingsPage';
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('SmsSettingsPage', () => {
beforeEach(() => { vi.resetAllMocks(); vi.mocked(api.get).mockImplementation(async (url) => ({ data: { data: url === '/settings/sms' ? { enabled: false, username: 'demo-user', otpBodyId: '123', adminPhone: '' } : url.includes('credit') ? { success: true, credit: 0 } : url.includes('logs') ? { logs: [], totalPages: 1 } : [] } })); vi.mocked(api.patch).mockResolvedValue({ data: {} }); vi.mocked(api.post).mockResolvedValue({ data: { success: true, message: 'Mock accepted' } }); });
const load = async (path='/settings/sms') => { render(<MemoryRouter initialEntries={[path]}><SmsSettingsPage /></MemoryRouter>); await screen.findByDisplayValue('demo-user'); };
it('trims config and converts IDs before save without dispatching SMS', async () => { await load(); fireEvent.change(screen.getByDisplayValue('demo-user'), { target: { value: ' edited-user ' } }); submit(); await waitFor(() => expect(api.patch).toHaveBeenCalledWith('/settings/sms', expect.objectContaining({ username: 'edited-user', otpBodyId: 123, enabled: false, rules: [] }))); expect(api.post).not.toHaveBeenCalled(); expect(toast.success).toHaveBeenCalledWith('تنظیمات درگاه پیامک با موفقیت در دیتابیس ذخیره شد'); });
it('retains config after rejected save and permits retry', async () => { await load(); vi.mocked(api.patch).mockRejectedValueOnce(new Error('offline')); submit(); await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در ذخیره‌سازی تنظیمات پیامک')); expect(screen.getByDisplayValue('demo-user')).toBeInTheDocument(); expect(toast.success).not.toHaveBeenCalled(); submit(); await waitFor(() => expect(toast.success).toHaveBeenCalled()); });
it('validates destination before test dispatch', async () => { await load(); const form=screen.getByPlaceholderText('0912...').closest('form')!; fireEvent.submit(form); expect(toast.error).toHaveBeenCalledWith('لطفاً شماره موبایل مقصد برای تست را وارد کنید'); expect(api.post).not.toHaveBeenCalled(); });
it('builds OTP dispatch payload using mocked API only', async () => { await load(); const phone=screen.getByPlaceholderText('0912...'); fireEvent.change(phone, { target: { value: '09120000000' } }); fireEvent.submit(phone.closest('form')!); await waitFor(() => expect(api.post).toHaveBeenCalledWith('/settings/sms/test', { phone: '09120000000', patternId: 123, args: ['123456'] })); await screen.findByText('Mock accepted'); });
it('displays backend dispatch error without false success', async () => { await load(); vi.mocked(api.post).mockRejectedValueOnce({ response: { data: { message: 'Insufficient credit' } } }); const phone=screen.getByPlaceholderText('0912...'); fireEvent.change(phone, { target: { value: '09120000000' } }); fireEvent.submit(phone.closest('form')!); await screen.findByText('Insufficient credit'); expect(toast.error).toHaveBeenCalledWith('Insufficient credit'); expect(toast.success).not.toHaveBeenCalled(); });
it('invalid tab falls back to settings without fetching logs', async () => { await load('/settings/sms?tab=invalid'); expect(api.get).not.toHaveBeenCalledWith('/settings/sms/logs', expect.anything()); });
it('deep-linked logs handles failure independently of settings hydration', async () => { vi.mocked(api.get).mockImplementation(async (url) => { if (url.includes('logs')) throw new Error('offline'); return { data: { data: [] } }; }); render(<MemoryRouter initialEntries={['/settings/sms?tab=logs']}><SmsSettingsPage /></MemoryRouter>); await waitFor(() => expect(toast.error).toHaveBeenCalledWith('خطا در دریافت لاگ‌های پیامک')); expect(api.get).toHaveBeenCalledWith('/settings/sms/logs', { params: { page: 1, limit: 15, sortOrder: 'desc' } }); });
});