index.spec.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import selectEvent from 'react-select-event';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  4. import ProjectsStore from 'sentry/stores/projectsStore';
  5. import TeamStore from 'sentry/stores/teamStore';
  6. import AlertsContainer from 'sentry/views/alerts';
  7. import IncidentsList from 'sentry/views/alerts/list/incidents';
  8. describe('IncidentsList', () => {
  9. let projectMock;
  10. const projects1 = ['a', 'b', 'c'];
  11. const projects2 = ['c', 'd'];
  12. const renderComponent = ({organization} = {}) => {
  13. const context = initializeOrg({
  14. organization: {
  15. features: ['incidents'],
  16. ...organization,
  17. },
  18. });
  19. const routerContext = context.routerContext;
  20. const org = context.organization;
  21. const router = context.router;
  22. return {
  23. component: render(
  24. <AlertsContainer>
  25. <IncidentsList
  26. params={{orgId: org.slug}}
  27. location={{query: {}, search: ''}}
  28. router={router}
  29. />
  30. </AlertsContainer>,
  31. {context: routerContext, organization: org}
  32. ),
  33. router,
  34. };
  35. };
  36. beforeEach(() => {
  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. const 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(() => {
  77. act(() => ProjectsStore.reset());
  78. MockApiClient.clearMockResponses();
  79. });
  80. it('displays list', async () => {
  81. renderComponent();
  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 () => {
  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. renderComponent();
  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 () => {
  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. renderComponent();
  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 () => {
  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. renderComponent();
  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 status', async () => {
  160. const {router} = renderComponent();
  161. await selectEvent.select(await screen.findByText('Status'), 'Active');
  162. expect(router.push).toHaveBeenCalledWith(
  163. expect.objectContaining({
  164. query: {
  165. status: 'open',
  166. },
  167. })
  168. );
  169. });
  170. it('disables the new alert button for those without alert:write', async () => {
  171. const noAccessOrg = {
  172. access: [],
  173. };
  174. renderComponent({organization: noAccessOrg});
  175. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  176. 'aria-disabled',
  177. 'true'
  178. );
  179. });
  180. it('does not disable the new alert button for those with alert:write', async () => {
  181. // Enabled with access
  182. renderComponent();
  183. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  184. 'aria-disabled',
  185. 'false'
  186. );
  187. });
  188. it('searches by name', async () => {
  189. const {router} = renderComponent();
  190. const input = await screen.findByPlaceholderText('Search by name');
  191. expect(input).toBeInTheDocument();
  192. const testQuery = 'test name';
  193. userEvent.type(input, `${testQuery}{enter}`);
  194. expect(router.push).toHaveBeenCalledWith(
  195. expect.objectContaining({
  196. query: {
  197. title: testQuery,
  198. },
  199. })
  200. );
  201. });
  202. it('displays owner from alert rule', async () => {
  203. const team = TestStubs.Team();
  204. MockApiClient.addMockResponse({
  205. url: '/organizations/org-slug/incidents/',
  206. body: [
  207. TestStubs.Incident({
  208. id: '123',
  209. identifier: '1',
  210. title: 'First incident',
  211. projects: projects1,
  212. alertRule: TestStubs.MetricRule({owner: `team:${team.id}`}),
  213. }),
  214. ],
  215. });
  216. TeamStore.getById = jest.fn().mockReturnValue(team);
  217. renderComponent();
  218. expect(await screen.findByText(team.name)).toBeInTheDocument();
  219. });
  220. });