table.spec.tsx 11 KB

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