| 1 |
import '@testing-library/jest-dom'; |
| 2 |
import { render, screen } from '@testing-library/react'; |
| 3 |
import { Provider } from 'react-redux'; |
| 4 |
import { MemoryRouter, Route, Routes } from 'react-router'; |
| 5 |
import Dashboard from '../../pages/Dashboard'; |
| 6 |
import NotFound from '../../pages/NotFound'; |
| 7 |
import { createTestStore } from '../setup/helpers'; |
| 8 |
|
| 9 |
// Mock Recharts |
| 10 |
jest.mock('recharts', () => ({ |
| 11 |
ResponsiveContainer: ({ children }) => <div>{children}</div>, |
| 12 |
LineChart: ({ children }) => <div>{children}</div>, |
| 13 |
PieChart: ({ children }) => <div>{children}</div>, |
| 14 |
Line: () => null, |
| 15 |
Pie: ({ children }) => <div>{children}</div>, |
| 16 |
Cell: () => null, |
| 17 |
CartesianGrid: () => null, |
| 18 |
XAxis: () => null, |
| 19 |
YAxis: () => null, |
| 20 |
Tooltip: () => null, |
| 21 |
Legend: () => null, |
| 22 |
})); |
| 23 |
|
| 24 |
// Mock RTK Query hooks |
| 25 |
jest.mock('../../features/summary/summaryApi', () => ({ |
| 26 |
useGetSummaryQuery: () => ({ |
| 27 |
data: undefined, |
| 28 |
isLoading: false, |
| 29 |
isFetching: false, |
| 30 |
}), |
| 31 |
useGetRevenueChartQuery: () => ({ |
| 32 |
data: { data: [], interval: 'day' }, |
| 33 |
isLoading: false, |
| 34 |
}), |
| 35 |
useGetIntentsPerformanceQuery: () => ({ |
| 36 |
data: { |
| 37 |
data: [{ intent: 'Product', revenue: 0, orders: 0, percentage: 0 }], |
| 38 |
total_revenue: 0, |
| 39 |
}, |
| 40 |
isLoading: false, |
| 41 |
}), |
| 42 |
})); |
| 43 |
|
| 44 |
jest.mock('../../features/campaigns/campaignsApi', () => ({ |
| 45 |
useGetCampaignsQuery: () => ({ |
| 46 |
data: { |
| 47 |
data: [ |
| 48 |
{ |
| 49 |
campaign_id: 1, |
| 50 |
campaign_name: 'Test', |
| 51 |
intent: 'Product', |
| 52 |
valid_date: {}, |
| 53 |
orders: 0, |
| 54 |
customers: 0, |
| 55 |
revenue: 0, |
| 56 |
status: 'active', |
| 57 |
}, |
| 58 |
], |
| 59 |
}, |
| 60 |
isLoading: false, |
| 61 |
isFetching: false, |
| 62 |
}), |
| 63 |
})); |
| 64 |
|
| 65 |
jest.mock('../../features/products/productsApi', () => ({ |
| 66 |
useGetProductsQuery: () => ({ |
| 67 |
data: { |
| 68 |
data: [ |
| 69 |
{ |
| 70 |
id: 1, |
| 71 |
name: 'TestProd', |
| 72 |
image: '', |
| 73 |
unit_price: 10, |
| 74 |
categories: [], |
| 75 |
campaigns: [], |
| 76 |
total_orders: 0, |
| 77 |
total_customers: 0, |
| 78 |
total_revenue: 0, |
| 79 |
}, |
| 80 |
], |
| 81 |
}, |
| 82 |
isLoading: false, |
| 83 |
isFetching: false, |
| 84 |
}), |
| 85 |
})); |
| 86 |
|
| 87 |
function renderApp(route = '/') { |
| 88 |
const store = createTestStore(); |
| 89 |
return render( |
| 90 |
<Provider store={store}> |
| 91 |
<MemoryRouter initialEntries={[route]}> |
| 92 |
<Routes> |
| 93 |
<Route index element={<Dashboard />} /> |
| 94 |
<Route path="*" element={<NotFound />} /> |
| 95 |
</Routes> |
| 96 |
</MemoryRouter> |
| 97 |
</Provider> |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
describe('App Routing', () => { |
| 102 |
it('renders Dashboard on root route', () => { |
| 103 |
renderApp('/'); |
| 104 |
expect(screen.getByText('Analytics Dashboard')).toBeInTheDocument(); |
| 105 |
// React-Redux may warn about selector memoization |
| 106 |
// expect(console).toHaveWarned(); |
| 107 |
}); |
| 108 |
|
| 109 |
it('renders NotFound for unknown routes', () => { |
| 110 |
renderApp('/unknown-page'); |
| 111 |
expect(screen.getByText('Page not found')).toBeInTheDocument(); |
| 112 |
}); |
| 113 |
|
| 114 |
it('renders NotFound page with back link', () => { |
| 115 |
renderApp('/unknown'); |
| 116 |
const link = screen.getByText('Back to Dashboard'); |
| 117 |
expect(link.closest('a')).toHaveAttribute('href', '/'); |
| 118 |
}); |
| 119 |
}); |
| 120 |
|