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