table.spec.tsx 11 KB

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