Actions.spec.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import React from 'react';
  2. import { render, WithRoute } from 'lib/testHelpers';
  3. import { clusterConnectConnectorPath } from 'lib/paths';
  4. import Actions from 'components/Connect/Details/Actions/Actions';
  5. import { ConnectorAction, ConnectorState } from 'generated-sources';
  6. import { screen, waitFor } from '@testing-library/react';
  7. import userEvent from '@testing-library/user-event';
  8. import {
  9. useConnector,
  10. useUpdateConnectorState,
  11. } from 'lib/hooks/api/kafkaConnect';
  12. import { connector } from 'lib/fixtures/kafkaConnect';
  13. import set from 'lodash/set';
  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('lib/hooks/api/kafkaConnect', () => ({
  22. useConnector: jest.fn(),
  23. useDeleteConnector: jest.fn(),
  24. useUpdateConnectorState: jest.fn(),
  25. }));
  26. const expectActionButtonsExists = () => {
  27. expect(screen.getByText('Restart Connector')).toBeInTheDocument();
  28. expect(screen.getByText('Restart All Tasks')).toBeInTheDocument();
  29. expect(screen.getByText('Restart Failed Tasks')).toBeInTheDocument();
  30. expect(screen.getByText('Delete')).toBeInTheDocument();
  31. };
  32. const afterClickDropDownButton = async () => {
  33. const dropDownButton = screen.getAllByRole('button');
  34. await userEvent.click(dropDownButton[1]);
  35. };
  36. const afterClickRestartButton = async () => {
  37. const dropDownButton = screen.getByText('Restart');
  38. await userEvent.click(dropDownButton);
  39. };
  40. describe('Actions', () => {
  41. afterEach(() => {
  42. mockHistoryPush.mockClear();
  43. deleteConnector.mockClear();
  44. cancelMock.mockClear();
  45. });
  46. describe('view', () => {
  47. const route = clusterConnectConnectorPath();
  48. const path = clusterConnectConnectorPath(
  49. 'myCluster',
  50. 'myConnect',
  51. 'myConnector'
  52. );
  53. const renderComponent = () =>
  54. render(
  55. <WithRoute path={route}>
  56. <Actions />
  57. </WithRoute>,
  58. { initialEntries: [path] }
  59. );
  60. it('renders buttons when paused', async () => {
  61. (useConnector as jest.Mock).mockImplementation(() => ({
  62. data: set({ ...connector }, 'status.state', ConnectorState.PAUSED),
  63. }));
  64. renderComponent();
  65. await afterClickRestartButton();
  66. expect(screen.getAllByRole('menuitem').length).toEqual(4);
  67. expect(screen.getByText('Resume')).toBeInTheDocument();
  68. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  69. expectActionButtonsExists();
  70. });
  71. it('renders buttons when failed', async () => {
  72. (useConnector as jest.Mock).mockImplementation(() => ({
  73. data: set({ ...connector }, 'status.state', ConnectorState.FAILED),
  74. }));
  75. renderComponent();
  76. await afterClickRestartButton();
  77. expect(screen.getAllByRole('menuitem').length).toEqual(3);
  78. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  79. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  80. expectActionButtonsExists();
  81. });
  82. it('renders buttons when unassigned', async () => {
  83. (useConnector as jest.Mock).mockImplementation(() => ({
  84. data: set({ ...connector }, 'status.state', ConnectorState.UNASSIGNED),
  85. }));
  86. renderComponent();
  87. await afterClickRestartButton();
  88. expect(screen.getAllByRole('menuitem').length).toEqual(3);
  89. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  90. expect(screen.queryByText('Pause')).not.toBeInTheDocument();
  91. expectActionButtonsExists();
  92. });
  93. it('renders buttons when running connector action', async () => {
  94. (useConnector as jest.Mock).mockImplementation(() => ({
  95. data: set({ ...connector }, 'status.state', ConnectorState.RUNNING),
  96. }));
  97. renderComponent();
  98. await afterClickRestartButton();
  99. expect(screen.getAllByRole('menuitem').length).toEqual(4);
  100. expect(screen.queryByText('Resume')).not.toBeInTheDocument();
  101. expect(screen.getByText('Pause')).toBeInTheDocument();
  102. expectActionButtonsExists();
  103. });
  104. describe('mutations', () => {
  105. beforeEach(() => {
  106. (useConnector as jest.Mock).mockImplementation(() => ({
  107. data: set({ ...connector }, 'status.state', ConnectorState.RUNNING),
  108. }));
  109. });
  110. it('opens confirmation modal when delete button clicked', async () => {
  111. renderComponent();
  112. await afterClickDropDownButton();
  113. await waitFor(async () =>
  114. userEvent.click(screen.getByRole('menuitem', { name: 'Delete' }))
  115. );
  116. expect(screen.getByRole('dialog')).toBeInTheDocument();
  117. });
  118. it('calls restartConnector when restart button clicked', async () => {
  119. const restartConnector = jest.fn();
  120. (useUpdateConnectorState as jest.Mock).mockImplementation(() => ({
  121. mutateAsync: restartConnector,
  122. }));
  123. renderComponent();
  124. await afterClickRestartButton();
  125. await userEvent.click(
  126. screen.getByRole('menuitem', { name: 'Restart Connector' })
  127. );
  128. expect(restartConnector).toHaveBeenCalledWith({
  129. action: ConnectorAction.RESTART,
  130. clusterName: 'myCluster',
  131. connectName: 'myConnect',
  132. connectorName: 'myConnector',
  133. });
  134. });
  135. it('calls restartAllTasks', async () => {
  136. const restartAllTasks = jest.fn();
  137. (useUpdateConnectorState as jest.Mock).mockImplementation(() => ({
  138. mutateAsync: restartAllTasks,
  139. }));
  140. renderComponent();
  141. await afterClickRestartButton();
  142. await userEvent.click(
  143. screen.getByRole('menuitem', { name: 'Restart All Tasks' })
  144. );
  145. expect(restartAllTasks).toHaveBeenCalledWith({
  146. action: ConnectorAction.RESTART_ALL_TASKS,
  147. clusterName: 'myCluster',
  148. connectName: 'myConnect',
  149. connectorName: 'myConnector',
  150. });
  151. });
  152. it('calls restartFailedTasks', async () => {
  153. const restartFailedTasks = jest.fn();
  154. (useUpdateConnectorState as jest.Mock).mockImplementation(() => ({
  155. mutateAsync: restartFailedTasks,
  156. }));
  157. renderComponent();
  158. await afterClickRestartButton();
  159. await userEvent.click(
  160. screen.getByRole('menuitem', { name: 'Restart Failed Tasks' })
  161. );
  162. expect(restartFailedTasks).toHaveBeenCalledWith({
  163. action: ConnectorAction.RESTART_FAILED_TASKS,
  164. clusterName: 'myCluster',
  165. connectName: 'myConnect',
  166. connectorName: 'myConnector',
  167. });
  168. });
  169. it('calls pauseConnector when pause button clicked', async () => {
  170. const pauseConnector = jest.fn();
  171. (useUpdateConnectorState as jest.Mock).mockImplementation(() => ({
  172. mutateAsync: pauseConnector,
  173. }));
  174. renderComponent();
  175. await afterClickRestartButton();
  176. await userEvent.click(screen.getByRole('menuitem', { name: 'Pause' }));
  177. expect(pauseConnector).toHaveBeenCalledWith({
  178. action: ConnectorAction.PAUSE,
  179. clusterName: 'myCluster',
  180. connectName: 'myConnect',
  181. connectorName: 'myConnector',
  182. });
  183. });
  184. it('calls resumeConnector when resume button clicked', async () => {
  185. const resumeConnector = jest.fn();
  186. (useConnector as jest.Mock).mockImplementation(() => ({
  187. data: set({ ...connector }, 'status.state', ConnectorState.PAUSED),
  188. }));
  189. (useUpdateConnectorState as jest.Mock).mockImplementation(() => ({
  190. mutateAsync: resumeConnector,
  191. }));
  192. renderComponent();
  193. await afterClickRestartButton();
  194. await userEvent.click(screen.getByRole('menuitem', { name: 'Resume' }));
  195. expect(resumeConnector).toHaveBeenCalledWith({
  196. action: ConnectorAction.RESUME,
  197. clusterName: 'myCluster',
  198. connectName: 'myConnect',
  199. connectorName: 'myConnector',
  200. });
  201. });
  202. });
  203. });
  204. });