2
0

SupplyChainMap.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React from 'react';
  2. import { render, screen, fireEvent } from '@testing-library/react';
  3. import { describe, it, expect, vi } from 'vitest';
  4. import SupplyChainMap from '@/client/home/pages/SupplyChainDashboards/components/SupplyChainMap';
  5. describe('SupplyChainMap', () => {
  6. it('should render the map with default points and connections', () => {
  7. render(<SupplyChainMap />);
  8. // Check if map container is rendered
  9. const mapContainer = screen.getByRole('generic');
  10. expect(mapContainer).toBeInTheDocument();
  11. // Check if legend is rendered
  12. expect(screen.getByText('基地')).toBeInTheDocument();
  13. expect(screen.getByText('产业链')).toBeInTheDocument();
  14. });
  15. it('should render custom points when provided', () => {
  16. const customPoints = [
  17. { id: 'custom1', type: 'base' as const, position: { x: 100, y: 100 }, name: 'Custom Base' },
  18. { id: 'custom2', type: 'industryChain' as const, position: { x: 200, y: 200 }, name: 'Custom Chain' }
  19. ];
  20. render(<SupplyChainMap points={customPoints} />);
  21. // Check if custom point names are rendered
  22. expect(screen.getByText('Custom Base')).toBeInTheDocument();
  23. expect(screen.getByText('Custom Chain')).toBeInTheDocument();
  24. });
  25. it('should handle point clicks', () => {
  26. const mockOnPointClick = vi.fn();
  27. const customPoints = [
  28. { id: 'test-point', type: 'base' as const, position: { x: 100, y: 100 }, name: 'Test Point' }
  29. ];
  30. render(<SupplyChainMap points={customPoints} onPointClick={mockOnPointClick} />);
  31. // Find and click the point
  32. const point = screen.getByText('Test Point').closest('div[class*="cursor-pointer"]');
  33. expect(point).toBeInTheDocument();
  34. if (point) {
  35. fireEvent.click(point);
  36. expect(mockOnPointClick).toHaveBeenCalledWith(customPoints[0]);
  37. }
  38. });
  39. it('should render with correct positioning', () => {
  40. render(<SupplyChainMap />);
  41. const mapContainer = screen.getByRole('generic');
  42. // Check positioning classes
  43. expect(mapContainer).toHaveClass('absolute');
  44. expect(mapContainer).toHaveClass('contents');
  45. expect(mapContainer).toHaveClass('left-[529.88px]');
  46. });
  47. it('should render map border and legend', () => {
  48. render(<SupplyChainMap />);
  49. // Check if legend containers are rendered
  50. const baseLegend = screen.getByText('基地').closest('div[class*="rounded-[10px]"]');
  51. const chainLegend = screen.getByText('产业链').closest('div[class*="rounded-[10px]"]');
  52. expect(baseLegend).toBeInTheDocument();
  53. expect(chainLegend).toBeInTheDocument();
  54. });
  55. it('should handle empty points array', () => {
  56. render(<SupplyChainMap points={[]} />);
  57. // Should still render with default points
  58. expect(screen.getByText('基地')).toBeInTheDocument();
  59. expect(screen.getByText('产业链')).toBeInTheDocument();
  60. });
  61. it('should handle empty connections array', () => {
  62. render(<SupplyChainMap connections={[]} />);
  63. // Should still render with default connections
  64. expect(screen.getByText('基地')).toBeInTheDocument();
  65. expect(screen.getByText('产业链')).toBeInTheDocument();
  66. });
  67. });