table.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. function 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 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. team_key_transaction: 0,
  149. transaction: '<< unparameterized >>',
  150. project: '3',
  151. user: 'uhoh@example.com',
  152. tpm: 1,
  153. p50: 100,
  154. p95: 500,
  155. failure_rate: 0.1,
  156. apdex: 0.6,
  157. count_unique_user: 500,
  158. count_miserable_user: 31,
  159. user_misery: 0.514,
  160. project_threshold_config: ['duration', 300],
  161. },
  162. ];
  163. eventsMock = MockApiClient.addMockResponse({
  164. url: '/organizations/org-slug/events/',
  165. body: {
  166. meta: {fields: eventsMetaFieldsMock},
  167. data: eventsBodyMock,
  168. },
  169. });
  170. MockApiClient.addMockResponse({
  171. method: 'GET',
  172. url: `/organizations/org-slug/key-transactions-list/`,
  173. body: [],
  174. });
  175. });
  176. afterEach(function () {
  177. MockApiClient.clearMockResponses();
  178. });
  179. describe('with events', function () {
  180. it('renders correct cell actions without feature', async function () {
  181. const data = initializeData({
  182. query: 'event.type:transaction transaction:/api*',
  183. });
  184. ProjectsStore.loadInitialData(data.organization.projects);
  185. render(
  186. <WrappedComponent
  187. data={data}
  188. eventView={mockEventView(data)}
  189. setError={jest.fn()}
  190. summaryConditions=""
  191. projects={data.projects}
  192. />,
  193. {context: data.routerContext}
  194. );
  195. const rows = await screen.findAllByTestId('grid-body-row');
  196. const transactionCells = within(rows[0]).getAllByTestId('grid-body-cell');
  197. const transactionCell = transactionCells[1];
  198. const link = within(transactionCell).getByRole('link', {name: '/apple/cart'});
  199. expect(link).toHaveAttribute(
  200. 'href',
  201. '/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'
  202. );
  203. const cellActionContainers = screen.getAllByTestId('cell-action-container');
  204. expect(cellActionContainers).toHaveLength(27); // 9 cols x 3 rows
  205. const cellActionTriggers = screen.getAllByRole('button', {name: 'Actions'});
  206. expect(cellActionTriggers[8]).toBeInTheDocument();
  207. await userEvent.click(cellActionTriggers[8]);
  208. expect(
  209. screen.getByRole('menuitemradio', {name: 'Add to filter'})
  210. ).toBeInTheDocument();
  211. expect(
  212. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  213. ).toBeInTheDocument();
  214. await userEvent.keyboard('{Escape}'); // Close actions menu
  215. const transactionCellTrigger = cellActionTriggers[0]; // Transaction name
  216. expect(transactionCellTrigger).toBeInTheDocument();
  217. await userEvent.click(transactionCellTrigger);
  218. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  219. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  220. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  221. expect(browserHistory.push).toHaveBeenNthCalledWith(1, {
  222. pathname: undefined,
  223. query: expect.objectContaining({
  224. query: 'transaction:/apple/cart',
  225. }),
  226. });
  227. });
  228. it('hides cell actions when withStaticFilters is true', function () {
  229. const data = initializeData({
  230. query: 'event.type:transaction transaction:/api*',
  231. });
  232. render(
  233. <WrappedComponent
  234. data={data}
  235. eventView={mockEventView(data)}
  236. setError={jest.fn()}
  237. summaryConditions=""
  238. projects={data.projects}
  239. withStaticFilters
  240. />
  241. );
  242. const cellActionContainers = screen.queryByTestId('cell-action-container');
  243. expect(cellActionContainers).not.toBeInTheDocument();
  244. });
  245. it('shows unparameterized tooltip when project only recently sent events', async function () {
  246. const projects = [
  247. TestStubs.Project({id: '1', slug: '1'}),
  248. TestStubs.Project({id: '2', slug: '2'}),
  249. TestStubs.Project({id: '3', slug: '3', firstEvent: new Date().toISOString()}),
  250. ];
  251. const data = initializeData({
  252. query: 'event.type:transaction transaction:/api*',
  253. projects,
  254. });
  255. ProjectsStore.loadInitialData(data.organization.projects);
  256. render(
  257. <WrappedComponent
  258. data={data}
  259. eventView={mockEventView(data)}
  260. setError={jest.fn()}
  261. summaryConditions=""
  262. projects={data.projects}
  263. />
  264. );
  265. const indicatorContainer = await screen.findByTestId('unparameterized-indicator');
  266. expect(indicatorContainer).toBeInTheDocument();
  267. });
  268. it('does not show unparameterized tooltip when project only recently sent events', async function () {
  269. const projects = [
  270. TestStubs.Project({id: '1', slug: '1'}),
  271. TestStubs.Project({id: '2', slug: '2'}),
  272. TestStubs.Project({
  273. id: '3',
  274. slug: '3',
  275. firstEvent: new Date(+new Date() - 25920e5).toISOString(),
  276. }),
  277. ];
  278. const data = initializeData({
  279. query: 'event.type:transaction transaction:/api*',
  280. projects,
  281. });
  282. ProjectsStore.loadInitialData(data.organization.projects);
  283. render(
  284. <WrappedComponent
  285. data={data}
  286. eventView={mockEventView(data)}
  287. setError={jest.fn()}
  288. summaryConditions=""
  289. projects={data.projects}
  290. />
  291. );
  292. await screen.findByTestId('grid-editable');
  293. const indicatorContainer = screen.queryByTestId('unparameterized-indicator');
  294. expect(indicatorContainer).not.toBeInTheDocument();
  295. });
  296. it('sends MEP param when setting enabled', function () {
  297. const data = initializeData(
  298. {
  299. query: 'event.type:transaction transaction:/api*',
  300. },
  301. ['performance-use-metrics']
  302. );
  303. render(
  304. <WrappedComponent
  305. data={data}
  306. eventView={mockEventView(data)}
  307. setError={jest.fn()}
  308. summaryConditions=""
  309. projects={data.projects}
  310. isMEPEnabled
  311. />
  312. );
  313. expect(eventsMock).toHaveBeenCalledTimes(1);
  314. expect(eventsMock).toHaveBeenNthCalledWith(
  315. 1,
  316. expect.anything(),
  317. expect.objectContaining({
  318. query: expect.objectContaining({
  319. environment: [],
  320. field: [
  321. 'team_key_transaction',
  322. 'transaction',
  323. 'project',
  324. 'tpm()',
  325. 'p50()',
  326. 'p95()',
  327. 'failure_rate()',
  328. 'apdex()',
  329. 'count_unique(user)',
  330. 'count_miserable(user)',
  331. 'user_misery()',
  332. ],
  333. dataset: 'metrics',
  334. per_page: 50,
  335. project: ['1', '2'],
  336. query: 'event.type:transaction transaction:/api*',
  337. referrer: 'api.performance.landing-table',
  338. sort: '-team_key_transaction',
  339. statsPeriod: '14d',
  340. }),
  341. })
  342. );
  343. });
  344. });
  345. });