pageload.spec.tsx 5.3 KB

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