import { render,screen,waitFor } from '@testing-library/react'; import { MemoryRouter,Route,Routes } from 'react-router-dom'; import { beforeEach,describe,expect,it,vi } from 'vitest'; import api from '../services/api'; import { useAdminAuthStore } from '../store/adminAuthStore'; import type { AdminUser } from '../types/admin'; import ProtectedRoute from './ProtectedRoute'; vi.mock('../services/api', () => ({ default: { post: vi.fn(), get: vi.fn(), }, })); describe('ProtectedRoute Guard Component', () => { const superAdminUser: AdminUser = { id: 'user-1', mobile: '09120000000', role: 'SUPER_ADMIN', }; const regularAdminUser: AdminUser = { id: 'user-2', mobile: '09121111111', role: 'ADMIN', }; const customerUser: AdminUser = { id: 'user-3', mobile: '09122222222', role: 'CUSTOMER', }; beforeEach(() => { vi.clearAllMocks(); useAdminAuthStore.getState().clearAuth(); localStorage.clear(); }); const renderWithRouter = () => { return render( Login Page} /> }> Admin Dashboard Content} /> ); }; it('renders protected child content immediately when already authenticated with ADMIN role', async () => { useAdminAuthStore.getState().setAuth('valid-token', regularAdminUser); renderWithRouter(); expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument(); }); it('renders protected child content immediately when authenticated with SUPER_ADMIN role', async () => { useAdminAuthStore.getState().setAuth('valid-token', superAdminUser); renderWithRouter(); expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument(); }); it('redirects to /login if user is authenticated but does not have ADMIN or SUPER_ADMIN role', async () => { useAdminAuthStore.getState().setAuth('valid-token', customerUser); renderWithRouter(); expect(await screen.findByText('Login Page')).toBeInTheDocument(); expect(screen.queryByText('Admin Dashboard Content')).not.toBeInTheDocument(); }); it('redirects to /login when completely unauthenticated (no token, no user)', async () => { renderWithRouter(); expect(await screen.findByText('Login Page')).toBeInTheDocument(); expect(screen.queryByText('Admin Dashboard Content')).not.toBeInTheDocument(); }); it('refreshes token successfully via /auth/refresh when accessToken exists but state is unauthenticated', async () => { useAdminAuthStore.setState({ accessToken: 'stale-token', isAuthenticated: false, adminUser: null, }); vi.mocked(api.post).mockResolvedValueOnce({ data: { success: true, data: { accessToken: 'fresh-token', user: regularAdminUser, }, }, }); renderWithRouter(); expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument(); expect(api.post).toHaveBeenCalledWith('/auth/refresh'); expect(useAdminAuthStore.getState().accessToken).toBe('fresh-token'); expect(useAdminAuthStore.getState().isAuthenticated).toBe(true); }); it('falls back to /users/profile when /auth/refresh fails, and succeeds if profile is valid', async () => { useAdminAuthStore.setState({ accessToken: 'persisted-token', isAuthenticated: false, adminUser: null, }); vi.mocked(api.post).mockRejectedValueOnce(new Error('Refresh failed')); vi.mocked(api.get).mockResolvedValueOnce({ data: { success: true, data: superAdminUser, }, }); renderWithRouter(); expect(await screen.findByText('Admin Dashboard Content')).toBeInTheDocument(); expect(api.get).toHaveBeenCalledWith('/users/profile'); expect(useAdminAuthStore.getState().isAuthenticated).toBe(true); }); it('clears auth and redirects to /login when both refresh and profile validation fail', async () => { useAdminAuthStore.setState({ accessToken: 'invalid-token', isAuthenticated: false, adminUser: null, }); vi.mocked(api.post).mockRejectedValueOnce(new Error('Refresh failed')); vi.mocked(api.get).mockRejectedValueOnce(new Error('Profile failed')); renderWithRouter(); expect(await screen.findByText('Login Page')).toBeInTheDocument(); await waitFor(() => { expect(useAdminAuthStore.getState().isAuthenticated).toBe(false); expect(useAdminAuthStore.getState().accessToken).toBeNull(); }); }); });