groupReplays.spec.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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;
  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} = 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};
  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} = init({
  116. organizationProps: {features: []},
  117. }));
  118. render(<GroupReplays group={mockGroup} />, {
  119. router,
  120. organization,
  121. });
  122. expect(
  123. screen.getByText("You don't have access to this feature")
  124. ).toBeInTheDocument();
  125. });
  126. });
  127. describe('Replay Feature Enabled', () => {
  128. beforeEach(() => {
  129. ({router, organization} = init({}));
  130. });
  131. it('should query the replay-count endpoint with the fetched replayIds', async () => {
  132. const mockGroup = GroupFixture();
  133. const mockReplayCountApi = MockApiClient.addMockResponse({
  134. url: mockReplayCountUrl,
  135. body: {
  136. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  137. },
  138. });
  139. const mockReplayApi = MockApiClient.addMockResponse({
  140. url: mockReplayUrl,
  141. body: {
  142. data: [],
  143. },
  144. });
  145. render(<GroupReplays group={mockGroup} />, {
  146. router,
  147. organization,
  148. });
  149. await waitFor(() => {
  150. expect(mockReplayCountApi).toHaveBeenCalledWith(
  151. mockReplayCountUrl,
  152. expect.objectContaining({
  153. query: {
  154. returnIds: true,
  155. data_source: 'discover',
  156. query: `issue.id:[${mockGroup.id}]`,
  157. statsPeriod: '90d',
  158. project: -1,
  159. },
  160. })
  161. );
  162. // Expect api path to have the correct query params
  163. expect(mockReplayApi).toHaveBeenCalledWith(
  164. mockReplayUrl,
  165. expect.objectContaining({
  166. query: expect.objectContaining({
  167. environment: [],
  168. field: [
  169. 'activity',
  170. 'browser',
  171. 'count_dead_clicks',
  172. 'count_errors',
  173. 'count_rage_clicks',
  174. 'duration',
  175. 'finished_at',
  176. 'has_viewed',
  177. 'id',
  178. 'is_archived',
  179. 'os',
  180. 'project_id',
  181. 'started_at',
  182. 'user',
  183. ],
  184. per_page: 50,
  185. project: -1,
  186. queryReferrer: 'issueReplays',
  187. query: `id:[${REPLAY_ID_1},${REPLAY_ID_2}]`,
  188. sort: '-started_at',
  189. statsPeriod: '90d',
  190. }),
  191. })
  192. );
  193. });
  194. });
  195. it('should show empty message when no replays are found', async () => {
  196. const mockGroup = GroupFixture();
  197. const mockReplayCountApi = MockApiClient.addMockResponse({
  198. url: mockReplayCountUrl,
  199. body: {
  200. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  201. },
  202. });
  203. const mockReplayApi = MockApiClient.addMockResponse({
  204. url: mockReplayUrl,
  205. body: {
  206. data: [],
  207. },
  208. });
  209. render(<GroupReplays group={mockGroup} />, {
  210. router,
  211. organization,
  212. });
  213. expect(
  214. await screen.findByText('There are no items to display')
  215. ).toBeInTheDocument();
  216. expect(mockReplayCountApi).toHaveBeenCalled();
  217. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  218. });
  219. it('should display error message when api call fails', async () => {
  220. const mockGroup = GroupFixture();
  221. const mockReplayCountApi = MockApiClient.addMockResponse({
  222. url: mockReplayCountUrl,
  223. body: {
  224. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  225. },
  226. });
  227. const mockReplayApi = MockApiClient.addMockResponse({
  228. url: mockReplayUrl,
  229. statusCode: 500,
  230. body: {
  231. detail: 'Invalid number: asdf. Expected number.',
  232. },
  233. });
  234. render(<GroupReplays group={mockGroup} />, {
  235. router,
  236. organization,
  237. });
  238. expect(
  239. await screen.findByText('Invalid number: asdf. Expected number.')
  240. ).toBeInTheDocument();
  241. await waitFor(() => {
  242. expect(mockReplayCountApi).toHaveBeenCalled();
  243. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  244. });
  245. });
  246. it('should display default error message when api call fails without a body', async () => {
  247. const mockGroup = GroupFixture();
  248. const mockReplayCountApi = MockApiClient.addMockResponse({
  249. url: mockReplayCountUrl,
  250. body: {
  251. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  252. },
  253. });
  254. const mockReplayApi = MockApiClient.addMockResponse({
  255. url: mockReplayUrl,
  256. statusCode: 500,
  257. body: {},
  258. });
  259. render(<GroupReplays group={mockGroup} />, {
  260. router,
  261. organization,
  262. });
  263. expect(
  264. await screen.findByText(
  265. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  266. )
  267. ).toBeInTheDocument();
  268. await waitFor(() => {
  269. expect(mockReplayCountApi).toHaveBeenCalled();
  270. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  271. });
  272. });
  273. it('should show loading indicator when loading replays', async () => {
  274. const mockGroup = GroupFixture();
  275. const mockReplayCountApi = MockApiClient.addMockResponse({
  276. url: mockReplayCountUrl,
  277. body: {
  278. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  279. },
  280. });
  281. const mockReplayApi = MockApiClient.addMockResponse({
  282. url: mockReplayUrl,
  283. statusCode: 200,
  284. body: {
  285. data: [],
  286. },
  287. });
  288. render(<GroupReplays group={mockGroup} />, {
  289. router,
  290. organization,
  291. });
  292. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  293. await waitFor(() => {
  294. expect(mockReplayCountApi).toHaveBeenCalled();
  295. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  296. });
  297. });
  298. it('should show a list of replays and have the correct values', async () => {
  299. const mockGroup = GroupFixture();
  300. const mockReplayCountApi = MockApiClient.addMockResponse({
  301. url: mockReplayCountUrl,
  302. body: {
  303. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  304. },
  305. });
  306. const mockReplayApi = MockApiClient.addMockResponse({
  307. url: mockReplayUrl,
  308. statusCode: 200,
  309. body: {
  310. data: [
  311. {
  312. ...ReplayListFixture()[0],
  313. count_errors: 1,
  314. duration: 52346,
  315. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  316. id: REPLAY_ID_1,
  317. started_at: new Date('2022-09-15T06:50:00+00:00'),
  318. urls: [
  319. 'https://dev.getsentry.net:7999/replays/',
  320. '/organizations/org-slug/replays/?project=2',
  321. ],
  322. },
  323. {
  324. ...ReplayListFixture()[0],
  325. count_errors: 4,
  326. duration: 400,
  327. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  328. id: REPLAY_ID_2,
  329. started_at: new Date('2022-09-21T21:30:44+00:00'),
  330. urls: [
  331. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  332. '/organizations/org-slug/issues/',
  333. '/organizations/org-slug/issues/?project=2',
  334. ],
  335. },
  336. ].map(hydrated => ({
  337. ...hydrated,
  338. started_at: hydrated.started_at.toString(),
  339. finished_at: hydrated.finished_at.toString(),
  340. })),
  341. },
  342. });
  343. // Mock the system date to be 2022-09-28
  344. setMockDate(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  345. render(<GroupReplays group={mockGroup} />, {
  346. router,
  347. organization,
  348. });
  349. await waitFor(() => {
  350. expect(mockReplayCountApi).toHaveBeenCalled();
  351. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  352. });
  353. // Expect the table to have 2 rows
  354. expect(await screen.findAllByText('testDisplayName')).toHaveLength(2);
  355. const expectedQuery =
  356. 'query=&referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  357. // Expect the first row to have the correct href
  358. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  359. 'href',
  360. `/organizations/org-slug/replays/${REPLAY_ID_1}/?${expectedQuery}`
  361. );
  362. // Expect the second row to have the correct href
  363. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  364. 'href',
  365. `/organizations/org-slug/replays/${REPLAY_ID_2}/?${expectedQuery}`
  366. );
  367. // Expect the first row to have the correct duration
  368. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  369. // Expect the second row to have the correct duration
  370. expect(screen.getByText('06:40')).toBeInTheDocument();
  371. // Expect the first row to have the correct errors
  372. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent(
  373. '1'
  374. );
  375. // Expect the second row to have the correct errors
  376. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent(
  377. '4'
  378. );
  379. // Expect the first row to have the correct date
  380. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  381. // Expect the second row to have the correct date
  382. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  383. });
  384. it('Should render the replay player when replay-play-from-replay-tab is enabled', async () => {
  385. ({router, organization} = init({
  386. organizationProps: {features: ['replay-play-from-replay-tab', 'session-replay']},
  387. }));
  388. const mockGroup = GroupFixture();
  389. const mockReplayCountApi = MockApiClient.addMockResponse({
  390. url: mockReplayCountUrl,
  391. body: {
  392. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  393. },
  394. });
  395. MockApiClient.addMockResponse({
  396. url: mockReplayUrl,
  397. statusCode: 200,
  398. body: {
  399. data: [
  400. {
  401. ...ReplayListFixture()[0],
  402. count_errors: 1,
  403. duration: 52346,
  404. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  405. id: REPLAY_ID_1,
  406. started_at: new Date('2022-09-15T06:50:00+00:00'),
  407. urls: [
  408. 'https://dev.getsentry.net:7999/replays/',
  409. '/organizations/org-slug/replays/?project=2',
  410. ],
  411. },
  412. {
  413. ...ReplayListFixture()[0],
  414. count_errors: 4,
  415. duration: 400,
  416. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  417. id: REPLAY_ID_2,
  418. started_at: new Date('2022-09-21T21:30:44+00:00'),
  419. urls: [
  420. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  421. '/organizations/org-slug/issues/',
  422. '/organizations/org-slug/issues/?project=2',
  423. ],
  424. },
  425. ].map(hydrated => ({
  426. ...hydrated,
  427. started_at: hydrated.started_at.toString(),
  428. finished_at: hydrated.finished_at.toString(),
  429. })),
  430. },
  431. });
  432. render(<GroupReplays group={mockGroup} />, {
  433. router,
  434. organization,
  435. });
  436. expect(await screen.findByText('See Full Replay')).toBeInTheDocument();
  437. expect(mockReplayCountApi).toHaveBeenCalledWith(
  438. mockReplayCountUrl,
  439. expect.objectContaining({
  440. query: {
  441. returnIds: true,
  442. data_source: 'discover',
  443. query: `issue.id:[${mockGroup.id}]`,
  444. statsPeriod: '90d',
  445. project: -1,
  446. },
  447. })
  448. );
  449. });
  450. it('Should switch replays when clicking and replay-play-from-replay-tab is enabled', async () => {
  451. ({router, organization} = init({
  452. organizationProps: {features: ['session-replay']},
  453. }));
  454. const mockGroup = GroupFixture();
  455. const mockReplayRecord = mockReplay?.getReplay();
  456. const mockReplayCountApi = MockApiClient.addMockResponse({
  457. url: mockReplayCountUrl,
  458. body: {
  459. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  460. },
  461. });
  462. MockApiClient.addMockResponse({
  463. url: mockReplayUrl,
  464. statusCode: 200,
  465. body: {
  466. data: [
  467. {
  468. ...ReplayListFixture()[0],
  469. count_errors: 1,
  470. duration: 52346,
  471. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  472. id: REPLAY_ID_1,
  473. started_at: new Date('2022-09-15T06:50:00+00:00'),
  474. urls: [
  475. 'https://dev.getsentry.net:7999/replays/',
  476. '/organizations/org-slug/replays/?project=2',
  477. ],
  478. },
  479. {
  480. ...ReplayListFixture()[0],
  481. count_errors: 4,
  482. duration: 400,
  483. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  484. id: REPLAY_ID_2,
  485. started_at: new Date('2022-09-21T21:30:44+00:00'),
  486. urls: [
  487. 'https://dev.getsentry.net:7999/organizations/org-slug/replays/?project=2&statsPeriod=24h',
  488. '/organizations/org-slug/issues/',
  489. '/organizations/org-slug/issues/?project=2',
  490. ],
  491. },
  492. ].map(hydrated => ({
  493. ...hydrated,
  494. started_at: hydrated.started_at.toString(),
  495. finished_at: hydrated.finished_at.toString(),
  496. })),
  497. },
  498. });
  499. MockApiClient.addMockResponse({
  500. method: 'POST',
  501. url: `/projects/${organization.slug}/${mockReplayRecord?.project_id}/replays/${mockReplayRecord?.id}/viewed-by/`,
  502. });
  503. render(<GroupReplays group={mockGroup} />, {
  504. router,
  505. organization,
  506. });
  507. await waitFor(() => {
  508. expect(mockReplayCountApi).toHaveBeenCalledWith(
  509. mockReplayCountUrl,
  510. expect.objectContaining({
  511. query: {
  512. returnIds: true,
  513. data_source: 'discover',
  514. query: `issue.id:[${mockGroup.id}]`,
  515. statsPeriod: '90d',
  516. project: -1,
  517. },
  518. })
  519. );
  520. });
  521. const mockReplace = jest.mocked(browserHistory.replace);
  522. const replayPlayPlause = (
  523. await screen.findAllByTestId('replay-table-play-button')
  524. )[0];
  525. await userEvent.click(replayPlayPlause);
  526. await waitFor(() =>
  527. expect(mockReplace).toHaveBeenCalledWith(
  528. expect.objectContaining({
  529. pathname: '/organizations/org-slug/replays/',
  530. query: {
  531. selected_replay_index: 1,
  532. },
  533. })
  534. )
  535. );
  536. });
  537. });
  538. });