quickTraceQuery.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {Fragment} from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  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', async function () {
  61. const wrapper = mountWithTheme(
  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. await tick();
  73. wrapper.update();
  74. expect(traceLiteMock).toHaveBeenCalledTimes(1);
  75. expect(traceFullMock).toHaveBeenCalledTimes(1);
  76. });
  77. it('doesnt fetches meta when not needed', async function () {
  78. const wrapper = mountWithTheme(
  79. <QuickTraceQuery
  80. withMeta={false}
  81. event={event}
  82. api={api}
  83. location={location}
  84. orgSlug="test-org"
  85. statsPeriod="24h"
  86. >
  87. {renderQuickTrace}
  88. </QuickTraceQuery>
  89. );
  90. await tick();
  91. wrapper.update();
  92. expect(traceLiteMock).toHaveBeenCalledTimes(1);
  93. expect(traceFullMock).toHaveBeenCalledTimes(1);
  94. expect(traceMetaMock).toHaveBeenCalledTimes(0);
  95. });
  96. it('uses lite results when it cannot find current event in full results', async function () {
  97. const wrapper = mountWithTheme(
  98. <QuickTraceQuery
  99. withMeta={false}
  100. event={event}
  101. api={api}
  102. location={location}
  103. orgSlug="test-org"
  104. statsPeriod="24h"
  105. >
  106. {renderQuickTrace}
  107. </QuickTraceQuery>
  108. );
  109. await tick();
  110. wrapper.update();
  111. expect(wrapper.find('div[data-test-id="type"]').text()).toEqual('partial');
  112. });
  113. it('uses full results when it finds current event', async function () {
  114. traceLiteMock = MockApiClient.addMockResponse({
  115. url: `/organizations/test-org/events-trace-light/0${traceId}/`,
  116. body: [],
  117. match: [MockApiClient.matchQuery({event_id: eventId})],
  118. });
  119. traceFullMock = MockApiClient.addMockResponse({
  120. url: `/organizations/test-org/events-trace/0${traceId}/`,
  121. body: [{event_id: eventId, children: []}],
  122. });
  123. traceMetaMock = MockApiClient.addMockResponse({
  124. url: `/organizations/test-org/events-trace-meta/0${traceId}/`,
  125. body: {
  126. projects: 4,
  127. transactions: 5,
  128. errors: 2,
  129. },
  130. });
  131. event.contexts.trace.trace_id = `0${traceId}`;
  132. const wrapper = mountWithTheme(
  133. <QuickTraceQuery
  134. withMeta={false}
  135. event={event}
  136. api={api}
  137. location={location}
  138. orgSlug="test-org"
  139. statsPeriod="24h"
  140. >
  141. {renderQuickTrace}
  142. </QuickTraceQuery>
  143. );
  144. await tick();
  145. wrapper.update();
  146. expect(wrapper.find('div[data-test-id="type"]').text()).toEqual('full');
  147. });
  148. });