useExternalAMSelector.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import * as reactRedux from 'react-redux';
  2. import { useExternalAmSelector } from './useExternalAmSelector';
  3. const createMockStoreState = (
  4. activeAlertmanagers: Array<{ url: string }>,
  5. droppedAlertmanagers: Array<{ url: string }>,
  6. alertmanagerConfig: string[]
  7. ) => ({
  8. unifiedAlerting: {
  9. externalAlertmanagers: {
  10. discoveredAlertmanagers: {
  11. result: {
  12. data: {
  13. activeAlertManagers: activeAlertmanagers,
  14. droppedAlertManagers: droppedAlertmanagers,
  15. },
  16. },
  17. },
  18. alertmanagerConfig: {
  19. result: {
  20. alertmanagers: alertmanagerConfig,
  21. },
  22. },
  23. },
  24. },
  25. });
  26. describe('useExternalAmSelector', () => {
  27. const useSelectorMock = jest.spyOn(reactRedux, 'useSelector');
  28. beforeEach(() => {
  29. useSelectorMock.mockClear();
  30. });
  31. it('should have one in pending', () => {
  32. useSelectorMock.mockImplementation((callback) => {
  33. return callback(createMockStoreState([], [], ['some/url/to/am']));
  34. });
  35. const alertmanagers = useExternalAmSelector();
  36. expect(alertmanagers).toEqual([
  37. {
  38. url: 'some/url/to/am',
  39. status: 'pending',
  40. actualUrl: '',
  41. },
  42. ]);
  43. });
  44. it('should have one active, one pending', () => {
  45. useSelectorMock.mockImplementation((callback) => {
  46. return callback(
  47. createMockStoreState([{ url: 'some/url/to/am/api/v2/alerts' }], [], ['some/url/to/am', 'some/url/to/am1'])
  48. );
  49. });
  50. const alertmanagers = useExternalAmSelector();
  51. expect(alertmanagers).toEqual([
  52. {
  53. url: 'some/url/to/am',
  54. actualUrl: 'some/url/to/am/api/v2/alerts',
  55. status: 'active',
  56. },
  57. {
  58. url: 'some/url/to/am1',
  59. actualUrl: '',
  60. status: 'pending',
  61. },
  62. ]);
  63. });
  64. it('should have two active', () => {
  65. useSelectorMock.mockImplementation((callback) => {
  66. return callback(
  67. createMockStoreState(
  68. [{ url: 'some/url/to/am/api/v2/alerts' }, { url: 'some/url/to/am1/api/v2/alerts' }],
  69. [],
  70. ['some/url/to/am', 'some/url/to/am1']
  71. )
  72. );
  73. });
  74. const alertmanagers = useExternalAmSelector();
  75. expect(alertmanagers).toEqual([
  76. {
  77. url: 'some/url/to/am',
  78. actualUrl: 'some/url/to/am/api/v2/alerts',
  79. status: 'active',
  80. },
  81. {
  82. url: 'some/url/to/am1',
  83. actualUrl: 'some/url/to/am1/api/v2/alerts',
  84. status: 'active',
  85. },
  86. ]);
  87. });
  88. it('should have one active, one dropped, one pending', () => {
  89. useSelectorMock.mockImplementation((callback) => {
  90. return callback(
  91. createMockStoreState(
  92. [{ url: 'some/url/to/am/api/v2/alerts' }],
  93. [{ url: 'some/dropped/url/api/v2/alerts' }],
  94. ['some/url/to/am', 'some/url/to/am1']
  95. )
  96. );
  97. });
  98. const alertmanagers = useExternalAmSelector();
  99. expect(alertmanagers).toEqual([
  100. {
  101. url: 'some/url/to/am',
  102. actualUrl: 'some/url/to/am/api/v2/alerts',
  103. status: 'active',
  104. },
  105. {
  106. url: 'some/url/to/am1',
  107. actualUrl: '',
  108. status: 'pending',
  109. },
  110. {
  111. url: 'some/dropped/url',
  112. actualUrl: 'some/dropped/url/api/v2/alerts',
  113. status: 'dropped',
  114. },
  115. ]);
  116. });
  117. it('The number of alert managers should match config entries when there are multiple entries of the same url', () => {
  118. useSelectorMock.mockImplementation((callback) => {
  119. return callback(
  120. createMockStoreState(
  121. [
  122. { url: 'same/url/to/am/api/v2/alerts' },
  123. { url: 'same/url/to/am/api/v2/alerts' },
  124. { url: 'same/url/to/am/api/v2/alerts' },
  125. ],
  126. [],
  127. ['same/url/to/am', 'same/url/to/am', 'same/url/to/am']
  128. )
  129. );
  130. });
  131. const alertmanagers = useExternalAmSelector();
  132. expect(alertmanagers.length).toBe(3);
  133. expect(alertmanagers).toEqual([
  134. {
  135. url: 'same/url/to/am',
  136. actualUrl: 'same/url/to/am/api/v2/alerts',
  137. status: 'active',
  138. },
  139. {
  140. url: 'same/url/to/am',
  141. actualUrl: 'same/url/to/am/api/v2/alerts',
  142. status: 'active',
  143. },
  144. {
  145. url: 'same/url/to/am',
  146. actualUrl: 'same/url/to/am/api/v2/alerts',
  147. status: 'active',
  148. },
  149. ]);
  150. });
  151. });