2
0

BaseChart.test.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * BaseChart 组件基础测试
  3. */
  4. import React from 'react';
  5. import { render } from '@testing-library/react';
  6. import '@testing-library/jest-dom';
  7. import { BaseChart } from '../../src/components/BaseChart';
  8. declare const describe: any;
  9. declare const it: any;
  10. declare const expect: any;
  11. declare const beforeEach: any;
  12. declare const afterEach: any;
  13. declare const jest: any;
  14. // Mock @tarojs/components
  15. jest.mock('@tarojs/components', () => ({
  16. Canvas: ({ children, ...props }: any) => React.createElement('canvas', props, children)
  17. }));
  18. // Mock uCharts
  19. jest.mock('../../src/lib/charts/index.ts', () => ({
  20. uCharts: jest.fn().mockImplementation(() => ({
  21. showToolTip: jest.fn(),
  22. scrollStart: jest.fn(),
  23. scroll: jest.fn(),
  24. scrollEnd: jest.fn()
  25. }))
  26. }));
  27. describe('BaseChart 组件', () => {
  28. beforeEach(() => {
  29. jest.clearAllMocks();
  30. });
  31. afterEach(() => {
  32. jest.restoreAllMocks();
  33. });
  34. it('应该渲染 Canvas 元素', () => {
  35. const { container } = render(
  36. <BaseChart
  37. canvasId="test-chart"
  38. type="column"
  39. categories={['A', 'B', 'C']}
  40. series={[]}
  41. />
  42. );
  43. const canvas = container.querySelector('canvas');
  44. expect(canvas).toBeInTheDocument();
  45. });
  46. it('应该设置正确的 canvas-id 和 id 属性', () => {
  47. const { container } = render(
  48. <BaseChart
  49. canvasId="test-chart"
  50. type="column"
  51. categories={[]}
  52. series={[]}
  53. />
  54. );
  55. const canvas = container.querySelector('canvas') as HTMLCanvasElement;
  56. expect(canvas).toHaveAttribute('canvas-id', 'test-chart');
  57. expect(canvas).toHaveAttribute('id', 'test-chart');
  58. });
  59. it('应该使用指定的宽高', () => {
  60. const { container } = render(
  61. <BaseChart
  62. canvasId="test-chart"
  63. type="column"
  64. width={600}
  65. height={400}
  66. categories={[]}
  67. series={[]}
  68. />
  69. );
  70. const canvas = container.querySelector('canvas') as HTMLCanvasElement;
  71. expect(canvas?.style.width).toBe('600px');
  72. expect(canvas?.style.height).toBe('400px');
  73. });
  74. it('应该支持触摸事件处理', () => {
  75. const onTouchStart = jest.fn();
  76. const onTouchMove = jest.fn();
  77. const onTouchEnd = jest.fn();
  78. const { container } = render(
  79. <BaseChart
  80. canvasId="test-chart"
  81. type="column"
  82. categories={[]}
  83. series={[]}
  84. onTouchStart={onTouchStart}
  85. onTouchMove={onTouchMove}
  86. onTouchEnd={onTouchEnd}
  87. />
  88. );
  89. const canvas = container.querySelector('canvas');
  90. expect(canvas).toBeInTheDocument();
  91. });
  92. });