index.spec.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import localStorage from 'sentry/utils/localStorage';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import useProjects from 'sentry/utils/useProjects';
  11. import {useOnboardingProject} from 'sentry/views/performance/browser/webVitals/utils/useOnboardingProject';
  12. import PageloadModule from 'sentry/views/performance/screenload';
  13. import {PLATFORM_LOCAL_STORAGE_KEY} from 'sentry/views/performance/screenload/screens/platformSelector';
  14. jest.mock('sentry/utils/useOrganization');
  15. jest.mock('sentry/views/performance/browser/webVitals/utils/useOnboardingProject');
  16. jest.mock('sentry/utils/useLocation');
  17. jest.mock('sentry/utils/usePageFilters');
  18. jest.mock('sentry/utils/useProjects');
  19. describe('PageloadModule', function () {
  20. const project = ProjectFixture({platform: 'react-native'});
  21. const organization = OrganizationFixture({
  22. features: ['spans-first-ui'],
  23. projects: [project],
  24. });
  25. jest.mocked(useOrganization).mockReturnValue(organization);
  26. jest.mocked(useOnboardingProject).mockReturnValue(undefined);
  27. jest.mocked(useProjects).mockReturnValue({
  28. fetchError: null,
  29. fetching: false,
  30. hasMore: false,
  31. initiallyLoaded: false,
  32. onSearch: jest.fn(),
  33. placeholders: [],
  34. projects: [project],
  35. });
  36. jest.mocked(useLocation).mockReturnValue({
  37. action: 'PUSH',
  38. hash: '',
  39. key: '',
  40. pathname: '/organizations/org-slug/performance/mobile/screens/',
  41. query: {},
  42. search: '',
  43. state: undefined,
  44. } as Location);
  45. jest.mocked(usePageFilters).mockReturnValue({
  46. isReady: true,
  47. desyncedFilters: new Set(),
  48. pinnedFilters: new Set(),
  49. shouldPersist: true,
  50. selection: {
  51. datetime: {
  52. period: '10d',
  53. start: null,
  54. end: null,
  55. utc: false,
  56. },
  57. environments: [],
  58. projects: [parseInt(project.id, 10)],
  59. },
  60. });
  61. let eventsMock;
  62. beforeEach(function () {
  63. localStorage.clear();
  64. browserHistory.push = jest.fn();
  65. MockApiClient.addMockResponse({
  66. url: `/organizations/${organization.slug}/releases/`,
  67. body: [
  68. {
  69. id: 970136705,
  70. version: 'com.example.vu.android@2.10.5',
  71. dateCreated: '2023-12-19T21:37:53.895495Z',
  72. },
  73. {
  74. id: 969902997,
  75. version: 'com.example.vu.android@2.10.3+42',
  76. dateCreated: '2023-12-19T18:04:06.953025Z',
  77. },
  78. ],
  79. });
  80. MockApiClient.addMockResponse({
  81. url: `/organizations/${organization.slug}/events/`,
  82. query: {
  83. dataset: 'metrics',
  84. environment: [],
  85. field: ['release', 'count()'],
  86. per_page: 50,
  87. project: ['2'],
  88. query:
  89. 'transaction.op:ui.load release:["com.example.vu.android@2.10.5","com.example.vu.android@2.10.3+42"]',
  90. referrer: 'api.starfish.mobile-release-selector',
  91. statsPeriod: '10d',
  92. },
  93. body: {
  94. meta: {},
  95. data: [
  96. {
  97. release: 'com.example.vu.android@2.10.5',
  98. 'count()': 9768,
  99. },
  100. {
  101. release: 'com.example.vu.android@2.10.3+42',
  102. 'count()': 826,
  103. },
  104. ],
  105. },
  106. });
  107. eventsMock = MockApiClient.addMockResponse({
  108. url: `/organizations/${organization.slug}/events/`,
  109. });
  110. });
  111. afterEach(function () {
  112. MockApiClient.clearMockResponses();
  113. jest.clearAllMocks();
  114. });
  115. it('defaults requests with android platform filter', async function () {
  116. render(<PageloadModule />, {organization});
  117. await waitFor(() => {
  118. expect(
  119. screen.getByRole('radiogroup', {name: 'Filter platform'})
  120. ).toBeInTheDocument();
  121. });
  122. await waitFor(() => {
  123. expect(eventsMock).toHaveBeenNthCalledWith(
  124. 4,
  125. expect.anything(),
  126. expect.objectContaining({
  127. query: expect.objectContaining({
  128. query: 'event.type:transaction transaction.op:ui.load os.name:Android',
  129. }),
  130. })
  131. );
  132. });
  133. await waitFor(() => {
  134. expect(eventsMock).toHaveBeenNthCalledWith(
  135. 4,
  136. expect.anything(),
  137. expect.objectContaining({
  138. query: expect.objectContaining({
  139. query: 'event.type:transaction transaction.op:ui.load os.name:Android',
  140. }),
  141. })
  142. );
  143. });
  144. });
  145. it('uses value from local storage if available', async function () {
  146. localStorage.setItem(PLATFORM_LOCAL_STORAGE_KEY, 'iOS');
  147. render(<PageloadModule />, {organization});
  148. await waitFor(() => {
  149. expect(
  150. screen.getByRole('radiogroup', {name: 'Filter platform'})
  151. ).toBeInTheDocument();
  152. });
  153. await waitFor(() => {
  154. expect(eventsMock).toHaveBeenNthCalledWith(
  155. 4,
  156. expect.anything(),
  157. expect.objectContaining({
  158. query: expect.objectContaining({
  159. query: 'event.type:transaction transaction.op:ui.load os.name:iOS',
  160. }),
  161. })
  162. );
  163. });
  164. });
  165. });