index.spec.jsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  3. import ProjectsStore from 'sentry/stores/projectsStore';
  4. import TeamStore from 'sentry/stores/teamStore';
  5. import IncidentsList from 'sentry/views/alerts/list';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. describe('IncidentsList', function () {
  8. let routerContext;
  9. let router;
  10. let organization;
  11. let projectMock;
  12. let projects;
  13. const projects1 = ['a', 'b', 'c'];
  14. const projects2 = ['c', 'd'];
  15. const createWrapper = (props = {}) => {
  16. return render(
  17. <OrganizationContext.Provider value={organization}>
  18. <IncidentsList
  19. params={{orgId: organization.slug}}
  20. location={{query: {}, search: ''}}
  21. router={router}
  22. {...props}
  23. />
  24. </OrganizationContext.Provider>,
  25. {context: routerContext}
  26. );
  27. };
  28. beforeEach(function () {
  29. const context = initializeOrg({
  30. organization: {
  31. features: ['incidents'],
  32. },
  33. });
  34. routerContext = context.routerContext;
  35. router = context.router;
  36. organization = context.organization;
  37. MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/incidents/',
  39. body: [
  40. TestStubs.Incident({
  41. id: '123',
  42. identifier: '1',
  43. title: 'First incident',
  44. projects: projects1,
  45. }),
  46. TestStubs.Incident({
  47. id: '342',
  48. identifier: '2',
  49. title: 'Second incident',
  50. projects: projects2,
  51. }),
  52. ],
  53. });
  54. MockApiClient.addMockResponse({
  55. url: '/organizations/org-slug/incidents/2/stats/',
  56. body: TestStubs.IncidentStats({
  57. totalEvents: 1000,
  58. uniqueUsers: 32,
  59. eventStats: {
  60. data: [[1591390293327, [{count: 42}]]],
  61. },
  62. }),
  63. });
  64. projects = [
  65. TestStubs.Project({slug: 'a', platform: 'javascript'}),
  66. TestStubs.Project({slug: 'b'}),
  67. TestStubs.Project({slug: 'c'}),
  68. TestStubs.Project({slug: 'd'}),
  69. ];
  70. projectMock = MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/projects/',
  72. body: projects,
  73. });
  74. act(() => ProjectsStore.loadInitialData(projects));
  75. });
  76. afterEach(function () {
  77. act(() => ProjectsStore.reset());
  78. MockApiClient.clearMockResponses();
  79. });
  80. it('displays list', async function () {
  81. createWrapper();
  82. const items = await screen.findAllByTestId('alert-title');
  83. expect(items).toHaveLength(2);
  84. expect(within(items[0]).getByText('First incident')).toBeInTheDocument();
  85. expect(within(items[1]).getByText('Second incident')).toBeInTheDocument();
  86. expect(projectMock).toHaveBeenCalledTimes(1);
  87. expect(projectMock).toHaveBeenLastCalledWith(
  88. expect.anything(),
  89. expect.objectContaining({
  90. query: expect.objectContaining({query: 'slug:a slug:b slug:c'}),
  91. })
  92. );
  93. const projectBadges = screen.getAllByTestId('badge-display-name');
  94. expect(within(projectBadges[0]).getByText('a')).toBeInTheDocument();
  95. });
  96. it('displays empty state (first time experience)', async function () {
  97. MockApiClient.addMockResponse({
  98. url: '/organizations/org-slug/incidents/',
  99. body: [],
  100. });
  101. const rulesMock = MockApiClient.addMockResponse({
  102. url: '/organizations/org-slug/alert-rules/',
  103. body: [],
  104. });
  105. const promptsMock = MockApiClient.addMockResponse({
  106. url: '/prompts-activity/',
  107. body: {data: {dismissed_ts: null}},
  108. });
  109. const promptsUpdateMock = MockApiClient.addMockResponse({
  110. url: '/prompts-activity/',
  111. method: 'PUT',
  112. });
  113. createWrapper();
  114. expect(await screen.findByText('More signal, less noise')).toBeInTheDocument();
  115. expect(rulesMock).toHaveBeenCalledTimes(1);
  116. expect(promptsMock).toHaveBeenCalledTimes(1);
  117. expect(promptsUpdateMock).toHaveBeenCalledTimes(1);
  118. });
  119. it('displays empty state (rules not yet created)', async function () {
  120. MockApiClient.addMockResponse({
  121. url: '/organizations/org-slug/incidents/',
  122. body: [],
  123. });
  124. const rulesMock = MockApiClient.addMockResponse({
  125. url: '/organizations/org-slug/alert-rules/',
  126. body: [],
  127. });
  128. const promptsMock = MockApiClient.addMockResponse({
  129. url: '/prompts-activity/',
  130. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  131. });
  132. createWrapper();
  133. expect(
  134. await screen.findByText('No incidents exist for the current query.')
  135. ).toBeInTheDocument();
  136. expect(rulesMock).toHaveBeenCalledTimes(1);
  137. expect(promptsMock).toHaveBeenCalledTimes(1);
  138. });
  139. it('displays empty state (rules created)', async function () {
  140. MockApiClient.addMockResponse({
  141. url: '/organizations/org-slug/incidents/',
  142. body: [],
  143. });
  144. const rulesMock = MockApiClient.addMockResponse({
  145. url: '/organizations/org-slug/alert-rules/',
  146. body: [{id: 1}],
  147. });
  148. const promptsMock = MockApiClient.addMockResponse({
  149. url: '/prompts-activity/',
  150. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  151. });
  152. createWrapper();
  153. expect(
  154. await screen.findByText('No incidents exist for the current query.')
  155. ).toBeInTheDocument();
  156. expect(rulesMock).toHaveBeenCalledTimes(1);
  157. expect(promptsMock).toHaveBeenCalledTimes(0);
  158. });
  159. it('filters by opened issues', function () {
  160. createWrapper();
  161. userEvent.click(screen.getByTestId('filter-button'));
  162. const resolved = screen.getByText('Resolved');
  163. expect(resolved).toBeInTheDocument();
  164. expect(
  165. within(resolved.parentElement).getByTestId('checkbox-fancy')
  166. ).not.toBeChecked();
  167. userEvent.click(resolved);
  168. expect(router.push).toHaveBeenCalledWith({
  169. pathname: undefined,
  170. query: {
  171. expand: ['original_alert_rule'],
  172. status: ['closed'],
  173. team: ['myteams', 'unassigned'],
  174. },
  175. });
  176. });
  177. it('disables the new alert button for those without alert:write', function () {
  178. const noAccessOrg = {
  179. ...organization,
  180. access: [],
  181. };
  182. createWrapper({organization: noAccessOrg});
  183. expect(screen.getByLabelText('Create Alert')).toHaveAttribute(
  184. 'aria-disabled',
  185. 'true'
  186. );
  187. });
  188. it('does not disable the new alert button for those with alert:write', function () {
  189. // Enabled with access
  190. createWrapper();
  191. expect(screen.getByLabelText('Create Alert')).toHaveAttribute(
  192. 'aria-disabled',
  193. 'false'
  194. );
  195. });
  196. it('searches by name', async () => {
  197. createWrapper();
  198. const input = screen.getByPlaceholderText('Search by name');
  199. expect(input).toBeInTheDocument();
  200. const testQuery = 'test name';
  201. userEvent.type(input, `${testQuery}{enter}`);
  202. expect(router.push).toHaveBeenCalledWith(
  203. expect.objectContaining({
  204. query: {
  205. title: testQuery,
  206. expand: ['original_alert_rule'],
  207. team: ['myteams', 'unassigned'],
  208. },
  209. })
  210. );
  211. });
  212. it('displays owner from alert rule', async () => {
  213. const team = TestStubs.Team();
  214. MockApiClient.addMockResponse({
  215. url: '/organizations/org-slug/incidents/',
  216. body: [
  217. TestStubs.Incident({
  218. id: '123',
  219. identifier: '1',
  220. title: 'First incident',
  221. projects: projects1,
  222. alertRule: TestStubs.IncidentRule({owner: `team:${team.id}`}),
  223. }),
  224. ],
  225. });
  226. TeamStore.getById = jest.fn().mockReturnValue(team);
  227. const org = {
  228. ...organization,
  229. features: ['incidents', 'team-alerts-ownership'],
  230. };
  231. const {container} = createWrapper({organization: org});
  232. expect(screen.getByText(team.name)).toBeInTheDocument();
  233. expect(container).toSnapshot();
  234. });
  235. });