index.spec.tsx 5.0 KB

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