metricsRequest.spec.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {mountWithTheme, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import MetricsRequest from 'sentry/utils/metrics/metricsRequest';
  3. describe('MetricsRequest', () => {
  4. const project = TestStubs.Project();
  5. const organization = TestStubs.Organization();
  6. const childrenMock = jest.fn(() => null);
  7. const props = {
  8. api: new MockApiClient(),
  9. orgSlug: organization.slug,
  10. field: ['fieldA'],
  11. project: [project.id],
  12. environment: ['prod'],
  13. statsPeriod: '14d',
  14. query: 'abc',
  15. groupBy: ['status'],
  16. orderBy: 'fieldA',
  17. limit: 3,
  18. };
  19. let metricsMock;
  20. beforeEach(() => {
  21. metricsMock = MockApiClient.addMockResponse({
  22. method: 'GET',
  23. url: `/organizations/org-slug/metrics/data/`,
  24. body: {intervals: [], groups: []},
  25. });
  26. childrenMock.mockClear();
  27. });
  28. it('makes request and passes correct render props', async () => {
  29. mountWithTheme(<MetricsRequest {...props}>{childrenMock}</MetricsRequest>);
  30. expect(childrenMock).toHaveBeenNthCalledWith(1, {
  31. errored: false,
  32. error: null,
  33. loading: true,
  34. isLoading: true,
  35. reloading: false,
  36. response: null,
  37. responsePrevious: null,
  38. tableData: undefined,
  39. pageLinks: null,
  40. });
  41. expect(metricsMock).toHaveBeenCalledTimes(1);
  42. expect(metricsMock).toHaveBeenCalledWith(
  43. expect.anything(),
  44. expect.objectContaining({
  45. query: {
  46. environment: ['prod'],
  47. field: ['fieldA'],
  48. groupBy: ['status'],
  49. interval: '1h',
  50. per_page: 3,
  51. orderBy: 'fieldA',
  52. project: ['2'],
  53. query: 'abc',
  54. statsPeriod: '14d',
  55. },
  56. })
  57. );
  58. await waitFor(() =>
  59. expect(childrenMock).toHaveBeenLastCalledWith({
  60. errored: false,
  61. error: null,
  62. loading: false,
  63. isLoading: false,
  64. reloading: false,
  65. response: {groups: [], intervals: []},
  66. responsePrevious: null,
  67. tableData: undefined,
  68. pageLinks: null,
  69. })
  70. );
  71. });
  72. it('does not make request if isDisabled', () => {
  73. mountWithTheme(
  74. <MetricsRequest {...props} isDisabled>
  75. {childrenMock}
  76. </MetricsRequest>
  77. );
  78. expect(metricsMock).toHaveBeenCalledTimes(0);
  79. expect(childrenMock).toHaveBeenCalledTimes(1);
  80. expect(childrenMock).toHaveBeenCalledWith({
  81. errored: false,
  82. error: null,
  83. loading: false,
  84. isLoading: false,
  85. reloading: false,
  86. response: null,
  87. responsePrevious: null,
  88. tableData: undefined,
  89. pageLinks: null,
  90. });
  91. });
  92. it('refetches when props change', () => {
  93. const {rerender} = mountWithTheme(
  94. <MetricsRequest {...props}>{childrenMock}</MetricsRequest>
  95. );
  96. expect(metricsMock).toHaveBeenCalledTimes(1);
  97. rerender(
  98. <MetricsRequest {...props} field={['fieldB']}>
  99. {childrenMock}
  100. </MetricsRequest>
  101. );
  102. expect(metricsMock).toHaveBeenCalledTimes(2);
  103. expect(metricsMock).toHaveBeenLastCalledWith(
  104. expect.anything(),
  105. expect.objectContaining({
  106. query: expect.objectContaining({field: ['fieldB']}),
  107. })
  108. );
  109. });
  110. it('does not refetch when ignored props change', () => {
  111. const {rerender} = mountWithTheme(
  112. <MetricsRequest {...props}>{childrenMock}</MetricsRequest>
  113. );
  114. const differentChildrenMock = jest.fn(() => 'lorem ipsum');
  115. rerender(<MetricsRequest {...props}>{differentChildrenMock}</MetricsRequest>);
  116. expect(metricsMock).toHaveBeenCalledTimes(1);
  117. });
  118. it('make two requests if includePrevious is enabled', async () => {
  119. mountWithTheme(
  120. <MetricsRequest {...props} includePrevious>
  121. {childrenMock}
  122. </MetricsRequest>
  123. );
  124. expect(childrenMock).toHaveBeenNthCalledWith(1, {
  125. errored: false,
  126. error: null,
  127. loading: true,
  128. isLoading: true,
  129. reloading: false,
  130. response: null,
  131. responsePrevious: null,
  132. tableData: undefined,
  133. pageLinks: null,
  134. });
  135. expect(metricsMock).toHaveBeenCalledTimes(2);
  136. expect(metricsMock).toHaveBeenNthCalledWith(
  137. 1,
  138. expect.anything(),
  139. expect.objectContaining({
  140. query: {
  141. environment: ['prod'],
  142. field: ['fieldA'],
  143. groupBy: ['status'],
  144. interval: '1h',
  145. per_page: 3,
  146. orderBy: 'fieldA',
  147. project: ['2'],
  148. query: 'abc',
  149. statsPeriod: '14d',
  150. },
  151. })
  152. );
  153. expect(metricsMock).toHaveBeenLastCalledWith(
  154. expect.anything(),
  155. expect.objectContaining({
  156. query: {
  157. project: ['2'],
  158. environment: ['prod'],
  159. field: ['fieldA'],
  160. query: 'abc',
  161. groupBy: ['status'],
  162. orderBy: 'fieldA',
  163. per_page: 3,
  164. interval: '1h',
  165. statsPeriodStart: '28d',
  166. statsPeriodEnd: '14d',
  167. },
  168. })
  169. );
  170. await waitFor(() =>
  171. expect(childrenMock).toHaveBeenLastCalledWith({
  172. errored: false,
  173. error: null,
  174. loading: false,
  175. isLoading: false,
  176. reloading: false,
  177. response: {groups: [], intervals: []},
  178. responsePrevious: {groups: [], intervals: []},
  179. tableData: undefined,
  180. pageLinks: null,
  181. })
  182. );
  183. });
  184. it('make one request with absolute date', () => {
  185. mountWithTheme(
  186. <MetricsRequest
  187. {...props}
  188. statsPeriod=""
  189. start="Wed Dec 01 2021 01:00:00 GMT+0100 (Central European Standard Time)"
  190. end="Fri Dec 17 2021 00:59:59 GMT+0100 (Central European Standard Time)"
  191. includePrevious
  192. >
  193. {childrenMock}
  194. </MetricsRequest>
  195. );
  196. expect(childrenMock).toHaveBeenNthCalledWith(1, {
  197. errored: false,
  198. error: null,
  199. loading: true,
  200. isLoading: true,
  201. reloading: false,
  202. response: null,
  203. responsePrevious: null,
  204. tableData: undefined,
  205. pageLinks: null,
  206. });
  207. // if start and end are provided, it will not perform a request to fetch previous data
  208. expect(metricsMock).toHaveBeenCalledTimes(1);
  209. expect(metricsMock).toHaveBeenCalledWith(
  210. expect.anything(),
  211. expect.objectContaining({
  212. query: {
  213. end: '2021-12-17T00:59:59.000',
  214. environment: ['prod'],
  215. field: ['fieldA'],
  216. groupBy: ['status'],
  217. interval: '1h',
  218. per_page: 3,
  219. orderBy: 'fieldA',
  220. project: ['2'],
  221. query: 'abc',
  222. start: '2021-12-01T01:00:00.000',
  223. },
  224. })
  225. );
  226. });
  227. });