groupDetails.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import {Event as EventFixture} from 'sentry-fixture/event';
  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/groupDetails';
  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 = EventFixture();
  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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  90. body: {...group},
  91. });
  92. MockApiClient.addMockResponse({
  93. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  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: `/organizations/${defaultInit.organization.slug}/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: `/organizations/${defaultInit.organization.slug}/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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  152. statusCode: 404,
  153. });
  154. MockApiClient.addMockResponse({
  155. url: `/organization/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  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: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  169. statusCode: 403,
  170. });
  171. MockApiClient.addMockResponse({
  172. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  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. it('renders issue event error', async function () {
  202. MockApiClient.addMockResponse({
  203. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  204. statusCode: 404,
  205. });
  206. createWrapper();
  207. expect(await screen.findByText('eventError')).toBeInTheDocument();
  208. });
  209. it('renders for review reason', async function () {
  210. MockApiClient.addMockResponse({
  211. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  212. body: {
  213. ...group,
  214. inbox: {
  215. date_added: '2020-11-24T13:17:42.248751Z',
  216. reason: 0,
  217. reason_details: null,
  218. },
  219. },
  220. });
  221. createWrapper();
  222. expect(await screen.findByText('New Issue')).toBeInTheDocument();
  223. });
  224. it('renders substatus badge', async function () {
  225. MockApiClient.addMockResponse({
  226. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  227. body: {
  228. ...group,
  229. inbox: null,
  230. status: 'unresolved',
  231. substatus: 'ongoing',
  232. },
  233. });
  234. createWrapper({
  235. ...defaultInit,
  236. organization: {...defaultInit.organization, features: ['escalating-issues']},
  237. });
  238. expect(await screen.findByText('Ongoing')).toBeInTheDocument();
  239. });
  240. it('renders alert for sample event', async function () {
  241. MockApiClient.addMockResponse({
  242. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  243. body: [{key: 'sample_event'}],
  244. });
  245. createWrapper();
  246. expect(await screen.findByText(SAMPLE_EVENT_ALERT_TEXT)).toBeInTheDocument();
  247. });
  248. it('renders error when project does not exist', async function () {
  249. MockApiClient.addMockResponse({
  250. url: `/projects/org-slug/other-project-slug/issues/`,
  251. method: 'PUT',
  252. });
  253. MockApiClient.addMockResponse({
  254. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  255. body: {...group, project: {slug: 'other-project-slug'}},
  256. });
  257. createWrapper();
  258. expect(
  259. await screen.findByText('The project other-project-slug does not exist')
  260. ).toBeInTheDocument();
  261. });
  262. it('uses /recommended endpoint when feature flag is on and no event is provided', async function () {
  263. const recommendedMock = MockApiClient.addMockResponse({
  264. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  265. statusCode: 200,
  266. body: event,
  267. });
  268. createWrapper();
  269. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  270. });
  271. it('uses /latest endpoint when default is set to latest', async function () {
  272. ConfigStore.loadInitialData(TestStubs.Config({user: latestUser}));
  273. const latestMock = MockApiClient.addMockResponse({
  274. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/latest/`,
  275. statusCode: 200,
  276. body: event,
  277. });
  278. createWrapper();
  279. await waitFor(() => expect(latestMock).toHaveBeenCalledTimes(1));
  280. });
  281. it('uses /oldest endpoint when default is set to oldest', async function () {
  282. ConfigStore.loadInitialData(TestStubs.Config({user: oldestUser}));
  283. const oldestMock = MockApiClient.addMockResponse({
  284. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/oldest/`,
  285. statusCode: 200,
  286. body: event,
  287. });
  288. createWrapper();
  289. await waitFor(() => expect(oldestMock).toHaveBeenCalledTimes(1));
  290. });
  291. it('uses /recommended endpoint when default is set to recommended', async function () {
  292. ConfigStore.loadInitialData(TestStubs.Config({user: recommendedUser}));
  293. const recommendedMock = MockApiClient.addMockResponse({
  294. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  295. statusCode: 200,
  296. body: event,
  297. });
  298. createWrapper();
  299. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  300. });
  301. });