databaseLandingPage.spec.tsx 6.1 KB

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