index.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, 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 mockUrl = '/organizations/org-slug/replays/';
  25. let mockRouterContext: {
  26. childContextTypes?: any;
  27. context?: any;
  28. } = {};
  29. const getComponent = ({
  30. location,
  31. organizationProps = {features: ['performance-view', 'session-replay-ui']},
  32. }: InitializeOrgProps) => {
  33. const {router, organization, routerContext} = initializeOrg({
  34. organization: {
  35. ...organizationProps,
  36. },
  37. project: TestStubs.Project(),
  38. projects: [TestStubs.Project()],
  39. router: {
  40. routes: [
  41. {path: '/'},
  42. {path: '/organizations/:orgId/performance/summary/'},
  43. {path: 'replays/'},
  44. ],
  45. location: {
  46. pathname: '/organizations/org-slug/replays/',
  47. query: {
  48. project: '1',
  49. transaction: 'transaction',
  50. },
  51. ...location,
  52. },
  53. },
  54. });
  55. ProjectsStore.init();
  56. ProjectsStore.loadInitialData(organization.projects);
  57. mockRouterContext = routerContext;
  58. return (
  59. <OrganizationContext.Provider value={organization}>
  60. <RouteContext.Provider
  61. value={{
  62. router,
  63. location: router.location,
  64. params: router.params,
  65. routes: router.routes,
  66. }}
  67. >
  68. <TransactionReplays location={router.location} organization={organization} />
  69. </RouteContext.Provider>
  70. </OrganizationContext.Provider>
  71. );
  72. };
  73. const renderComponent = (componentProps: InitializeOrgProps = {}) => {
  74. return render(getComponent(componentProps), {context: mockRouterContext});
  75. };
  76. describe('TransactionReplays', () => {
  77. let eventsMockApi: jest.Mock<any, any>;
  78. let replaysMockApi: jest.Mock<any, any>;
  79. beforeEach(() => {
  80. MockApiClient.addMockResponse({
  81. url: '/organizations/org-slug/events-has-measurements/',
  82. body: {measurements: false},
  83. });
  84. eventsMockApi = MockApiClient.addMockResponse({
  85. url: '/organizations/org-slug/events/',
  86. body: {
  87. data: [],
  88. },
  89. statusCode: 200,
  90. });
  91. replaysMockApi = MockApiClient.addMockResponse({
  92. url: '/organizations/org-slug/replays/',
  93. body: {
  94. data: [],
  95. },
  96. statusCode: 200,
  97. });
  98. });
  99. afterEach(() => {
  100. MockApiClient.clearMockResponses();
  101. });
  102. it('should query the events endpoint for replayIds of a transaction', async () => {
  103. renderComponent();
  104. await waitFor(() => {
  105. expect(eventsMockApi).toHaveBeenCalled();
  106. expect(eventsMockApi).toHaveBeenCalledWith(
  107. '/organizations/org-slug/events/',
  108. expect.objectContaining({
  109. query: expect.objectContaining({
  110. statsPeriod: '14d',
  111. project: ['1'],
  112. environment: [],
  113. field: expect.arrayContaining([
  114. 'replayId',
  115. 'count()',
  116. ...SPAN_OP_BREAKDOWN_FIELDS,
  117. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  118. 'transaction.duration',
  119. 'trace',
  120. 'timestamp',
  121. ]),
  122. per_page: 50,
  123. query: 'event.type:transaction transaction:transaction !replayId:""',
  124. }),
  125. })
  126. );
  127. });
  128. });
  129. it('should snapshot empty state', async () => {
  130. MockApiClient.addMockResponse({
  131. url: mockUrl,
  132. body: {
  133. data: [],
  134. },
  135. statusCode: 200,
  136. });
  137. const {container} = renderComponent();
  138. await waitFor(() => {
  139. expect(container).toSnapshot();
  140. });
  141. });
  142. it('should show empty message when no replays are found', async () => {
  143. renderComponent();
  144. await waitFor(() => {
  145. expect(replaysMockApi).toHaveBeenCalledTimes(1);
  146. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  147. });
  148. });
  149. it('should show loading indicator when loading replays', async () => {
  150. const mockApi = MockApiClient.addMockResponse({
  151. url: mockUrl,
  152. statusCode: 200,
  153. body: {
  154. data: [],
  155. },
  156. });
  157. renderComponent();
  158. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  159. await waitFor(() => {
  160. expect(mockApi).toHaveBeenCalledTimes(1);
  161. });
  162. });
  163. it('should show a list of replays and have the correct values', async () => {
  164. const mockApi = MockApiClient.addMockResponse({
  165. url: mockUrl,
  166. statusCode: 200,
  167. body: {
  168. data: [
  169. {
  170. countErrors: 1,
  171. duration: 52346,
  172. finishedAt: '2022-09-15T06:54:00+00:00',
  173. id: '346789a703f6454384f1de473b8b9fcc',
  174. projectId: '2',
  175. startedAt: '2022-09-15T06:50:03+00:00',
  176. urls: [
  177. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  178. '/organizations/sentry-emerging-tech/replays/?project=2',
  179. ],
  180. user: {
  181. id: '147086',
  182. name: '',
  183. email: '',
  184. ip_address: '127.0.0.1',
  185. displayName: 'testDisplayName',
  186. },
  187. },
  188. {
  189. countErrors: 4,
  190. duration: 400,
  191. finishedAt: '2022-09-21T21:40:38+00:00',
  192. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  193. projectId: '2',
  194. startedAt: '2022-09-21T21:30:44+00:00',
  195. urls: [
  196. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  197. '/organizations/sentry-emerging-tech/issues/',
  198. '/organizations/sentry-emerging-tech/issues/?project=2',
  199. ],
  200. user: {
  201. id: '147086',
  202. name: '',
  203. email: '',
  204. ip_address: '127.0.0.1',
  205. displayName: 'testDisplayName',
  206. },
  207. },
  208. ],
  209. },
  210. });
  211. // Mock the system date to be 2022-09-28
  212. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  213. renderComponent();
  214. await waitFor(() => {
  215. expect(mockApi).toHaveBeenCalledTimes(1);
  216. });
  217. // Expect the table to have 2 rows
  218. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  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/?referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F'
  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/?referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F'
  228. );
  229. // Expect the first row to have the correct duration
  230. expect(screen.getByText('14hr 32min 26s')).toBeInTheDocument();
  231. // Expect the second row to have the correct duration
  232. expect(screen.getByText('6min 40s')).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 be able to click the `Start Time` column and request data sorted by startedAt query', async () => {
  243. const {rerender} = renderComponent();
  244. await waitFor(() => {
  245. expect(replaysMockApi).toHaveBeenCalledWith(
  246. mockUrl,
  247. expect.objectContaining({
  248. query: expect.objectContaining({
  249. sort: '-startedAt',
  250. }),
  251. })
  252. );
  253. });
  254. // Click on the start time header and expect the sort to be startedAt
  255. userEvent.click(screen.getByRole('columnheader', {name: 'Start Time'}));
  256. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  257. pathname: '/organizations/org-slug/replays/',
  258. query: {
  259. sort: 'startedAt',
  260. project: '1',
  261. transaction: 'transaction',
  262. },
  263. });
  264. // Need to simulate a rerender to get the new sort
  265. act(() => {
  266. rerender(
  267. getComponent({
  268. location: {
  269. query: {
  270. sort: 'startedAt',
  271. project: '1',
  272. transaction: 'transaction',
  273. },
  274. },
  275. })
  276. );
  277. });
  278. await waitFor(() => {
  279. expect(replaysMockApi).toHaveBeenCalledTimes(2);
  280. expect(replaysMockApi).toHaveBeenCalledWith(
  281. mockUrl,
  282. expect.objectContaining({
  283. query: expect.objectContaining({
  284. sort: 'startedAt',
  285. }),
  286. })
  287. );
  288. });
  289. });
  290. it('should be able to click the `Duration` column and request data sorted by duration query', async () => {
  291. const mockApi = MockApiClient.addMockResponse({
  292. url: mockUrl,
  293. body: {
  294. data: [],
  295. },
  296. statusCode: 200,
  297. });
  298. const {rerender} = renderComponent();
  299. await waitFor(() => {
  300. expect(mockApi).toHaveBeenCalledWith(
  301. mockUrl,
  302. expect.objectContaining({
  303. query: expect.objectContaining({
  304. sort: '-startedAt',
  305. }),
  306. })
  307. );
  308. });
  309. // Click on the duration header and expect the sort to be duration
  310. userEvent.click(screen.getByRole('columnheader', {name: 'Duration'}));
  311. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  312. pathname: '/organizations/org-slug/replays/',
  313. query: {
  314. sort: '-duration',
  315. project: '1',
  316. transaction: 'transaction',
  317. },
  318. });
  319. // Need to simulate a rerender to get the new sort
  320. act(() => {
  321. rerender(
  322. getComponent({
  323. location: {
  324. query: {
  325. sort: '-duration',
  326. project: '1',
  327. transaction: 'transaction',
  328. },
  329. },
  330. })
  331. );
  332. });
  333. await waitFor(() => {
  334. expect(mockApi).toHaveBeenCalledTimes(2);
  335. expect(mockApi).toHaveBeenCalledWith(
  336. mockUrl,
  337. expect.objectContaining({
  338. query: expect.objectContaining({
  339. sort: '-duration',
  340. }),
  341. })
  342. );
  343. });
  344. });
  345. it('should be able to click the `Errors` column and request data sorted by countErrors query', async () => {
  346. const mockApi = MockApiClient.addMockResponse({
  347. url: mockUrl,
  348. body: {
  349. data: [],
  350. },
  351. statusCode: 200,
  352. });
  353. const {rerender} = renderComponent();
  354. await waitFor(() => {
  355. expect(mockApi).toHaveBeenCalledWith(
  356. mockUrl,
  357. expect.objectContaining({
  358. query: expect.objectContaining({
  359. sort: '-startedAt',
  360. }),
  361. })
  362. );
  363. });
  364. // Click on the errors header and expect the sort to be countErrors
  365. userEvent.click(screen.getByRole('columnheader', {name: 'Errors'}));
  366. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  367. pathname: '/organizations/org-slug/replays/',
  368. query: {
  369. sort: '-countErrors',
  370. project: '1',
  371. transaction: 'transaction',
  372. },
  373. });
  374. // Need to simulate a rerender to get the new sort
  375. act(() => {
  376. rerender(
  377. getComponent({
  378. location: {
  379. query: {
  380. sort: '-countErrors',
  381. project: '1',
  382. transaction: 'transaction',
  383. },
  384. },
  385. })
  386. );
  387. });
  388. await waitFor(() => {
  389. expect(mockApi).toHaveBeenCalledTimes(2);
  390. expect(mockApi).toHaveBeenCalledWith(
  391. mockUrl,
  392. expect.objectContaining({
  393. query: expect.objectContaining({
  394. sort: '-countErrors',
  395. }),
  396. })
  397. );
  398. });
  399. });
  400. it("should show a message when the organization doesn't have access to the replay feature", async () => {
  401. renderComponent({
  402. organizationProps: {
  403. features: ['performance-view'],
  404. },
  405. });
  406. await waitFor(() => {
  407. expect(
  408. screen.getByText("You don't have access to this feature")
  409. ).toBeInTheDocument();
  410. });
  411. });
  412. });