index.spec.jsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 params={{}} location={{query: {}, search: ''}} router={router} />
  26. </AlertsContainer>,
  27. {context: routerContext, organization: org}
  28. ),
  29. router,
  30. };
  31. };
  32. beforeEach(() => {
  33. MockApiClient.addMockResponse({
  34. url: '/organizations/org-slug/incidents/',
  35. body: [
  36. TestStubs.Incident({
  37. id: '123',
  38. identifier: '1',
  39. title: 'First incident',
  40. projects: projects1,
  41. }),
  42. TestStubs.Incident({
  43. id: '342',
  44. identifier: '2',
  45. title: 'Second incident',
  46. projects: projects2,
  47. }),
  48. ],
  49. });
  50. MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/incidents/2/stats/',
  52. body: TestStubs.IncidentStats({
  53. totalEvents: 1000,
  54. uniqueUsers: 32,
  55. eventStats: {
  56. data: [[1591390293327, [{count: 42}]]],
  57. },
  58. }),
  59. });
  60. const projects = [
  61. TestStubs.Project({slug: 'a', platform: 'javascript'}),
  62. TestStubs.Project({slug: 'b'}),
  63. TestStubs.Project({slug: 'c'}),
  64. TestStubs.Project({slug: 'd'}),
  65. ];
  66. projectMock = MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/projects/',
  68. body: projects,
  69. });
  70. act(() => ProjectsStore.loadInitialData(projects));
  71. });
  72. afterEach(() => {
  73. act(() => ProjectsStore.reset());
  74. MockApiClient.clearMockResponses();
  75. });
  76. it('displays list', async () => {
  77. renderComponent();
  78. const items = await screen.findAllByTestId('alert-title');
  79. expect(items).toHaveLength(2);
  80. expect(within(items[0]).getByText('First incident')).toBeInTheDocument();
  81. expect(within(items[1]).getByText('Second incident')).toBeInTheDocument();
  82. expect(projectMock).toHaveBeenCalledTimes(1);
  83. expect(projectMock).toHaveBeenLastCalledWith(
  84. expect.anything(),
  85. expect.objectContaining({
  86. query: expect.objectContaining({query: 'slug:a slug:b slug:c'}),
  87. })
  88. );
  89. const projectBadges = screen.getAllByTestId('badge-display-name');
  90. expect(within(projectBadges[0]).getByText('a')).toBeInTheDocument();
  91. });
  92. it('displays empty state (first time experience)', async () => {
  93. MockApiClient.addMockResponse({
  94. url: '/organizations/org-slug/incidents/',
  95. body: [],
  96. });
  97. const rulesMock = MockApiClient.addMockResponse({
  98. url: '/organizations/org-slug/alert-rules/',
  99. body: [],
  100. });
  101. const promptsMock = MockApiClient.addMockResponse({
  102. url: '/prompts-activity/',
  103. body: {data: {dismissed_ts: null}},
  104. });
  105. const promptsUpdateMock = MockApiClient.addMockResponse({
  106. url: '/prompts-activity/',
  107. method: 'PUT',
  108. });
  109. renderComponent();
  110. expect(await screen.findByText('More signal, less noise')).toBeInTheDocument();
  111. expect(rulesMock).toHaveBeenCalledTimes(1);
  112. expect(promptsMock).toHaveBeenCalledTimes(1);
  113. expect(promptsUpdateMock).toHaveBeenCalledTimes(1);
  114. });
  115. it('displays empty state (rules not yet created)', async () => {
  116. MockApiClient.addMockResponse({
  117. url: '/organizations/org-slug/incidents/',
  118. body: [],
  119. });
  120. const rulesMock = MockApiClient.addMockResponse({
  121. url: '/organizations/org-slug/alert-rules/',
  122. body: [],
  123. });
  124. const promptsMock = MockApiClient.addMockResponse({
  125. url: '/prompts-activity/',
  126. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  127. });
  128. renderComponent();
  129. expect(
  130. await screen.findByText('No incidents exist for the current query.')
  131. ).toBeInTheDocument();
  132. expect(rulesMock).toHaveBeenCalledTimes(1);
  133. expect(promptsMock).toHaveBeenCalledTimes(1);
  134. });
  135. it('displays empty state (rules created)', async () => {
  136. MockApiClient.addMockResponse({
  137. url: '/organizations/org-slug/incidents/',
  138. body: [],
  139. });
  140. const rulesMock = MockApiClient.addMockResponse({
  141. url: '/organizations/org-slug/alert-rules/',
  142. body: [{id: 1}],
  143. });
  144. const promptsMock = MockApiClient.addMockResponse({
  145. url: '/prompts-activity/',
  146. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  147. });
  148. renderComponent();
  149. expect(
  150. await screen.findByText('No incidents exist for the current query.')
  151. ).toBeInTheDocument();
  152. expect(rulesMock).toHaveBeenCalledTimes(1);
  153. expect(promptsMock).toHaveBeenCalledTimes(0);
  154. });
  155. it('filters by status', async () => {
  156. const {router} = renderComponent();
  157. await selectEvent.select(await screen.findByText('Status'), 'Active');
  158. expect(router.push).toHaveBeenCalledWith(
  159. expect.objectContaining({
  160. query: {
  161. status: 'open',
  162. },
  163. })
  164. );
  165. });
  166. it('disables the new alert button for those without alert:write', async () => {
  167. const noAccessOrg = {
  168. access: [],
  169. };
  170. renderComponent({organization: noAccessOrg});
  171. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  172. 'aria-disabled',
  173. 'true'
  174. );
  175. });
  176. it('does not disable the new alert button for those with alert:write', async () => {
  177. // Enabled with access
  178. renderComponent();
  179. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  180. 'aria-disabled',
  181. 'false'
  182. );
  183. });
  184. it('searches by name', async () => {
  185. const {router} = renderComponent();
  186. const input = await screen.findByPlaceholderText('Search by name');
  187. expect(input).toBeInTheDocument();
  188. const testQuery = 'test name';
  189. await userEvent.type(input, `${testQuery}{enter}`);
  190. expect(router.push).toHaveBeenCalledWith(
  191. expect.objectContaining({
  192. query: {
  193. title: testQuery,
  194. },
  195. })
  196. );
  197. });
  198. it('displays owner from alert rule', async () => {
  199. const team = TestStubs.Team();
  200. MockApiClient.addMockResponse({
  201. url: '/organizations/org-slug/incidents/',
  202. body: [
  203. TestStubs.Incident({
  204. id: '123',
  205. identifier: '1',
  206. title: 'First incident',
  207. projects: projects1,
  208. alertRule: TestStubs.MetricRule({owner: `team:${team.id}`}),
  209. }),
  210. ],
  211. });
  212. TeamStore.getById = jest.fn().mockReturnValue(team);
  213. renderComponent();
  214. expect(await screen.findByText(team.name)).toBeInTheDocument();
  215. });
  216. });