index.spec.tsx 7.2 KB

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