eventsTable.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, waitForElementToBeRemoved} from 'sentry-test/reactTestingLibrary';
  3. import {t} from 'sentry/locale';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {
  7. SPAN_OP_BREAKDOWN_FIELDS,
  8. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  9. } from 'sentry/utils/discover/fields';
  10. import EventsTable from 'sentry/views/performance/transactionSummary/transactionEvents/eventsTable';
  11. type Data = {
  12. features?: string[];
  13. };
  14. export const MOCK_EVENTS_TABLE_DATA = [
  15. {
  16. id: 'deadbeef',
  17. 'user.display': 'uhoh@example.com',
  18. 'transaction.duration': 400,
  19. 'project.id': 1,
  20. timestamp: '2020-05-21T15:31:18+00:00',
  21. trace: '1234',
  22. 'span_ops_breakdown.relative': '',
  23. 'spans.browser': 100,
  24. 'spans.db': 30,
  25. 'spans.http': 170,
  26. 'spans.resource': 100,
  27. 'spans.total.time': 400,
  28. },
  29. {
  30. id: 'moredeadbeef',
  31. 'user.display': 'moreuhoh@example.com',
  32. 'transaction.duration': 600,
  33. 'project.id': 1,
  34. timestamp: '2020-05-22T15:31:18+00:00',
  35. trace: '4321',
  36. 'span_ops_breakdown.relative': '',
  37. 'spans.browser': 100,
  38. 'spans.db': 300,
  39. 'spans.http': 100,
  40. 'spans.resource': 100,
  41. 'spans.total.time': 600,
  42. },
  43. ];
  44. export const EVENTS_TABLE_RESPONSE_FIELDS = [
  45. 'id',
  46. 'user.display',
  47. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  48. 'transaction.duration',
  49. 'trace',
  50. 'timestamp',
  51. 'spans.total.time',
  52. ...SPAN_OP_BREAKDOWN_FIELDS,
  53. ];
  54. function initializeData({features: additionalFeatures = []}: Data = {}) {
  55. const features = ['discover-basic', 'performance-view', ...additionalFeatures];
  56. const organization = TestStubs.Organization({
  57. features,
  58. projects: [TestStubs.Project()],
  59. });
  60. const initialData = initializeOrg({
  61. organization,
  62. router: {
  63. location: {
  64. query: {
  65. transaction: '/performance',
  66. project: '1',
  67. transactionCursor: '1:0:0',
  68. },
  69. },
  70. },
  71. projects: [],
  72. });
  73. ProjectsStore.loadInitialData(initialData.organization.projects);
  74. return initialData;
  75. }
  76. describe('Performance GridEditable Table', function () {
  77. const transactionsListTitles = [
  78. t('event id'),
  79. t('user'),
  80. t('operation duration'),
  81. t('total duration'),
  82. t('trace id'),
  83. t('timestamp'),
  84. ];
  85. let fields = EVENTS_TABLE_RESPONSE_FIELDS;
  86. const organization = TestStubs.Organization();
  87. const transactionName = 'transactionName';
  88. let data;
  89. const query =
  90. 'transaction.duration:<15m event.type:transaction transaction:/api/0/organizations/{organization_slug}/events/';
  91. beforeEach(function () {
  92. MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/projects/',
  94. body: [],
  95. });
  96. MockApiClient.addMockResponse({
  97. url: '/prompts-activity/',
  98. body: {},
  99. });
  100. MockApiClient.addMockResponse({
  101. url: '/organizations/org-slug/sdk-updates/',
  102. body: [],
  103. });
  104. fields = EVENTS_TABLE_RESPONSE_FIELDS;
  105. data = MOCK_EVENTS_TABLE_DATA;
  106. // Total events count response
  107. MockApiClient.addMockResponse({
  108. url: '/organizations/org-slug/events/',
  109. headers: {
  110. Link:
  111. '<http://localhost/api/0/organizations/org-slug/events/?cursor=2:0:0>; rel="next"; results="true"; cursor="2:0:0",' +
  112. '<http://localhost/api/0/organizations/org-slug/events/?cursor=1:0:0>; rel="previous"; results="false"; cursor="1:0:0"',
  113. },
  114. body: {
  115. meta: {
  116. fields: {
  117. 'count()': 'integer',
  118. },
  119. },
  120. data: [{'count()': 100}],
  121. },
  122. match: [
  123. (_url, options) => {
  124. return options.query?.field?.includes('count()');
  125. },
  126. ],
  127. });
  128. // Transaction list response
  129. MockApiClient.addMockResponse({
  130. url: '/organizations/org-slug/events/',
  131. headers: {
  132. Link:
  133. '<http://localhost/api/0/organizations/org-slug/events/?cursor=2:0:0>; rel="next"; results="true"; cursor="2:0:0",' +
  134. '<http://localhost/api/0/organizations/org-slug/events/?cursor=1:0:0>; rel="previous"; results="false"; cursor="1:0:0"',
  135. },
  136. body: {
  137. meta: {
  138. fields: {
  139. id: 'string',
  140. 'user.display': 'string',
  141. 'transaction.duration': 'duration',
  142. 'project.id': 'integer',
  143. timestamp: 'date',
  144. },
  145. },
  146. data,
  147. },
  148. match: [
  149. (_url, options) => {
  150. return options.query?.field?.includes('user.display');
  151. },
  152. ],
  153. });
  154. });
  155. afterEach(function () {
  156. MockApiClient.clearMockResponses();
  157. ProjectsStore.reset();
  158. jest.clearAllMocks();
  159. });
  160. it('renders ops breakdown bar when querying for span_ops_breakdown.relative', async function () {
  161. const initialData = initializeData();
  162. const eventView = EventView.fromNewQueryWithLocation(
  163. {
  164. id: undefined,
  165. version: 2,
  166. name: 'transactionName',
  167. fields,
  168. query,
  169. projects: [],
  170. orderby: '-timestamp',
  171. },
  172. initialData.router.location
  173. );
  174. render(
  175. <EventsTable
  176. eventView={eventView}
  177. organization={organization}
  178. routes={initialData.router.routes}
  179. location={initialData.router.location}
  180. setError={() => {}}
  181. columnTitles={transactionsListTitles}
  182. transactionName={transactionName}
  183. />,
  184. {context: initialData.routerContext}
  185. );
  186. expect(await screen.findAllByTestId('relative-ops-breakdown')).toHaveLength(2);
  187. expect(screen.getAllByRole('columnheader')).toHaveLength(6);
  188. expect(screen.getByText('operation duration')).toBeInTheDocument();
  189. expect(screen.queryByTestId('grid-head-cell-static')).not.toBeInTheDocument();
  190. });
  191. it('renders basic columns without ops breakdown when not querying for span_ops_breakdown.relative', function () {
  192. const initialData = initializeData();
  193. fields = [
  194. 'id',
  195. 'user.display',
  196. 'transaction.duration',
  197. 'trace',
  198. 'timestamp',
  199. 'spans.http',
  200. ];
  201. data.forEach(result => {
  202. delete result['span_ops_breakdown.relative'];
  203. delete result['spans.resource'];
  204. delete result['spans.browser'];
  205. delete result['spans.db'];
  206. delete result['spans.total.time'];
  207. });
  208. const eventView = EventView.fromNewQueryWithLocation(
  209. {
  210. id: undefined,
  211. version: 2,
  212. name: 'transactionName',
  213. fields,
  214. query,
  215. projects: [],
  216. orderby: '-timestamp',
  217. },
  218. initialData.router.location
  219. );
  220. const {container} = render(
  221. <EventsTable
  222. eventView={eventView}
  223. organization={organization}
  224. routes={initialData.router.routes}
  225. location={initialData.router.location}
  226. setError={() => {}}
  227. columnTitles={transactionsListTitles}
  228. transactionName={transactionName}
  229. />,
  230. {context: initialData.routerContext}
  231. );
  232. expect(screen.getAllByRole('columnheader')).toHaveLength(6);
  233. expect(screen.queryByText(SPAN_OP_RELATIVE_BREAKDOWN_FIELD)).not.toBeInTheDocument();
  234. expect(screen.queryByTestId('relative-ops-breakdown')).not.toBeInTheDocument();
  235. expect(screen.queryByTestId('grid-head-cell-static')).not.toBeInTheDocument();
  236. expect(container).toSnapshot();
  237. });
  238. it('renders event id and trace id url', async function () {
  239. const initialData = initializeData();
  240. const eventView = EventView.fromNewQueryWithLocation(
  241. {
  242. id: undefined,
  243. version: 2,
  244. name: 'transactionName',
  245. fields,
  246. query,
  247. projects: [],
  248. orderby: '-timestamp',
  249. },
  250. initialData.router.location
  251. );
  252. render(
  253. <EventsTable
  254. eventView={eventView}
  255. organization={organization}
  256. routes={initialData.router.routes}
  257. location={initialData.router.location}
  258. setError={() => {}}
  259. columnTitles={transactionsListTitles}
  260. transactionName={transactionName}
  261. />,
  262. {context: initialData.routerContext}
  263. );
  264. expect(await screen.findByRole('link', {name: 'deadbeef'})).toHaveAttribute(
  265. 'href',
  266. '/organizations/org-slug/performance/undefined:deadbeef/?project=1&transaction=transactionName&transactionCursor=1%3A0%3A0'
  267. );
  268. expect(screen.getByRole('link', {name: '1234'})).toHaveAttribute(
  269. 'href',
  270. '/organizations/org-slug/performance/trace/1234/?'
  271. );
  272. });
  273. it('renders replay id', async function () {
  274. const initialData = initializeData();
  275. fields = [...fields, 'replayId'];
  276. data.forEach(result => {
  277. result.replayId = 'mock_replay_id';
  278. });
  279. const eventView = EventView.fromNewQueryWithLocation(
  280. {
  281. id: undefined,
  282. version: 2,
  283. name: 'transactionName',
  284. fields,
  285. query,
  286. projects: [],
  287. orderby: '-timestamp',
  288. },
  289. initialData.router.location
  290. );
  291. const {container} = render(
  292. <EventsTable
  293. eventView={eventView}
  294. organization={organization}
  295. routes={initialData.router.routes}
  296. location={initialData.router.location}
  297. setError={() => {}}
  298. columnTitles={transactionsListTitles}
  299. transactionName={transactionName}
  300. />,
  301. {context: initialData.routerContext}
  302. );
  303. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  304. expect(screen.getAllByRole('columnheader')).toHaveLength(7);
  305. expect(container).toSnapshot();
  306. });
  307. it('renders profile id', async function () {
  308. const initialData = initializeData();
  309. fields = [...fields, 'profile.id'];
  310. data.forEach(result => {
  311. result['profile.id'] = 'mock_profile_id';
  312. });
  313. const eventView = EventView.fromNewQueryWithLocation(
  314. {
  315. id: undefined,
  316. version: 2,
  317. name: 'transactionName',
  318. fields,
  319. query,
  320. projects: [],
  321. orderby: '-timestamp',
  322. },
  323. initialData.router.location
  324. );
  325. const {container} = render(
  326. <EventsTable
  327. eventView={eventView}
  328. organization={organization}
  329. routes={initialData.router.routes}
  330. location={initialData.router.location}
  331. setError={() => {}}
  332. columnTitles={transactionsListTitles}
  333. transactionName={transactionName}
  334. />,
  335. {context: initialData.routerContext}
  336. );
  337. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  338. expect(screen.getAllByRole('columnheader')).toHaveLength(7);
  339. expect(container).toSnapshot();
  340. });
  341. });