index.spec.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import ProjectsStore from 'app/stores/projectsStore';
  4. import TeamStore from 'app/stores/teamStore';
  5. import IncidentsList from 'app/views/alerts/list';
  6. describe('IncidentsList', function () {
  7. let routerContext;
  8. let router;
  9. let organization;
  10. let projectMock;
  11. let wrapper;
  12. let projects;
  13. const projects1 = ['a', 'b', 'c'];
  14. const projects2 = ['c', 'd'];
  15. const createWrapper = async (props = {}) => {
  16. wrapper = mountWithTheme(
  17. <IncidentsList
  18. params={{orgId: organization.slug}}
  19. location={{query: {}, search: ''}}
  20. router={router}
  21. {...props}
  22. />,
  23. routerContext
  24. );
  25. await tick();
  26. wrapper.update();
  27. return wrapper;
  28. };
  29. beforeEach(function () {
  30. const context = initializeOrg({
  31. organization: {
  32. features: ['incidents'],
  33. },
  34. });
  35. routerContext = context.routerContext;
  36. router = context.router;
  37. organization = context.organization;
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/incidents/',
  40. body: [
  41. TestStubs.Incident({
  42. id: '123',
  43. identifier: '1',
  44. title: 'First incident',
  45. projects: projects1,
  46. }),
  47. TestStubs.Incident({
  48. id: '342',
  49. identifier: '2',
  50. title: 'Second incident',
  51. projects: projects2,
  52. }),
  53. ],
  54. });
  55. MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/incidents/2/stats/',
  57. body: TestStubs.IncidentStats({
  58. totalEvents: 1000,
  59. uniqueUsers: 32,
  60. eventStats: {
  61. data: [[1591390293327, [{count: 42}]]],
  62. },
  63. }),
  64. });
  65. projects = [
  66. TestStubs.Project({slug: 'a', platform: 'javascript'}),
  67. TestStubs.Project({slug: 'b'}),
  68. TestStubs.Project({slug: 'c'}),
  69. TestStubs.Project({slug: 'd'}),
  70. ];
  71. projectMock = MockApiClient.addMockResponse({
  72. url: '/organizations/org-slug/projects/',
  73. body: projects,
  74. });
  75. });
  76. afterEach(function () {
  77. wrapper.unmount();
  78. ProjectsStore.reset();
  79. MockApiClient.clearMockResponses();
  80. });
  81. it('displays list', async function () {
  82. ProjectsStore.loadInitialData(projects);
  83. wrapper = await createWrapper();
  84. await tick();
  85. await tick();
  86. await tick();
  87. wrapper.update();
  88. const items = wrapper.find('AlertListRow');
  89. expect(items).toHaveLength(2);
  90. expect(items.at(0).text()).toContain('First incident');
  91. expect(items.at(1).text()).toContain('Second incident');
  92. // GlobalSelectionHeader loads projects + the Projects render-prop
  93. // component to load projects for all rows.
  94. expect(projectMock).toHaveBeenCalledTimes(2);
  95. expect(projectMock).toHaveBeenLastCalledWith(
  96. expect.anything(),
  97. expect.objectContaining({
  98. query: expect.objectContaining({query: 'slug:a slug:b slug:c'}),
  99. })
  100. );
  101. expect(items.at(0).find('IdBadge').prop('project')).toMatchObject({
  102. slug: 'a',
  103. });
  104. });
  105. it('displays empty state (first time experience)', async function () {
  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: null}},
  117. });
  118. const promptsUpdateMock = MockApiClient.addMockResponse({
  119. url: '/prompts-activity/',
  120. method: 'PUT',
  121. });
  122. wrapper = await createWrapper();
  123. expect(rulesMock).toHaveBeenCalledTimes(1);
  124. expect(promptsMock).toHaveBeenCalledTimes(1);
  125. expect(promptsUpdateMock).toHaveBeenCalledTimes(1);
  126. await tick();
  127. wrapper.update();
  128. expect(wrapper.find('PanelItem')).toHaveLength(0);
  129. expect(wrapper.find('Onboarding').text()).toContain('More signal, less noise');
  130. });
  131. it('displays empty state (rules not yet created)', async function () {
  132. MockApiClient.addMockResponse({
  133. url: '/organizations/org-slug/incidents/',
  134. body: [],
  135. });
  136. const rulesMock = MockApiClient.addMockResponse({
  137. url: '/organizations/org-slug/alert-rules/',
  138. body: [],
  139. });
  140. const promptsMock = MockApiClient.addMockResponse({
  141. url: '/prompts-activity/',
  142. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  143. });
  144. wrapper = await createWrapper();
  145. expect(rulesMock).toHaveBeenCalledTimes(1);
  146. expect(promptsMock).toHaveBeenCalledTimes(1);
  147. await tick();
  148. wrapper.update();
  149. expect(wrapper.find('PanelItem')).toHaveLength(0);
  150. expect(wrapper.text()).toContain('No incidents exist for the current query');
  151. });
  152. it('displays empty state (rules created)', async function () {
  153. MockApiClient.addMockResponse({
  154. url: '/organizations/org-slug/incidents/',
  155. body: [],
  156. });
  157. const rulesMock = MockApiClient.addMockResponse({
  158. url: '/organizations/org-slug/alert-rules/',
  159. body: [{id: 1}],
  160. });
  161. const promptsMock = MockApiClient.addMockResponse({
  162. url: '/prompts-activity/',
  163. body: {data: {dismissed_ts: Math.floor(Date.now() / 1000)}},
  164. });
  165. wrapper = await createWrapper();
  166. expect(rulesMock).toHaveBeenCalledTimes(1);
  167. expect(promptsMock).toHaveBeenCalledTimes(0);
  168. await tick();
  169. wrapper.update();
  170. expect(wrapper.find('PanelItem')).toHaveLength(0);
  171. expect(wrapper.text()).toContain('No incidents exist for the current query.');
  172. });
  173. it('filters by opened issues', async function () {
  174. ProjectsStore.loadInitialData(projects);
  175. wrapper = await createWrapper();
  176. wrapper.find('[data-test-id="filter-button"]').at(1).simulate('click');
  177. const resolved = wrapper.find('Filter').find('ListItem').at(1);
  178. expect(resolved.text()).toBe('Resolved');
  179. expect(resolved.find('[data-test-id="checkbox-fancy"]').props()['aria-checked']).toBe(
  180. false
  181. );
  182. wrapper.setProps({
  183. location: {query: {status: ['closed']}, search: '?status=closed`'},
  184. });
  185. expect(
  186. wrapper
  187. .find('Filter')
  188. .find('ListItem')
  189. .at(1)
  190. .find('[data-test-id="checkbox-fancy"]')
  191. .props()['aria-checked']
  192. ).toBe(true);
  193. });
  194. it('disables the new alert button for those without alert:write', async function () {
  195. const noAccessOrg = {
  196. ...organization,
  197. access: [],
  198. };
  199. wrapper = await createWrapper({organization: noAccessOrg});
  200. const addButton = wrapper.find('button[aria-label="Create Alert Rule"]');
  201. expect(addButton.props()['aria-disabled']).toBe(true);
  202. // Enabled with access
  203. wrapper.unmount();
  204. wrapper = await createWrapper();
  205. const addLink = wrapper.find('button[aria-label="Create Alert Rule"]');
  206. expect(addLink.props()['aria-disabled']).toBe(false);
  207. });
  208. it('searches by name', async () => {
  209. wrapper = await createWrapper();
  210. expect(wrapper.find('StyledSearchBar').exists()).toBe(true);
  211. const testQuery = 'test name';
  212. wrapper
  213. .find('StyledSearchBar')
  214. .find('input')
  215. .simulate('change', {target: {value: testQuery}})
  216. .simulate('submit', {preventDefault() {}});
  217. expect(router.push).toHaveBeenCalledWith(
  218. expect.objectContaining({
  219. query: {
  220. title: testQuery,
  221. team: ['myteams', 'unassigned'],
  222. },
  223. })
  224. );
  225. });
  226. it('displays owner from alert rule', async () => {
  227. const team = TestStubs.Team();
  228. MockApiClient.addMockResponse({
  229. url: '/organizations/org-slug/incidents/',
  230. body: [
  231. TestStubs.Incident({
  232. id: '123',
  233. identifier: '1',
  234. title: 'First incident',
  235. projects: projects1,
  236. alertRule: TestStubs.IncidentRule({owner: `team:${team.id}`}),
  237. }),
  238. ],
  239. });
  240. TeamStore.getById = jest.fn().mockReturnValue(team);
  241. const org = {
  242. ...organization,
  243. features: ['incidents', 'team-alerts-ownership'],
  244. };
  245. wrapper = await createWrapper({organization: org});
  246. expect(wrapper.find('TeamWrapper').text()).toBe(team.name);
  247. expect(wrapper).toSnapshot();
  248. });
  249. });