table.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. import {browserHistory} from 'react-router';
  2. import {initializeData as _initializeData} from 'sentry-test/performance/initializePerformanceData';
  3. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  7. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  8. import {OrganizationContext} from 'sentry/views/organizationContext';
  9. import Table from 'sentry/views/performance/table';
  10. const FEATURES = ['performance-view'];
  11. const initializeData = (settings = {}, features: string[] = []) => {
  12. const projects = [
  13. TestStubs.Project({id: '1', slug: '1'}),
  14. TestStubs.Project({id: '2', slug: '2'}),
  15. ];
  16. return _initializeData({
  17. features: [...FEATURES, ...features],
  18. projects,
  19. selectedProject: projects[0],
  20. ...settings,
  21. });
  22. };
  23. const WrappedComponent = ({data, ...rest}) => {
  24. return (
  25. <OrganizationContext.Provider value={data.organization}>
  26. <MEPSettingProvider>
  27. <Table
  28. organization={data.organization}
  29. location={data.router.location}
  30. setError={jest.fn()}
  31. summaryConditions=""
  32. {...data}
  33. {...rest}
  34. />
  35. </MEPSettingProvider>
  36. </OrganizationContext.Provider>
  37. );
  38. };
  39. function mockEventView(data) {
  40. const eventView = new EventView({
  41. id: '1',
  42. name: 'my query',
  43. fields: [
  44. {
  45. field: 'team_key_transaction',
  46. },
  47. {
  48. field: 'transaction',
  49. },
  50. {
  51. field: 'project',
  52. },
  53. {
  54. field: 'tpm()',
  55. },
  56. {
  57. field: 'p50()',
  58. },
  59. {
  60. field: 'p95()',
  61. },
  62. {
  63. field: 'failure_rate()',
  64. },
  65. {
  66. field: 'apdex()',
  67. },
  68. {
  69. field: 'count_unique(user)',
  70. },
  71. {
  72. field: 'count_miserable(user)',
  73. },
  74. {
  75. field: 'user_misery()',
  76. },
  77. ],
  78. sorts: [{field: 'tpm ', kind: 'desc'}],
  79. query: 'event.type:transaction transaction:/api*',
  80. project: [data.projects[0].id, data.projects[1].id],
  81. start: '2019-10-01T00:00:00',
  82. end: '2019-10-02T00:00:00',
  83. statsPeriod: '14d',
  84. environment: [],
  85. additionalConditions: new MutableSearch(''),
  86. createdBy: undefined,
  87. interval: undefined,
  88. display: '',
  89. team: [],
  90. topEvents: undefined,
  91. yAxis: undefined,
  92. });
  93. return eventView;
  94. }
  95. describe('Performance > Table', function () {
  96. let eventsV2Mock, eventsMock;
  97. beforeEach(function () {
  98. browserHistory.push = jest.fn();
  99. MockApiClient.addMockResponse({
  100. url: '/organizations/org-slug/projects/',
  101. body: [],
  102. });
  103. const eventsMetaFieldsMock = {
  104. user: 'string',
  105. transaction: 'string',
  106. project: 'string',
  107. tpm: 'number',
  108. p50: 'number',
  109. p95: 'number',
  110. failure_rate: 'number',
  111. apdex: 'number',
  112. count_unique_user: 'number',
  113. count_miserable_user: 'number',
  114. user_misery: 'number',
  115. };
  116. const eventsBodyMock = [
  117. {
  118. team_key_transaction: 1,
  119. transaction: '/apple/cart',
  120. project: '2',
  121. user: 'uhoh@example.com',
  122. tpm: 30,
  123. p50: 100,
  124. p95: 500,
  125. failure_rate: 0.1,
  126. apdex: 0.6,
  127. count_unique_user: 1000,
  128. count_miserable_user: 122,
  129. user_misery: 0.114,
  130. project_threshold_config: ['duration', 300],
  131. },
  132. {
  133. team_key_transaction: 0,
  134. transaction: '/apple/checkout',
  135. project: '1',
  136. user: 'uhoh@example.com',
  137. tpm: 30,
  138. p50: 100,
  139. p95: 500,
  140. failure_rate: 0.1,
  141. apdex: 0.6,
  142. count_unique_user: 1000,
  143. count_miserable_user: 122,
  144. user_misery: 0.114,
  145. project_threshold_config: ['duration', 300],
  146. },
  147. ];
  148. eventsV2Mock = MockApiClient.addMockResponse({
  149. url: '/organizations/org-slug/eventsv2/',
  150. body: {
  151. meta: eventsMetaFieldsMock,
  152. data: eventsBodyMock,
  153. },
  154. });
  155. eventsMock = MockApiClient.addMockResponse({
  156. url: '/organizations/org-slug/events/',
  157. body: {
  158. meta: {fields: eventsMetaFieldsMock},
  159. data: eventsBodyMock,
  160. },
  161. });
  162. MockApiClient.addMockResponse({
  163. method: 'GET',
  164. url: `/organizations/org-slug/key-transactions-list/`,
  165. body: [],
  166. });
  167. });
  168. afterEach(function () {
  169. MockApiClient.clearMockResponses();
  170. });
  171. describe('with eventsv2', function () {
  172. it('renders correct cell actions without feature', async function () {
  173. const data = initializeData({
  174. query: 'event.type:transaction transaction:/api*',
  175. });
  176. ProjectsStore.loadInitialData(data.organization.projects);
  177. render(
  178. <WrappedComponent
  179. data={data}
  180. eventView={mockEventView(data)}
  181. setError={jest.fn()}
  182. summaryConditions=""
  183. projects={data.projects}
  184. />,
  185. {context: data.routerContext}
  186. );
  187. const rows = await screen.findAllByTestId('grid-body-row');
  188. const transactionCells = within(rows[0]).getAllByTestId('grid-body-cell');
  189. const transactionCell = transactionCells[1];
  190. const link = within(transactionCell).getByRole('link', {name: '/apple/cart'});
  191. expect(link).toHaveAttribute(
  192. 'href',
  193. '/organizations/org-slug/performance/summary/?end=2019-10-02T00%3A00%3A00&project=2&query=&referrer=performance-transaction-summary&start=2019-10-01T00%3A00%3A00&statsPeriod=14d&transaction=%2Fapple%2Fcart&unselectedSeries=p100%28%29'
  194. );
  195. const cellActionContainers = screen.getAllByTestId('cell-action-container');
  196. expect(cellActionContainers).toHaveLength(18); // 9 cols x 2 rows
  197. userEvent.hover(cellActionContainers[8]);
  198. const cellActions = await screen.findByTestId('cell-action');
  199. expect(cellActions).toBeInTheDocument();
  200. userEvent.click(cellActions);
  201. expect(await screen.findByTestId('add-to-filter')).toBeInTheDocument();
  202. expect(screen.getByTestId('exclude-from-filter')).toBeInTheDocument();
  203. userEvent.hover(cellActionContainers[0]); // Transaction name
  204. const transactionCellActions = await screen.findAllByTestId('cell-action');
  205. expect(transactionCellActions[0]).toBeInTheDocument();
  206. userEvent.click(transactionCellActions[0]);
  207. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  208. userEvent.click(screen.getByTestId('add-to-filter'));
  209. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  210. expect(browserHistory.push).toHaveBeenNthCalledWith(1, {
  211. pathname: undefined,
  212. query: expect.objectContaining({
  213. query: 'transaction:/apple/cart',
  214. }),
  215. });
  216. });
  217. it('hides cell actions when withStaticFilters is true', function () {
  218. const data = initializeData(
  219. {
  220. query: 'event.type:transaction transaction:/api*',
  221. },
  222. ['performance-frontend-use-events-endpoint']
  223. );
  224. render(
  225. <WrappedComponent
  226. data={data}
  227. eventView={mockEventView(data)}
  228. setError={jest.fn()}
  229. summaryConditions=""
  230. projects={data.projects}
  231. withStaticFilters
  232. />
  233. );
  234. const cellActionContainers = screen.queryByTestId('cell-action-container');
  235. expect(cellActionContainers).not.toBeInTheDocument();
  236. });
  237. it('sends MEP param when setting enabled', function () {
  238. const data = initializeData(
  239. {
  240. query: 'event.type:transaction transaction:/api*',
  241. },
  242. ['performance-use-metrics']
  243. );
  244. render(
  245. <WrappedComponent
  246. data={data}
  247. eventView={mockEventView(data)}
  248. setError={jest.fn()}
  249. summaryConditions=""
  250. projects={data.projects}
  251. isMEPEnabled
  252. />
  253. );
  254. expect(eventsV2Mock).toHaveBeenCalledTimes(1);
  255. expect(eventsV2Mock).toHaveBeenNthCalledWith(
  256. 1,
  257. expect.anything(),
  258. expect.objectContaining({
  259. query: expect.objectContaining({
  260. environment: [],
  261. field: [
  262. 'team_key_transaction',
  263. 'transaction',
  264. 'project',
  265. 'tpm()',
  266. 'p50()',
  267. 'p95()',
  268. 'failure_rate()',
  269. 'apdex()',
  270. 'count_unique(user)',
  271. 'count_miserable(user)',
  272. 'user_misery()',
  273. ],
  274. dataset: 'metrics',
  275. per_page: 50,
  276. project: ['1', '2'],
  277. query: 'event.type:transaction transaction:/api*',
  278. referrer: 'api.performance.landing-table',
  279. sort: '-team_key_transaction',
  280. statsPeriod: '14d',
  281. }),
  282. })
  283. );
  284. });
  285. });
  286. describe('with events', function () {
  287. it('renders correct cell actions without feature', async function () {
  288. const data = initializeData(
  289. {
  290. query: 'event.type:transaction transaction:/api*',
  291. },
  292. ['performance-frontend-use-events-endpoint']
  293. );
  294. ProjectsStore.loadInitialData(data.organization.projects);
  295. render(
  296. <WrappedComponent
  297. data={data}
  298. eventView={mockEventView(data)}
  299. setError={jest.fn()}
  300. summaryConditions=""
  301. projects={data.projects}
  302. />,
  303. {context: data.routerContext}
  304. );
  305. const rows = await screen.findAllByTestId('grid-body-row');
  306. const transactionCells = within(rows[0]).getAllByTestId('grid-body-cell');
  307. const transactionCell = transactionCells[1];
  308. const link = within(transactionCell).getByRole('link', {name: '/apple/cart'});
  309. expect(link).toHaveAttribute(
  310. 'href',
  311. '/organizations/org-slug/performance/summary/?end=2019-10-02T00%3A00%3A00&project=2&query=&referrer=performance-transaction-summary&start=2019-10-01T00%3A00%3A00&statsPeriod=14d&transaction=%2Fapple%2Fcart&unselectedSeries=p100%28%29'
  312. );
  313. const cellActionContainers = screen.getAllByTestId('cell-action-container');
  314. expect(cellActionContainers).toHaveLength(18); // 9 cols x 2 rows
  315. userEvent.hover(cellActionContainers[8]);
  316. const cellActions = await screen.findByTestId('cell-action');
  317. expect(cellActions).toBeInTheDocument();
  318. userEvent.click(cellActions);
  319. expect(await screen.findByTestId('add-to-filter')).toBeInTheDocument();
  320. expect(screen.getByTestId('exclude-from-filter')).toBeInTheDocument();
  321. userEvent.hover(cellActionContainers[0]); // Transaction name
  322. const transactionCellActions = await screen.findAllByTestId('cell-action');
  323. expect(transactionCellActions[0]).toBeInTheDocument();
  324. userEvent.click(transactionCellActions[0]);
  325. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  326. userEvent.click(screen.getByTestId('add-to-filter'));
  327. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  328. expect(browserHistory.push).toHaveBeenNthCalledWith(1, {
  329. pathname: undefined,
  330. query: expect.objectContaining({
  331. query: 'transaction:/apple/cart',
  332. }),
  333. });
  334. });
  335. it('hides cell actions when withStaticFilters is true', function () {
  336. const data = initializeData(
  337. {
  338. query: 'event.type:transaction transaction:/api*',
  339. },
  340. ['performance-frontend-use-events-endpoint']
  341. );
  342. render(
  343. <WrappedComponent
  344. data={data}
  345. eventView={mockEventView(data)}
  346. setError={jest.fn()}
  347. summaryConditions=""
  348. projects={data.projects}
  349. withStaticFilters
  350. />
  351. );
  352. const cellActionContainers = screen.queryByTestId('cell-action-container');
  353. expect(cellActionContainers).not.toBeInTheDocument();
  354. });
  355. it('sends MEP param when setting enabled', function () {
  356. const data = initializeData(
  357. {
  358. query: 'event.type:transaction transaction:/api*',
  359. },
  360. ['performance-use-metrics', 'performance-frontend-use-events-endpoint']
  361. );
  362. render(
  363. <WrappedComponent
  364. data={data}
  365. eventView={mockEventView(data)}
  366. setError={jest.fn()}
  367. summaryConditions=""
  368. projects={data.projects}
  369. isMEPEnabled
  370. />
  371. );
  372. expect(eventsMock).toHaveBeenCalledTimes(1);
  373. expect(eventsMock).toHaveBeenNthCalledWith(
  374. 1,
  375. expect.anything(),
  376. expect.objectContaining({
  377. query: expect.objectContaining({
  378. environment: [],
  379. field: [
  380. 'team_key_transaction',
  381. 'transaction',
  382. 'project',
  383. 'tpm()',
  384. 'p50()',
  385. 'p95()',
  386. 'failure_rate()',
  387. 'apdex()',
  388. 'count_unique(user)',
  389. 'count_miserable(user)',
  390. 'user_misery()',
  391. ],
  392. dataset: 'metrics',
  393. per_page: 50,
  394. project: ['1', '2'],
  395. query: 'event.type:transaction transaction:/api*',
  396. referrer: 'api.performance.landing-table',
  397. sort: '-team_key_transaction',
  398. statsPeriod: '14d',
  399. }),
  400. })
  401. );
  402. });
  403. });
  404. });