All files / src/components/features/admin/monitoring/PerformanceDashboard PerformanceDashboard.test.tsx

0% Statements 0/210
100% Branches 0/0
0% Functions 0/1
0% Lines 0/210

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211                                                                                                                                                                                                                                                                                                                                                                                                                                     
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { PerformanceDashboard, type PerformanceData } from './index';

// Mock the usePerformancePolling hook
jest.mock('@/hooks/usePerformancePolling', () => ({
  usePerformancePolling: jest.fn(() => ({
    data: null,
    isLoading: false,
    isRefetching: false,
    lastUpdated: new Date(),
    refetch: jest.fn(),
  })),
  formatLastUpdated: jest.fn(() => 'Just now'),
}));

// Mock recharts
jest.mock('recharts', () => ({
  ResponsiveContainer: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="responsive-container">{children}</div>
  ),
  LineChart: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="line-chart">{children}</div>
  ),
  AreaChart: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="area-chart">{children}</div>
  ),
  BarChart: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="bar-chart">{children}</div>
  ),
  PieChart: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="pie-chart">{children}</div>
  ),
  Line: () => <div data-testid="line" />,
  Area: () => <div data-testid="area" />,
  Bar: ({ children }: { children: React.ReactNode }) => <div data-testid="bar">{children}</div>,
  Pie: ({ children }: { children: React.ReactNode }) => <div data-testid="pie">{children}</div>,
  Cell: () => <div data-testid="cell" />,
  XAxis: () => <div data-testid="x-axis" />,
  YAxis: () => <div data-testid="y-axis" />,
  CartesianGrid: () => <div data-testid="cartesian-grid" />,
  Tooltip: () => <div data-testid="tooltip" />,
  Legend: () => <div data-testid="legend" />,
}));

const mockData: PerformanceData = {
  summary: {
    avgResponseTime: 67.4,
    totalRequests: 15420,
    successRate: 98.5,
    p95ResponseTime: 145.2,
    p99ResponseTime: 287.6,
  },
  timeSeries: [
    {
      timestamp: new Date().toISOString(),
      avgDuration: 50,
      p95Duration: 100,
      p99Duration: 150,
    },
  ],
  volume: [
    {
      timestamp: new Date().toISOString(),
      total: 100,
      successful: 95,
      clientErrors: 3,
      serverErrors: 2,
    },
  ],
  endpoints: [
    { path: '/api/products', count: 4500, percentage: 50 },
    { path: '/api/users', count: 2800, percentage: 30 },
  ],
  statusCodes: [
    { statusCode: 200, count: 9500 },
    { statusCode: 400, count: 30 },
  ],
  slowRequests: [
    {
      id: '1',
      name: 'GET /api/slow',
      duration: 1250,
      timestamp: new Date(),
    },
  ],
};

describe('PerformanceDashboard', () => {
  describe('Rendering', () => {
    it('renders without crashing', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('performance-dashboard')).toBeInTheDocument();
    });

    it('displays time range selector', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('time-range-selector')).toBeInTheDocument();
    });

    it('displays auto-refresh toggle', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('Auto-refresh')).toBeInTheDocument();
    });

    it('displays refresh button', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('Refresh')).toBeInTheDocument();
    });

    it('displays last updated time', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText(/Updated:/)).toBeInTheDocument();
    });
  });

  describe('Summary Cards', () => {
    it('displays total requests', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('Total Requests')).toBeInTheDocument();
      expect(screen.getByText('15.4K')).toBeInTheDocument();
    });

    it('displays average response time', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('Avg Response Time')).toBeInTheDocument();
      expect(screen.getByText('67ms')).toBeInTheDocument();
    });

    it('displays P95 response time', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('P95 Response')).toBeInTheDocument();
      expect(screen.getByText('145ms')).toBeInTheDocument();
    });

    it('displays success rate', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('Success Rate')).toBeInTheDocument();
      expect(screen.getByText('98.5%')).toBeInTheDocument();
    });
  });

  describe('Charts', () => {
    it('renders response time chart', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('response-time-chart')).toBeInTheDocument();
    });

    it('renders request volume chart', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('request-volume-chart')).toBeInTheDocument();
    });

    it('renders endpoint distribution chart', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('endpoint-distribution-chart')).toBeInTheDocument();
    });

    it('renders status code chart', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByTestId('status-code-chart')).toBeInTheDocument();
    });
  });

  describe('Slow Requests', () => {
    it('displays slow requests section', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText(/Slow Requests/)).toBeInTheDocument();
    });

    it('displays slow request details', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      expect(screen.getByText('GET /api/slow')).toBeInTheDocument();
      expect(screen.getByText('1.25s')).toBeInTheDocument();
    });
  });

  describe('Interactions', () => {
    it('toggles auto-refresh when checkbox is clicked', () => {
      render(<PerformanceDashboard initialData={mockData} defaultAutoRefresh={true} />);
      const checkbox = screen.getByRole('checkbox');
      expect(checkbox).toBeChecked();

      fireEvent.click(checkbox);
      expect(checkbox).not.toBeChecked();
    });

    it('changes time range when selector is clicked', () => {
      render(<PerformanceDashboard initialData={mockData} defaultTimeRange="24h" />);
      const oneHourButton = screen.getByText('1 Hour');

      fireEvent.click(oneHourButton);
      expect(oneHourButton).toHaveAttribute('aria-pressed', 'true');
    });
  });

  describe('Default Props', () => {
    it('uses default time range of 24h', () => {
      render(<PerformanceDashboard initialData={mockData} />);
      const button24h = screen.getByText('24 Hours');
      expect(button24h).toHaveAttribute('aria-pressed', 'true');
    });

    it('respects defaultTimeRange prop', () => {
      render(<PerformanceDashboard initialData={mockData} defaultTimeRange="7d" />);
      const button7d = screen.getByText('7 Days');
      expect(button7d).toHaveAttribute('aria-pressed', 'true');
    });
  });
});