quickTraceQuery.spec.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {Fragment} from 'react';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import {Client} from 'sentry/api';
  4. import QuickTraceQuery from 'sentry/utils/performance/quickTrace/quickTraceQuery';
  5. const traceId = 'abcdef1234567890';
  6. const eventId = '0987654321fedcba';
  7. function renderQuickTrace({isLoading, error, trace, type}) {
  8. if (isLoading) {
  9. return 'loading';
  10. }
  11. if (error !== null) {
  12. return error;
  13. }
  14. return (
  15. <Fragment>
  16. <div key="type" data-test-id="type">
  17. {type}
  18. </div>
  19. <div key="trace" data-test-id="trace">
  20. {trace.length}
  21. </div>
  22. </Fragment>
  23. );
  24. }
  25. describe('TraceLiteQuery', function () {
  26. let api, location, event, traceLiteMock, traceFullMock, traceMetaMock;
  27. beforeEach(function () {
  28. api = new Client();
  29. location = {
  30. pathname: '/',
  31. query: {},
  32. };
  33. event = {
  34. id: eventId,
  35. contexts: {
  36. trace: {
  37. trace_id: traceId,
  38. },
  39. },
  40. type: 'transaction',
  41. };
  42. traceLiteMock = MockApiClient.addMockResponse({
  43. url: `/organizations/test-org/events-trace-light/${traceId}/`,
  44. body: [],
  45. match: [MockApiClient.matchQuery({event_id: eventId})],
  46. });
  47. traceFullMock = MockApiClient.addMockResponse({
  48. url: `/organizations/test-org/events-trace/${traceId}/`,
  49. body: [],
  50. });
  51. traceMetaMock = MockApiClient.addMockResponse({
  52. url: `/organizations/test-org/events-trace-meta/${traceId}/`,
  53. body: {
  54. projects: 4,
  55. transactions: 5,
  56. errors: 2,
  57. },
  58. });
  59. });
  60. it('fetches data on mount and passes the event id', function () {
  61. render(
  62. <QuickTraceQuery
  63. event={event}
  64. api={api}
  65. location={location}
  66. orgSlug="test-org"
  67. statsPeriod="24h"
  68. >
  69. {renderQuickTrace}
  70. </QuickTraceQuery>
  71. );
  72. expect(traceLiteMock).toHaveBeenCalledTimes(1);
  73. expect(traceFullMock).toHaveBeenCalledTimes(1);
  74. });
  75. it('doesnt fetch meta when not needed', function () {
  76. render(
  77. <QuickTraceQuery
  78. withMeta={false}
  79. event={event}
  80. api={api}
  81. location={location}
  82. orgSlug="test-org"
  83. statsPeriod="24h"
  84. >
  85. {renderQuickTrace}
  86. </QuickTraceQuery>
  87. );
  88. expect(traceLiteMock).toHaveBeenCalledTimes(1);
  89. expect(traceFullMock).toHaveBeenCalledTimes(1);
  90. expect(traceMetaMock).toHaveBeenCalledTimes(0);
  91. });
  92. it('uses lite results when it cannot find current event in full results', async function () {
  93. render(
  94. <QuickTraceQuery
  95. withMeta={false}
  96. event={event}
  97. api={api}
  98. location={location}
  99. orgSlug="test-org"
  100. statsPeriod="24h"
  101. >
  102. {renderQuickTrace}
  103. </QuickTraceQuery>
  104. );
  105. expect(await screen.findByTestId('type')).toHaveTextContent('partial');
  106. });
  107. it('uses full results when it finds current event', async function () {
  108. traceLiteMock = MockApiClient.addMockResponse({
  109. url: `/organizations/test-org/events-trace-light/0${traceId}/`,
  110. body: [],
  111. match: [MockApiClient.matchQuery({event_id: eventId})],
  112. });
  113. traceFullMock = MockApiClient.addMockResponse({
  114. url: `/organizations/test-org/events-trace/0${traceId}/`,
  115. body: [{event_id: eventId, children: []}],
  116. });
  117. traceMetaMock = MockApiClient.addMockResponse({
  118. url: `/organizations/test-org/events-trace-meta/0${traceId}/`,
  119. body: {
  120. projects: 4,
  121. transactions: 5,
  122. errors: 2,
  123. },
  124. });
  125. event.contexts.trace.trace_id = `0${traceId}`;
  126. render(
  127. <QuickTraceQuery
  128. withMeta={false}
  129. event={event}
  130. api={api}
  131. location={location}
  132. orgSlug="test-org"
  133. statsPeriod="24h"
  134. >
  135. {renderQuickTrace}
  136. </QuickTraceQuery>
  137. );
  138. expect(await screen.findByTestId('type')).toHaveTextContent('full');
  139. });
  140. });