index.spec.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 {Team} from 'sentry-fixture/team';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import TeamStore from 'sentry/stores/teamStore';
  10. import {Organization} from 'sentry/types';
  11. import AlertsContainer from 'sentry/views/alerts';
  12. import IncidentsList from 'sentry/views/alerts/list/incidents';
  13. describe('IncidentsList', () => {
  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. act(() => ProjectsStore.loadInitialData(projects));
  68. });
  69. afterEach(() => {
  70. act(() => ProjectsStore.reset());
  71. MockApiClient.clearMockResponses();
  72. });
  73. it('displays list', async () => {
  74. renderComponent();
  75. const items = await screen.findAllByTestId('alert-title');
  76. expect(items).toHaveLength(2);
  77. expect(within(items[0]).getByText('First incident')).toBeInTheDocument();
  78. expect(within(items[1]).getByText('Second incident')).toBeInTheDocument();
  79. const projectBadges = screen.getAllByTestId('badge-display-name');
  80. expect(within(projectBadges[0]).getByText('a')).toBeInTheDocument();
  81. });
  82. it('displays empty state (first time experience)', async () => {
  83. MockApiClient.addMockResponse({
  84. url: '/organizations/org-slug/incidents/',
  85. body: [],
  86. });
  87. const rulesMock = MockApiClient.addMockResponse({
  88. url: '/organizations/org-slug/alert-rules/',
  89. body: [],
  90. });
  91. const promptsMock = MockApiClient.addMockResponse({
  92. url: '/prompts-activity/',
  93. body: {data: {dismissed_ts: null}},
  94. });
  95. const promptsUpdateMock = MockApiClient.addMockResponse({
  96. url: '/prompts-activity/',
  97. method: 'PUT',
  98. });
  99. renderComponent();
  100. expect(await screen.findByText('More signal, less noise')).toBeInTheDocument();
  101. expect(rulesMock).toHaveBeenCalledTimes(1);
  102. expect(promptsMock).toHaveBeenCalledTimes(1);
  103. expect(promptsUpdateMock).toHaveBeenCalledTimes(1);
  104. });
  105. it('displays empty state (rules not yet created)', async () => {
  106. MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/incidents/',
  108. body: [],
  109. });
  110. const rulesMock = MockApiClient.addMockResponse({
  111. url: '/organizations/org-slug/alert-rules/',
  112. body: [],
  113. });
  114. const promptsMock = MockApiClient.addMockResponse({
  115. url: '/prompts-activity/',
  116. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  117. });
  118. renderComponent();
  119. expect(
  120. await screen.findByText('No incidents exist for the current query.')
  121. ).toBeInTheDocument();
  122. expect(rulesMock).toHaveBeenCalledTimes(1);
  123. expect(promptsMock).toHaveBeenCalledTimes(1);
  124. });
  125. it('displays empty state (rules created)', async () => {
  126. MockApiClient.addMockResponse({
  127. url: '/organizations/org-slug/incidents/',
  128. body: [],
  129. });
  130. const rulesMock = MockApiClient.addMockResponse({
  131. url: '/organizations/org-slug/alert-rules/',
  132. body: [{id: 1}],
  133. });
  134. const promptsMock = MockApiClient.addMockResponse({
  135. url: '/prompts-activity/',
  136. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  137. });
  138. renderComponent();
  139. expect(
  140. await screen.findByText('No incidents exist for the current query.')
  141. ).toBeInTheDocument();
  142. expect(rulesMock).toHaveBeenCalledTimes(1);
  143. expect(promptsMock).toHaveBeenCalledTimes(0);
  144. });
  145. it('filters by status', async () => {
  146. const {router} = renderComponent();
  147. await selectEvent.select(await screen.findByText('Status'), 'Active');
  148. expect(router.push).toHaveBeenCalledWith(
  149. expect.objectContaining({
  150. query: {
  151. status: 'open',
  152. },
  153. })
  154. );
  155. });
  156. it('disables the new alert button for those without alert:write', async () => {
  157. const noAccessOrg = {
  158. access: [],
  159. };
  160. renderComponent({orgOverride: noAccessOrg});
  161. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  162. 'aria-disabled',
  163. 'true'
  164. );
  165. });
  166. it('does not disable the new alert button for those with alert:write', async () => {
  167. // Enabled with access
  168. renderComponent();
  169. expect(await screen.findByLabelText('Create Alert')).toHaveAttribute(
  170. 'aria-disabled',
  171. 'false'
  172. );
  173. });
  174. it('searches by name', async () => {
  175. const {router} = renderComponent();
  176. const input = await screen.findByPlaceholderText('Search by name');
  177. expect(input).toBeInTheDocument();
  178. const testQuery = 'test name';
  179. await userEvent.type(input, `${testQuery}{enter}`);
  180. expect(router.push).toHaveBeenCalledWith(
  181. expect.objectContaining({
  182. query: {
  183. title: testQuery,
  184. },
  185. })
  186. );
  187. });
  188. it('displays owner from alert rule', async () => {
  189. const team = Team();
  190. MockApiClient.addMockResponse({
  191. url: '/organizations/org-slug/incidents/',
  192. body: [
  193. Incident({
  194. id: '123',
  195. identifier: '1',
  196. title: 'First incident',
  197. projects: projects1,
  198. alertRule: MetricRule({owner: `team:${team.id}`}),
  199. }),
  200. ],
  201. });
  202. TeamStore.getById = jest.fn().mockReturnValue(team);
  203. renderComponent();
  204. expect(await screen.findByText(team.name)).toBeInTheDocument();
  205. });
  206. });