- 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.
91 lines
3.6 KiB
TypeScript
91 lines
3.6 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
import { renderHookWithClient } from '@/test/utils';
|
|
|
|
vi.mock('@/lib/api', () => ({
|
|
api: { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), delete: vi.fn() },
|
|
ApiError: class extends Error {},
|
|
}));
|
|
|
|
import { api } from '@/lib/api';
|
|
import { useAuthStore } from '@/stores/authStore';
|
|
import { useSubscription } from '@/hooks/useSubscription';
|
|
import { usePaymentConfig } from '@/hooks/usePaymentConfig';
|
|
import { usePwaInstall } from '@/hooks/usePwaInstall';
|
|
|
|
const get = api.get as ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
get.mockReset();
|
|
useAuthStore.setState({ primaryRole: null });
|
|
});
|
|
|
|
describe('useSubscription', () => {
|
|
it('برای نقش doctor فعال است و hasFeature/maxSecretaries را برمیگرداند', async () => {
|
|
useAuthStore.setState({ primaryRole: 'doctor' });
|
|
get.mockResolvedValue({
|
|
data: {
|
|
subscription: {
|
|
plan: { features: { sms: true, claims: false }, max_secretaries: 3 },
|
|
days_remaining: 5,
|
|
},
|
|
},
|
|
});
|
|
|
|
const { result } = renderHookWithClient(() => useSubscription());
|
|
await waitFor(() => expect(result.current.hasPlan).toBe(true));
|
|
|
|
expect(result.current.hasFeature('sms')).toBe(true);
|
|
expect(result.current.hasFeature('claims')).toBe(false);
|
|
expect(result.current.hasFeature('unknown')).toBe(false);
|
|
expect(result.current.maxSecretaries).toBe(3);
|
|
expect(result.current.isExpiringSoon).toBe(true);
|
|
});
|
|
|
|
it('برای نقش admin غیرفعال است (query اجرا نمیشود)', async () => {
|
|
useAuthStore.setState({ primaryRole: 'admin' });
|
|
const { result } = renderHookWithClient(() => useSubscription());
|
|
expect(result.current.hasPlan).toBe(false);
|
|
expect(get).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('usePaymentConfig', () => {
|
|
it('test_mode را به isTestMode مپ میکند', async () => {
|
|
get.mockResolvedValue({ data: { test_mode: true } });
|
|
const { result } = renderHookWithClient(() => usePaymentConfig());
|
|
await waitFor(() => expect(result.current.isTestMode).toBe(true));
|
|
});
|
|
it('پیشفرض isTestMode=false', () => {
|
|
get.mockResolvedValue({ data: { test_mode: false } });
|
|
const { result } = renderHookWithClient(() => usePaymentConfig());
|
|
expect(result.current.isTestMode).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('usePwaInstall', () => {
|
|
it('رویداد beforeinstallprompt را میگیرد', () => {
|
|
const { result } = renderHook(() => usePwaInstall());
|
|
expect(result.current.promptEvent).toBeNull();
|
|
act(() => {
|
|
window.dispatchEvent(new Event('beforeinstallprompt'));
|
|
});
|
|
expect(result.current.promptEvent).not.toBeNull();
|
|
});
|
|
|
|
it('dismiss در localStorage ذخیره و isDismissed را true میکند', () => {
|
|
const { result } = renderHook(() => usePwaInstall());
|
|
expect(result.current.isDismissed).toBe(false);
|
|
act(() => result.current.dismiss());
|
|
expect(localStorage.getItem('pwa-dismissed')).toBe('1');
|
|
expect(result.current.isDismissed).toBe(true);
|
|
});
|
|
|
|
it('اگر قبلاً dismiss شده باشد، اولیه isDismissed=true', () => {
|
|
localStorage.setItem('pwa-dismissed', '1');
|
|
const { result } = renderHook(() => usePwaInstall());
|
|
expect(result.current.isDismissed).toBe(true);
|
|
});
|
|
});
|