index.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. MockApiClient.addMockResponse({
  85. url: '/organizations/org-slug/replay-count/',
  86. body: {
  87. data: [],
  88. },
  89. statusCode: 200,
  90. });
  91. eventsMockApi = MockApiClient.addMockResponse({
  92. url: '/organizations/org-slug/events/',
  93. body: {
  94. data: [],
  95. },
  96. statusCode: 200,
  97. });
  98. replaysMockApi = MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/replays/',
  100. body: {
  101. data: [],
  102. },
  103. statusCode: 200,
  104. });
  105. });
  106. afterEach(() => {
  107. MockApiClient.clearMockResponses();
  108. });
  109. it('should query the events endpoint for replayIds of a transaction', async () => {
  110. renderComponent();
  111. await waitFor(() => {
  112. expect(eventsMockApi).toHaveBeenCalled();
  113. expect(eventsMockApi).toHaveBeenCalledWith(
  114. '/organizations/org-slug/events/',
  115. expect.objectContaining({
  116. query: expect.objectContaining({
  117. statsPeriod: '14d',
  118. project: ['1'],
  119. environment: [],
  120. field: expect.arrayContaining([
  121. 'replayId',
  122. 'count()',
  123. ...SPAN_OP_BREAKDOWN_FIELDS,
  124. SPAN_OP_RELATIVE_BREAKDOWN_FIELD,
  125. 'transaction.duration',
  126. 'trace',
  127. 'timestamp',
  128. ]),
  129. per_page: 50,
  130. query: 'event.type:transaction transaction:transaction !replayId:""',
  131. }),
  132. })
  133. );
  134. });
  135. });
  136. it('should snapshot empty state', async () => {
  137. MockApiClient.addMockResponse({
  138. url: mockUrl,
  139. body: {
  140. data: [],
  141. },
  142. statusCode: 200,
  143. });
  144. const {container} = renderComponent();
  145. await waitFor(() => {
  146. expect(container).toSnapshot();
  147. });
  148. });
  149. it('should show empty message when no replays are found', async () => {
  150. renderComponent();
  151. await waitFor(() => {
  152. expect(replaysMockApi).toHaveBeenCalledTimes(1);
  153. expect(screen.getByText('There are no items to display')).toBeInTheDocument();
  154. });
  155. });
  156. it('should show loading indicator when loading replays', async () => {
  157. const mockApi = MockApiClient.addMockResponse({
  158. url: mockUrl,
  159. statusCode: 200,
  160. body: {
  161. data: [],
  162. },
  163. });
  164. renderComponent();
  165. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  166. await waitFor(() => {
  167. expect(mockApi).toHaveBeenCalledTimes(1);
  168. });
  169. });
  170. it('should show a list of replays and have the correct values', async () => {
  171. const mockApi = MockApiClient.addMockResponse({
  172. url: mockUrl,
  173. statusCode: 200,
  174. body: {
  175. data: [
  176. {
  177. count_errors: 1,
  178. duration: 52346,
  179. finished_at: '2022-09-15T06:54:00+00:00',
  180. id: '346789a703f6454384f1de473b8b9fcc',
  181. project_id: '2',
  182. started_at: '2022-09-15T06:50:03+00:00',
  183. urls: [
  184. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/',
  185. '/organizations/sentry-emerging-tech/replays/?project=2',
  186. ],
  187. user: {
  188. id: '147086',
  189. name: '',
  190. email: '',
  191. ip: '127.0.0.1',
  192. display_name: 'testDisplayName',
  193. },
  194. },
  195. {
  196. count_errors: 4,
  197. duration: 400,
  198. finished_at: '2022-09-21T21:40:38+00:00',
  199. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  200. project_id: '2',
  201. started_at: '2022-09-21T21:30:44+00:00',
  202. urls: [
  203. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  204. '/organizations/sentry-emerging-tech/issues/',
  205. '/organizations/sentry-emerging-tech/issues/?project=2',
  206. ],
  207. user: {
  208. id: '147086',
  209. name: '',
  210. email: '',
  211. ip: '127.0.0.1',
  212. display_name: 'testDisplayName',
  213. },
  214. },
  215. ],
  216. },
  217. });
  218. // Mock the system date to be 2022-09-28
  219. jest.useFakeTimers().setSystemTime(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  220. renderComponent();
  221. await waitFor(() => {
  222. expect(mockApi).toHaveBeenCalledTimes(1);
  223. });
  224. // Expect the table to have 2 rows
  225. expect(screen.getAllByText('testDisplayName')).toHaveLength(2);
  226. const expectedQuery =
  227. 'project=1&query=&referrer=%2Forganizations%2F%3AorgId%2Fperformance%2Fsummary%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  228. // Expect the first row to have the correct href
  229. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  230. 'href',
  231. `/organizations/org-slug/replays/project-slug:346789a703f6454384f1de473b8b9fcc/?${expectedQuery}`
  232. );
  233. // Expect the second row to have the correct href
  234. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  235. 'href',
  236. `/organizations/org-slug/replays/project-slug:b05dae9b6be54d21a4d5ad9f8f02b780/?${expectedQuery}`
  237. );
  238. // Expect the first row to have the correct duration
  239. expect(screen.getByText('14hr 32min 26s')).toBeInTheDocument();
  240. // Expect the second row to have the correct duration
  241. expect(screen.getByText('6min 40s')).toBeInTheDocument();
  242. // Expect the first row to have the correct errors
  243. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent('1');
  244. // Expect the second row to have the correct errors
  245. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent('4');
  246. // Expect the first row to have the correct date
  247. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  248. // Expect the second row to have the correct date
  249. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  250. });
  251. it('should be able to click the `Start Time` column and request data sorted by started_at query', async () => {
  252. const {rerender} = renderComponent();
  253. await waitFor(() => {
  254. expect(replaysMockApi).toHaveBeenCalledWith(
  255. mockUrl,
  256. expect.objectContaining({
  257. query: expect.objectContaining({
  258. sort: '-started_at',
  259. }),
  260. })
  261. );
  262. });
  263. // Click on the start time header and expect the sort to be started_at
  264. userEvent.click(screen.getByRole('columnheader', {name: 'Start Time'}));
  265. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  266. pathname: '/organizations/org-slug/replays/',
  267. query: {
  268. sort: 'started_at',
  269. project: '1',
  270. transaction: 'transaction',
  271. },
  272. });
  273. // Need to simulate a rerender to get the new sort
  274. act(() => {
  275. rerender(
  276. getComponent({
  277. location: {
  278. query: {
  279. sort: 'started_at',
  280. project: '1',
  281. transaction: 'transaction',
  282. },
  283. },
  284. })
  285. );
  286. });
  287. await waitFor(() => {
  288. expect(replaysMockApi).toHaveBeenCalledTimes(2);
  289. expect(replaysMockApi).toHaveBeenCalledWith(
  290. mockUrl,
  291. expect.objectContaining({
  292. query: expect.objectContaining({
  293. sort: 'started_at',
  294. }),
  295. })
  296. );
  297. });
  298. });
  299. it('should be able to click the `Duration` column and request data sorted by duration query', async () => {
  300. const mockApi = MockApiClient.addMockResponse({
  301. url: mockUrl,
  302. body: {
  303. data: [],
  304. },
  305. statusCode: 200,
  306. });
  307. const {rerender} = renderComponent();
  308. await waitFor(() => {
  309. expect(mockApi).toHaveBeenCalledWith(
  310. mockUrl,
  311. expect.objectContaining({
  312. query: expect.objectContaining({
  313. sort: '-started_at',
  314. }),
  315. })
  316. );
  317. });
  318. // Click on the duration header and expect the sort to be duration
  319. userEvent.click(screen.getByRole('columnheader', {name: 'Duration'}));
  320. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  321. pathname: '/organizations/org-slug/replays/',
  322. query: {
  323. sort: '-duration',
  324. project: '1',
  325. transaction: 'transaction',
  326. },
  327. });
  328. // Need to simulate a rerender to get the new sort
  329. act(() => {
  330. rerender(
  331. getComponent({
  332. location: {
  333. query: {
  334. sort: '-duration',
  335. project: '1',
  336. transaction: 'transaction',
  337. },
  338. },
  339. })
  340. );
  341. });
  342. await waitFor(() => {
  343. expect(mockApi).toHaveBeenCalledTimes(2);
  344. expect(mockApi).toHaveBeenCalledWith(
  345. mockUrl,
  346. expect.objectContaining({
  347. query: expect.objectContaining({
  348. sort: '-duration',
  349. }),
  350. })
  351. );
  352. });
  353. });
  354. it('should be able to click the `Errors` column and request data sorted by count_errors query', async () => {
  355. const mockApi = MockApiClient.addMockResponse({
  356. url: mockUrl,
  357. body: {
  358. data: [],
  359. },
  360. statusCode: 200,
  361. });
  362. const {rerender} = renderComponent();
  363. await waitFor(() => {
  364. expect(mockApi).toHaveBeenCalledWith(
  365. mockUrl,
  366. expect.objectContaining({
  367. query: expect.objectContaining({
  368. sort: '-started_at',
  369. }),
  370. })
  371. );
  372. });
  373. // Click on the errors header and expect the sort to be count_errors
  374. userEvent.click(screen.getByRole('columnheader', {name: 'Errors'}));
  375. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  376. pathname: '/organizations/org-slug/replays/',
  377. query: {
  378. sort: '-count_errors',
  379. project: '1',
  380. transaction: 'transaction',
  381. },
  382. });
  383. // Need to simulate a rerender to get the new sort
  384. act(() => {
  385. rerender(
  386. getComponent({
  387. location: {
  388. query: {
  389. sort: '-count_errors',
  390. project: '1',
  391. transaction: 'transaction',
  392. },
  393. },
  394. })
  395. );
  396. });
  397. await waitFor(() => {
  398. expect(mockApi).toHaveBeenCalledTimes(2);
  399. expect(mockApi).toHaveBeenCalledWith(
  400. mockUrl,
  401. expect.objectContaining({
  402. query: expect.objectContaining({
  403. sort: '-count_errors',
  404. }),
  405. })
  406. );
  407. });
  408. });
  409. it('should be able to click the `Activity` column and request data sorted by started_at query', async () => {
  410. const mockApi = MockApiClient.addMockResponse({
  411. url: mockUrl,
  412. body: {
  413. data: [],
  414. },
  415. statusCode: 200,
  416. });
  417. const {rerender} = renderComponent();
  418. await waitFor(() => {
  419. expect(mockApi).toHaveBeenCalledWith(
  420. mockUrl,
  421. expect.objectContaining({
  422. query: expect.objectContaining({
  423. sort: '-started_at',
  424. }),
  425. })
  426. );
  427. });
  428. // Click on the activity header and expect the sort to be activity
  429. userEvent.click(screen.getByRole('columnheader', {name: 'Activity'}));
  430. expect(mockRouterContext.context.router.push).toHaveBeenCalledWith({
  431. pathname: '/organizations/org-slug/replays/',
  432. query: {
  433. sort: '-activity',
  434. project: '1',
  435. transaction: 'transaction',
  436. },
  437. });
  438. // Need to simulate a rerender to get the new sort
  439. act(() => {
  440. rerender(
  441. getComponent({
  442. location: {
  443. query: {
  444. sort: '-activity',
  445. project: '1',
  446. transaction: 'transaction',
  447. },
  448. },
  449. })
  450. );
  451. });
  452. await waitFor(() => {
  453. expect(mockApi).toHaveBeenCalledTimes(2);
  454. expect(mockApi).toHaveBeenCalledWith(
  455. mockUrl,
  456. expect.objectContaining({
  457. query: expect.objectContaining({
  458. sort: '-activity',
  459. }),
  460. })
  461. );
  462. });
  463. });
  464. it("should show a message when the organization doesn't have access to the replay feature", async () => {
  465. renderComponent({
  466. organizationProps: {
  467. features: ['performance-view'],
  468. },
  469. });
  470. await waitFor(() => {
  471. expect(
  472. screen.getByText("You don't have access to this feature")
  473. ).toBeInTheDocument();
  474. });
  475. });
  476. });