metrics.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {
  3. doMetricsRequest,
  4. fetchMetricsFields,
  5. fetchMetricsTags,
  6. } from 'sentry/actionCreators/metrics';
  7. import MetricsMetaActions from 'sentry/actions/metricsMetaActions';
  8. import MetricsTagActions from 'sentry/actions/metricTagActions';
  9. import {Client} from 'sentry/api';
  10. import MetricsMetaStore from 'sentry/stores/metricsMetaStore';
  11. import MetricsTagStore from 'sentry/stores/metricsTagStore';
  12. import {SessionMetric} from 'sentry/utils/metrics/fields';
  13. describe('Metrics ActionCreator', function () {
  14. const api = new Client();
  15. const orgSlug = TestStubs.Organization().slug;
  16. describe('doMetricsRequest', function () {
  17. const options = {
  18. field: [SessionMetric.SESSION],
  19. orgSlug,
  20. cursor: undefined,
  21. environment: [],
  22. groupBy: ['session.status'],
  23. interval: '1h',
  24. limit: 5,
  25. orderBy: SessionMetric.SESSION,
  26. project: [TestStubs.Project().id],
  27. query: 'release:123',
  28. statsPeriod: '14d',
  29. };
  30. let mock;
  31. beforeEach(function () {
  32. MockApiClient.clearMockResponses();
  33. mock = MockApiClient.addMockResponse({
  34. url: `/organizations/${orgSlug}/metrics/data/`,
  35. body: {
  36. data: [],
  37. },
  38. });
  39. });
  40. it('requests metrics data', function () {
  41. doMetricsRequest(api, options);
  42. expect(mock).toHaveBeenCalledTimes(1);
  43. expect(mock).toHaveBeenLastCalledWith(
  44. `/organizations/${orgSlug}/metrics/data/`,
  45. expect.objectContaining({
  46. query: {
  47. environment: [],
  48. field: ['sentry.sessions.session'],
  49. groupBy: ['session.status'],
  50. interval: '1h',
  51. orderBy: 'sentry.sessions.session',
  52. per_page: 5,
  53. project: ['2'],
  54. query: 'release:123',
  55. statsPeriod: '14d',
  56. },
  57. })
  58. );
  59. });
  60. it('fills in interval based on start and end', function () {
  61. doMetricsRequest(api, {
  62. ...options,
  63. statsPeriod: undefined,
  64. interval: undefined,
  65. start: '2022-01-01T00:00:00',
  66. end: '2022-03-01T00:00:00',
  67. });
  68. expect(mock).toHaveBeenCalledTimes(1);
  69. expect(mock).toHaveBeenLastCalledWith(
  70. `/organizations/${orgSlug}/metrics/data/`,
  71. expect.objectContaining({
  72. query: {
  73. environment: [],
  74. field: ['sentry.sessions.session'],
  75. groupBy: ['session.status'],
  76. interval: '4h',
  77. orderBy: 'sentry.sessions.session',
  78. per_page: 5,
  79. project: ['2'],
  80. query: 'release:123',
  81. start: '2022-01-01T00:00:00.000',
  82. end: '2022-03-01T00:00:00.000',
  83. },
  84. })
  85. );
  86. });
  87. it('ignores falsy fields and groupBys', function () {
  88. doMetricsRequest(api, {
  89. ...options,
  90. field: [SessionMetric.SESSION, ''],
  91. groupBy: ['session.status', ''],
  92. });
  93. expect(mock).toHaveBeenCalledTimes(1);
  94. expect(mock).toHaveBeenLastCalledWith(
  95. `/organizations/${orgSlug}/metrics/data/`,
  96. expect.objectContaining({
  97. query: expect.objectContaining({
  98. field: ['sentry.sessions.session'],
  99. groupBy: ['session.status'],
  100. }),
  101. })
  102. );
  103. });
  104. });
  105. describe('fetchMetricsTags', function () {
  106. let mock;
  107. const tags = [{key: 'release'}, {key: 'environment'}];
  108. beforeEach(function () {
  109. MockApiClient.clearMockResponses();
  110. mock = MockApiClient.addMockResponse({
  111. url: `/organizations/${orgSlug}/metrics/tags/`,
  112. body: tags,
  113. });
  114. jest.restoreAllMocks();
  115. jest.spyOn(MetricsTagActions, 'loadMetricsTagsSuccess');
  116. jest.spyOn(MetricsTagStore, 'reset');
  117. });
  118. it('fetches api and updates store', async function () {
  119. fetchMetricsTags(api, orgSlug, [1], [`sum(${SessionMetric.SESSION})`]);
  120. await waitFor(() => expect(MetricsTagStore.reset).toHaveBeenCalledTimes(1));
  121. expect(mock).toHaveBeenCalledTimes(1);
  122. expect(mock).toHaveBeenLastCalledWith(
  123. `/organizations/${orgSlug}/metrics/tags/`,
  124. expect.objectContaining({
  125. query: {metric: ['sum(sentry.sessions.session)'], project: [1]},
  126. })
  127. );
  128. expect(MetricsTagActions.loadMetricsTagsSuccess).toHaveBeenCalledTimes(1);
  129. expect(MetricsTagActions.loadMetricsTagsSuccess).toHaveBeenCalledWith(tags);
  130. });
  131. });
  132. describe('fetchMetricsFields', function () {
  133. let mock;
  134. const meta = [
  135. {name: 'sentry.sessions.session', type: 'counter', operations: ['sum'], unit: null},
  136. {
  137. name: 'sentry.sessions.user',
  138. type: 'set',
  139. operations: ['count_unique'],
  140. unit: null,
  141. },
  142. ];
  143. beforeEach(function () {
  144. MockApiClient.clearMockResponses();
  145. mock = MockApiClient.addMockResponse({
  146. url: `/organizations/${orgSlug}/metrics/meta/`,
  147. body: meta,
  148. });
  149. jest.restoreAllMocks();
  150. jest.spyOn(MetricsMetaActions, 'loadMetricsMetaSuccess');
  151. jest.spyOn(MetricsMetaStore, 'reset');
  152. });
  153. it('fetches api and updates store', async function () {
  154. fetchMetricsFields(api, orgSlug, [1]);
  155. await waitFor(() => expect(MetricsMetaStore.reset).toHaveBeenCalledTimes(1));
  156. expect(mock).toHaveBeenCalledTimes(1);
  157. expect(mock).toHaveBeenLastCalledWith(
  158. `/organizations/${orgSlug}/metrics/meta/`,
  159. expect.objectContaining({
  160. query: {project: [1]},
  161. })
  162. );
  163. expect(MetricsMetaActions.loadMetricsMetaSuccess).toHaveBeenCalledTimes(1);
  164. expect(MetricsMetaActions.loadMetricsMetaSuccess).toHaveBeenCalledWith(meta);
  165. });
  166. });
  167. });