chart.spec.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import BaseChart from 'sentry/components/charts/baseChart';
  3. import MarkLine from 'sentry/components/charts/components/markLine';
  4. import type {Series} from 'sentry/types/echarts';
  5. import Chart, {ChartType} from 'sentry/views/insights/common/components/chart';
  6. jest.mock('sentry/components/charts/baseChart', () => {
  7. return jest.fn().mockImplementation(() => <div />);
  8. });
  9. jest.mock('react', () => {
  10. return {
  11. ...jest.requireActual('react'),
  12. useRef: jest.fn(),
  13. };
  14. });
  15. // XXX: Mocking useRef throws an error for AnimatePrecense, so it must be mocked as well
  16. jest.mock('framer-motion', () => {
  17. return {
  18. ...jest.requireActual('framer-motion'),
  19. AnimatePresence: jest.fn().mockImplementation(({children}) => <div>{children}</div>),
  20. };
  21. });
  22. describe('Chart', function () {
  23. test('it shows an error panel if an error prop is supplied', function () {
  24. const parsingError = new Error('Could not parse chart data');
  25. render(
  26. <Chart error={parsingError} data={[]} loading={false} type={ChartType.LINE} />,
  27. // Mocked useRef breaks router
  28. {disableRouterMocks: true}
  29. );
  30. expect(screen.getByTestId('chart-error-panel')).toBeInTheDocument();
  31. });
  32. it('does not propagate mark line to the incomplete series', function () {
  33. const start = Date.now();
  34. const mockedSeries: Series[] = [
  35. {
  36. seriesName: 'series',
  37. data: [
  38. {name: start - 120_000, value: 5},
  39. {name: start - 90_000, value: 4},
  40. {name: start - 60_000, value: 3},
  41. {name: start - 30_000, value: 2},
  42. {name: start, value: 1},
  43. ],
  44. markLine: MarkLine({
  45. data: [
  46. {
  47. type: 'average',
  48. yAxis: 3,
  49. },
  50. ],
  51. }),
  52. },
  53. ];
  54. render(<Chart data={mockedSeries} loading={false} type={ChartType.LINE} />, {
  55. // Mocked useRef breaks router
  56. disableRouterMocks: true,
  57. });
  58. expect(jest.mocked(BaseChart).mock.calls[0]![0].series?.[0]).toHaveProperty(
  59. 'markLine'
  60. );
  61. expect(jest.mocked(BaseChart).mock.calls[0]![0].series?.[1]).not.toHaveProperty(
  62. 'markLine'
  63. );
  64. });
  65. });