groupReplays.spec.tsx 18 KB

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