groupReplays.spec.tsx 11 KB

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