ruleDetails.spec.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. eventId: 'eventId',
  56. },
  57. ],
  58. headers: {
  59. Link:
  60. '<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", ' +
  61. '<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"',
  62. },
  63. });
  64. MockApiClient.addMockResponse({
  65. url: `/organizations/${organization.slug}/projects/`,
  66. body: [project],
  67. });
  68. MockApiClient.addMockResponse({
  69. url: `/organizations/${organization.slug}/users/`,
  70. body: [],
  71. });
  72. ProjectsStore.init();
  73. ProjectsStore.loadInitialData([project]);
  74. });
  75. afterEach(() => {
  76. ProjectsStore.reset();
  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. expect(screen.getByText('RequestError:')).toHaveAttribute(
  85. 'href',
  86. expect.stringMatching(
  87. RegExp(
  88. `/organizations/${organization.slug}/issues/${
  89. TestStubs.Group().id
  90. }/events/eventId.*`
  91. )
  92. )
  93. );
  94. });
  95. it('should allow paginating results', async () => {
  96. createWrapper();
  97. expect(await screen.findByLabelText('Next')).toBeEnabled();
  98. userEvent.click(screen.getByLabelText('Next'));
  99. expect(browserHistory.push).toHaveBeenCalledWith({
  100. pathname: '/mock-pathname/',
  101. query: {
  102. cursor: '0:100:0',
  103. },
  104. });
  105. });
  106. it('should reset pagination cursor on date change', async () => {
  107. createWrapper();
  108. expect(await screen.findByText('Last 7 days')).toBeInTheDocument();
  109. userEvent.click(screen.getByText('Last 7 days'));
  110. userEvent.click(screen.getByText('Last 24 hours'));
  111. expect(context.router.push).toHaveBeenCalledWith({
  112. query: {
  113. pageStatsPeriod: '24h',
  114. cursor: undefined,
  115. pageEnd: undefined,
  116. pageStart: undefined,
  117. pageUtc: undefined,
  118. },
  119. });
  120. });
  121. it('should show the time since last triggered in sidebar', async () => {
  122. createWrapper();
  123. expect(await screen.findAllByText('Last Triggered')).toHaveLength(2);
  124. expect(screen.getByText('2 days ago')).toBeInTheDocument();
  125. });
  126. it('renders not found on 404', async () => {
  127. MockApiClient.addMockResponse({
  128. url: `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/`,
  129. statusCode: 404,
  130. body: {},
  131. match: [MockApiClient.matchQuery({expand: 'lastTriggered'})],
  132. });
  133. createWrapper();
  134. expect(
  135. await screen.findByText('The alert rule you were looking for was not found.')
  136. ).toBeInTheDocument();
  137. });
  138. });