groupDetails.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. project,
  49. router: initRouter,
  50. });
  51. const recommendedUser = UserFixture({
  52. options: {
  53. ...UserFixture().options,
  54. defaultIssueEvent: 'recommended',
  55. },
  56. });
  57. const latestUser = UserFixture({
  58. options: {
  59. ...UserFixture().options,
  60. defaultIssueEvent: 'latest',
  61. },
  62. });
  63. const oldestUser = UserFixture({
  64. options: {
  65. ...UserFixture().options,
  66. defaultIssueEvent: 'oldest',
  67. },
  68. });
  69. function MockComponent({
  70. group: groupProp,
  71. environments,
  72. eventError,
  73. }: {
  74. environments?: Environment[];
  75. eventError?: boolean;
  76. group?: Group;
  77. }) {
  78. return (
  79. <div>
  80. Group Details Mock
  81. <div>title: {groupProp?.title}</div>
  82. <div>environment: {environments?.join(' ')}</div>
  83. {eventError && <div>eventError</div>}
  84. </div>
  85. );
  86. }
  87. const createWrapper = (init = defaultInit) => {
  88. return render(
  89. <GroupDetails {...init.routerProps}>
  90. <MockComponent />
  91. </GroupDetails>,
  92. {context: init.routerContext, organization: init.organization, router: init.router}
  93. );
  94. };
  95. beforeEach(() => {
  96. MockApiClient.clearMockResponses();
  97. OrganizationStore.onUpdate(defaultInit.organization);
  98. act(() => ProjectsStore.loadInitialData(defaultInit.organization.projects));
  99. MockApiClient.addMockResponse({
  100. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  101. body: {...group},
  102. });
  103. MockApiClient.addMockResponse({
  104. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  105. statusCode: 200,
  106. body: {
  107. ...event,
  108. },
  109. });
  110. MockApiClient.addMockResponse({
  111. url: `/projects/org-slug/${project.slug}/issues/`,
  112. method: 'PUT',
  113. body: {
  114. hasSeen: false,
  115. },
  116. });
  117. MockApiClient.addMockResponse({
  118. url: '/organizations/org-slug/projects/',
  119. body: [project],
  120. });
  121. MockApiClient.addMockResponse({
  122. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/first-last-release/`,
  123. method: 'GET',
  124. });
  125. MockApiClient.addMockResponse({
  126. url: `/organizations/${defaultInit.organization.slug}/events/`,
  127. statusCode: 200,
  128. body: {
  129. data: [
  130. {
  131. 'count()': 1,
  132. },
  133. ],
  134. },
  135. });
  136. MockApiClient.addMockResponse({
  137. url: `/organizations/${defaultInit.organization.slug}/environments/`,
  138. body: EnvironmentsFixture(),
  139. });
  140. MockApiClient.addMockResponse({
  141. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  142. body: [],
  143. });
  144. MockApiClient.addMockResponse({
  145. url: '/organizations/org-slug/replay-count/',
  146. body: {},
  147. });
  148. });
  149. afterEach(() => {
  150. act(() => ProjectsStore.reset());
  151. GroupStore.reset();
  152. PageFiltersStore.reset();
  153. MockApiClient.clearMockResponses();
  154. });
  155. it('renders', async function () {
  156. act(() => ProjectsStore.reset());
  157. createWrapper();
  158. expect(screen.queryByText(group.title)).not.toBeInTheDocument();
  159. act(() => ProjectsStore.loadInitialData(defaultInit.organization.projects));
  160. expect(await screen.findByText(group.title, {exact: false})).toBeInTheDocument();
  161. // Sample event alert should not show up
  162. expect(screen.queryByText(SAMPLE_EVENT_ALERT_TEXT)).not.toBeInTheDocument();
  163. });
  164. it('renders error when issue is not found', async function () {
  165. MockApiClient.addMockResponse({
  166. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  167. statusCode: 404,
  168. });
  169. MockApiClient.addMockResponse({
  170. url: `/organization/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  171. statusCode: 404,
  172. });
  173. createWrapper();
  174. await waitFor(() =>
  175. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  176. );
  177. expect(
  178. await screen.findByText('The issue you were looking for was not found.')
  179. ).toBeInTheDocument();
  180. });
  181. it('renders MissingProjectMembership when trying to access issue in project the user does not belong to', async function () {
  182. MockApiClient.addMockResponse({
  183. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  184. statusCode: 403,
  185. });
  186. MockApiClient.addMockResponse({
  187. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  188. statusCode: 403,
  189. });
  190. createWrapper();
  191. await waitFor(() =>
  192. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  193. );
  194. expect(
  195. await screen.findByText(
  196. 'No teams have access to this project yet. Ask an admin to add your team to this project.'
  197. )
  198. ).toBeInTheDocument();
  199. });
  200. it('fetches issue details for a given environment', async function () {
  201. const init = initializeOrg({
  202. router: {
  203. ...initRouter,
  204. location: LocationFixture({
  205. ...initRouter.location,
  206. query: {environment: 'staging'},
  207. }),
  208. },
  209. });
  210. createWrapper(init);
  211. await waitFor(() =>
  212. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  213. );
  214. expect(await screen.findByText('environment: staging')).toBeInTheDocument();
  215. });
  216. it('renders issue event error', async function () {
  217. MockApiClient.addMockResponse({
  218. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  219. statusCode: 404,
  220. });
  221. createWrapper();
  222. expect(await screen.findByText('eventError')).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},
  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(ConfigFixture({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(ConfigFixture({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(ConfigFixture({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. });