table.spec.tsx 11 KB

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