traceTimeline.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import {EventFixture} from 'sentry-fixture/event';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import {trackAnalytics} from 'sentry/utils/analytics';
  7. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  8. import {TraceTimeline} from './traceTimeline';
  9. import type {TraceEventResponse} from './useTraceTimelineEvents';
  10. jest.mock('sentry/utils/routeAnalytics/useRouteAnalyticsParams');
  11. jest.mock('sentry/utils/analytics');
  12. describe('TraceTimeline', () => {
  13. // Paid plans have global-views enabled
  14. // Include project: -1 in all matchQuery calls to ensure we are looking at all projects
  15. const organization = OrganizationFixture({
  16. features: ['global-views'],
  17. });
  18. // This creates the ApiException event
  19. const event = EventFixture({
  20. // This is used to determine the presence of seconds
  21. dateCreated: '2024-01-24T09:09:03+00:00',
  22. contexts: {
  23. trace: {
  24. // This is used to determine if we should attempt
  25. // to render the trace timeline
  26. trace_id: '123',
  27. },
  28. },
  29. });
  30. const project = ProjectFixture();
  31. const emptyBody: TraceEventResponse = {data: [], meta: {fields: {}, units: {}}};
  32. const issuePlatformBody: TraceEventResponse = {
  33. data: [
  34. {
  35. // In issuePlatform, we store the subtitle within the message
  36. message: '/api/slow/ Slow DB Query SELECT "sentry_monitorcheckin"."monitor_id"',
  37. timestamp: '2024-01-24T09:09:03+00:00',
  38. 'issue.id': 1000,
  39. project: project.slug,
  40. 'project.name': project.name,
  41. title: 'Slow DB Query',
  42. id: 'abc',
  43. transaction: '/api/slow/',
  44. },
  45. ],
  46. meta: {fields: {}, units: {}},
  47. };
  48. const discoverBody: TraceEventResponse = {
  49. data: [
  50. {
  51. message: 'This is the subtitle of the issue',
  52. timestamp: '2024-01-23T22:11:42+00:00',
  53. 'issue.id': event['issue.id'],
  54. project: project.slug,
  55. 'project.name': project.name,
  56. title: event.title,
  57. id: event.id,
  58. transaction: 'important.task',
  59. 'event.type': event.type,
  60. 'stack.function': ['important.task', 'task.run'],
  61. },
  62. ],
  63. meta: {fields: {}, units: {}},
  64. };
  65. beforeEach(() => {
  66. ProjectsStore.loadInitialData([project]);
  67. jest.clearAllMocks();
  68. });
  69. it('renders items and highlights the current event', async () => {
  70. MockApiClient.addMockResponse({
  71. url: `/organizations/${organization.slug}/events/`,
  72. body: issuePlatformBody,
  73. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  74. });
  75. MockApiClient.addMockResponse({
  76. url: `/organizations/${organization.slug}/events/`,
  77. body: discoverBody,
  78. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  79. });
  80. render(<TraceTimeline event={event} />, {organization});
  81. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  82. await userEvent.hover(screen.getByTestId('trace-timeline-tooltip-1'));
  83. expect(await screen.findByText('You are here')).toBeInTheDocument();
  84. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  85. has_related_trace_issue: false,
  86. trace_timeline_status: 'shown',
  87. trace_timeline_two_issues: true,
  88. });
  89. });
  90. it('displays nothing if the only event is the current event', async () => {
  91. MockApiClient.addMockResponse({
  92. url: `/organizations/${organization.slug}/events/`,
  93. body: emptyBody,
  94. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  95. });
  96. MockApiClient.addMockResponse({
  97. url: `/organizations/${organization.slug}/events/`,
  98. body: discoverBody,
  99. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  100. });
  101. const {container} = render(<TraceTimeline event={event} />, {organization});
  102. await waitFor(() =>
  103. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  104. has_related_trace_issue: false,
  105. trace_timeline_status: 'empty',
  106. trace_timeline_two_issues: false,
  107. })
  108. );
  109. expect(container).toBeEmptyDOMElement();
  110. });
  111. it('displays nothing if there are no events', async () => {
  112. MockApiClient.addMockResponse({
  113. url: `/organizations/${organization.slug}/events/`,
  114. body: emptyBody,
  115. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  116. });
  117. MockApiClient.addMockResponse({
  118. url: `/organizations/${organization.slug}/events/`,
  119. body: emptyBody,
  120. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  121. });
  122. const {container} = render(<TraceTimeline event={event} />, {organization});
  123. await waitFor(() =>
  124. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  125. has_related_trace_issue: false,
  126. trace_timeline_status: 'empty',
  127. trace_timeline_two_issues: false,
  128. })
  129. );
  130. expect(container).toBeEmptyDOMElement();
  131. });
  132. it('shows seconds for very short timelines', async () => {
  133. MockApiClient.addMockResponse({
  134. url: `/organizations/${organization.slug}/events/`,
  135. body: issuePlatformBody,
  136. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  137. });
  138. MockApiClient.addMockResponse({
  139. url: `/organizations/${organization.slug}/events/`,
  140. body: emptyBody,
  141. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  142. });
  143. render(<TraceTimeline event={event} />, {organization});
  144. // Checking for the presence of seconds
  145. expect(await screen.findAllByText(/\d{1,2}:\d{2}:\d{2} (AM|PM)/)).toHaveLength(5);
  146. });
  147. it('adds the current event if not in the api response', async () => {
  148. MockApiClient.addMockResponse({
  149. url: `/organizations/${organization.slug}/events/`,
  150. body: issuePlatformBody,
  151. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  152. });
  153. MockApiClient.addMockResponse({
  154. url: `/organizations/${organization.slug}/events/`,
  155. body: emptyBody,
  156. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  157. });
  158. render(<TraceTimeline event={event} />, {organization});
  159. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  160. });
  161. it('skips the timeline and shows related issues (2 issues)', async () => {
  162. MockApiClient.addMockResponse({
  163. url: `/organizations/${organization.slug}/events/`,
  164. body: issuePlatformBody,
  165. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  166. });
  167. MockApiClient.addMockResponse({
  168. url: `/organizations/${organization.slug}/events/`,
  169. body: emptyBody,
  170. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  171. });
  172. // I believe the call to projects is to determine what projects a user belongs to
  173. MockApiClient.addMockResponse({
  174. url: `/organizations/${organization.slug}/projects/`,
  175. body: [],
  176. });
  177. render(<TraceTimeline event={event} />, {
  178. organization: OrganizationFixture({
  179. features: ['related-issues-issue-details-page', 'global-views'],
  180. }),
  181. });
  182. // Instead of a timeline, we should see the other related issue
  183. expect(await screen.findByText('Slow DB Query')).toBeInTheDocument();
  184. expect(
  185. await screen.findByText('SELECT "sentry_monitorcheckin"."monitor_id"')
  186. ).toBeInTheDocument();
  187. expect(screen.queryByLabelText('Current Event')).not.toBeInTheDocument();
  188. // Test analytics
  189. await userEvent.click(await screen.findByText('Slow DB Query'));
  190. expect(useRouteAnalyticsParams).toHaveBeenLastCalledWith({
  191. has_related_trace_issue: true,
  192. trace_timeline_status: 'empty',
  193. // Even though the trace timeline has not been rendered, we still
  194. // track that it would have been the two issues case that related issues is replacing
  195. trace_timeline_two_issues: true,
  196. });
  197. expect(trackAnalytics).toHaveBeenCalledTimes(1);
  198. expect(trackAnalytics).toHaveBeenCalledWith(
  199. 'issue_details.related_trace_issue.trace_issue_clicked',
  200. {
  201. group_id: issuePlatformBody.data[0]['issue.id'],
  202. organization: OrganizationFixture({
  203. features: ['related-issues-issue-details-page', 'global-views'],
  204. }),
  205. }
  206. );
  207. });
  208. it('skips the timeline and shows NO related issues (only 1 issue)', async () => {
  209. MockApiClient.addMockResponse({
  210. url: `/organizations/${organization.slug}/events/`,
  211. body: emptyBody,
  212. match: [MockApiClient.matchQuery({dataset: 'issuePlatform', project: -1})],
  213. });
  214. MockApiClient.addMockResponse({
  215. url: `/organizations/${organization.slug}/events/`,
  216. // Only 1 issue
  217. body: discoverBody,
  218. match: [MockApiClient.matchQuery({dataset: 'discover', project: -1})],
  219. });
  220. // I believe the call to projects is to determine what projects a user belongs to
  221. MockApiClient.addMockResponse({
  222. url: `/organizations/${organization.slug}/projects/`,
  223. body: [],
  224. });
  225. render(<TraceTimeline event={event} />, {
  226. organization: OrganizationFixture({
  227. features: ['related-issues-issue-details-page', 'global-views'],
  228. }),
  229. });
  230. // We do not display any related issues because we only have 1 issue
  231. expect(await screen.queryByText('Slow DB Query')).not.toBeInTheDocument();
  232. expect(
  233. await screen.queryByText('AttributeError: Something Failed')
  234. ).not.toBeInTheDocument();
  235. // We do not display the timeline because we only have 1 event
  236. expect(await screen.queryByLabelText('Current Event')).not.toBeInTheDocument();
  237. expect(useRouteAnalyticsParams).toHaveBeenCalledWith({
  238. has_related_trace_issue: false,
  239. trace_timeline_status: 'empty',
  240. trace_timeline_two_issues: false,
  241. });
  242. });
  243. it('works for plans with no global-views feature', async () => {
  244. MockApiClient.addMockResponse({
  245. url: `/organizations/${organization.slug}/events/`,
  246. body: issuePlatformBody,
  247. match: [
  248. MockApiClient.matchQuery({
  249. dataset: 'issuePlatform',
  250. // Since we don't have global-views, we only look at the current project
  251. project: event.projectID,
  252. }),
  253. ],
  254. });
  255. MockApiClient.addMockResponse({
  256. url: `/organizations/${organization.slug}/events/`,
  257. body: emptyBody,
  258. match: [
  259. MockApiClient.matchQuery({
  260. dataset: 'discover',
  261. // Since we don't have global-views, we only look at the current project
  262. project: event.projectID,
  263. }),
  264. ],
  265. });
  266. render(<TraceTimeline event={event} />, {
  267. organization: OrganizationFixture({
  268. features: [], // No global-views feature
  269. }),
  270. });
  271. expect(await screen.findByLabelText('Current Event')).toBeInTheDocument();
  272. });
  273. });