visualizationStep.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 screen.findByText('Table');
  89. await waitFor(() => expect(eventsv2Mock).toHaveBeenCalledTimes(1));
  90. userEvent.type(screen.getByPlaceholderText('Alias'), 'First Alias{enter}');
  91. act(() => {
  92. jest.advanceTimersByTime(DEFAULT_DEBOUNCE_DURATION + 1);
  93. });
  94. await waitFor(() => expect(eventsv2Mock).toHaveBeenCalledTimes(2));
  95. });
  96. it('displays stored data alert', async function () {
  97. MockApiClient.addMockResponse({
  98. url: `/organizations/${organization.slug}/eventsv2/`,
  99. method: 'GET',
  100. statusCode: 200,
  101. body: {
  102. meta: {isMetricsData: false},
  103. data: [],
  104. },
  105. });
  106. render(
  107. <WidgetBuilder
  108. route={{}}
  109. router={router}
  110. routes={router.routes}
  111. routeParams={router.params}
  112. location={router.location}
  113. dashboard={{
  114. id: 'new',
  115. title: 'Dashboard',
  116. createdBy: undefined,
  117. dateCreated: '2020-01-01T00:00:00.000Z',
  118. widgets: [],
  119. projects: [],
  120. filters: {},
  121. }}
  122. onSave={jest.fn()}
  123. params={{
  124. orgId: organization.slug,
  125. dashboardId: 'new',
  126. }}
  127. />,
  128. {
  129. context: routerContext,
  130. organization,
  131. }
  132. );
  133. await screen.findByText(/we've automatically adjusted your results/i);
  134. });
  135. });