groupReplays.spec.tsx 11 KB

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