groupDetails.spec.tsx 10 KB

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