groupDetails.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {act, render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import GroupStore from 'sentry/stores/groupStore';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import {Environment, Group, IssueCategory} from 'sentry/types';
  10. import GroupDetails from 'sentry/views/issueDetails';
  11. jest.unmock('sentry/utils/recreateRoute');
  12. const SAMPLE_EVENT_ALERT_TEXT =
  13. 'You are viewing a sample error. Configure Sentry to start viewing real errors.';
  14. describe('groupDetails', () => {
  15. const group = TestStubs.Group({issueCategory: IssueCategory.ERROR});
  16. const event = TestStubs.Event();
  17. const project = TestStubs.Project({teams: [TestStubs.Team()]});
  18. const routes = [
  19. {path: '/', childRoutes: []},
  20. {childRoutes: []},
  21. {
  22. path: '/organizations/:orgId/issues/:groupId/',
  23. childRoutes: [],
  24. },
  25. {},
  26. ];
  27. const initRouter = {
  28. location: {
  29. pathname: `/organizations/org-slug/issues/${group.id}/`,
  30. query: {},
  31. search: '?foo=bar',
  32. hash: '#hash',
  33. },
  34. params: {
  35. groupId: group.id,
  36. },
  37. routes,
  38. };
  39. const defaultInit = initializeOrg<{groupId: string}>({
  40. project,
  41. router: initRouter,
  42. });
  43. const recommendedUser = TestStubs.User({
  44. options: {
  45. defaultIssueEvent: 'recommended',
  46. },
  47. });
  48. const latestUser = TestStubs.User({
  49. options: {
  50. defaultIssueEvent: 'latest',
  51. },
  52. });
  53. const oldestUser = TestStubs.User({
  54. options: {
  55. defaultIssueEvent: 'oldest',
  56. },
  57. });
  58. function MockComponent({
  59. group: groupProp,
  60. environments,
  61. eventError,
  62. }: {
  63. environments?: Environment[];
  64. eventError?: boolean;
  65. group?: Group;
  66. }) {
  67. return (
  68. <div>
  69. Group Details Mock
  70. <div>title: {groupProp?.title}</div>
  71. <div>environment: {environments?.join(' ')}</div>
  72. {eventError && <div>eventError</div>}
  73. </div>
  74. );
  75. }
  76. const createWrapper = (init = defaultInit) => {
  77. return render(
  78. <GroupDetails {...init.routerProps}>
  79. <MockComponent />
  80. </GroupDetails>,
  81. {context: init.routerContext, organization: init.organization, router: init.router}
  82. );
  83. };
  84. beforeEach(() => {
  85. MockApiClient.clearMockResponses();
  86. OrganizationStore.onUpdate(defaultInit.organization);
  87. act(() => ProjectsStore.loadInitialData(defaultInit.organization.projects));
  88. MockApiClient.addMockResponse({
  89. url: `/issues/${group.id}/`,
  90. body: {...group},
  91. });
  92. MockApiClient.addMockResponse({
  93. url: `/issues/${group.id}/events/latest/`,
  94. statusCode: 200,
  95. body: {
  96. ...event,
  97. },
  98. });
  99. MockApiClient.addMockResponse({
  100. url: `/projects/org-slug/${project.slug}/issues/`,
  101. method: 'PUT',
  102. body: {
  103. hasSeen: false,
  104. },
  105. });
  106. MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/projects/',
  108. body: [project],
  109. });
  110. MockApiClient.addMockResponse({
  111. url: `/issues/${group.id}/first-last-release/`,
  112. method: 'GET',
  113. });
  114. MockApiClient.addMockResponse({
  115. url: `/organizations/${defaultInit.organization.slug}/events/`,
  116. statusCode: 200,
  117. body: {
  118. data: [
  119. {
  120. 'count()': 1,
  121. },
  122. ],
  123. },
  124. });
  125. MockApiClient.addMockResponse({
  126. url: `/organizations/${defaultInit.organization.slug}/environments/`,
  127. body: TestStubs.Environments(),
  128. });
  129. MockApiClient.addMockResponse({
  130. url: `/issues/${group.id}/tags/`,
  131. body: [],
  132. });
  133. });
  134. afterEach(() => {
  135. act(() => ProjectsStore.reset());
  136. GroupStore.reset();
  137. PageFiltersStore.reset();
  138. MockApiClient.clearMockResponses();
  139. });
  140. it('renders', async function () {
  141. act(() => ProjectsStore.reset());
  142. createWrapper();
  143. expect(screen.queryByText(group.title)).not.toBeInTheDocument();
  144. act(() => ProjectsStore.loadInitialData(defaultInit.organization.projects));
  145. expect(await screen.findByText(group.title, {exact: false})).toBeInTheDocument();
  146. // Sample event alert should not show up
  147. expect(screen.queryByText(SAMPLE_EVENT_ALERT_TEXT)).not.toBeInTheDocument();
  148. });
  149. it('renders error when issue is not found', async function () {
  150. MockApiClient.addMockResponse({
  151. url: `/issues/${group.id}/`,
  152. statusCode: 404,
  153. });
  154. MockApiClient.addMockResponse({
  155. url: `/issues/${group.id}/events/latest/`,
  156. statusCode: 404,
  157. });
  158. createWrapper();
  159. await waitFor(() =>
  160. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  161. );
  162. expect(
  163. await screen.findByText('The issue you were looking for was not found.')
  164. ).toBeInTheDocument();
  165. });
  166. it('renders MissingProjectMembership when trying to access issue in project the user does not belong to', async function () {
  167. MockApiClient.addMockResponse({
  168. url: `/issues/${group.id}/`,
  169. statusCode: 403,
  170. });
  171. MockApiClient.addMockResponse({
  172. url: `/issues/${group.id}/events/latest/`,
  173. statusCode: 403,
  174. });
  175. createWrapper();
  176. await waitFor(() =>
  177. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  178. );
  179. expect(
  180. await screen.findByText(
  181. 'No teams have access to this project yet. Ask an admin to add your team to this project.'
  182. )
  183. ).toBeInTheDocument();
  184. });
  185. it('fetches issue details for a given environment', async function () {
  186. const init = initializeOrg({
  187. router: {
  188. ...initRouter,
  189. location: TestStubs.location({
  190. ...initRouter.location,
  191. query: {environment: 'staging'},
  192. }),
  193. },
  194. });
  195. createWrapper(init);
  196. await waitFor(() =>
  197. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  198. );
  199. expect(await screen.findByText('environment: staging')).toBeInTheDocument();
  200. });
  201. /**
  202. * This is legacy code that I'm not even sure still happens
  203. */
  204. it('redirects to new issue if params id !== id returned from API request', async function () {
  205. MockApiClient.addMockResponse({
  206. url: `/issues/${group.id}/`,
  207. body: {...group, id: 'new-id'},
  208. });
  209. createWrapper();
  210. expect(screen.queryByText('Group Details Mock')).not.toBeInTheDocument();
  211. await waitFor(() => {
  212. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  213. });
  214. expect(browserHistory.push).toHaveBeenCalledWith(
  215. '/organizations/org-slug/issues/new-id/?foo=bar#hash'
  216. );
  217. });
  218. it('renders issue event error', async function () {
  219. MockApiClient.addMockResponse({
  220. url: `/issues/${group.id}/events/latest/`,
  221. statusCode: 404,
  222. });
  223. createWrapper();
  224. expect(await screen.findByText('eventError')).toBeInTheDocument();
  225. });
  226. it('renders for review reason', async function () {
  227. MockApiClient.addMockResponse({
  228. url: `/issues/${group.id}/`,
  229. body: {
  230. ...group,
  231. inbox: {
  232. date_added: '2020-11-24T13:17:42.248751Z',
  233. reason: 0,
  234. reason_details: null,
  235. },
  236. },
  237. });
  238. createWrapper();
  239. expect(await screen.findByText('New Issue')).toBeInTheDocument();
  240. });
  241. it('renders substatus badge', async function () {
  242. MockApiClient.addMockResponse({
  243. url: `/issues/${group.id}/`,
  244. body: {
  245. ...group,
  246. inbox: null,
  247. status: 'unresolved',
  248. substatus: 'ongoing',
  249. },
  250. });
  251. createWrapper({
  252. ...defaultInit,
  253. organization: {...defaultInit.organization, features: ['escalating-issues']},
  254. });
  255. expect(await screen.findByText('Ongoing')).toBeInTheDocument();
  256. });
  257. it('renders alert for sample event', async function () {
  258. const sampleGroup = TestStubs.Group({issueCategory: IssueCategory.ERROR});
  259. sampleGroup.tags.push({key: 'sample_event'});
  260. MockApiClient.addMockResponse({
  261. url: `/issues/${group.id}/tags/`,
  262. body: [{key: 'sample_event'}],
  263. });
  264. createWrapper();
  265. expect(await screen.findByText(SAMPLE_EVENT_ALERT_TEXT)).toBeInTheDocument();
  266. });
  267. it('renders error when project does not exist', async function () {
  268. MockApiClient.addMockResponse({
  269. url: `/projects/org-slug/other-project-slug/issues/`,
  270. method: 'PUT',
  271. });
  272. MockApiClient.addMockResponse({
  273. url: `/issues/${group.id}/`,
  274. body: {...group, project: {slug: 'other-project-slug'}},
  275. });
  276. createWrapper();
  277. expect(
  278. await screen.findByText('The project other-project-slug does not exist')
  279. ).toBeInTheDocument();
  280. });
  281. it('uses /helpful endpoint when feature flag is on and no event is provided', async function () {
  282. const helpfulMock = MockApiClient.addMockResponse({
  283. url: `/issues/${group.id}/events/helpful/`,
  284. statusCode: 200,
  285. body: event,
  286. });
  287. createWrapper({
  288. ...defaultInit,
  289. organization: {
  290. ...defaultInit.organization,
  291. features: ['issue-details-most-helpful-event'],
  292. },
  293. });
  294. await waitFor(() => expect(helpfulMock).toHaveBeenCalledTimes(1));
  295. });
  296. it('uses /latest endpoint when default is set to latest', async function () {
  297. ConfigStore.loadInitialData(TestStubs.Config({user: latestUser}));
  298. const latestMock = MockApiClient.addMockResponse({
  299. url: `/issues/${group.id}/events/latest/`,
  300. statusCode: 200,
  301. body: event,
  302. });
  303. createWrapper({
  304. ...defaultInit,
  305. organization: {
  306. ...defaultInit.organization,
  307. features: ['issue-details-most-helpful-event'],
  308. },
  309. });
  310. await waitFor(() => expect(latestMock).toHaveBeenCalledTimes(1));
  311. });
  312. it('uses /oldest endpoint when default is set to oldest', async function () {
  313. ConfigStore.loadInitialData(TestStubs.Config({user: oldestUser}));
  314. const oldestMock = MockApiClient.addMockResponse({
  315. url: `/issues/${group.id}/events/oldest/`,
  316. statusCode: 200,
  317. body: event,
  318. });
  319. createWrapper({
  320. ...defaultInit,
  321. organization: {
  322. ...defaultInit.organization,
  323. features: ['issue-details-most-helpful-event'],
  324. },
  325. });
  326. await waitFor(() => expect(oldestMock).toHaveBeenCalledTimes(1));
  327. });
  328. it('uses /helpful endpoint when default is set to recommended', async function () {
  329. ConfigStore.loadInitialData(TestStubs.Config({user: recommendedUser}));
  330. const recommendedMock = MockApiClient.addMockResponse({
  331. url: `/issues/${group.id}/events/helpful/`,
  332. statusCode: 200,
  333. body: event,
  334. });
  335. createWrapper({
  336. ...defaultInit,
  337. organization: {
  338. ...defaultInit.organization,
  339. features: ['issue-details-most-helpful-event'],
  340. },
  341. });
  342. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  343. });
  344. });