groupReplays.spec.tsx 11 KB

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