visualizationStep.spec.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import {DashboardFixture} from 'sentry-fixture/dashboard';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {PageFiltersFixture} from 'sentry-fixture/pageFilters';
  4. import {TagsFixture} from 'sentry-fixture/tags';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import type {Organization} from 'sentry/types/organization';
  9. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  10. import {
  11. DashboardWidgetSource,
  12. DisplayType,
  13. WidgetType,
  14. } from 'sentry/views/dashboards/types';
  15. import WidgetBuilder from 'sentry/views/dashboards/widgetBuilder';
  16. import {VisualizationStep} from 'sentry/views/dashboards/widgetBuilder/buildSteps/visualizationStep';
  17. import WidgetLegendSelectionState from '../../widgetLegendSelectionState';
  18. jest.unmock('lodash/debounce');
  19. function mockRequests(orgSlug: Organization['slug']) {
  20. const eventsMock = MockApiClient.addMockResponse({
  21. url: `/organizations/${orgSlug}/events/`,
  22. method: 'GET',
  23. statusCode: 200,
  24. body: {
  25. meta: {},
  26. data: [],
  27. },
  28. });
  29. MockApiClient.addMockResponse({
  30. url: '/organizations/org-slug/tags/',
  31. method: 'GET',
  32. body: TagsFixture(),
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/users/',
  36. body: [],
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/projects/',
  40. method: 'GET',
  41. body: [],
  42. });
  43. MockApiClient.addMockResponse({
  44. url: '/organizations/org-slug/measurements-meta/',
  45. method: 'GET',
  46. body: {'measurements.custom.measurement': {functions: ['p99']}},
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/metrics-compatibility/',
  50. method: 'GET',
  51. body: {
  52. incompatible_projects: [],
  53. compatible_projects: [1],
  54. },
  55. });
  56. MockApiClient.addMockResponse({
  57. url: '/organizations/org-slug/metrics-compatibility-sums/',
  58. method: 'GET',
  59. body: {
  60. sum: {
  61. metrics: 988803,
  62. metrics_null: 0,
  63. metrics_unparam: 132,
  64. },
  65. },
  66. });
  67. MockApiClient.addMockResponse({
  68. url: '/organizations/org-slug/releases/',
  69. body: [],
  70. });
  71. MockApiClient.addMockResponse({
  72. url: '/organizations/org-slug/recent-searches/',
  73. method: 'GET',
  74. body: [],
  75. });
  76. return {eventsMock};
  77. }
  78. describe('VisualizationStep', function () {
  79. const {organization, projects, router} = initializeOrg({
  80. organization: {
  81. features: ['performance-view', 'dashboards-edit', 'global-views', 'dashboards-mep'],
  82. },
  83. router: {
  84. location: {
  85. query: {
  86. source: DashboardWidgetSource.DASHBOARDS,
  87. },
  88. },
  89. },
  90. });
  91. const widgetLegendState = new WidgetLegendSelectionState({
  92. location: LocationFixture(),
  93. dashboard: DashboardFixture([], {id: 'new', title: 'Dashboard'}),
  94. organization,
  95. router,
  96. });
  97. beforeEach(function () {
  98. ProjectsStore.loadInitialData(projects);
  99. });
  100. it('debounce works as expected and requests are not triggered often', async function () {
  101. const {eventsMock} = mockRequests(organization.slug);
  102. render(
  103. <WidgetBuilder
  104. route={{}}
  105. router={router}
  106. routes={router.routes}
  107. routeParams={router.params}
  108. location={router.location}
  109. dashboard={{
  110. id: 'new',
  111. title: 'Dashboard',
  112. createdBy: undefined,
  113. dateCreated: '2020-01-01T00:00:00.000Z',
  114. widgets: [],
  115. projects: [],
  116. filters: {},
  117. }}
  118. onSave={jest.fn()}
  119. params={{
  120. orgId: organization.slug,
  121. dashboardId: 'new',
  122. }}
  123. widgetLegendState={widgetLegendState}
  124. />,
  125. {
  126. router,
  127. organization,
  128. }
  129. );
  130. await waitFor(() => expect(eventsMock).toHaveBeenCalledTimes(1));
  131. await userEvent.type(await screen.findByPlaceholderText('Alias'), 'abc', {
  132. delay: null,
  133. });
  134. await waitFor(() => expect(eventsMock).toHaveBeenCalledTimes(1));
  135. });
  136. it('displays stored data alert', async function () {
  137. mockRequests(organization.slug);
  138. MockApiClient.addMockResponse({
  139. url: `/organizations/${organization.slug}/events/`,
  140. method: 'GET',
  141. statusCode: 200,
  142. body: {
  143. meta: {isMetricsData: false},
  144. data: [],
  145. },
  146. });
  147. render(
  148. <WidgetBuilder
  149. route={{}}
  150. router={router}
  151. routes={router.routes}
  152. routeParams={router.params}
  153. location={router.location}
  154. dashboard={{
  155. id: 'new',
  156. title: 'Dashboard',
  157. createdBy: undefined,
  158. dateCreated: '2020-01-01T00:00:00.000Z',
  159. widgets: [],
  160. projects: [],
  161. filters: {},
  162. }}
  163. onSave={jest.fn()}
  164. params={{
  165. orgId: organization.slug,
  166. dashboardId: 'new',
  167. }}
  168. widgetLegendState={widgetLegendState}
  169. />,
  170. {
  171. router,
  172. organization: {
  173. ...organization,
  174. features: [...organization.features, 'dynamic-sampling', 'mep-rollout-flag'],
  175. },
  176. }
  177. );
  178. await screen.findByText(/we've automatically adjusted your results/i);
  179. });
  180. it('uses release from URL params when querying', async function () {
  181. const {eventsMock} = mockRequests(organization.slug);
  182. render(
  183. <WidgetBuilder
  184. route={{}}
  185. router={router}
  186. routes={router.routes}
  187. routeParams={router.params}
  188. location={{
  189. ...router.location,
  190. query: {
  191. ...router.location.query,
  192. release: ['v1'],
  193. },
  194. }}
  195. dashboard={{
  196. id: 'new',
  197. title: 'Dashboard',
  198. createdBy: undefined,
  199. dateCreated: '2020-01-01T00:00:00.000Z',
  200. widgets: [],
  201. projects: [],
  202. filters: {},
  203. }}
  204. onSave={jest.fn()}
  205. params={{
  206. orgId: organization.slug,
  207. dashboardId: 'new',
  208. }}
  209. widgetLegendState={widgetLegendState}
  210. />,
  211. {
  212. router,
  213. organization,
  214. }
  215. );
  216. await waitFor(() =>
  217. expect(eventsMock).toHaveBeenCalledWith(
  218. '/organizations/org-slug/events/',
  219. expect.objectContaining({
  220. query: expect.objectContaining({query: ' release:"v1" '}),
  221. })
  222. )
  223. );
  224. });
  225. it('does not trigger an extra events request when adding a column', async function () {
  226. const {eventsMock} = mockRequests(organization.slug);
  227. render(
  228. <WidgetBuilder
  229. route={{}}
  230. router={router}
  231. routes={router.routes}
  232. routeParams={router.params}
  233. location={{
  234. ...router.location,
  235. query: {
  236. ...router.location.query,
  237. release: ['v1'],
  238. },
  239. }}
  240. dashboard={{
  241. id: 'new',
  242. title: 'Dashboard',
  243. createdBy: undefined,
  244. dateCreated: '2020-01-01T00:00:00.000Z',
  245. widgets: [],
  246. projects: [],
  247. filters: {},
  248. }}
  249. onSave={jest.fn()}
  250. params={{
  251. orgId: organization.slug,
  252. dashboardId: 'new',
  253. }}
  254. widgetLegendState={widgetLegendState}
  255. />,
  256. {
  257. router,
  258. organization,
  259. }
  260. );
  261. await userEvent.click(screen.getByText('Add a Column'));
  262. // Only called once on the initial render
  263. await waitFor(() => expect(eventsMock).toHaveBeenCalledTimes(1));
  264. });
  265. it('makes a request to the spans dataset for a table widget', async function () {
  266. const {eventsMock} = mockRequests(organization.slug);
  267. const mockSpanWidget = {
  268. interval: '1d',
  269. title: 'Title',
  270. widgetType: WidgetType.SPANS,
  271. displayType: DisplayType.TABLE,
  272. queries: [
  273. {
  274. conditions: '',
  275. name: '',
  276. aggregates: ['count()'],
  277. columns: [],
  278. fields: [],
  279. orderby: '',
  280. },
  281. ],
  282. };
  283. render(
  284. <MEPSettingProvider>
  285. <VisualizationStep
  286. organization={organization}
  287. pageFilters={PageFiltersFixture()}
  288. displayType={DisplayType.TABLE}
  289. error={undefined}
  290. onChange={jest.fn()}
  291. widget={mockSpanWidget}
  292. isWidgetInvalid={false}
  293. location={router.location}
  294. widgetLegendState={widgetLegendState}
  295. />
  296. </MEPSettingProvider>,
  297. {organization}
  298. );
  299. await waitFor(() =>
  300. expect(eventsMock).toHaveBeenCalledWith(
  301. '/organizations/org-slug/events/',
  302. expect.objectContaining({
  303. query: expect.objectContaining({
  304. dataset: 'spans',
  305. }),
  306. })
  307. )
  308. );
  309. });
  310. });