Actions.spec.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import React from 'react';
  2. import { render, WithRoute } from 'lib/testHelpers';
  3. import { clusterConnectConnectorPath, clusterConnectorsPath } from 'lib/paths';
  4. import ActionsContainer from 'components/Connect/Details/Actions/ActionsContainer';
  5. import Actions, {
  6. ActionsProps,
  7. } from 'components/Connect/Details/Actions/Actions';
  8. import { ConnectorState } from 'generated-sources';
  9. import { screen } from '@testing-library/react';
  10. import userEvent from '@testing-library/user-event';
  11. import ConfirmationModal, {
  12. ConfirmationModalProps,
  13. } from 'components/common/ConfirmationModal/ConfirmationModal';
  14. const mockHistoryPush = jest.fn();
  15. const deleteConnector = jest.fn();
  16. const cancelMock = jest.fn();
  17. jest.mock('react-router-dom', () => ({
  18. ...jest.requireActual('react-router-dom'),
  19. useNavigate: () => mockHistoryPush,
  20. }));
  21. jest.mock(
  22. 'components/common/ConfirmationModal/ConfirmationModal',
  23. () => 'mock-ConfirmationModal'
  24. );
  25. const expectActionButtonsExists = () => {
  26. expect(screen.getByText('Restart Connector')).toBeInTheDocument();
  27. expect(screen.getByText('Restart All Tasks')).toBeInTheDocument();
  28. expect(screen.getByText('Restart Failed Tasks')).toBeInTheDocument();
  29. expect(screen.getByText('Edit Config')).toBeInTheDocument();
  30. expect(screen.getByText('Delete')).toBeInTheDocument();
  31. };
  32. describe('Actions', () => {
  33. afterEach(() => {
  34. mockHistoryPush.mockClear();
  35. deleteConnector.mockClear();
  36. cancelMock.mockClear();
  37. });
  38. const actionsContainer = (props: Partial<ActionsProps> = {}) => (
  39. <ActionsContainer>
  40. <Actions
  41. deleteConnector={jest.fn()}
  42. isConnectorDeleting={false}
  43. connectorStatus={ConnectorState.RUNNING}
  44. restartConnector={jest.fn()}
  45. restartTasks={jest.fn()}
  46. pauseConnector={jest.fn()}
  47. resumeConnector={jest.fn()}
  48. isConnectorActionRunning={false}
  49. {...props}
  50. />
  51. </ActionsContainer>
  52. );
  53. it('container renders view', () => {
  54. const { container } = render(actionsContainer());
  55. expect(container).toBeInTheDocument();
  56. });
  57. describe('view', () => {
  58. const pathname = clusterConnectConnectorPath();
  59. const clusterName = 'my-cluster';
  60. const connectName = 'my-connect';
  61. const connectorName = 'my-connector';
  62. const confirmationModal = (props: Partial<ConfirmationModalProps> = {}) => (
  63. <WithRoute path={pathname}>
  64. <ConfirmationModal
  65. onCancel={cancelMock}
  66. onConfirm={() =>
  67. deleteConnector(clusterName, connectName, connectorName)
  68. }
  69. {...props}
  70. >
  71. <button type="button" onClick={cancelMock}>
  72. Cancel
  73. </button>
  74. <button
  75. type="button"
  76. onClick={() => {
  77. deleteConnector(clusterName, connectName, connectorName);
  78. mockHistoryPush(clusterConnectorsPath(clusterName));
  79. }}
  80. >
  81. Confirm
  82. </button>
  83. </ConfirmationModal>
  84. </WithRoute>
  85. );
  86. const component = (props: Partial<ActionsProps> = {}) => (
  87. <WithRoute path={pathname}>
  88. <Actions
  89. deleteConnector={jest.fn()}
  90. isConnectorDeleting={false}
  91. connectorStatus={ConnectorState.RUNNING}
  92. restartConnector={jest.fn()}
  93. restartTasks={jest.fn()}
  94. pauseConnector={jest.fn()}
  95. resumeConnector={jest.fn()}
  96. isConnectorActionRunning={false}
  97. {...props}
  98. />
  99. </WithRoute>
  100. );
  101. it('renders buttons when paused', () => {
  102. render(component({ connectorStatus: ConnectorState.PAUSED }), {
  103. initialEntries: [
  104. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  105. ],
  106. });
  107. expect(screen.getAllByRole('button').length).toEqual(6);
  108. expect(screen.getByText('Resume')).toBeInTheDocument();
  109. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  110. expectActionButtonsExists();
  111. });
  112. it('renders buttons when failed', () => {
  113. render(component({ connectorStatus: ConnectorState.FAILED }), {
  114. initialEntries: [
  115. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  116. ],
  117. });
  118. expect(screen.getAllByRole('button').length).toEqual(5);
  119. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  120. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  121. expectActionButtonsExists();
  122. });
  123. it('renders buttons when unassigned', () => {
  124. render(component({ connectorStatus: ConnectorState.UNASSIGNED }), {
  125. initialEntries: [
  126. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  127. ],
  128. });
  129. expect(screen.getAllByRole('button').length).toEqual(5);
  130. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  131. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  132. expectActionButtonsExists();
  133. });
  134. it('renders buttons when running connector action', () => {
  135. render(component({ connectorStatus: ConnectorState.RUNNING }), {
  136. initialEntries: [
  137. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  138. ],
  139. });
  140. expect(screen.getAllByRole('button').length).toEqual(6);
  141. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  142. expect(screen.getByText('Pause')).toBeInTheDocument();
  143. expectActionButtonsExists();
  144. });
  145. it('opens confirmation modal when delete button clicked', () => {
  146. render(component({ deleteConnector }), {
  147. initialEntries: [
  148. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  149. ],
  150. });
  151. userEvent.click(screen.getByRole('button', { name: 'Delete' }));
  152. expect(
  153. screen.getByText(/Are you sure you want to remove/i)
  154. ).toHaveAttribute('isopen', 'true');
  155. });
  156. it('closes when cancel button clicked', () => {
  157. render(confirmationModal({ isOpen: true }), {
  158. initialEntries: [
  159. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  160. ],
  161. });
  162. const cancelBtn = screen.getByRole('button', { name: 'Cancel' });
  163. userEvent.click(cancelBtn);
  164. expect(cancelMock).toHaveBeenCalledTimes(1);
  165. });
  166. it('calls deleteConnector when confirm button clicked', () => {
  167. render(confirmationModal({ isOpen: true }), {
  168. initialEntries: [
  169. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  170. ],
  171. });
  172. const confirmBtn = screen.getByRole('button', { name: 'Confirm' });
  173. userEvent.click(confirmBtn);
  174. expect(deleteConnector).toHaveBeenCalledTimes(1);
  175. expect(deleteConnector).toHaveBeenCalledWith(
  176. clusterName,
  177. connectName,
  178. connectorName
  179. );
  180. });
  181. it('redirects after delete', async () => {
  182. render(confirmationModal({ isOpen: true }), {
  183. initialEntries: [
  184. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  185. ],
  186. });
  187. const confirmBtn = screen.getByRole('button', { name: 'Confirm' });
  188. userEvent.click(confirmBtn);
  189. expect(mockHistoryPush).toHaveBeenCalledTimes(1);
  190. expect(mockHistoryPush).toHaveBeenCalledWith(
  191. clusterConnectorsPath(clusterName)
  192. );
  193. });
  194. it('calls restartConnector when restart button clicked', () => {
  195. const restartConnector = jest.fn();
  196. render(component({ restartConnector }), {
  197. initialEntries: [
  198. clusterConnectConnectorPath(clusterName, connectName, connectorName),
  199. ],
  200. });
  201. userEvent.click(
  202. screen.getByRole('button', { name: 'Restart Connector' })
  203. );
  204. expect(restartConnector).toHaveBeenCalledTimes(1);
  205. expect(restartConnector).toHaveBeenCalledWith({
  206. clusterName,
  207. connectName,
  208. connectorName,
  209. });
  210. });
  211. it('calls pauseConnector when pause button clicked', () => {
  212. const pauseConnector = jest.fn();
  213. render(
  214. component({
  215. connectorStatus: ConnectorState.RUNNING,
  216. pauseConnector,
  217. }),
  218. {
  219. initialEntries: [
  220. clusterConnectConnectorPath(
  221. clusterName,
  222. connectName,
  223. connectorName
  224. ),
  225. ],
  226. }
  227. );
  228. userEvent.click(screen.getByRole('button', { name: 'Pause' }));
  229. expect(pauseConnector).toHaveBeenCalledTimes(1);
  230. expect(pauseConnector).toHaveBeenCalledWith({
  231. clusterName,
  232. connectName,
  233. connectorName,
  234. });
  235. });
  236. it('calls resumeConnector when resume button clicked', () => {
  237. const resumeConnector = jest.fn();
  238. render(
  239. component({
  240. connectorStatus: ConnectorState.PAUSED,
  241. resumeConnector,
  242. }),
  243. {
  244. initialEntries: [
  245. clusterConnectConnectorPath(
  246. clusterName,
  247. connectName,
  248. connectorName
  249. ),
  250. ],
  251. }
  252. );
  253. userEvent.click(screen.getByRole('button', { name: 'Resume' }));
  254. expect(resumeConnector).toHaveBeenCalledTimes(1);
  255. expect(resumeConnector).toHaveBeenCalledWith({
  256. clusterName,
  257. connectName,
  258. connectorName,
  259. });
  260. });
  261. });
  262. });