groupDetails.spec.tsx 11 KB

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