index.spec.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import {EventsStatsFixture} 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: EventsStatsFixture(),
  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. anomalies={[]}
  33. location={router.location}
  34. organization={organization}
  35. projects={[project]}
  36. query="event.type:error"
  37. timeWindow={1}
  38. aggregate="count()"
  39. dataset={Dataset.ERRORS}
  40. triggers={[]}
  41. environment={null}
  42. comparisonType={AlertRuleComparisonType.COUNT}
  43. resolveThreshold={null}
  44. thresholdType={AlertRuleThresholdType.BELOW}
  45. newAlertOrQuery
  46. onDataLoaded={() => {}}
  47. isQueryValid
  48. showTotalCount
  49. />
  50. );
  51. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  52. expect(await screen.findByTestId('alert-total-events')).toBeInTheDocument();
  53. expect(eventStatsMock).toHaveBeenCalledWith(
  54. expect.anything(),
  55. expect.objectContaining({
  56. query: {
  57. interval: '1m',
  58. project: [2],
  59. query: 'event.type:error',
  60. statsPeriod: '9998m',
  61. yAxis: 'count()',
  62. referrer: 'api.organization-event-stats',
  63. },
  64. })
  65. );
  66. expect(eventCountsMock).toHaveBeenCalledWith(
  67. expect.anything(),
  68. expect.objectContaining({
  69. query: {
  70. project: ['2'],
  71. query: 'event.type:error',
  72. statsPeriod: '9998m',
  73. environment: [],
  74. },
  75. })
  76. );
  77. });
  78. it('does not show & query total count if showTotalCount === false', async () => {
  79. const {organization, project, router} = initializeOrg();
  80. render(
  81. <TriggersChart
  82. api={api}
  83. location={router.location}
  84. organization={organization}
  85. projects={[project]}
  86. query="event.type:error"
  87. timeWindow={1}
  88. aggregate="count()"
  89. dataset={Dataset.ERRORS}
  90. triggers={[]}
  91. environment={null}
  92. comparisonType={AlertRuleComparisonType.COUNT}
  93. resolveThreshold={null}
  94. thresholdType={AlertRuleThresholdType.BELOW}
  95. newAlertOrQuery
  96. onDataLoaded={() => {}}
  97. isQueryValid
  98. />
  99. );
  100. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  101. expect(screen.queryByTestId('alert-total-events')).not.toBeInTheDocument();
  102. expect(eventStatsMock).toHaveBeenCalledWith(
  103. expect.anything(),
  104. expect.objectContaining({
  105. query: {
  106. interval: '1m',
  107. project: [2],
  108. query: 'event.type:error',
  109. statsPeriod: '9998m',
  110. yAxis: 'count()',
  111. referrer: 'api.organization-event-stats',
  112. },
  113. })
  114. );
  115. expect(eventCountsMock).not.toHaveBeenCalledWith(
  116. expect.anything(),
  117. expect.objectContaining({
  118. query: {
  119. project: ['2'],
  120. query: 'event.type:error',
  121. statsPeriod: '9998m',
  122. environment: [],
  123. },
  124. })
  125. );
  126. });
  127. it('queries the errors dataset if dataset is errors', async () => {
  128. const {organization, project, router} = initializeOrg({
  129. organization: {features: ['performance-discover-dataset-selector']},
  130. });
  131. render(
  132. <TriggersChart
  133. api={api}
  134. location={router.location}
  135. organization={organization}
  136. projects={[project]}
  137. query="event.type:error"
  138. timeWindow={1}
  139. aggregate="count()"
  140. dataset={Dataset.ERRORS}
  141. triggers={[]}
  142. environment={null}
  143. comparisonType={AlertRuleComparisonType.COUNT}
  144. resolveThreshold={null}
  145. thresholdType={AlertRuleThresholdType.BELOW}
  146. newAlertOrQuery
  147. onDataLoaded={() => {}}
  148. isQueryValid
  149. showTotalCount
  150. />
  151. );
  152. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  153. expect(await screen.findByTestId('alert-total-events')).toBeInTheDocument();
  154. expect(eventStatsMock).toHaveBeenCalledWith(
  155. expect.anything(),
  156. expect.objectContaining({
  157. query: {
  158. interval: '1m',
  159. project: [2],
  160. query: 'event.type:error',
  161. statsPeriod: '9998m',
  162. yAxis: 'count()',
  163. referrer: 'api.organization-event-stats',
  164. dataset: 'errors',
  165. },
  166. })
  167. );
  168. expect(eventCountsMock).toHaveBeenCalledWith(
  169. expect.anything(),
  170. expect.objectContaining({
  171. query: {
  172. project: ['2'],
  173. query: 'event.type:error',
  174. statsPeriod: '9998m',
  175. environment: [],
  176. dataset: 'errors',
  177. },
  178. })
  179. );
  180. });
  181. it('queries custom metrics using the metricsEnhanced dataset and metrics layer', async () => {
  182. const {organization, project, router} = initializeOrg({
  183. organization: {features: ['custom-metrics']},
  184. });
  185. render(
  186. <TriggersChart
  187. api={api}
  188. location={router.location}
  189. organization={organization}
  190. projects={[project]}
  191. query=""
  192. timeWindow={1}
  193. aggregate="count(d:custom/my_metric@seconds)"
  194. dataset={Dataset.GENERIC_METRICS}
  195. triggers={[]}
  196. environment={null}
  197. comparisonType={AlertRuleComparisonType.COUNT}
  198. resolveThreshold={null}
  199. thresholdType={AlertRuleThresholdType.BELOW}
  200. newAlertOrQuery
  201. onDataLoaded={() => {}}
  202. isQueryValid
  203. showTotalCount
  204. />
  205. );
  206. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  207. expect(await screen.findByTestId('alert-total-events')).toBeInTheDocument();
  208. expect(eventStatsMock).toHaveBeenCalledWith(
  209. expect.anything(),
  210. expect.objectContaining({
  211. query: {
  212. interval: '1m',
  213. project: [2],
  214. query: '',
  215. statsPeriod: '9998m',
  216. yAxis: 'count(d:custom/my_metric@seconds)',
  217. referrer: 'api.organization-event-stats',
  218. forceMetricsLayer: 'true',
  219. dataset: 'metricsEnhanced',
  220. },
  221. })
  222. );
  223. });
  224. it('queries insights metrics using the metricsEnhanced dataset and without the metrics layer', async () => {
  225. const {organization, project, router} = initializeOrg({
  226. organization: {features: ['custom-metrics']},
  227. });
  228. render(
  229. <TriggersChart
  230. api={api}
  231. location={router.location}
  232. organization={organization}
  233. projects={[project]}
  234. query="span.module:db"
  235. timeWindow={1}
  236. aggregate="spm()"
  237. dataset={Dataset.GENERIC_METRICS}
  238. triggers={[]}
  239. environment={null}
  240. comparisonType={AlertRuleComparisonType.COUNT}
  241. resolveThreshold={null}
  242. thresholdType={AlertRuleThresholdType.BELOW}
  243. newAlertOrQuery
  244. onDataLoaded={() => {}}
  245. isQueryValid
  246. showTotalCount
  247. />
  248. );
  249. expect(await screen.findByTestId('area-chart')).toBeInTheDocument();
  250. expect(await screen.findByTestId('alert-total-events')).toBeInTheDocument();
  251. expect(eventStatsMock).toHaveBeenCalledWith(
  252. expect.anything(),
  253. expect.objectContaining({
  254. query: {
  255. interval: '1m',
  256. project: [2],
  257. query: 'span.module:db',
  258. statsPeriod: '9998m',
  259. yAxis: 'spm()',
  260. referrer: 'api.organization-event-stats',
  261. forceMetricsLayer: undefined,
  262. dataset: 'metricsEnhanced',
  263. },
  264. })
  265. );
  266. });
  267. });