insightsMetricField.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import InsightsMetricField from 'sentry/views/alerts/rules/metric/insightsMetricField';
  4. describe('InsightsMetricField', () => {
  5. let metaMock;
  6. beforeEach(() => {
  7. metaMock = MockApiClient.addMockResponse({
  8. url: '/organizations/org-slug/metrics/meta/',
  9. body: [
  10. {
  11. type: 'd',
  12. name: 'exclusive_time',
  13. unit: 'millisecond',
  14. mri: 'd:spans/exclusive_time@millisecond',
  15. operations: [
  16. 'avg',
  17. 'count',
  18. 'histogram',
  19. 'max',
  20. 'max_timestamp',
  21. 'min',
  22. 'min_timestamp',
  23. 'p50',
  24. 'p75',
  25. 'p90',
  26. 'p95',
  27. 'p99',
  28. 'sum',
  29. ],
  30. projectIds: [1],
  31. blockingStatus: [],
  32. },
  33. ],
  34. });
  35. });
  36. it('renders', async () => {
  37. const {project} = initializeOrg();
  38. render(
  39. <InsightsMetricField
  40. aggregate={'avg(d:spans/exclusive_time@millisecond)'}
  41. onChange={() => {}}
  42. project={project}
  43. />
  44. );
  45. await waitFor(() => {
  46. expect(metaMock).toHaveBeenCalledWith(
  47. '/organizations/org-slug/metrics/meta/',
  48. expect.objectContaining({
  49. query: {
  50. project: [2],
  51. useCase: ['spans'],
  52. },
  53. })
  54. );
  55. });
  56. screen.getByText('avg');
  57. screen.getByText('span.exclusive_time');
  58. });
  59. it('should call onChange with the new aggregate string when switching aggregates', async () => {
  60. const {project} = initializeOrg();
  61. const onChange = jest.fn();
  62. render(
  63. <InsightsMetricField
  64. aggregate={'avg(d:spans/exclusive_time@millisecond)'}
  65. onChange={onChange}
  66. project={project}
  67. />
  68. );
  69. await userEvent.click(screen.getByText('avg'));
  70. await userEvent.click(await screen.findByText('sum'));
  71. await waitFor(() =>
  72. expect(onChange).toHaveBeenCalledWith('sum(d:spans/exclusive_time@millisecond)', {})
  73. );
  74. });
  75. it('should call onChange using the spm function with no arguments when switching to spm', async () => {
  76. const {project} = initializeOrg();
  77. const onChange = jest.fn();
  78. render(
  79. <InsightsMetricField
  80. aggregate={'avg(d:spans/exclusive_time@millisecond)'}
  81. onChange={onChange}
  82. project={project}
  83. />
  84. );
  85. await userEvent.click(screen.getByText('avg'));
  86. await userEvent.click(await screen.findByText('spm'));
  87. await waitFor(() => expect(onChange).toHaveBeenCalledWith('spm()', {}));
  88. });
  89. it('should call onChange using the http_response_rate function defaulting with argument 3 when switching to http_response_rate', async () => {
  90. const {project} = initializeOrg();
  91. const onChange = jest.fn();
  92. render(
  93. <InsightsMetricField
  94. aggregate={'avg(d:spans/exclusive_time@millisecond)'}
  95. onChange={onChange}
  96. project={project}
  97. />
  98. );
  99. await userEvent.click(screen.getByText('avg'));
  100. await userEvent.click(await screen.findByText('http_response_rate'));
  101. await waitFor(() =>
  102. expect(onChange).toHaveBeenCalledWith('http_response_rate(3)', {})
  103. );
  104. });
  105. });