Upload src/pages/Analytics.test.tsx with huggingface_hub
Browse files- src/pages/Analytics.test.tsx +83 -0
src/pages/Analytics.test.tsx
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
| 2 |
+
import { render, screen, waitFor } from '@testing-library/react';
|
| 3 |
+
import Analytics from './Analytics';
|
| 4 |
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
| 5 |
+
import { apiClient } from '../lib/apiClient';
|
| 6 |
+
|
| 7 |
+
// Mock recharts to avoid rendering complex SVG elements in JSDOM
|
| 8 |
+
vi.mock('recharts', () => ({
|
| 9 |
+
ResponsiveContainer: ({ children }: any) => <div>{children}</div>,
|
| 10 |
+
LineChart: () => <div>LineChart</div>,
|
| 11 |
+
BarChart: () => <div>BarChart</div>,
|
| 12 |
+
Line: () => <div />,
|
| 13 |
+
Bar: () => <div />,
|
| 14 |
+
XAxis: () => <div />,
|
| 15 |
+
YAxis: () => <div />,
|
| 16 |
+
CartesianGrid: () => <div />,
|
| 17 |
+
Tooltip: () => <div />,
|
| 18 |
+
Legend: () => <div />
|
| 19 |
+
}));
|
| 20 |
+
|
| 21 |
+
vi.mock('../lib/apiClient', () => ({
|
| 22 |
+
apiClient: {
|
| 23 |
+
get: vi.fn()
|
| 24 |
+
}
|
| 25 |
+
}));
|
| 26 |
+
|
| 27 |
+
const queryClient = new QueryClient({
|
| 28 |
+
defaultOptions: {
|
| 29 |
+
queries: {
|
| 30 |
+
retry: false,
|
| 31 |
+
},
|
| 32 |
+
},
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
describe('Analytics Page', () => {
|
| 36 |
+
beforeEach(() => {
|
| 37 |
+
vi.clearAllMocks();
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
it('renders loading states initially', () => {
|
| 41 |
+
(apiClient.get as any).mockImplementation(() => new Promise(() => {}));
|
| 42 |
+
|
| 43 |
+
render(
|
| 44 |
+
<QueryClientProvider client={queryClient}>
|
| 45 |
+
<Analytics />
|
| 46 |
+
</QueryClientProvider>
|
| 47 |
+
);
|
| 48 |
+
|
| 49 |
+
expect(screen.getByText(/Outbound Analytics & Intelligence/i)).toBeTruthy();
|
| 50 |
+
});
|
| 51 |
+
|
| 52 |
+
it('renders correctly with empty data', async () => {
|
| 53 |
+
(apiClient.get as any).mockResolvedValue({ data: [] });
|
| 54 |
+
|
| 55 |
+
render(
|
| 56 |
+
<QueryClientProvider client={queryClient}>
|
| 57 |
+
<Analytics />
|
| 58 |
+
</QueryClientProvider>
|
| 59 |
+
);
|
| 60 |
+
|
| 61 |
+
await waitFor(() => {
|
| 62 |
+
// Ensure the chart wrappers rendered
|
| 63 |
+
expect(screen.getByText(/LineChart/i)).toBeTruthy();
|
| 64 |
+
expect(screen.getByText(/BarChart/i)).toBeTruthy();
|
| 65 |
+
});
|
| 66 |
+
});
|
| 67 |
+
|
| 68 |
+
it('handles network error state gracefully', async () => {
|
| 69 |
+
(apiClient.get as any).mockRejectedValue(new Error('Network error'));
|
| 70 |
+
|
| 71 |
+
render(
|
| 72 |
+
<QueryClientProvider client={queryClient}>
|
| 73 |
+
<Analytics />
|
| 74 |
+
</QueryClientProvider>
|
| 75 |
+
);
|
| 76 |
+
|
| 77 |
+
// Should still render headers and fallback data
|
| 78 |
+
await waitFor(() => {
|
| 79 |
+
expect(screen.getByText(/Outbound Analytics & Intelligence/i)).toBeTruthy();
|
| 80 |
+
expect(screen.getByText(/LineChart/i)).toBeTruthy(); // Falls back to static data currently
|
| 81 |
+
});
|
| 82 |
+
});
|
| 83 |
+
});
|