spanEvidencePreview.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. MockSpan,
  3. ProblemSpan,
  4. TransactionEventBuilder,
  5. } from 'sentry-test/performance/utils';
  6. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  7. import * as useApi from 'sentry/utils/useApi';
  8. import {SpanEvidencePreview} from './spanEvidencePreview';
  9. describe('SpanEvidencePreview', () => {
  10. beforeEach(() => {
  11. jest.useFakeTimers();
  12. jest.restoreAllMocks();
  13. });
  14. it('does not fetch before hover', () => {
  15. const api = new MockApiClient();
  16. jest.spyOn(useApi, 'default').mockReturnValue(api);
  17. const spy = jest.spyOn(api, 'requestPromise');
  18. render(
  19. <SpanEvidencePreview
  20. eventId="event-id"
  21. projectSlug="project-slug"
  22. groupId="group-id"
  23. >
  24. Hover me
  25. </SpanEvidencePreview>
  26. );
  27. jest.runAllTimers();
  28. expect(spy).not.toHaveBeenCalled();
  29. });
  30. it('fetches from event URL when event and project are provided', async () => {
  31. const mock = MockApiClient.addMockResponse({
  32. url: `/projects/org-slug/project-slug/events/event-id/`,
  33. body: null,
  34. });
  35. render(
  36. <SpanEvidencePreview
  37. eventId="event-id"
  38. projectSlug="project-slug"
  39. groupId="group-id"
  40. >
  41. Hover me
  42. </SpanEvidencePreview>
  43. );
  44. userEvent.hover(screen.getByText('Hover me'));
  45. await waitFor(() => {
  46. expect(mock).toHaveBeenCalled();
  47. });
  48. });
  49. it('fetches from group URL when only group ID is provided', async () => {
  50. const mock = MockApiClient.addMockResponse({
  51. url: `/issues/group-id/events/latest/`,
  52. body: null,
  53. });
  54. render(<SpanEvidencePreview groupId="group-id">Hover me</SpanEvidencePreview>);
  55. userEvent.hover(screen.getByText('Hover me'));
  56. await waitFor(() => {
  57. expect(mock).toHaveBeenCalled();
  58. });
  59. });
  60. it('shows error when request fails', async () => {
  61. const api = new MockApiClient();
  62. jest.spyOn(useApi, 'default').mockReturnValue(api);
  63. jest.spyOn(api, 'requestPromise').mockRejectedValue(new Error());
  64. render(<SpanEvidencePreview groupId="group-id">Hover me</SpanEvidencePreview>);
  65. userEvent.hover(screen.getByText('Hover me'));
  66. await screen.findByText('Failed to load preview');
  67. });
  68. it('renders the span evidence correctly when request succeeds', async () => {
  69. const event = new TransactionEventBuilder()
  70. .addSpan(
  71. new MockSpan({
  72. startTimestamp: 0,
  73. endTimestamp: 100,
  74. op: 'http',
  75. description: 'do a thing',
  76. })
  77. )
  78. .addSpan(
  79. new MockSpan({
  80. startTimestamp: 100,
  81. endTimestamp: 200,
  82. op: 'db',
  83. description: 'SELECT col FROM table',
  84. })
  85. )
  86. .addSpan(
  87. new MockSpan({
  88. startTimestamp: 200,
  89. endTimestamp: 300,
  90. op: 'db',
  91. description: 'SELECT col2 FROM table',
  92. })
  93. )
  94. .addSpan(
  95. new MockSpan({
  96. startTimestamp: 200,
  97. endTimestamp: 300,
  98. op: 'db',
  99. description: 'SELECT col3 FROM table',
  100. })
  101. )
  102. .addSpan(
  103. new MockSpan({
  104. startTimestamp: 300,
  105. endTimestamp: 600,
  106. op: 'db',
  107. description: 'connect',
  108. problemSpan: ProblemSpan.PARENT,
  109. }).addChild(
  110. {
  111. startTimestamp: 300,
  112. endTimestamp: 600,
  113. op: 'db',
  114. description: 'group me',
  115. problemSpan: ProblemSpan.OFFENDER,
  116. },
  117. 9
  118. )
  119. )
  120. .getEvent();
  121. MockApiClient.addMockResponse({
  122. url: `/issues/group-id/events/latest/`,
  123. body: event,
  124. });
  125. render(<SpanEvidencePreview groupId="group-id">Hover me</SpanEvidencePreview>);
  126. userEvent.hover(screen.getByText('Hover me'));
  127. await screen.findByTestId('span-evidence-preview-body');
  128. expect(screen.getByRole('cell', {name: 'Transaction'})).toBeInTheDocument();
  129. expect(screen.getByRole('cell', {name: event.title})).toBeInTheDocument();
  130. expect(screen.getByRole('cell', {name: 'Parent Span'})).toBeInTheDocument();
  131. expect(screen.getByRole('cell', {name: 'db - connect'})).toBeInTheDocument();
  132. expect(screen.getByRole('cell', {name: 'Repeating Span'})).toBeInTheDocument();
  133. expect(screen.getByRole('cell', {name: 'db - group me'})).toBeInTheDocument();
  134. });
  135. });