import React from 'react';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MemoryRouter, Navigate } from 'react-router-dom';
import { AppRouter } from './index';
import { useAuthStore } from '@/features/auth/store/authStore';
// Mock the stores
vi.mock('@/features/auth/store/authStore', () => ({
useAuthStore: vi.fn(),
}));
// Mock PublicRoute: redirect to dashboard if auth, else render children
vi.mock('./PublicRoute', () => ({
PublicRoute: ({ children }: { children: React.ReactNode }) => {
const store = useAuthStore();
const isAuth = (store as any)?.isAuthenticated === true;
const isLoading = (store as any)?.isLoading === true;
if (isLoading) return
Loading...
;
if (isAuth) return ;
return <>{children}>;
},
}));
// Mock ProtectedRoute + ProtectedLayoutRoute: redirect to login if not auth, else render layout + children
vi.mock('@/components/auth/ProtectedRoute', () => ({
ProtectedRoute: ({ children }: { children: React.ReactNode }) => {
const store = useAuthStore();
const isAuth = (store as any)?.isAuthenticated === true;
const isLoading = (store as any)?.isLoading === true;
if (isLoading) return Loading...
;
if (!isAuth) return ;
return <>{children}>;
},
}));
vi.mock('./ProtectedLayoutRoute', () => ({
ProtectedLayoutRoute: ({ children }: { children: React.ReactNode }) => (
{children}
),
}));
// Mock lazy components (all exports used by routeConfig)
vi.mock('@/components/ui/LazyComponent', () => ({
LazyLogin: () => Login Page
,
LazyRegister: () => Register Page
,
LazyForgotPassword: () => Forgot Password Page
,
LazyVerifyEmail: () => Verify Email Page
,
LazyResetPassword: () => Reset Password Page
,
LazyDashboard: () => Dashboard Page
,
LazyChat: () => Chat Page
,
LazyLibrary: () => Library Page
,
LazyProfile: () => Profile Page
,
LazySettings: () => Settings Page
,
LazySessions: () => Sessions Page
,
LazyNotFound: () => 404 Not Found
,
LazyServerError: () => 500 Server Error
,
LazyUserProfile: () => User Profile Page
,
LazyRoles: () => Roles Page
,
LazyTrackDetail: () => Track Detail Page
,
LazyPlaylistRoutes: () => Playlists Page
,
LazyMarketplace: () => Marketplace Page
,
LazySearch: () => Search Page
,
LazyNotifications: () => Notifications Page
,
LazyAnalytics: () => Analytics Page
,
LazyWebhooks: () => Webhooks Page
,
LazyAdminDashboard: () => Admin Page
,
LazyDesignSystemDemo: () => Design System
,
LazySocial: () => Social Page
,
LazyGear: () => Gear Page
,
LazyLive: () => Live Page
,
LazyEducation: () => Education Page
,
LazyQueue: () => Queue Page
,
LazyDeveloper: () => Developer Page
,
LazySellerDashboard: () => Seller Page
,
LazyWishlist: () => Wishlist Page
,
LazyPurchases: () => Purchases Page
,
}));
// Mock DashboardLayout (used by ProtectedLayoutRoute)
vi.mock('@/components/layout/DashboardLayout', () => ({
DashboardLayout: ({ children }: { children: React.ReactNode }) => (
{children}
),
}));
// Mock LoadingSpinner
vi.mock('@/components/ui/loading-spinner', () => ({
LoadingSpinner: () => Loading...
,
}));
describe('AppRouter', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('Public Routes', () => {
it('should render login page for /login route when not authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('Login Page')).toBeInTheDocument();
});
it('should redirect to dashboard when authenticated user tries to access login', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as any);
render(
,
);
// Should redirect to dashboard (mocked as Dashboard Page)
expect(screen.getByText('Dashboard Page')).toBeInTheDocument();
});
it('should render register page for /register route when not authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('Register Page')).toBeInTheDocument();
});
});
describe('Protected Routes', () => {
it('should render dashboard page when authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByTestId('layout')).toBeInTheDocument();
expect(screen.getByText('Dashboard Page')).toBeInTheDocument();
});
it('should redirect to login when not authenticated user tries to access protected route', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
// Should redirect to login
expect(screen.getByText('Login Page')).toBeInTheDocument();
});
it('should show loading spinner when checking authentication', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: true,
} as any);
render(
,
);
expect(screen.getByText('Loading...')).toBeInTheDocument();
});
it('should render chat page when authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByTestId('layout')).toBeInTheDocument();
expect(screen.getByText('Chat Page')).toBeInTheDocument();
});
it('should render library page when authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByTestId('layout')).toBeInTheDocument();
expect(screen.getByText('Library Page')).toBeInTheDocument();
});
});
describe('Error Routes', () => {
it('should render 404 page for /404 route', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('404 Not Found')).toBeInTheDocument();
});
it('should render 500 page for /500 route', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('500 Server Error')).toBeInTheDocument();
});
it('should redirect to 404 for unknown routes', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('404 Not Found')).toBeInTheDocument();
});
});
describe('Default Routes', () => {
it('should redirect root path to dashboard when authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: true,
isLoading: false,
} as any);
render(
,
);
expect(screen.getByText('Dashboard Page')).toBeInTheDocument();
});
it('should redirect root path to login when not authenticated', () => {
vi.mocked(useAuthStore).mockReturnValue({
isAuthenticated: false,
isLoading: false,
} as any);
render(
,
);
// Should redirect to dashboard, which then redirects to login
expect(screen.getByText('Login Page')).toBeInTheDocument();
});
});
});