groupDetails.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  89. body: {...group},
  90. });
  91. MockApiClient.addMockResponse({
  92. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  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: `/organizations/${defaultInit.organization.slug}/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: `/organizations/${defaultInit.organization.slug}/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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  151. statusCode: 404,
  152. });
  153. MockApiClient.addMockResponse({
  154. url: `/organization/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  168. statusCode: 403,
  169. });
  170. MockApiClient.addMockResponse({
  171. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  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: `/organizations/${defaultInit.organization.slug}/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: `/organizations/${defaultInit.organization.slug}/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. MockApiClient.addMockResponse({
  241. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  242. body: [{key: 'sample_event'}],
  243. });
  244. createWrapper();
  245. expect(await screen.findByText(SAMPLE_EVENT_ALERT_TEXT)).toBeInTheDocument();
  246. });
  247. it('renders error when project does not exist', async function () {
  248. MockApiClient.addMockResponse({
  249. url: `/projects/org-slug/other-project-slug/issues/`,
  250. method: 'PUT',
  251. });
  252. MockApiClient.addMockResponse({
  253. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  254. body: {...group, project: {slug: 'other-project-slug'}},
  255. });
  256. createWrapper();
  257. expect(
  258. await screen.findByText('The project other-project-slug does not exist')
  259. ).toBeInTheDocument();
  260. });
  261. it('uses /helpful endpoint when feature flag is on and no event is provided', async function () {
  262. const helpfulMock = MockApiClient.addMockResponse({
  263. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  264. statusCode: 200,
  265. body: event,
  266. });
  267. createWrapper();
  268. await waitFor(() => expect(helpfulMock).toHaveBeenCalledTimes(1));
  269. });
  270. it('uses /latest endpoint when default is set to latest', async function () {
  271. ConfigStore.loadInitialData(TestStubs.Config({user: latestUser}));
  272. const latestMock = MockApiClient.addMockResponse({
  273. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/latest/`,
  274. statusCode: 200,
  275. body: event,
  276. });
  277. createWrapper();
  278. await waitFor(() => expect(latestMock).toHaveBeenCalledTimes(1));
  279. });
  280. it('uses /oldest endpoint when default is set to oldest', async function () {
  281. ConfigStore.loadInitialData(TestStubs.Config({user: oldestUser}));
  282. const oldestMock = MockApiClient.addMockResponse({
  283. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/oldest/`,
  284. statusCode: 200,
  285. body: event,
  286. });
  287. createWrapper();
  288. await waitFor(() => expect(oldestMock).toHaveBeenCalledTimes(1));
  289. });
  290. it('uses /helpful endpoint when default is set to recommended', async function () {
  291. ConfigStore.loadInitialData(TestStubs.Config({user: recommendedUser}));
  292. const recommendedMock = MockApiClient.addMockResponse({
  293. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/helpful/`,
  294. statusCode: 200,
  295. body: event,
  296. });
  297. createWrapper();
  298. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  299. });
  300. });