- Implement tests for Pagination, StatusBadge, ConfirmDialog, and MobileInput components. - Add tests for useSubscription, usePaymentConfig, and usePwaInstall hooks. - Create tests for API requests in the api module, including success and error handling. - Add utility function tests for formatting and validating Iranian mobile numbers. - Implement tests for BlogFormPage and BlogsPage to validate form submissions and data fetching. - Add tests for LoginPage to ensure proper validation and state management. - Create tests for authStore and uiStore to validate state management and functionality. - Set up Vitest configuration and testing utilities for consistent testing environment.
42 lines
2.0 KiB
TypeScript
42 lines
2.0 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { renderWithProviders } from '@/test/utils';
|
|
|
|
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
|
|
vi.mock('@/components/ui/PwaLoginCard', () => ({ default: () => null }));
|
|
|
|
import { toast } from 'sonner';
|
|
import LoginPage from '@/pages/LoginPage';
|
|
|
|
const errorToast = toast.error as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
vi.stubGlobal('fetch', vi.fn());
|
|
});
|
|
|
|
describe('LoginPage — حالت رمز عبور', () => {
|
|
it('submit با فیلد خالی → خطای اعتبارسنجی و بدون fetch', async () => {
|
|
renderWithProviders(<LoginPage />, { route: '/admin/login' });
|
|
await userEvent.click(screen.getByRole('button', { name: 'ورود به سیستم' }));
|
|
expect(errorToast).toHaveBeenCalledWith('شماره موبایل و رمز عبور الزامی است');
|
|
expect(fetch).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('LoginPage — تعویض حالت', () => {
|
|
it('کلیک «ورود با پیامک» فرم پیامک را نشان میدهد', async () => {
|
|
renderWithProviders(<LoginPage />, { route: '/admin/login' });
|
|
await userEvent.click(screen.getByRole('button', { name: 'ورود با پیامک' }));
|
|
expect(screen.getByRole('button', { name: 'ارسال کد تأیید' })).toBeInTheDocument();
|
|
});
|
|
|
|
it('شماره موبایل نامعتبر در حالت پیامک → خطا', async () => {
|
|
renderWithProviders(<LoginPage />, { route: '/admin/login' });
|
|
await userEvent.click(screen.getByRole('button', { name: 'ورود با پیامک' }));
|
|
await userEvent.type(screen.getByPlaceholderText('09xxxxxxxxx'), '12345');
|
|
await userEvent.click(screen.getByRole('button', { name: 'ارسال کد تأیید' }));
|
|
expect(errorToast).toHaveBeenCalledWith('شماره موبایل معتبر نیست');
|
|
});
|
|
});
|