groupDetails.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 {Environment, Group, IssueCategory} from 'sentry/types';
  17. import GroupDetails from 'sentry/views/issueDetails/groupDetails';
  18. jest.unmock('sentry/utils/recreateRoute');
  19. const SAMPLE_EVENT_ALERT_TEXT =
  20. 'You are viewing a sample error. Configure Sentry to start viewing real errors.';
  21. describe('groupDetails', () => {
  22. const group = GroupFixture({issueCategory: IssueCategory.ERROR});
  23. const event = EventFixture();
  24. const project = ProjectFixture({teams: [TeamFixture()]});
  25. const routes = [
  26. {path: '/', childRoutes: []},
  27. {childRoutes: []},
  28. {
  29. path: '/organizations/:orgId/issues/:groupId/',
  30. childRoutes: [],
  31. },
  32. {},
  33. ];
  34. const initRouter = {
  35. location: {
  36. pathname: `/organizations/org-slug/issues/${group.id}/`,
  37. query: {},
  38. search: '?foo=bar',
  39. hash: '#hash',
  40. },
  41. params: {
  42. groupId: group.id,
  43. },
  44. routes,
  45. };
  46. const defaultInit = initializeOrg<{groupId: string}>({
  47. project,
  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. {context: init.routerContext, organization: init.organization, router: init.router}
  92. );
  93. };
  94. beforeEach(() => {
  95. MockApiClient.clearMockResponses();
  96. OrganizationStore.onUpdate(defaultInit.organization);
  97. act(() => ProjectsStore.loadInitialData(defaultInit.organization.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. });
  148. afterEach(() => {
  149. act(() => ProjectsStore.reset());
  150. GroupStore.reset();
  151. PageFiltersStore.reset();
  152. MockApiClient.clearMockResponses();
  153. });
  154. it('renders', async function () {
  155. act(() => ProjectsStore.reset());
  156. createWrapper();
  157. expect(screen.queryByText(group.title)).not.toBeInTheDocument();
  158. act(() => ProjectsStore.loadInitialData(defaultInit.organization.projects));
  159. expect(await screen.findByText(group.title, {exact: false})).toBeInTheDocument();
  160. // Sample event alert should not show up
  161. expect(screen.queryByText(SAMPLE_EVENT_ALERT_TEXT)).not.toBeInTheDocument();
  162. });
  163. it('renders error when issue is not found', async function () {
  164. MockApiClient.addMockResponse({
  165. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  166. statusCode: 404,
  167. });
  168. MockApiClient.addMockResponse({
  169. url: `/organization/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  170. statusCode: 404,
  171. });
  172. createWrapper();
  173. await waitFor(() =>
  174. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  175. );
  176. expect(
  177. await screen.findByText('The issue you were looking for was not found.')
  178. ).toBeInTheDocument();
  179. });
  180. it('renders MissingProjectMembership when trying to access issue in project the user does not belong to', async function () {
  181. MockApiClient.addMockResponse({
  182. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  183. statusCode: 403,
  184. });
  185. MockApiClient.addMockResponse({
  186. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  187. statusCode: 403,
  188. });
  189. createWrapper();
  190. await waitFor(() =>
  191. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  192. );
  193. expect(
  194. await screen.findByText(
  195. 'No teams have access to this project yet. Ask an admin to add your team to this project.'
  196. )
  197. ).toBeInTheDocument();
  198. });
  199. it('fetches issue details for a given environment', async function () {
  200. const init = initializeOrg({
  201. router: {
  202. ...initRouter,
  203. location: LocationFixture({
  204. ...initRouter.location,
  205. query: {environment: 'staging'},
  206. }),
  207. },
  208. });
  209. createWrapper(init);
  210. await waitFor(() =>
  211. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  212. );
  213. expect(await screen.findByText('environment: staging')).toBeInTheDocument();
  214. });
  215. it('renders issue event error', async function () {
  216. MockApiClient.addMockResponse({
  217. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  218. statusCode: 404,
  219. });
  220. createWrapper();
  221. expect(await screen.findByText('eventError')).toBeInTheDocument();
  222. });
  223. it('renders substatus badge', async function () {
  224. MockApiClient.addMockResponse({
  225. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  226. body: {
  227. ...group,
  228. inbox: null,
  229. status: 'unresolved',
  230. substatus: 'ongoing',
  231. },
  232. });
  233. createWrapper({
  234. ...defaultInit,
  235. organization: {...defaultInit.organization},
  236. });
  237. expect(await screen.findByText('Ongoing')).toBeInTheDocument();
  238. });
  239. it('renders alert for sample event', async function () {
  240. MockApiClient.addMockResponse({
  241. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/tags/`,
  242. body: [{key: 'sample_event'}],
  243. });
  244. createWrapper();
  245. expect(await screen.findByText(SAMPLE_EVENT_ALERT_TEXT)).toBeInTheDocument();
  246. });
  247. it('renders error when project does not exist', async function () {
  248. MockApiClient.addMockResponse({
  249. url: `/projects/org-slug/other-project-slug/issues/`,
  250. method: 'PUT',
  251. });
  252. MockApiClient.addMockResponse({
  253. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/`,
  254. body: {...group, project: {slug: 'other-project-slug'}},
  255. });
  256. createWrapper();
  257. expect(
  258. await screen.findByText('The project other-project-slug does not exist')
  259. ).toBeInTheDocument();
  260. });
  261. it('uses /recommended endpoint when feature flag is on and no event is provided', async function () {
  262. const recommendedMock = MockApiClient.addMockResponse({
  263. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  264. statusCode: 200,
  265. body: event,
  266. });
  267. createWrapper();
  268. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  269. });
  270. it('uses /latest endpoint when default is set to latest', async function () {
  271. ConfigStore.loadInitialData(ConfigFixture({user: latestUser}));
  272. const latestMock = MockApiClient.addMockResponse({
  273. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/latest/`,
  274. statusCode: 200,
  275. body: event,
  276. });
  277. createWrapper();
  278. await waitFor(() => expect(latestMock).toHaveBeenCalledTimes(1));
  279. });
  280. it('uses /oldest endpoint when default is set to oldest', async function () {
  281. ConfigStore.loadInitialData(ConfigFixture({user: oldestUser}));
  282. const oldestMock = MockApiClient.addMockResponse({
  283. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/oldest/`,
  284. statusCode: 200,
  285. body: event,
  286. });
  287. createWrapper();
  288. await waitFor(() => expect(oldestMock).toHaveBeenCalledTimes(1));
  289. });
  290. it('uses /recommended endpoint when default is set to recommended', async function () {
  291. ConfigStore.loadInitialData(ConfigFixture({user: recommendedUser}));
  292. const recommendedMock = MockApiClient.addMockResponse({
  293. url: `/organizations/${defaultInit.organization.slug}/issues/${group.id}/events/recommended/`,
  294. statusCode: 200,
  295. body: event,
  296. });
  297. createWrapper();
  298. await waitFor(() => expect(recommendedMock).toHaveBeenCalledTimes(1));
  299. });
  300. });