New.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React from 'react';
  2. import { containerRendersView, render } from 'lib/testHelpers';
  3. import {
  4. clusterConnectConnectorPath,
  5. clusterConnectorNewPath,
  6. } from 'lib/paths';
  7. import NewContainer from 'components/Connect/New/NewContainer';
  8. import New, { NewProps } from 'components/Connect/New/New';
  9. import { connects, connector } from 'redux/reducers/connect/__test__/fixtures';
  10. import { Route } from 'react-router';
  11. import { act, fireEvent, screen } from '@testing-library/react';
  12. import userEvent from '@testing-library/user-event';
  13. import { waitFor } from '@testing-library/dom';
  14. import { ControllerRenderProps } from 'react-hook-form';
  15. jest.mock('components/common/PageLoader/PageLoader', () => 'mock-PageLoader');
  16. jest.mock(
  17. 'components/common/Editor/Editor',
  18. () => (props: ControllerRenderProps) => {
  19. return <textarea {...props} placeholder="json" />;
  20. }
  21. );
  22. const mockHistoryPush = jest.fn();
  23. jest.mock('react-router-dom', () => ({
  24. ...jest.requireActual('react-router-dom'),
  25. useHistory: () => ({
  26. push: mockHistoryPush,
  27. }),
  28. }));
  29. describe('New', () => {
  30. containerRendersView(<NewContainer />, New);
  31. describe('view', () => {
  32. const clusterName = 'my-cluster';
  33. const simulateFormSubmit = async () => {
  34. userEvent.type(
  35. screen.getByPlaceholderText('Connector Name'),
  36. 'my-connector'
  37. );
  38. userEvent.type(
  39. screen.getByPlaceholderText('json'),
  40. '{"class":"MyClass"}'.replace(/[{[]/g, '$&$&')
  41. );
  42. expect(screen.getByPlaceholderText('json')).toHaveValue(
  43. '{"class":"MyClass"}'
  44. );
  45. await waitFor(() => {
  46. fireEvent.submit(screen.getByRole('form'));
  47. });
  48. };
  49. const renderComponent = (props: Partial<NewProps> = {}) =>
  50. render(
  51. <Route path={clusterConnectorNewPath(':clusterName')}>
  52. <New
  53. fetchConnects={jest.fn()}
  54. areConnectsFetching={false}
  55. connects={connects}
  56. createConnector={jest.fn()}
  57. {...props}
  58. />
  59. </Route>,
  60. { pathname: clusterConnectorNewPath(clusterName) }
  61. );
  62. it('fetches connects on mount', async () => {
  63. const fetchConnects = jest.fn();
  64. await act(async () => {
  65. renderComponent({ fetchConnects });
  66. });
  67. expect(fetchConnects).toHaveBeenCalledTimes(1);
  68. expect(fetchConnects).toHaveBeenCalledWith(clusterName);
  69. });
  70. it('calls createConnector on form submit', async () => {
  71. const createConnector = jest.fn();
  72. renderComponent({ createConnector });
  73. await simulateFormSubmit();
  74. expect(createConnector).toHaveBeenCalledTimes(1);
  75. expect(createConnector).toHaveBeenCalledWith({
  76. clusterName,
  77. connectName: connects[0].name,
  78. newConnector: {
  79. name: 'my-connector',
  80. config: { class: 'MyClass' },
  81. },
  82. });
  83. });
  84. it('redirects to connector details view on successful submit', async () => {
  85. const createConnector = jest.fn().mockResolvedValue({ connector });
  86. renderComponent({ createConnector });
  87. await simulateFormSubmit();
  88. expect(mockHistoryPush).toHaveBeenCalledTimes(1);
  89. expect(mockHistoryPush).toHaveBeenCalledWith(
  90. clusterConnectConnectorPath(
  91. clusterName,
  92. connects[0].name,
  93. connector.name
  94. )
  95. );
  96. });
  97. it('does not redirect to connector details view on unsuccessful submit', async () => {
  98. const createConnector = jest.fn().mockResolvedValueOnce(undefined);
  99. renderComponent({ createConnector });
  100. await simulateFormSubmit();
  101. expect(mockHistoryPush).not.toHaveBeenCalled();
  102. });
  103. });
  104. });