index.spec.tsx 8.5 KB

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