table.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import {ProjectFixture} from 'sentry-fixture/project';
  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 {browserHistory} from 'sentry/utils/browserHistory';
  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', async 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. expect(await screen.findByTestId('grid-editable')).toBeInTheDocument();
  244. const cellActionContainers = screen.queryByTestId('cell-action-container');
  245. expect(cellActionContainers).not.toBeInTheDocument();
  246. });
  247. it('shows unparameterized tooltip when project only recently sent events', async function () {
  248. const projects = [
  249. ProjectFixture({id: '1', slug: '1'}),
  250. ProjectFixture({id: '2', slug: '2'}),
  251. ProjectFixture({id: '3', slug: '3', firstEvent: new Date().toISOString()}),
  252. ];
  253. const data = initializeData({
  254. query: 'event.type:transaction transaction:/api*',
  255. projects,
  256. });
  257. ProjectsStore.loadInitialData(data.organization.projects);
  258. render(
  259. <WrappedComponent
  260. data={data}
  261. eventView={mockEventView(data)}
  262. setError={jest.fn()}
  263. summaryConditions=""
  264. projects={data.projects}
  265. />
  266. );
  267. const indicatorContainer = await screen.findByTestId('unparameterized-indicator');
  268. expect(indicatorContainer).toBeInTheDocument();
  269. });
  270. it('does not show unparameterized tooltip when project only recently sent events', async function () {
  271. const projects = [
  272. ProjectFixture({id: '1', slug: '1'}),
  273. ProjectFixture({id: '2', slug: '2'}),
  274. ProjectFixture({
  275. id: '3',
  276. slug: '3',
  277. firstEvent: new Date(+new Date() - 25920e5).toISOString(),
  278. }),
  279. ];
  280. const data = initializeData({
  281. query: 'event.type:transaction transaction:/api*',
  282. projects,
  283. });
  284. ProjectsStore.loadInitialData(data.organization.projects);
  285. render(
  286. <WrappedComponent
  287. data={data}
  288. eventView={mockEventView(data)}
  289. setError={jest.fn()}
  290. summaryConditions=""
  291. projects={data.projects}
  292. />
  293. );
  294. expect(await screen.findByTestId('grid-editable')).toBeInTheDocument();
  295. const indicatorContainer = screen.queryByTestId('unparameterized-indicator');
  296. expect(indicatorContainer).not.toBeInTheDocument();
  297. });
  298. it('sends MEP param when setting enabled', async function () {
  299. const data = initializeData(
  300. {
  301. query: 'event.type:transaction transaction:/api*',
  302. },
  303. ['performance-use-metrics']
  304. );
  305. render(
  306. <WrappedComponent
  307. data={data}
  308. eventView={mockEventView(data)}
  309. setError={jest.fn()}
  310. summaryConditions=""
  311. projects={data.projects}
  312. isMEPEnabled
  313. />
  314. );
  315. expect(await screen.findByTestId('grid-editable')).toBeInTheDocument();
  316. expect(eventsMock).toHaveBeenCalledTimes(1);
  317. expect(eventsMock).toHaveBeenNthCalledWith(
  318. 1,
  319. expect.anything(),
  320. expect.objectContaining({
  321. query: expect.objectContaining({
  322. environment: [],
  323. field: [
  324. 'team_key_transaction',
  325. 'transaction',
  326. 'project',
  327. 'tpm()',
  328. 'p50()',
  329. 'p95()',
  330. 'failure_rate()',
  331. 'apdex()',
  332. 'count_unique(user)',
  333. 'count_miserable(user)',
  334. 'user_misery()',
  335. ],
  336. dataset: 'metrics',
  337. per_page: 50,
  338. project: ['1', '2'],
  339. query: 'event.type:transaction transaction:/api*',
  340. referrer: 'api.performance.landing-table',
  341. sort: '-team_key_transaction',
  342. statsPeriod: '14d',
  343. }),
  344. })
  345. );
  346. });
  347. });
  348. });