groupReplays.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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, 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 {ReplayListFixture} from 'sentry-fixture/replayList';
  19. import {resetMockDate, setMockDate} from 'sentry-test/utils';
  20. const REPLAY_ID_1 = '346789a703f6454384f1de473b8b9fcc';
  21. const REPLAY_ID_2 = 'b05dae9b6be54d21a4d5ad9f8f02b780';
  22. function init({organizationProps = {features: ['session-replay']}}: InitializeOrgProps) {
  23. const mockProject = ProjectFixture();
  24. const {router, organization, routerContext} = initializeOrg({
  25. organization: {
  26. ...organizationProps,
  27. },
  28. project: mockProject,
  29. projects: [mockProject],
  30. router: {
  31. routes: [
  32. {path: '/'},
  33. {path: '/organizations/:orgId/issues/:groupId/'},
  34. {path: 'replays/'},
  35. ],
  36. location: {
  37. pathname: '/organizations/org-slug/replays/',
  38. query: {},
  39. },
  40. },
  41. });
  42. ProjectsStore.init();
  43. ProjectsStore.loadInitialData(organization.projects);
  44. return {router, organization, routerContext};
  45. }
  46. describe('GroupReplays', () => {
  47. beforeEach(() => {
  48. MockApiClient.clearMockResponses();
  49. MockApiClient.addMockResponse({
  50. method: 'GET',
  51. url: `/organizations/org-slug/sdk-updates/`,
  52. body: [],
  53. });
  54. });
  55. afterEach(() => {
  56. resetMockDate();
  57. });
  58. describe('Replay Feature Disabled', () => {
  59. const mockGroup = GroupFixture();
  60. const {router, organization, routerContext} = init({
  61. organizationProps: {features: []},
  62. });
  63. it("should show a message when the organization doesn't have access to the replay feature", () => {
  64. render(<GroupReplays group={mockGroup} />, {
  65. context: routerContext,
  66. organization,
  67. router,
  68. });
  69. expect(
  70. screen.getByText("You don't have access to this feature")
  71. ).toBeInTheDocument();
  72. });
  73. });
  74. describe('Replay Feature Enabled', () => {
  75. const {router, organization, routerContext} = init({});
  76. it('should query the replay-count endpoint with the fetched replayIds', async () => {
  77. const mockGroup = GroupFixture();
  78. const mockReplayCountApi = MockApiClient.addMockResponse({
  79. url: mockReplayCountUrl,
  80. body: {
  81. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  82. },
  83. });
  84. const mockReplayApi = MockApiClient.addMockResponse({
  85. url: mockReplayUrl,
  86. body: {
  87. data: [],
  88. },
  89. });
  90. render(<GroupReplays group={mockGroup} />, {
  91. context: routerContext,
  92. organization,
  93. router,
  94. });
  95. await waitFor(() => {
  96. expect(mockReplayCountApi).toHaveBeenCalledWith(
  97. mockReplayCountUrl,
  98. expect.objectContaining({
  99. query: {
  100. returnIds: true,
  101. data_source: 'discover',
  102. query: `issue.id:[${mockGroup.id}]`,
  103. statsPeriod: '14d',
  104. project: -1,
  105. },
  106. })
  107. );
  108. // Expect api path to have the correct query params
  109. expect(mockReplayApi).toHaveBeenCalledWith(
  110. mockReplayUrl,
  111. expect.objectContaining({
  112. query: expect.objectContaining({
  113. environment: [],
  114. field: [
  115. 'activity',
  116. 'browser',
  117. 'count_dead_clicks',
  118. 'count_errors',
  119. 'count_rage_clicks',
  120. 'duration',
  121. 'finished_at',
  122. 'id',
  123. 'is_archived',
  124. 'os',
  125. 'project_id',
  126. 'started_at',
  127. 'user',
  128. ],
  129. per_page: 50,
  130. project: -1,
  131. queryReferrer: 'issueReplays',
  132. query: `id:[${REPLAY_ID_1},${REPLAY_ID_2}]`,
  133. sort: '-started_at',
  134. statsPeriod: '14d',
  135. }),
  136. })
  137. );
  138. });
  139. });
  140. it('should show empty message when no replays are found', async () => {
  141. const mockGroup = GroupFixture();
  142. const mockReplayCountApi = MockApiClient.addMockResponse({
  143. url: mockReplayCountUrl,
  144. body: {
  145. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  146. },
  147. });
  148. const mockReplayApi = MockApiClient.addMockResponse({
  149. url: mockReplayUrl,
  150. body: {
  151. data: [],
  152. },
  153. });
  154. render(<GroupReplays group={mockGroup} />, {
  155. context: routerContext,
  156. organization,
  157. router,
  158. });
  159. expect(
  160. await screen.findByText('There are no items to display')
  161. ).toBeInTheDocument();
  162. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  163. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  164. });
  165. it('should display error message when api call fails', async () => {
  166. const mockGroup = GroupFixture();
  167. const mockReplayCountApi = MockApiClient.addMockResponse({
  168. url: mockReplayCountUrl,
  169. body: {
  170. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  171. },
  172. });
  173. const mockReplayApi = MockApiClient.addMockResponse({
  174. url: mockReplayUrl,
  175. statusCode: 500,
  176. body: {
  177. detail: 'Invalid number: asdf. Expected number.',
  178. },
  179. });
  180. render(<GroupReplays group={mockGroup} />, {
  181. context: routerContext,
  182. organization,
  183. router,
  184. });
  185. await waitFor(() => {
  186. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  187. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  188. expect(
  189. screen.getByText('Invalid number: asdf. Expected number.')
  190. ).toBeInTheDocument();
  191. });
  192. });
  193. it('should display default error message when api call fails without a body', async () => {
  194. const mockGroup = GroupFixture();
  195. const mockReplayCountApi = MockApiClient.addMockResponse({
  196. url: mockReplayCountUrl,
  197. body: {
  198. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  199. },
  200. });
  201. const mockReplayApi = MockApiClient.addMockResponse({
  202. url: mockReplayUrl,
  203. statusCode: 500,
  204. body: {},
  205. });
  206. render(<GroupReplays group={mockGroup} />, {
  207. context: routerContext,
  208. organization,
  209. router,
  210. });
  211. await waitFor(() => {
  212. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  213. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  214. expect(
  215. screen.getByText(
  216. 'Sorry, the list of replays could not be loaded. This could be due to invalid search parameters or an internal systems error.'
  217. )
  218. ).toBeInTheDocument();
  219. });
  220. });
  221. it('should show loading indicator when loading replays', async () => {
  222. const mockGroup = GroupFixture();
  223. const mockReplayCountApi = MockApiClient.addMockResponse({
  224. url: mockReplayCountUrl,
  225. body: {
  226. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  227. },
  228. });
  229. const mockReplayApi = MockApiClient.addMockResponse({
  230. url: mockReplayUrl,
  231. statusCode: 200,
  232. body: {
  233. data: [],
  234. },
  235. });
  236. render(<GroupReplays group={mockGroup} />, {
  237. context: routerContext,
  238. organization,
  239. router,
  240. });
  241. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  242. await waitFor(() => {
  243. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  244. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  245. });
  246. });
  247. it('should show a list of replays and have the correct values', async () => {
  248. const mockGroup = GroupFixture();
  249. const mockReplayCountApi = MockApiClient.addMockResponse({
  250. url: mockReplayCountUrl,
  251. body: {
  252. [mockGroup.id]: [REPLAY_ID_1, REPLAY_ID_2],
  253. },
  254. });
  255. const mockReplayApi = MockApiClient.addMockResponse({
  256. url: mockReplayUrl,
  257. statusCode: 200,
  258. body: {
  259. data: [
  260. {
  261. ...ReplayListFixture()[0],
  262. count_errors: 1,
  263. duration: 52346,
  264. finished_at: new Date('2022-09-15T06:54:00+00:00'),
  265. id: '346789a703f6454384f1de473b8b9fcc',
  266. started_at: new Date('2022-09-15T06:50:00+00:00'),
  267. urls: [
  268. 'https://dev.getsentry.net:7999/replays/',
  269. '/organizations/sentry-emerging-tech/replays/?project=2',
  270. ],
  271. },
  272. {
  273. ...ReplayListFixture()[0],
  274. count_errors: 4,
  275. duration: 400,
  276. finished_at: new Date('2022-09-21T21:40:38+00:00'),
  277. id: 'b05dae9b6be54d21a4d5ad9f8f02b780',
  278. started_at: new Date('2022-09-21T21:30:44+00:00'),
  279. urls: [
  280. 'https://dev.getsentry.net:7999/organizations/sentry-emerging-tech/replays/?project=2&statsPeriod=24h',
  281. '/organizations/sentry-emerging-tech/issues/',
  282. '/organizations/sentry-emerging-tech/issues/?project=2',
  283. ],
  284. },
  285. ].map(hydrated => ({
  286. ...hydrated,
  287. started_at: hydrated.started_at.toString(),
  288. finished_at: hydrated.finished_at.toString(),
  289. })),
  290. },
  291. });
  292. // Mock the system date to be 2022-09-28
  293. setMockDate(new Date('Sep 28, 2022 11:29:13 PM UTC'));
  294. render(<GroupReplays group={mockGroup} />, {
  295. context: routerContext,
  296. organization,
  297. router,
  298. });
  299. await waitFor(() => {
  300. expect(mockReplayCountApi).toHaveBeenCalledTimes(1);
  301. expect(mockReplayApi).toHaveBeenCalledTimes(1);
  302. });
  303. // Expect the table to have 2 rows
  304. expect(await screen.findAllByText('testDisplayName')).toHaveLength(2);
  305. const expectedQuery =
  306. 'query=&referrer=%2Forganizations%2F%3AorgId%2Fissues%2F%3AgroupId%2Freplays%2F&statsPeriod=14d&yAxis=count%28%29';
  307. // Expect the first row to have the correct href
  308. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[0]).toHaveAttribute(
  309. 'href',
  310. `/organizations/org-slug/replays/${REPLAY_ID_1}/?${expectedQuery}`
  311. );
  312. // Expect the second row to have the correct href
  313. expect(screen.getAllByRole('link', {name: 'testDisplayName'})[1]).toHaveAttribute(
  314. 'href',
  315. `/organizations/org-slug/replays/${REPLAY_ID_2}/?${expectedQuery}`
  316. );
  317. // Expect the first row to have the correct duration
  318. expect(screen.getByText('14:32:26')).toBeInTheDocument();
  319. // Expect the second row to have the correct duration
  320. expect(screen.getByText('06:40')).toBeInTheDocument();
  321. // Expect the first row to have the correct errors
  322. expect(screen.getAllByTestId('replay-table-count-errors')[0]).toHaveTextContent(
  323. '1'
  324. );
  325. // Expect the second row to have the correct errors
  326. expect(screen.getAllByTestId('replay-table-count-errors')[1]).toHaveTextContent(
  327. '4'
  328. );
  329. // Expect the first row to have the correct date
  330. expect(screen.getByText('14 days ago')).toBeInTheDocument();
  331. // Expect the second row to have the correct date
  332. expect(screen.getByText('7 days ago')).toBeInTheDocument();
  333. });
  334. });
  335. });