table.spec.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. eventsMock = MockApiClient.addMockResponse({
  149. url: '/organizations/org-slug/events/',
  150. body: {
  151. meta: {fields: eventsMetaFieldsMock},
  152. data: eventsBodyMock,
  153. },
  154. });
  155. MockApiClient.addMockResponse({
  156. method: 'GET',
  157. url: `/organizations/org-slug/key-transactions-list/`,
  158. body: [],
  159. });
  160. });
  161. afterEach(function () {
  162. MockApiClient.clearMockResponses();
  163. });
  164. describe('with events', function () {
  165. it('renders correct cell actions without feature', async function () {
  166. const data = initializeData({
  167. query: 'event.type:transaction transaction:/api*',
  168. });
  169. ProjectsStore.loadInitialData(data.organization.projects);
  170. render(
  171. <WrappedComponent
  172. data={data}
  173. eventView={mockEventView(data)}
  174. setError={jest.fn()}
  175. summaryConditions=""
  176. projects={data.projects}
  177. />,
  178. {context: data.routerContext}
  179. );
  180. const rows = await screen.findAllByTestId('grid-body-row');
  181. const transactionCells = within(rows[0]).getAllByTestId('grid-body-cell');
  182. const transactionCell = transactionCells[1];
  183. const link = within(transactionCell).getByRole('link', {name: '/apple/cart'});
  184. expect(link).toHaveAttribute(
  185. 'href',
  186. '/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'
  187. );
  188. const cellActionContainers = screen.getAllByTestId('cell-action-container');
  189. expect(cellActionContainers).toHaveLength(18); // 9 cols x 2 rows
  190. const cellActionTriggers = screen.getAllByRole('button', {name: 'Actions'});
  191. expect(cellActionTriggers[8]).toBeInTheDocument();
  192. await userEvent.click(cellActionTriggers[8]);
  193. expect(
  194. screen.getByRole('menuitemradio', {name: 'Add to filter'})
  195. ).toBeInTheDocument();
  196. expect(
  197. screen.getByRole('menuitemradio', {name: 'Exclude from filter'})
  198. ).toBeInTheDocument();
  199. await userEvent.keyboard('{Escape}'); // Close actions menu
  200. const transactionCellTrigger = cellActionTriggers[0]; // Transaction name
  201. expect(transactionCellTrigger).toBeInTheDocument();
  202. await userEvent.click(transactionCellTrigger);
  203. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  204. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Add to filter'}));
  205. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  206. expect(browserHistory.push).toHaveBeenNthCalledWith(1, {
  207. pathname: undefined,
  208. query: expect.objectContaining({
  209. query: 'transaction:/apple/cart',
  210. }),
  211. });
  212. });
  213. it('hides cell actions when withStaticFilters is true', function () {
  214. const data = initializeData({
  215. query: 'event.type:transaction transaction:/api*',
  216. });
  217. render(
  218. <WrappedComponent
  219. data={data}
  220. eventView={mockEventView(data)}
  221. setError={jest.fn()}
  222. summaryConditions=""
  223. projects={data.projects}
  224. withStaticFilters
  225. />
  226. );
  227. const cellActionContainers = screen.queryByTestId('cell-action-container');
  228. expect(cellActionContainers).not.toBeInTheDocument();
  229. });
  230. it('sends MEP param when setting enabled', function () {
  231. const data = initializeData(
  232. {
  233. query: 'event.type:transaction transaction:/api*',
  234. },
  235. ['performance-use-metrics']
  236. );
  237. render(
  238. <WrappedComponent
  239. data={data}
  240. eventView={mockEventView(data)}
  241. setError={jest.fn()}
  242. summaryConditions=""
  243. projects={data.projects}
  244. isMEPEnabled
  245. />
  246. );
  247. expect(eventsMock).toHaveBeenCalledTimes(1);
  248. expect(eventsMock).toHaveBeenNthCalledWith(
  249. 1,
  250. expect.anything(),
  251. expect.objectContaining({
  252. query: expect.objectContaining({
  253. environment: [],
  254. field: [
  255. 'team_key_transaction',
  256. 'transaction',
  257. 'project',
  258. 'tpm()',
  259. 'p50()',
  260. 'p95()',
  261. 'failure_rate()',
  262. 'apdex()',
  263. 'count_unique(user)',
  264. 'count_miserable(user)',
  265. 'user_misery()',
  266. ],
  267. dataset: 'metrics',
  268. per_page: 50,
  269. project: ['1', '2'],
  270. query: 'event.type:transaction transaction:/api*',
  271. referrer: 'api.performance.landing-table',
  272. sort: '-team_key_transaction',
  273. statsPeriod: '14d',
  274. }),
  275. })
  276. );
  277. });
  278. });
  279. });