databaseLandingPage.spec.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {render, screen, waitForElementToBeRemoved} from 'sentry-test/reactTestingLibrary';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import usePageFilters from 'sentry/utils/usePageFilters';
  5. import {DatabaseLandingPage} from 'sentry/views/performance/database/databaseLandingPage';
  6. jest.mock('sentry/utils/useLocation');
  7. jest.mock('sentry/utils/usePageFilters');
  8. describe('DatabaseLandingPage', function () {
  9. const organization = OrganizationFixture();
  10. let spanListRequestMock, spanChartsRequestMock;
  11. jest.mocked(usePageFilters).mockReturnValue({
  12. isReady: true,
  13. desyncedFilters: new Set(),
  14. pinnedFilters: new Set(),
  15. shouldPersist: true,
  16. selection: {
  17. datetime: {
  18. period: '10d',
  19. start: null,
  20. end: null,
  21. utc: false,
  22. },
  23. environments: [],
  24. projects: [],
  25. },
  26. });
  27. jest.mocked(useLocation).mockReturnValue({
  28. pathname: '',
  29. search: '',
  30. query: {statsPeriod: '10d'},
  31. hash: '',
  32. state: undefined,
  33. action: 'PUSH',
  34. key: '',
  35. });
  36. beforeEach(function () {
  37. MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/sdk-updates/',
  39. body: [],
  40. });
  41. MockApiClient.addMockResponse({
  42. url: `/organizations/${organization.slug}/events/`,
  43. method: 'GET',
  44. match: [MockApiClient.matchQuery({referrer: 'span-metrics'})],
  45. body: {
  46. data: [],
  47. },
  48. });
  49. MockApiClient.addMockResponse({
  50. url: `/organizations/${organization.slug}/events/`,
  51. method: 'GET',
  52. match: [MockApiClient.matchQuery({referrer: 'api.starfish.get-span-actions'})],
  53. body: {
  54. data: [{'span.action': 'SELECT', count: 1}],
  55. },
  56. });
  57. MockApiClient.addMockResponse({
  58. url: `/organizations/${organization.slug}/events/`,
  59. method: 'GET',
  60. match: [MockApiClient.matchQuery({referrer: 'api.starfish.get-span-domains'})],
  61. body: {
  62. data: [{'span.domain': ['sentry_users'], count: 1}],
  63. },
  64. });
  65. spanListRequestMock = MockApiClient.addMockResponse({
  66. url: `/organizations/${organization.slug}/events/`,
  67. method: 'GET',
  68. match: [MockApiClient.matchQuery({referrer: 'api.starfish.use-span-list'})],
  69. body: {
  70. data: [
  71. {
  72. 'span.group': '271536360c0b6f89',
  73. 'span.description': 'SELECT * FROM users',
  74. },
  75. {
  76. 'span.group': '360c0b6f89271536',
  77. 'span.description': 'SELECT * FROM organizations',
  78. },
  79. ],
  80. },
  81. });
  82. spanChartsRequestMock = MockApiClient.addMockResponse({
  83. url: `/organizations/${organization.slug}/events-stats/`,
  84. method: 'GET',
  85. body: {
  86. 'spm()': {
  87. data: [
  88. [1699907700, [{count: 7810.2}]],
  89. [1699908000, [{count: 1216.8}]],
  90. ],
  91. },
  92. },
  93. });
  94. });
  95. afterAll(function () {
  96. jest.resetAllMocks();
  97. });
  98. it('fetches module data', async function () {
  99. jest.spyOn(console, 'error').mockImplementation(jest.fn()); // This silences pointless unique key errors that React throws because of the tokenized query descriptions
  100. render(<DatabaseLandingPage />, {organization});
  101. expect(spanChartsRequestMock).toHaveBeenNthCalledWith(
  102. 1,
  103. `/organizations/${organization.slug}/events-stats/`,
  104. expect.objectContaining({
  105. method: 'GET',
  106. query: {
  107. cursor: undefined,
  108. dataset: 'spansMetrics',
  109. environment: [],
  110. excludeOther: 0,
  111. field: [],
  112. interval: '30m',
  113. orderby: undefined,
  114. partial: 1,
  115. per_page: 50,
  116. project: [],
  117. query: 'span.module:db has:span.description',
  118. referrer: 'api.starfish.span-landing-page-metrics-chart',
  119. statsPeriod: '10d',
  120. topEvents: undefined,
  121. yAxis: 'spm()',
  122. },
  123. })
  124. );
  125. expect(spanChartsRequestMock).toHaveBeenNthCalledWith(
  126. 2,
  127. `/organizations/${organization.slug}/events-stats/`,
  128. expect.objectContaining({
  129. method: 'GET',
  130. query: {
  131. cursor: undefined,
  132. dataset: 'spansMetrics',
  133. environment: [],
  134. excludeOther: 0,
  135. field: [],
  136. interval: '30m',
  137. orderby: undefined,
  138. partial: 1,
  139. per_page: 50,
  140. project: [],
  141. query: 'span.module:db has:span.description',
  142. referrer: 'api.starfish.span-landing-page-metrics-chart',
  143. statsPeriod: '10d',
  144. topEvents: undefined,
  145. yAxis: 'avg(span.self_time)',
  146. },
  147. })
  148. );
  149. expect(spanListRequestMock).toHaveBeenCalledWith(
  150. `/organizations/${organization.slug}/events/`,
  151. expect.objectContaining({
  152. method: 'GET',
  153. query: {
  154. dataset: 'spansMetrics',
  155. environment: [],
  156. field: [
  157. 'project.id',
  158. 'span.group',
  159. 'span.description',
  160. 'spm()',
  161. 'avg(span.self_time)',
  162. 'sum(span.self_time)',
  163. 'time_spent_percentage()',
  164. ],
  165. per_page: 25,
  166. project: [],
  167. query: 'span.module:db has:span.description',
  168. referrer: 'api.starfish.use-span-list',
  169. sort: '-time_spent_percentage()',
  170. statsPeriod: '10d',
  171. },
  172. })
  173. );
  174. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  175. });
  176. it('renders a list of queries', async function () {
  177. // eslint-disable-next-line no-console
  178. jest.spyOn(console, 'error').mockImplementation(jest.fn()); // This silences pointless unique key errors that React throws because of the tokenized query descriptions
  179. render(<DatabaseLandingPage />, {organization});
  180. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-indicator'));
  181. expect(screen.getByRole('cell', {name: 'SELECT * FROM users'})).toBeInTheDocument();
  182. expect(
  183. screen.getByRole('cell', {name: 'SELECT * FROM organizations'})
  184. ).toBeInTheDocument();
  185. });
  186. });