Tasks.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React from 'react';
  2. import { render, WithRoute } from 'lib/testHelpers';
  3. import { clusterConnectConnectorTasksPath } from 'lib/paths';
  4. import Tasks from 'components/Connect/Details/Tasks/Tasks';
  5. import { tasks } from 'lib/fixtures/kafkaConnect';
  6. import { screen, within, waitFor } from '@testing-library/react';
  7. import userEvent from '@testing-library/user-event';
  8. import {
  9. useConnectorTasks,
  10. useRestartConnectorTask,
  11. } from 'lib/hooks/api/kafkaConnect';
  12. import { Task } from 'generated-sources';
  13. jest.mock('lib/hooks/api/kafkaConnect', () => ({
  14. useConnectorTasks: jest.fn(),
  15. useRestartConnectorTask: jest.fn(),
  16. }));
  17. const path = clusterConnectConnectorTasksPath('local', 'ghp', '1');
  18. const restartConnectorMock = jest.fn();
  19. describe('Tasks', () => {
  20. beforeEach(() => {
  21. (useRestartConnectorTask as jest.Mock).mockImplementation(() => ({
  22. mutateAsync: restartConnectorMock,
  23. }));
  24. });
  25. const renderComponent = (currentData: Task[] | undefined = undefined) => {
  26. (useConnectorTasks as jest.Mock).mockImplementation(() => ({
  27. data: currentData,
  28. }));
  29. render(
  30. <WithRoute path={clusterConnectConnectorTasksPath()}>
  31. <Tasks />
  32. </WithRoute>,
  33. { initialEntries: [path] }
  34. );
  35. };
  36. it('renders empty table', () => {
  37. renderComponent();
  38. expect(screen.getByRole('table')).toBeInTheDocument();
  39. expect(screen.getByText('No tasks found')).toBeInTheDocument();
  40. });
  41. it('renders tasks table', () => {
  42. renderComponent(tasks);
  43. expect(screen.getAllByRole('row').length).toEqual(tasks.length + 1);
  44. expect(
  45. screen.getByRole('row', {
  46. name: '1 kafka-connect0:8083 RUNNING',
  47. })
  48. ).toBeInTheDocument();
  49. });
  50. it('renders truncates long trace and expands', () => {
  51. renderComponent(tasks);
  52. const trace = tasks[2]?.status?.trace || '';
  53. const truncatedTrace = trace.toString().substring(0, 100 - 3);
  54. const thirdRow = screen.getByRole('row', {
  55. name: `3 kafka-connect0:8083 RUNNING ${truncatedTrace}...`,
  56. });
  57. expect(thirdRow).toBeInTheDocument();
  58. const expandedDetails = screen.queryByText(trace);
  59. // Full trace is not visible
  60. expect(expandedDetails).not.toBeInTheDocument();
  61. userEvent.click(thirdRow);
  62. expect(
  63. screen.getByRole('row', {
  64. name: trace,
  65. })
  66. ).toBeInTheDocument();
  67. });
  68. describe('Action button', () => {
  69. const expectDropdownExists = () => {
  70. const firstTaskRow = screen.getByRole('row', {
  71. name: '1 kafka-connect0:8083 RUNNING',
  72. });
  73. expect(firstTaskRow).toBeInTheDocument();
  74. const extBtn = within(firstTaskRow).getByRole('button', {
  75. name: 'Dropdown Toggle',
  76. });
  77. expect(extBtn).toBeEnabled();
  78. userEvent.click(extBtn);
  79. expect(screen.getByRole('menu')).toBeInTheDocument();
  80. };
  81. it('renders action button', () => {
  82. renderComponent(tasks);
  83. expectDropdownExists();
  84. expect(
  85. screen.getAllByRole('button', { name: 'Dropdown Toggle' }).length
  86. ).toEqual(tasks.length);
  87. // Action buttons are enabled
  88. const actionBtn = screen.getAllByRole('menuitem');
  89. expect(actionBtn[0]).toHaveTextContent('Restart task');
  90. });
  91. it('works as expected', async () => {
  92. renderComponent(tasks);
  93. expectDropdownExists();
  94. const actionBtn = screen.getAllByRole('menuitem');
  95. expect(actionBtn[0]).toHaveTextContent('Restart task');
  96. userEvent.click(actionBtn[0]);
  97. expect(
  98. screen.getByText('Are you sure you want to restart the task?')
  99. ).toBeInTheDocument();
  100. expect(screen.getByText('Confirm the action')).toBeInTheDocument();
  101. userEvent.click(screen.getByRole('button', { name: 'Confirm' }));
  102. await waitFor(() => expect(restartConnectorMock).toHaveBeenCalled());
  103. });
  104. });
  105. });