groupDetails.spec.tsx 11 KB

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