index.spec.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import {ProjectFixture} from 'sentry-fixture/project';
  2. import {ReplayListFixture} from 'sentry-fixture/replayList';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import {resetMockDate, setMockDate} from 'sentry-test/utils';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. import {
  8. SPAN_OP_BREAKDOWN_FIELDS,
  9. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  10. } from 'sentry/utils/discover/fields';
  11. import TransactionReplays from 'sentry/views/performance/transactionSummary/transactionReplays';
  12. type InitializeOrgProps = {
  13. location?: {
  14. pathname?: string;
  15. query?: {[key: string]: string};
  16. };
  17. organizationProps?: {
  18. features?: string[];
  19. };
  20. };
  21. jest.mock('sentry/utils/useMedia', () => ({
  22. __esModule: true,
  23. default: jest.fn(() => true),
  24. }));
  25. const mockEventsUrl = '/organizations/org-slug/events/';
  26. const mockReplaysUrl = '/organizations/org-slug/replays/';
  27. const renderComponent = ({
  28. location,
  29. organizationProps = {features: ['performance-view', 'session-replay']},
  30. }: InitializeOrgProps = {}) => {
  31. const {organization, projects, router} = initializeOrg({
  32. organization: {
  33. ...organizationProps,
  34. },
  35. projects: [ProjectFixture()],
  36. router: {
  37. routes: [
  38. {path: '/'},
  39. {path: '/organizations/:orgId/performance/summary/'},
  40. {path: 'replays/'},
  41. ],
  42. location: {
  43. pathname: '/organizations/org-slug/replays/',
  44. query: {
  45. project: '1',
  46. transaction: 'Settings Page',
  47. },
  48. ...location,
  49. },
  50. },
  51. });
  52. ProjectsStore.init();
  53. ProjectsStore.loadInitialData(projects);
  54. return render(<TransactionReplays />, {router, organization});
  55. };
  56. describe('TransactionReplays', () => {
  57. let eventsMockApi: jest.Mock<any, any>;
  58. let replaysMockApi: jest.Mock<any, any>;
  59. beforeEach(() => {
  60. MockApiClient.addMockResponse({
  61. method: 'GET',
  62. url: `/organizations/org-slug/sdk-updates/`,
  63. body: [],
  64. });
  65. MockApiClient.addMockResponse({
  66. url: '/organizations/org-slug/events-has-measurements/',
  67. body: {measurements: false},
  68. });
  69. MockApiClient.addMockResponse({
  70. url: '/organizations/org-slug/replay-count/',
  71. body: {
  72. data: [],
  73. },
  74. statusCode: 200,
  75. });
  76. eventsMockApi = MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/events/',
  78. body: {
  79. data: [],
  80. },
  81. statusCode: 200,
  82. });
  83. replaysMockApi = MockApiClient.addMockResponse({
  84. url: '/organizations/org-slug/replays/',
  85. body: {
  86. data: [],
  87. },
  88. statusCode: 200,
  89. });
  90. });
  91. afterEach(() => {
  92. MockApiClient.clearMockResponses();
  93. resetMockDate();
  94. });
  95. it('should query the events endpoint for replayIds of a transaction', async () => {
  96. renderComponent();
  97. await waitFor(() => {
  98. expect(eventsMockApi).toHaveBeenCalledWith(
  99. '/organizations/org-slug/events/',
  100. expect.objectContaining({
  101. query: expect.objectContaining({
  102. cursor: undefined,
  103. statsPeriod: '14d',
  104. project: ['1'],
  105. environment: [],
  106. field: expect.arrayContaining([
  107. 'replayId',
  108. 'count()',
  109. 'transaction.duration',
  110. 'trace',
  111. 'timestamp',
  112. ...SPAN_OP_BREAKDOWN_FIELDS,
  113. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  114. ]),
  115. per_page: 50,
  116. query: 'event.type:transaction transaction:"Settings Page" !replayId:""',
  117. }),
  118. })
  119. );
  120. });
  121. });
  122. it('should snapshot empty state', async () => {
  123. const mockApi = MockApiClient.addMockResponse({
  124. url: mockReplaysUrl,
  125. body: {
  126. data: [],
  127. },
  128. statusCode: 200,
  129. });
  130. renderComponent();
  131. await waitFor(() => {
  132. expect(mockApi).toHaveBeenCalledTimes(1);
  133. });
  134. });
  135. it('should show empty message when no replays are found', async () => {
  136. renderComponent();
  137. await waitFor(() => {
  138. expect(replaysMockApi).toHaveBeenCalledTimes(1);
  139. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  140. });
  141. });
  142. it('should show loading indicator when loading replays', async () => {
  143. const mockApi = MockApiClient.addMockResponse({
  144. url: mockEventsUrl,
  145. statusCode: 200,
  146. body: {
  147. data: [],
  148. },
  149. });
  150. renderComponent();
  151. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  152. await waitFor(() => {
  153. expect(mockApi).toHaveBeenCalledTimes(1);
  154. });
  155. });
  156. it('should show a list of replays and have the correct values', async () => {
  157. const mockApi = MockApiClient.addMockResponse({
  158. url: mockReplaysUrl,
  159. statusCode: 200,
  160. body: {
  161. data: [
  162. {
  163. ...ReplayListFixture()[0],
  164. count_errors: 1,
  165. duration: 52346,
  166. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  167. id: '346789a703f6454384f1de473b8b9fcc',
  168. started_at: new Date('2022-09-15T06:50:00+00:00'),
  169. urls: [
  170. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  171. '/organizations/sentry-emerging-tech/replays/?project=2',
  172. ],
  173. },
  174. {
  175. ...ReplayListFixture()[0],
  176. count_errors: 4,
  177. duration: 400,
  178. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  179. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  180. started_at: new Date('2022-09-21T21:30:44+00:00'),
  181. urls: [
  182. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  183. '/organizations/sentry-emerging-tech/issues/',
  184. '/organizations/sentry-emerging-tech/issues/?project=2',
  185. ],
  186. },
  187. ].map(hydrated => ({
  188. ...hydrated,
  189. started_at: hydrated.started_at.toString(),
  190. finished_at: hydrated.finished_at.toString(),
  191. })),
  192. },
  193. });
  194. // Mock the system date to be 2022-09-28
  195. setMockDate(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  196. renderComponent();
  197. await waitFor(() => {
  198. expect(mockApi).toHaveBeenCalledTimes(1);
  199. });
  200. // Expect the table to have 2 rows
  201. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  202. const expectedQuery =
  203. 'project=1&query=&referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  204. // Expect the first row to have the correct href
  205. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  206. 'href',
  207. `/organizations/org-slug/replays/346789a703f6454384f1de473b8b9fcc/?${expectedQuery}`
  208. );
  209. // Expect the second row to have the correct href
  210. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  211. 'href',
  212. `/organizations/org-slug/replays/b05dae9b6be54d21a4d5ad9f8f02b780/?${expectedQuery}`
  213. );
  214. // Expect the first row to have the correct duration
  215. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  216. // Expect the second row to have the correct duration
  217. expect(screen.getByText('06:40')).toBeInTheDocument();
  218. // Expect the first row to have the correct errors
  219. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  220. // Expect the second row to have the correct errors
  221. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  222. // Expect the first row to have the correct date
  223. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  224. // Expect the second row to have the correct date
  225. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  226. });
  227. it("should show a message when the organization doesn't have access to the replay feature", async () => {
  228. renderComponent({
  229. organizationProps: {
  230. features: ['performance-view'],
  231. },
  232. });
  233. await waitFor(() => {
  234. expect(
  235. screen.getByText("You don't have access to this feature")
  236. ).toBeInTheDocument();
  237. });
  238. });
  239. });