groupReplays.spec.tsx 11 KB

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