groupDetails.spec.tsx 10 KB

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