useFetchThresholdsListData.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {makeTestQueryClient} from 'sentry-test/queryClient';
  3. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  4. import {QueryClientProvider} from 'sentry/utils/queryClient';
  5. import {
  6. AlertRuleTriggerType,
  7. type MetricRule,
  8. } from 'sentry/views/alerts/rules/metric/types';
  9. import {OrganizationContext} from 'sentry/views/organizationContext';
  10. import useFetchThresholdsListData from './useFetchThresholdsListData';
  11. describe('useFetchThresholdsListData', () => {
  12. const {organization} = initializeOrg({
  13. organization: {
  14. name: 'test-org',
  15. slug: 'test-thresholds',
  16. features: ['releases-v2'],
  17. },
  18. });
  19. const queryClient = makeTestQueryClient();
  20. const Wrapper = (org = organization) => {
  21. return function WrappedComponent({children}) {
  22. return (
  23. <QueryClientProvider client={queryClient}>
  24. <OrganizationContext.Provider value={org}>
  25. {children}
  26. </OrganizationContext.Provider>
  27. </QueryClientProvider>
  28. );
  29. };
  30. };
  31. const mockThresholdApis = (data = {}) => {
  32. const thresholdMock = MockApiClient.addMockResponse({
  33. url: `/organizations/${organization.slug}/release-thresholds/`,
  34. method: 'GET',
  35. body: data,
  36. });
  37. const alertMock = MockApiClient.addMockResponse({
  38. url: `/organizations/${organization.slug}/alert-rules/`,
  39. method: 'GET',
  40. body: data,
  41. });
  42. return {thresholdMock, alertMock};
  43. };
  44. afterEach(() => {
  45. queryClient.clear();
  46. });
  47. it('fetches release thresholds by default', async () => {
  48. const thresholds = [];
  49. const {thresholdMock, alertMock} = mockThresholdApis(thresholds);
  50. const {result, waitFor} = reactHooks.renderHook(() => useFetchThresholdsListData(), {
  51. wrapper: Wrapper(),
  52. });
  53. // ensure the async fetch is working
  54. expect(result.current.isLoading).toBeTruthy();
  55. await waitFor(() => expect(result.current.isLoading).toBeFalsy());
  56. expect(result.current.data).toEqual(thresholds);
  57. expect(thresholdMock).toHaveBeenCalled();
  58. expect(alertMock).not.toHaveBeenCalled();
  59. });
  60. it('fetches activated alerts if the feature is enabled', async () => {
  61. const alerts = [];
  62. const {thresholdMock, alertMock} = mockThresholdApis(alerts);
  63. const {organization: org} = initializeOrg({
  64. organization: {
  65. name: 'test-org',
  66. slug: 'test-thresholds',
  67. features: ['activated-alert-rules'],
  68. },
  69. });
  70. const {result, waitFor} = reactHooks.renderHook(() => useFetchThresholdsListData(), {
  71. wrapper: Wrapper(org),
  72. });
  73. expect(result.current.isLoading).toBeTruthy();
  74. await waitFor(() => expect(result.current.isLoading).toBeFalsy());
  75. expect(result.current.data).toEqual(alerts);
  76. expect(thresholdMock).not.toHaveBeenCalled();
  77. expect(alertMock).toHaveBeenCalled();
  78. });
  79. it('formats actiavted alerts as thresholds', async () => {
  80. const alerts: Partial<MetricRule>[] = [
  81. {
  82. id: '1',
  83. dateCreated: '2021-01-01',
  84. environment: 'production',
  85. projects: ['test-project'],
  86. timeWindow: 60,
  87. triggers: [
  88. {
  89. label: AlertRuleTriggerType.CRITICAL,
  90. actions: [],
  91. alertThreshold: 100,
  92. },
  93. ],
  94. },
  95. ];
  96. mockThresholdApis(alerts);
  97. const {organization: org} = initializeOrg({
  98. organization: {
  99. name: 'test-org',
  100. slug: 'test-thresholds',
  101. features: ['activated-alert-rules'],
  102. },
  103. });
  104. const {result, waitFor} = reactHooks.renderHook(() => useFetchThresholdsListData(), {
  105. wrapper: Wrapper(org),
  106. });
  107. await waitFor(() => expect(result.current.isLoading).toBeFalsy());
  108. const expectedResult = [
  109. {
  110. date_added: '2021-01-01',
  111. environment: {
  112. name: 'production',
  113. displayName: 'production',
  114. },
  115. id: '1',
  116. project: {
  117. id: 'test-project',
  118. slug: 'test-project',
  119. },
  120. threshold_type: 'total_error_count',
  121. trigger_type: 'over',
  122. value: 100,
  123. window_in_seconds: 60,
  124. },
  125. ];
  126. expect(result.current.data).toEqual(expectedResult);
  127. });
  128. });