table.spec.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. await userEvent.hover(cellActionContainers[8]);
  191. const cellActions = await screen.findByTestId('cell-action');
  192. expect(cellActions).toBeInTheDocument();
  193. await userEvent.click(cellActions);
  194. expect(await screen.findByTestId('add-to-filter')).toBeInTheDocument();
  195. expect(screen.getByTestId('exclude-from-filter')).toBeInTheDocument();
  196. await userEvent.hover(cellActionContainers[0]); // Transaction name
  197. const transactionCellActions = await screen.findAllByTestId('cell-action');
  198. expect(transactionCellActions[0]).toBeInTheDocument();
  199. await userEvent.click(transactionCellActions[0]);
  200. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  201. await userEvent.click(screen.getByTestId('add-to-filter'));
  202. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  203. expect(browserHistory.push).toHaveBeenNthCalledWith(1, {
  204. pathname: undefined,
  205. query: expect.objectContaining({
  206. query: 'transaction:/apple/cart',
  207. }),
  208. });
  209. });
  210. it('hides cell actions when withStaticFilters is true', function () {
  211. const data = initializeData({
  212. query: 'event.type:transaction transaction:/api*',
  213. });
  214. render(
  215. <WrappedComponent
  216. data={data}
  217. eventView={mockEventView(data)}
  218. setError={jest.fn()}
  219. summaryConditions=""
  220. projects={data.projects}
  221. withStaticFilters
  222. />
  223. );
  224. const cellActionContainers = screen.queryByTestId('cell-action-container');
  225. expect(cellActionContainers).not.toBeInTheDocument();
  226. });
  227. it('sends MEP param when setting enabled', function () {
  228. const data = initializeData(
  229. {
  230. query: 'event.type:transaction transaction:/api*',
  231. },
  232. ['performance-use-metrics']
  233. );
  234. render(
  235. <WrappedComponent
  236. data={data}
  237. eventView={mockEventView(data)}
  238. setError={jest.fn()}
  239. summaryConditions=""
  240. projects={data.projects}
  241. isMEPEnabled
  242. />
  243. );
  244. expect(eventsMock).toHaveBeenCalledTimes(1);
  245. expect(eventsMock).toHaveBeenNthCalledWith(
  246. 1,
  247. expect.anything(),
  248. expect.objectContaining({
  249. query: expect.objectContaining({
  250. environment: [],
  251. field: [
  252. 'team_key_transaction',
  253. 'transaction',
  254. 'project',
  255. 'tpm()',
  256. 'p50()',
  257. 'p95()',
  258. 'failure_rate()',
  259. 'apdex()',
  260. 'count_unique(user)',
  261. 'count_miserable(user)',
  262. 'user_misery()',
  263. ],
  264. dataset: 'metrics',
  265. per_page: 50,
  266. project: ['1', '2'],
  267. query: 'event.type:transaction transaction:/api*',
  268. referrer: 'api.performance.landing-table',
  269. sort: '-team_key_transaction',
  270. statsPeriod: '14d',
  271. }),
  272. })
  273. );
  274. });
  275. });
  276. });