groupReplays.spec.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. import {GroupFixture} from 'sentry-fixture/group';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import GroupReplays from 'sentry/views/issueDetails/groupReplays';
  7. jest.mock('sentry/utils/useMedia', () => ({
  8. __esModule: true,
  9. default: jest.fn(() => true),
  10. }));
  11. const mockReplayCountUrl = '/organizations/org-slug/replay-count/';
  12. const mockReplayUrl = '/organizations/org-slug/replays/';
  13. type InitializeOrgProps = {
  14. organizationProps?: {
  15. features?: string[];
  16. };
  17. };
  18. import {duration} from 'moment';
  19. import {RRWebInitFrameEventsFixture} from 'sentry-fixture/replay/rrweb';
  20. import {ReplayListFixture} from 'sentry-fixture/replayList';
  21. import {ReplayRecordFixture} from 'sentry-fixture/replayRecord';
  22. import {resetMockDate, setMockDate} from 'sentry-test/utils';
  23. import {browserHistory} from 'sentry/utils/browserHistory';
  24. import useReplayReader from 'sentry/utils/replays/hooks/useReplayReader';
  25. import ReplayReader from 'sentry/utils/replays/replayReader';
  26. const REPLAY_ID_1 = '346789a703f6454384f1de473b8b9fcc';
  27. const REPLAY_ID_2 = 'b05dae9b6be54d21a4d5ad9f8f02b780';
  28. let router, organization, routerContext;
  29. jest.mock('sentry/utils/replays/hooks/useReplayReader');
  30. // Mock screenfull library
  31. jest.mock('screenfull', () => ({
  32. enabled: true,
  33. isFullscreen: false,
  34. request: jest.fn(),
  35. exit: jest.fn(),
  36. on: jest.fn(),
  37. off: jest.fn(),
  38. }));
  39. const mockUseReplayReader = jest.mocked(useReplayReader);
  40. const mockEventTimestamp = new Date('2022-09-22T16:59:41Z');
  41. const mockEventTimestampMs = mockEventTimestamp.getTime();
  42. // Get replay data with the mocked replay reader params
  43. const mockReplay = ReplayReader.factory({
  44. replayRecord: ReplayRecordFixture({
  45. id: REPLAY_ID_1,
  46. browser: {
  47. name: 'Chrome',
  48. version: '110.0.0',
  49. },
  50. started_at: new Date('Sep 22, 2022 4:58:39 PM UTC'),
  51. finished_at: new Date(mockEventTimestampMs + 5_000),
  52. duration: duration(10, 'seconds'),
  53. }),
  54. errors: [],
  55. attachments: RRWebInitFrameEventsFixture({
  56. timestamp: new Date('Sep 22, 2022 4:58:39 PM UTC'),
  57. }),
  58. clipWindow: {
  59. startTimestampMs: mockEventTimestampMs - 5_000,
  60. endTimestampMs: mockEventTimestampMs + 5_000,
  61. },
  62. });
  63. mockUseReplayReader.mockImplementation(() => {
  64. return {
  65. attachments: [],
  66. errors: [],
  67. fetchError: undefined,
  68. fetching: false,
  69. onRetry: jest.fn(),
  70. projectSlug: ProjectFixture().slug,
  71. replay: mockReplay,
  72. replayId: REPLAY_ID_1,
  73. replayRecord: ReplayRecordFixture({id: REPLAY_ID_1}),
  74. };
  75. });
  76. function init({organizationProps = {features: ['session-replay']}}: InitializeOrgProps) {
  77. const mockProject = ProjectFixture();
  78. ({router, organization, routerContext} = initializeOrg({
  79. organization: {
  80. ...organizationProps,
  81. },
  82. project: mockProject,
  83. projects: [mockProject],
  84. router: {
  85. routes: [
  86. {path: '/'},
  87. {path: '/organizations/:orgId/issues/:groupId/'},
  88. {path: 'replays/'},
  89. ],
  90. location: {
  91. pathname: '/organizations/org-slug/replays/',
  92. query: {},
  93. },
  94. },
  95. }));
  96. ProjectsStore.init();
  97. ProjectsStore.loadInitialData(organization.projects);
  98. return {router, organization, routerContext};
  99. }
  100. describe('GroupReplays', () => {
  101. beforeEach(() => {
  102. MockApiClient.clearMockResponses();
  103. MockApiClient.addMockResponse({
  104. method: 'GET',
  105. url: `/organizations/org-slug/sdk-updates/`,
  106. body: [],
  107. });
  108. });
  109. afterEach(() => {
  110. resetMockDate();
  111. });
  112. describe('Replay Feature Disabled', () => {
  113. const mockGroup = GroupFixture();
  114. it("should show a message when the organization doesn't have access to the replay feature", () => {
  115. ({router, organization, routerContext} = init({
  116. organizationProps: {features: []},
  117. }));
  118. render(<GroupReplays group={mockGroup} />, {
  119. context: routerContext,
  120. organization,
  121. router,
  122. });
  123. expect(
  124. screen.getByText("You don't have access to this feature")
  125. ).toBeInTheDocument();
  126. });
  127. });
  128. describe('Replay Feature Enabled', () => {
  129. beforeEach(() => {
  130. ({router, organization, routerContext} = init({}));
  131. });
  132. it('should query the replay-count endpoint with the fetched replayIds', async () => {
  133. const mockGroup = GroupFixture();
  134. const mockReplayCountApi = MockApiClient.addMockResponse({
  135. url: mockReplayCountUrl,
  136. body: {
  137. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  138. },
  139. });
  140. const mockReplayApi = MockApiClient.addMockResponse({
  141. url: mockReplayUrl,
  142. body: {
  143. data: [],
  144. },
  145. });
  146. render(<GroupReplays group={mockGroup} />, {
  147. context: routerContext,
  148. organization,
  149. router,
  150. });
  151. await waitFor(() => {
  152. expect(mockReplayCountApi).toHaveBeenCalledWith(
  153. mockReplayCountUrl,
  154. expect.objectContaining({
  155. query: {
  156. returnIds: true,
  157. data_source: 'discover',
  158. query: `issue.id:[${mockGroup.id}]`,
  159. statsPeriod: '90d',
  160. project: -1,
  161. },
  162. })
  163. );
  164. // Expect api path to have the correct query params
  165. expect(mockReplayApi).toHaveBeenCalledWith(
  166. mockReplayUrl,
  167. expect.objectContaining({
  168. query: expect.objectContaining({
  169. environment: [],
  170. field: [
  171. 'activity',
  172. 'browser',
  173. 'count_dead_clicks',
  174. 'count_errors',
  175. 'count_rage_clicks',
  176. 'duration',
  177. 'finished_at',
  178. 'has_viewed',
  179. 'id',
  180. 'is_archived',
  181. 'os',
  182. 'project_id',
  183. 'started_at',
  184. 'user',
  185. ],
  186. per_page: 50,
  187. project: -1,
  188. queryReferrer: 'issueReplays',
  189. query: `id:[${REPLAY_ID_1},${REPLAY_ID_2}]`,
  190. sort: '-started_at',
  191. statsPeriod: '90d',
  192. }),
  193. })
  194. );
  195. });
  196. });
  197. it('should show empty message when no replays are found', async () => {
  198. const mockGroup = GroupFixture();
  199. const mockReplayCountApi = MockApiClient.addMockResponse({
  200. url: mockReplayCountUrl,
  201. body: {
  202. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  203. },
  204. });
  205. const mockReplayApi = MockApiClient.addMockResponse({
  206. url: mockReplayUrl,
  207. body: {
  208. data: [],
  209. },
  210. });
  211. render(<GroupReplays group={mockGroup} />, {
  212. context: routerContext,
  213. organization,
  214. router,
  215. });
  216. expect(
  217. await screen.findByText('There are no items to display')
  218. ).toBeInTheDocument();
  219. expect(mockReplayCountApi).toHaveBeenCalled();
  220. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  221. });
  222. it('should display error message when api call fails', async () => {
  223. const mockGroup = GroupFixture();
  224. const mockReplayCountApi = MockApiClient.addMockResponse({
  225. url: mockReplayCountUrl,
  226. body: {
  227. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  228. },
  229. });
  230. const mockReplayApi = MockApiClient.addMockResponse({
  231. url: mockReplayUrl,
  232. statusCode: 500,
  233. body: {
  234. detail: 'Invalid number: asdf. Expected number.',
  235. },
  236. });
  237. render(<GroupReplays group={mockGroup} />, {
  238. context: routerContext,
  239. organization,
  240. router,
  241. });
  242. expect(
  243. await screen.findByText('Invalid number: asdf. Expected number.')
  244. ).toBeInTheDocument();
  245. await waitFor(() => {
  246. expect(mockReplayCountApi).toHaveBeenCalled();
  247. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  248. });
  249. });
  250. it('should display default error message when api call fails without a body', async () => {
  251. const mockGroup = GroupFixture();
  252. const mockReplayCountApi = MockApiClient.addMockResponse({
  253. url: mockReplayCountUrl,
  254. body: {
  255. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  256. },
  257. });
  258. const mockReplayApi = MockApiClient.addMockResponse({
  259. url: mockReplayUrl,
  260. statusCode: 500,
  261. body: {},
  262. });
  263. render(<GroupReplays group={mockGroup} />, {
  264. context: routerContext,
  265. organization,
  266. router,
  267. });
  268. expect(
  269. await screen.findByText(
  270. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  271. )
  272. ).toBeInTheDocument();
  273. await waitFor(() => {
  274. expect(mockReplayCountApi).toHaveBeenCalled();
  275. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  276. });
  277. });
  278. it('should show loading indicator when loading replays', async () => {
  279. const mockGroup = GroupFixture();
  280. const mockReplayCountApi = MockApiClient.addMockResponse({
  281. url: mockReplayCountUrl,
  282. body: {
  283. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  284. },
  285. });
  286. const mockReplayApi = MockApiClient.addMockResponse({
  287. url: mockReplayUrl,
  288. statusCode: 200,
  289. body: {
  290. data: [],
  291. },
  292. });
  293. render(<GroupReplays group={mockGroup} />, {
  294. context: routerContext,
  295. organization,
  296. router,
  297. });
  298. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  299. await waitFor(() => {
  300. expect(mockReplayCountApi).toHaveBeenCalled();
  301. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  302. });
  303. });
  304. it('should show a list of replays and have the correct values', async () => {
  305. const mockGroup = GroupFixture();
  306. const mockReplayCountApi = MockApiClient.addMockResponse({
  307. url: mockReplayCountUrl,
  308. body: {
  309. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  310. },
  311. });
  312. const mockReplayApi = MockApiClient.addMockResponse({
  313. url: mockReplayUrl,
  314. statusCode: 200,
  315. body: {
  316. data: [
  317. {
  318. ...ReplayListFixture()[0],
  319. count_errors: 1,
  320. duration: 52346,
  321. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  322. id: REPLAY_ID_1,
  323. started_at: new Date('2022-09-15T06:50:00+00:00'),
  324. urls: [
  325. 'https://dev.getsentry.net:7999/replays/',
  326. '/organizations/org-slug/replays/?project=2',
  327. ],
  328. },
  329. {
  330. ...ReplayListFixture()[0],
  331. count_errors: 4,
  332. duration: 400,
  333. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  334. id: REPLAY_ID_2,
  335. started_at: new Date('2022-09-21T21:30:44+00:00'),
  336. urls: [
  337. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  338. '/organizations/org-slug/issues/',
  339. '/organizations/org-slug/issues/?project=2',
  340. ],
  341. },
  342. ].map(hydrated => ({
  343. ...hydrated,
  344. started_at: hydrated.started_at.toString(),
  345. finished_at: hydrated.finished_at.toString(),
  346. })),
  347. },
  348. });
  349. // Mock the system date to be 2022-09-28
  350. setMockDate(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  351. render(<GroupReplays group={mockGroup} />, {
  352. context: routerContext,
  353. organization,
  354. router,
  355. });
  356. await waitFor(() => {
  357. expect(mockReplayCountApi).toHaveBeenCalled();
  358. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  359. });
  360. // Expect the table to have 2 rows
  361. expect(await screen.findAllByText('testDisplayName')).toHaveLength(2);
  362. const expectedQuery =
  363. 'query=&referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  364. // Expect the first row to have the correct href
  365. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  366. 'href',
  367. `/organizations/org-slug/replays/${REPLAY_ID_1}/?${expectedQuery}`
  368. );
  369. // Expect the second row to have the correct href
  370. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  371. 'href',
  372. `/organizations/org-slug/replays/${REPLAY_ID_2}/?${expectedQuery}`
  373. );
  374. // Expect the first row to have the correct duration
  375. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  376. // Expect the second row to have the correct duration
  377. expect(screen.getByText('06:40')).toBeInTheDocument();
  378. // Expect the first row to have the correct errors
  379. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent(
  380. '1'
  381. );
  382. // Expect the second row to have the correct errors
  383. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent(
  384. '4'
  385. );
  386. // Expect the first row to have the correct date
  387. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  388. // Expect the second row to have the correct date
  389. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  390. });
  391. it('Should render the replay player when replay-play-from-replay-tab is enabled', async () => {
  392. ({router, organization, routerContext} = init({
  393. organizationProps: {features: ['replay-play-from-replay-tab', 'session-replay']},
  394. }));
  395. const mockGroup = GroupFixture();
  396. const mockReplayCountApi = MockApiClient.addMockResponse({
  397. url: mockReplayCountUrl,
  398. body: {
  399. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  400. },
  401. });
  402. MockApiClient.addMockResponse({
  403. url: mockReplayUrl,
  404. statusCode: 200,
  405. body: {
  406. data: [
  407. {
  408. ...ReplayListFixture()[0],
  409. count_errors: 1,
  410. duration: 52346,
  411. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  412. id: REPLAY_ID_1,
  413. started_at: new Date('2022-09-15T06:50:00+00:00'),
  414. urls: [
  415. 'https://dev.getsentry.net:7999/replays/',
  416. '/organizations/org-slug/replays/?project=2',
  417. ],
  418. },
  419. {
  420. ...ReplayListFixture()[0],
  421. count_errors: 4,
  422. duration: 400,
  423. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  424. id: REPLAY_ID_2,
  425. started_at: new Date('2022-09-21T21:30:44+00:00'),
  426. urls: [
  427. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  428. '/organizations/org-slug/issues/',
  429. '/organizations/org-slug/issues/?project=2',
  430. ],
  431. },
  432. ].map(hydrated => ({
  433. ...hydrated,
  434. started_at: hydrated.started_at.toString(),
  435. finished_at: hydrated.finished_at.toString(),
  436. })),
  437. },
  438. });
  439. render(<GroupReplays group={mockGroup} />, {
  440. context: routerContext,
  441. organization,
  442. router,
  443. });
  444. expect(await screen.findByText('See Full Replay')).toBeInTheDocument();
  445. expect(mockReplayCountApi).toHaveBeenCalledWith(
  446. mockReplayCountUrl,
  447. expect.objectContaining({
  448. query: {
  449. returnIds: true,
  450. data_source: 'discover',
  451. query: `issue.id:[${mockGroup.id}]`,
  452. statsPeriod: '90d',
  453. project: -1,
  454. },
  455. })
  456. );
  457. });
  458. it('Should switch replays when clicking and replay-play-from-replay-tab is enabled', async () => {
  459. ({router, organization, routerContext} = init({
  460. organizationProps: {features: ['session-replay']},
  461. }));
  462. const mockGroup = GroupFixture();
  463. const mockReplayRecord = mockReplay?.getReplay();
  464. const mockReplayCountApi = MockApiClient.addMockResponse({
  465. url: mockReplayCountUrl,
  466. body: {
  467. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  468. },
  469. });
  470. MockApiClient.addMockResponse({
  471. url: mockReplayUrl,
  472. statusCode: 200,
  473. body: {
  474. data: [
  475. {
  476. ...ReplayListFixture()[0],
  477. count_errors: 1,
  478. duration: 52346,
  479. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  480. id: REPLAY_ID_1,
  481. started_at: new Date('2022-09-15T06:50:00+00:00'),
  482. urls: [
  483. 'https://dev.getsentry.net:7999/replays/',
  484. '/organizations/org-slug/replays/?project=2',
  485. ],
  486. },
  487. {
  488. ...ReplayListFixture()[0],
  489. count_errors: 4,
  490. duration: 400,
  491. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  492. id: REPLAY_ID_2,
  493. started_at: new Date('2022-09-21T21:30:44+00:00'),
  494. urls: [
  495. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  496. '/organizations/org-slug/issues/',
  497. '/organizations/org-slug/issues/?project=2',
  498. ],
  499. },
  500. ].map(hydrated => ({
  501. ...hydrated,
  502. started_at: hydrated.started_at.toString(),
  503. finished_at: hydrated.finished_at.toString(),
  504. })),
  505. },
  506. });
  507. MockApiClient.addMockResponse({
  508. method: 'POST',
  509. url: `/projects/${organization.slug}/${mockReplayRecord?.project_id}/replays/${mockReplayRecord?.id}/viewed-by/`,
  510. });
  511. render(<GroupReplays group={mockGroup} />, {
  512. context: routerContext,
  513. organization,
  514. router,
  515. });
  516. await waitFor(() => {
  517. expect(mockReplayCountApi).toHaveBeenCalledWith(
  518. mockReplayCountUrl,
  519. expect.objectContaining({
  520. query: {
  521. returnIds: true,
  522. data_source: 'discover',
  523. query: `issue.id:[${mockGroup.id}]`,
  524. statsPeriod: '90d',
  525. project: -1,
  526. },
  527. })
  528. );
  529. });
  530. const mockReplace = jest.mocked(browserHistory.replace);
  531. const replayPlayPlause = (
  532. await screen.findAllByTestId('replay-table-play-button')
  533. )[0];
  534. await userEvent.click(replayPlayPlause);
  535. await waitFor(() =>
  536. expect(mockReplace).toHaveBeenCalledWith(
  537. expect.objectContaining({
  538. pathname: '/organizations/org-slug/replays/',
  539. query: {
  540. selected_replay_index: 1,
  541. },
  542. })
  543. )
  544. );
  545. });
  546. });
  547. });