visualizationStep.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  4. import {Organization} from 'sentry/types';
  5. import {DashboardWidgetSource} from 'sentry/views/dashboardsV2/types';
  6. import WidgetBuilder from 'sentry/views/dashboardsV2/widgetBuilder';
  7. jest.unmock('lodash/debounce');
  8. function mockRequests(orgSlug: Organization['slug']) {
  9. const eventsv2Mock = MockApiClient.addMockResponse({
  10. url: `/organizations/${orgSlug}/eventsv2/`,
  11. method: 'GET',
  12. statusCode: 200,
  13. body: {
  14. meta: {},
  15. data: [],
  16. },
  17. });
  18. MockApiClient.addMockResponse({
  19. url: '/organizations/org-slug/tags/',
  20. method: 'GET',
  21. body: TestStubs.Tags(),
  22. });
  23. MockApiClient.addMockResponse({
  24. url: '/organizations/org-slug/users/',
  25. body: [],
  26. });
  27. MockApiClient.addMockResponse({
  28. url: '/organizations/org-slug/projects/',
  29. method: 'GET',
  30. body: [],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/measurements-meta/',
  34. method: 'GET',
  35. body: {'measurements.custom.measurement': {functions: ['p99']}},
  36. });
  37. return {eventsv2Mock};
  38. }
  39. describe('VisualizationStep', function () {
  40. const {organization, router, routerContext} = initializeOrg({
  41. ...initializeOrg(),
  42. organization: {
  43. features: [
  44. 'dashboards-edit',
  45. 'global-views',
  46. 'new-widget-builder-experience-design',
  47. 'dashboards-mep',
  48. ],
  49. },
  50. router: {
  51. location: {
  52. query: {
  53. source: DashboardWidgetSource.DASHBOARDS,
  54. },
  55. },
  56. },
  57. });
  58. it('debounce works as expected and requests are not triggered often', async function () {
  59. const {eventsv2Mock} = mockRequests(organization.slug);
  60. jest.useFakeTimers();
  61. render(
  62. <WidgetBuilder
  63. route={{}}
  64. router={router}
  65. routes={router.routes}
  66. routeParams={router.params}
  67. location={router.location}
  68. dashboard={{
  69. id: 'new',
  70. title: 'Dashboard',
  71. createdBy: undefined,
  72. dateCreated: '2020-01-01T00:00:00.000Z',
  73. widgets: [],
  74. projects: [],
  75. filters: {},
  76. }}
  77. onSave={jest.fn()}
  78. params={{
  79. orgId: organization.slug,
  80. dashboardId: 'new',
  81. }}
  82. />,
  83. {
  84. context: routerContext,
  85. organization,
  86. }
  87. );
  88. await waitFor(() => expect(eventsv2Mock).toHaveBeenCalledTimes(1));
  89. userEvent.type(await screen.findByPlaceholderText('Alias'), 'abc');
  90. act(() => {
  91. jest.advanceTimersByTime(DEFAULT_DEBOUNCE_DURATION + 1);
  92. });
  93. await waitFor(() => expect(eventsv2Mock).toHaveBeenCalledTimes(2));
  94. });
  95. it('displays stored data alert', async function () {
  96. MockApiClient.addMockResponse({
  97. url: `/organizations/${organization.slug}/eventsv2/`,
  98. method: 'GET',
  99. statusCode: 200,
  100. body: {
  101. meta: {isMetricsData: false},
  102. data: [],
  103. },
  104. });
  105. render(
  106. <WidgetBuilder
  107. route={{}}
  108. router={router}
  109. routes={router.routes}
  110. routeParams={router.params}
  111. location={router.location}
  112. dashboard={{
  113. id: 'new',
  114. title: 'Dashboard',
  115. createdBy: undefined,
  116. dateCreated: '2020-01-01T00:00:00.000Z',
  117. widgets: [],
  118. projects: [],
  119. filters: {},
  120. }}
  121. onSave={jest.fn()}
  122. params={{
  123. orgId: organization.slug,
  124. dashboardId: 'new',
  125. }}
  126. />,
  127. {
  128. context: routerContext,
  129. organization,
  130. }
  131. );
  132. await screen.findByText(/we've automatically adjusted your results/i);
  133. });
  134. it('uses release from URL params when querying', async function () {
  135. const {eventsv2Mock} = mockRequests(organization.slug);
  136. render(
  137. <WidgetBuilder
  138. route={{}}
  139. router={router}
  140. routes={router.routes}
  141. routeParams={router.params}
  142. location={{
  143. ...router.location,
  144. query: {
  145. ...router.location.query,
  146. release: ['v1'],
  147. },
  148. }}
  149. dashboard={{
  150. id: 'new',
  151. title: 'Dashboard',
  152. createdBy: undefined,
  153. dateCreated: '2020-01-01T00:00:00.000Z',
  154. widgets: [],
  155. projects: [],
  156. filters: {},
  157. }}
  158. onSave={jest.fn()}
  159. params={{
  160. orgId: organization.slug,
  161. dashboardId: 'new',
  162. }}
  163. />,
  164. {
  165. context: routerContext,
  166. organization,
  167. }
  168. );
  169. await waitFor(() =>
  170. expect(eventsv2Mock).toHaveBeenCalledWith(
  171. '/organizations/org-slug/eventsv2/',
  172. expect.objectContaining({
  173. query: expect.objectContaining({query: ' release:v1 '}),
  174. })
  175. )
  176. );
  177. });
  178. });