index.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {EventsStats} from 'sentry-fixture/events';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen} from 'sentry-test/reactTestingLibrary';
  4. import TriggersChart from 'sentry/views/alerts/rules/metric/triggers/chart';
  5. import {
  6. AlertRuleComparisonType,
  7. AlertRuleThresholdType,
  8. Dataset,
  9. } from 'sentry/views/alerts/rules/metric/types';
  10. describe('Incident Rules Create', () => {
  11. let eventStatsMock: jest.Func;
  12. let eventCountsMock: jest.Func;
  13. beforeEach(() => {
  14. eventStatsMock = MockApiClient.addMockResponse({
  15. url: '/organizations/org-slug/events-stats/',
  16. body: EventsStats(),
  17. });
  18. eventCountsMock = MockApiClient.addMockResponse({
  19. url: '/organizations/org-slug/events-meta/',
  20. body: {count: 5},
  21. });
  22. });
  23. afterEach(() => {
  24. MockApiClient.clearMockResponses();
  25. });
  26. const api = new MockApiClient();
  27. it('renders a metric', async () => {
  28. const {organization, project, router} = initializeOrg();
  29. render(
  30. <TriggersChart
  31. api={api}
  32. location={router.location}
  33. organization={organization}
  34. projects={[project]}
  35. query="event.type:error"
  36. timeWindow={1}
  37. aggregate="count()"
  38. dataset={Dataset.ERRORS}
  39. triggers={[]}
  40. environment={null}
  41. comparisonType={AlertRuleComparisonType.COUNT}
  42. resolveThreshold={null}
  43. thresholdType={AlertRuleThresholdType.BELOW}
  44. newAlertOrQuery
  45. onDataLoaded={() => {}}
  46. isQueryValid
  47. showTotalCount
  48. />
  49. );
  50. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  51. expect(await screen.findByTestId('alert-total-events')).toBeInTheDocument();
  52. expect(eventStatsMock).toHaveBeenCalledWith(
  53. expect.anything(),
  54. expect.objectContaining({
  55. query: {
  56. interval: '1m',
  57. project: [2],
  58. query: 'event.type:error',
  59. statsPeriod: '9999m',
  60. yAxis: 'count()',
  61. referrer: 'api.organization-event-stats',
  62. },
  63. })
  64. );
  65. expect(eventCountsMock).toHaveBeenCalledWith(
  66. expect.anything(),
  67. expect.objectContaining({
  68. query: {
  69. project: ['2'],
  70. query: 'event.type:error',
  71. statsPeriod: '9999m',
  72. environment: [],
  73. },
  74. })
  75. );
  76. });
  77. it('does not show & query total count if showTotalCount === false', async () => {
  78. const {organization, project, router} = initializeOrg();
  79. render(
  80. <TriggersChart
  81. api={api}
  82. location={router.location}
  83. organization={organization}
  84. projects={[project]}
  85. query="event.type:error"
  86. timeWindow={1}
  87. aggregate="count()"
  88. dataset={Dataset.ERRORS}
  89. triggers={[]}
  90. environment={null}
  91. comparisonType={AlertRuleComparisonType.COUNT}
  92. resolveThreshold={null}
  93. thresholdType={AlertRuleThresholdType.BELOW}
  94. newAlertOrQuery
  95. onDataLoaded={() => {}}
  96. isQueryValid
  97. />
  98. );
  99. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  100. expect(screen.queryByTestId('alert-total-events')).not.toBeInTheDocument();
  101. expect(eventStatsMock).toHaveBeenCalledWith(
  102. expect.anything(),
  103. expect.objectContaining({
  104. query: {
  105. interval: '1m',
  106. project: [2],
  107. query: 'event.type:error',
  108. statsPeriod: '9999m',
  109. yAxis: 'count()',
  110. referrer: 'api.organization-event-stats',
  111. },
  112. })
  113. );
  114. expect(eventCountsMock).not.toHaveBeenCalledWith(
  115. expect.anything(),
  116. expect.objectContaining({
  117. query: {
  118. project: ['2'],
  119. query: 'event.type:error',
  120. statsPeriod: '9999m',
  121. environment: [],
  122. },
  123. })
  124. );
  125. });
  126. });