ruleDetails.spec.jsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {browserHistory} from 'react-router';
  2. import moment from 'moment';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import RuleDetailsContainer from 'sentry/views/alerts/rules/issue/details/index';
  7. import AlertRuleDetails from 'sentry/views/alerts/rules/issue/details/ruleDetails';
  8. describe('AlertRuleDetails', () => {
  9. const context = initializeOrg();
  10. const organization = context.organization;
  11. const project = TestStubs.Project();
  12. const rule = TestStubs.ProjectAlertRule({
  13. lastTriggered: moment().subtract(2, 'day').format(),
  14. });
  15. const createWrapper = (props = {}) => {
  16. const params = {
  17. orgId: organization.slug,
  18. projectId: project.slug,
  19. ruleId: rule.id,
  20. };
  21. return render(
  22. <RuleDetailsContainer
  23. params={params}
  24. location={{query: {}}}
  25. router={context.router}
  26. >
  27. <AlertRuleDetails
  28. params={params}
  29. location={{query: {}}}
  30. router={context.router}
  31. {...props}
  32. />
  33. </RuleDetailsContainer>,
  34. {context: context.routerContext, organization}
  35. );
  36. };
  37. beforeEach(() => {
  38. browserHistory.push = jest.fn();
  39. MockApiClient.addMockResponse({
  40. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  41. body: rule,
  42. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  43. });
  44. MockApiClient.addMockResponse({
  45. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/stats/`,
  46. body: [],
  47. });
  48. MockApiClient.addMockResponse({
  49. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/group-history/`,
  50. body: [
  51. {
  52. count: 1,
  53. group: TestStubs.Group(),
  54. lastTriggered: moment('Apr 11, 2019 1:08:59 AM UTC').format(),
  55. },
  56. ],
  57. headers: {
  58. Link:
  59. '<https://sentry.io/api/0/projects/org-slug/project-slug/rules/1/group-history/?cursor=0:0:1>; rel="previous"; results="false"; cursor="0:0:1", ' +
  60. '<https://sentry.io/api/0/projects/org-slug/project-slug/rules/1/group-history/?cursor=0:100:0>; rel="next"; results="true"; cursor="0:100:0"',
  61. },
  62. });
  63. MockApiClient.addMockResponse({
  64. url: `/organizations/${organization.slug}/projects/`,
  65. body: [project],
  66. });
  67. MockApiClient.addMockResponse({
  68. url: `/organizations/${organization.slug}/users/`,
  69. body: [],
  70. });
  71. ProjectsStore.init();
  72. ProjectsStore.loadInitialData([project]);
  73. });
  74. afterEach(() => {
  75. ProjectsStore.reset();
  76. ProjectsStore.teardown();
  77. MockApiClient.clearMockResponses();
  78. });
  79. it('displays alert rule with list of issues', async () => {
  80. createWrapper();
  81. expect(await screen.findAllByText('My alert rule')).toHaveLength(2);
  82. expect(screen.getByText('RequestError:')).toBeInTheDocument();
  83. expect(screen.getByText('Apr 11, 2019 1:08:59 AM UTC')).toBeInTheDocument();
  84. });
  85. it('should allow paginating results', async () => {
  86. createWrapper();
  87. expect(await screen.findByLabelText('Next')).toBeEnabled();
  88. userEvent.click(screen.getByLabelText('Next'));
  89. expect(browserHistory.push).toHaveBeenCalledWith({
  90. pathname: '/mock-pathname/',
  91. query: {
  92. cursor: '0:100:0',
  93. },
  94. });
  95. });
  96. it('should reset pagination cursor on date change', async () => {
  97. createWrapper();
  98. expect(await screen.findByText('Last 7 days')).toBeInTheDocument();
  99. userEvent.click(screen.getByText('Last 7 days'));
  100. userEvent.click(screen.getByText('Last 24 hours'));
  101. expect(context.router.push).toHaveBeenCalledWith({
  102. query: {
  103. pageStatsPeriod: '24h',
  104. cursor: undefined,
  105. pageEnd: undefined,
  106. pageStart: undefined,
  107. pageUtc: undefined,
  108. },
  109. });
  110. });
  111. it('should show the time since last triggered in sidebar', async () => {
  112. createWrapper();
  113. expect(await screen.findAllByText('Last Triggered')).toHaveLength(2);
  114. expect(screen.getByText('2 days ago')).toBeInTheDocument();
  115. });
  116. it('renders not found on 404', async () => {
  117. MockApiClient.addMockResponse({
  118. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  119. statusCode: 404,
  120. body: {},
  121. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  122. });
  123. createWrapper();
  124. expect(
  125. await screen.findByText('The alert rule you were looking for was not found.')
  126. ).toBeInTheDocument();
  127. });
  128. });