groupDetails.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import {ConfigFixture} from 'sentry-fixture/config';
  2. import {EnvironmentsFixture} from 'sentry-fixture/environments';
  3. import {EventFixture} from 'sentry-fixture/event';
  4. import {EventsStatsFixture} from 'sentry-fixture/events';
  5. import {GroupFixture} from 'sentry-fixture/group';
  6. import {LocationFixture} from 'sentry-fixture/locationFixture';
  7. import {ProjectFixture} from 'sentry-fixture/project';
  8. import {TeamFixture} from 'sentry-fixture/team';
  9. import {UserFixture} from 'sentry-fixture/user';
  10. import {initializeOrg} from 'sentry-test/initializeOrg';
  11. import {act, render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  12. import ConfigStore from 'sentry/stores/configStore';
  13. import GroupStore from 'sentry/stores/groupStore';
  14. import OrganizationStore from 'sentry/stores/organizationStore';
  15. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  16. import ProjectsStore from 'sentry/stores/projectsStore';
  17. import {IssueCategory} from 'sentry/types/group';
  18. import {useNavigate} from 'sentry/utils/useNavigate';
  19. import GroupDetails from 'sentry/views/issueDetails/groupDetails';
  20. const SAMPLE_EVENT_ALERT_TEXT =
  21. 'You are viewing a sample error. Configure Sentry to start viewing real errors.';
  22. jest.mock('sentry/utils/useNavigate', () => ({
  23. useNavigate: jest.fn(),
  24. }));
  25. describe('groupDetails', () => {
  26. let mockNavigate: jest.Mock;
  27. const group = GroupFixture({issueCategory: IssueCategory.ERROR});
  28. const event = EventFixture();
  29. const project = ProjectFixture({teams: [TeamFixture()]});
  30. const routes = [
  31. {path: '/', childRoutes: []},
  32. {childRoutes: []},
  33. {
  34. path: '/organizations/:orgId/issues/:groupId/',
  35. childRoutes: [],
  36. },
  37. {},
  38. ];
  39. const initRouter = {
  40. location: {
  41. pathname: `/organizations/org-slug/issues/${group.id}/`,
  42. query: {},
  43. search: '?foo=bar',
  44. hash: '#hash',
  45. },
  46. params: {
  47. groupId: group.id,
  48. },
  49. routes,
  50. };
  51. const defaultInit = initializeOrg<{groupId: string}>({
  52. router: initRouter,
  53. });
  54. const recommendedUser = UserFixture({
  55. options: {
  56. ...UserFixture().options,
  57. defaultIssueEvent: 'recommended',
  58. },
  59. });
  60. const latestUser = UserFixture({
  61. options: {
  62. ...UserFixture().options,
  63. defaultIssueEvent: 'latest',
  64. },
  65. });
  66. const oldestUser = UserFixture({
  67. options: {
  68. ...UserFixture().options,
  69. defaultIssueEvent: 'oldest',
  70. },
  71. });
  72. function MockComponent() {
  73. return <div>Group Details Mock</div>;
  74. }
  75. const createWrapper = (init = defaultInit) => {
  76. return render(
  77. <GroupDetails {...init.routerProps}>
  78. <MockComponent />
  79. </GroupDetails>,
  80. {organization: init.organization, router: init.router}
  81. );
  82. };
  83. beforeEach(() => {
  84. mockNavigate = jest.fn();
  85. MockApiClient.clearMockResponses();
  86. OrganizationStore.onUpdate(defaultInit.organization);
  87. act(() => ProjectsStore.loadInitialData(defaultInit.projects));
  88. jest.mocked(useNavigate).mockReturnValue(mockNavigate);
  89. MockApiClient.addMockResponse({
  90. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  91. body: {...group},
  92. });
  93. MockApiClient.addMockResponse({
  94. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  95. statusCode: 200,
  96. body: {
  97. ...event,
  98. },
  99. });
  100. MockApiClient.addMockResponse({
  101. url: `/projects/org-slug/${project.slug}/issues/`,
  102. method: 'PUT',
  103. body: {
  104. hasSeen: false,
  105. },
  106. });
  107. MockApiClient.addMockResponse({
  108. url: '/organizations/org-slug/projects/',
  109. body: [project],
  110. });
  111. MockApiClient.addMockResponse({
  112. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/first-last-release/`,
  113. method: 'GET',
  114. });
  115. MockApiClient.addMockResponse({
  116. url: `/organizations/${defaultInit.organization.slug}/events/`,
  117. statusCode: 200,
  118. body: {
  119. data: [
  120. {
  121. 'count()': 1,
  122. },
  123. ],
  124. },
  125. });
  126. MockApiClient.addMockResponse({
  127. url: `/organizations/${defaultInit.organization.slug}/environments/`,
  128. body: EnvironmentsFixture(),
  129. });
  130. MockApiClient.addMockResponse({
  131. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  132. body: [],
  133. });
  134. MockApiClient.addMockResponse({
  135. url: '/organizations/org-slug/replay-count/',
  136. body: {},
  137. });
  138. MockApiClient.addMockResponse({
  139. url: `/projects/${defaultInit.organization.slug}/${project.slug}/`,
  140. body: project,
  141. });
  142. });
  143. afterEach(() => {
  144. act(() => ProjectsStore.reset());
  145. GroupStore.reset();
  146. PageFiltersStore.reset();
  147. MockApiClient.clearMockResponses();
  148. });
  149. it('renders', async function () {
  150. act(() => ProjectsStore.reset());
  151. createWrapper();
  152. expect(screen.queryByText(group.title)).not.toBeInTheDocument();
  153. act(() => ProjectsStore.loadInitialData(defaultInit.projects));
  154. expect(await screen.findByText(group.shortId)).toBeInTheDocument();
  155. // Sample event alert should not show up
  156. expect(screen.queryByText(SAMPLE_EVENT_ALERT_TEXT)).not.toBeInTheDocument();
  157. });
  158. it('renders error when issue is not found', async function () {
  159. MockApiClient.addMockResponse({
  160. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  161. statusCode: 404,
  162. });
  163. MockApiClient.addMockResponse({
  164. url: `/organization/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  165. statusCode: 404,
  166. });
  167. createWrapper();
  168. await waitFor(() =>
  169. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  170. );
  171. expect(
  172. await screen.findByText('The issue you were looking for was not found.')
  173. ).toBeInTheDocument();
  174. });
  175. it('renders MissingProjectMembership when trying to access issue in project the user does not belong to', async function () {
  176. MockApiClient.addMockResponse({
  177. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  178. statusCode: 403,
  179. });
  180. MockApiClient.addMockResponse({
  181. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  182. statusCode: 403,
  183. });
  184. createWrapper();
  185. expect(
  186. await screen.findByText(
  187. 'No teams have access to this project yet. Ask an admin to add your team to this project.'
  188. )
  189. ).toBeInTheDocument();
  190. });
  191. it('fetches issue details for a given environment', async function () {
  192. const mock = MockApiClient.addMockResponse({
  193. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  194. body: group,
  195. });
  196. const init = initializeOrg({
  197. router: {
  198. ...initRouter,
  199. location: LocationFixture({
  200. ...initRouter.location,
  201. query: {environment: 'staging'},
  202. }),
  203. },
  204. });
  205. createWrapper(init);
  206. await waitFor(() =>
  207. expect(mock).toHaveBeenCalledWith(
  208. expect.anything(),
  209. expect.objectContaining({
  210. query: {
  211. collapse: ['release', 'tags'],
  212. environment: ['staging'],
  213. expand: ['inbox', 'owners'],
  214. },
  215. })
  216. )
  217. );
  218. });
  219. it('renders substatus badge', async function () {
  220. MockApiClient.addMockResponse({
  221. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  222. body: {
  223. ...group,
  224. inbox: null,
  225. status: 'unresolved',
  226. substatus: 'ongoing',
  227. },
  228. });
  229. createWrapper({
  230. ...defaultInit,
  231. organization: {...defaultInit.organization},
  232. });
  233. expect(await screen.findByText('Ongoing')).toBeInTheDocument();
  234. });
  235. it('renders alert for sample event', async function () {
  236. MockApiClient.addMockResponse({
  237. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  238. body: [{key: 'sample_event'}],
  239. });
  240. createWrapper();
  241. expect(await screen.findByText(SAMPLE_EVENT_ALERT_TEXT)).toBeInTheDocument();
  242. });
  243. it('renders error when project does not exist', async function () {
  244. MockApiClient.addMockResponse({
  245. url: `/projects/org-slug/other-project-slug/issues/`,
  246. method: 'PUT',
  247. });
  248. MockApiClient.addMockResponse({
  249. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  250. body: {...group, project: {slug: 'other-project-slug'}},
  251. });
  252. MockApiClient.addMockResponse({
  253. url: `/projects/${defaultInit.organization.slug}/other-project-slug/`,
  254. body: {},
  255. });
  256. createWrapper();
  257. expect(
  258. await screen.findByText('The project other-project-slug does not exist')
  259. ).toBeInTheDocument();
  260. });
  261. it('uses /recommended endpoint when feature flag is on and no event is provided', async function () {
  262. const recommendedMock = MockApiClient.addMockResponse({
  263. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  264. statusCode: 200,
  265. body: event,
  266. });
  267. createWrapper();
  268. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  269. });
  270. it("refires request when recommended endpoint doesn't return an event", async function () {
  271. const recommendedWithSearchMock = MockApiClient.addMockResponse({
  272. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  273. query: {
  274. query: 'foo:bar',
  275. statsPeriod: '14d',
  276. },
  277. statusCode: 404,
  278. body: {
  279. detail: 'No matching event',
  280. },
  281. });
  282. createWrapper({
  283. ...defaultInit,
  284. router: {
  285. ...defaultInit.router,
  286. location: LocationFixture({
  287. query: {
  288. query: 'foo:bar',
  289. statsPeriod: '14d',
  290. },
  291. }),
  292. },
  293. });
  294. await waitFor(() => expect(recommendedWithSearchMock).toHaveBeenCalledTimes(1));
  295. await waitFor(() =>
  296. expect(mockNavigate).toHaveBeenCalledWith(
  297. expect.objectContaining(
  298. // query and period should be removed
  299. {query: {}}
  300. ),
  301. {
  302. replace: true,
  303. }
  304. )
  305. );
  306. });
  307. it('does not refire for request with streamlined UI', async function () {
  308. // Bunch of mocks to load streamlined UI
  309. MockApiClient.addMockResponse({
  310. url: '/organizations/org-slug/flags/logs/',
  311. body: {data: []},
  312. });
  313. MockApiClient.addMockResponse({
  314. url: `/organizations/${defaultInit.organization.slug}/users/`,
  315. body: [],
  316. });
  317. MockApiClient.addMockResponse({
  318. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/attachments/`,
  319. body: [],
  320. });
  321. MockApiClient.addMockResponse({
  322. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  323. body: [],
  324. });
  325. MockApiClient.addMockResponse({
  326. url: `/organizations/${defaultInit.organization.slug}/releases/stats/`,
  327. body: [],
  328. });
  329. MockApiClient.addMockResponse({
  330. url: `/organizations/${defaultInit.organization.slug}/events-stats/`,
  331. body: {'count()': EventsStatsFixture(), 'count_unique(user)': EventsStatsFixture()},
  332. method: 'GET',
  333. });
  334. MockApiClient.addMockResponse({
  335. url: `/organizations/${defaultInit.organization.slug}/events/`,
  336. body: {data: [{'count_unique(user)': 21}]},
  337. });
  338. MockApiClient.addMockResponse({
  339. url: `/organizations/${defaultInit.organization.slug}/sentry-app-installations/`,
  340. body: [],
  341. });
  342. MockApiClient.addMockResponse({
  343. url: `/organizations/${defaultInit.organization.slug}/sentry-app-components/`,
  344. body: [],
  345. });
  346. const recommendedWithSearchMock = MockApiClient.addMockResponse({
  347. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  348. query: {
  349. query: 'foo:bar',
  350. statsPeriod: '14d',
  351. },
  352. statusCode: 404,
  353. body: {
  354. detail: 'No matching event',
  355. },
  356. });
  357. createWrapper({
  358. ...defaultInit,
  359. router: {
  360. ...defaultInit.router,
  361. location: LocationFixture({
  362. query: {
  363. query: 'foo:bar',
  364. statsPeriod: '14d',
  365. streamline: '1',
  366. },
  367. }),
  368. },
  369. });
  370. await waitFor(() => expect(recommendedWithSearchMock).toHaveBeenCalledTimes(1));
  371. await waitFor(() => expect(mockNavigate).not.toHaveBeenCalled());
  372. });
  373. it('uses /latest endpoint when default is set to latest', async function () {
  374. ConfigStore.loadInitialData(ConfigFixture({user: latestUser}));
  375. const latestMock = MockApiClient.addMockResponse({
  376. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/latest/`,
  377. statusCode: 200,
  378. body: event,
  379. });
  380. createWrapper();
  381. await waitFor(() => expect(latestMock).toHaveBeenCalledTimes(1));
  382. });
  383. it('uses /oldest endpoint when default is set to oldest', async function () {
  384. ConfigStore.loadInitialData(ConfigFixture({user: oldestUser}));
  385. const oldestMock = MockApiClient.addMockResponse({
  386. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/oldest/`,
  387. statusCode: 200,
  388. body: event,
  389. });
  390. createWrapper();
  391. await waitFor(() => expect(oldestMock).toHaveBeenCalledTimes(1));
  392. });
  393. it('uses /recommended endpoint when default is set to recommended', async function () {
  394. ConfigStore.loadInitialData(ConfigFixture({user: recommendedUser}));
  395. const recommendedMock = MockApiClient.addMockResponse({
  396. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  397. statusCode: 200,
  398. body: event,
  399. });
  400. createWrapper();
  401. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  402. });
  403. });