Edit.spec.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React from 'react';
  2. import { create } from 'react-test-renderer';
  3. import { mount } from 'enzyme';
  4. import { act } from 'react-dom/test-utils';
  5. import { containerRendersView, TestRouterWrapper } from 'lib/testHelpers';
  6. import {
  7. clusterConnectConnectorConfigPath,
  8. clusterConnectConnectorEditPath,
  9. } from 'lib/paths';
  10. import EditContainer from 'components/Connect/Edit/EditContainer';
  11. import Edit, { EditProps } from 'components/Connect/Edit/Edit';
  12. import { connector } from 'redux/reducers/connect/__test__/fixtures';
  13. jest.mock('components/common/PageLoader/PageLoader', () => 'mock-PageLoader');
  14. jest.mock('components/common/JSONEditor/JSONEditor', () => 'mock-JSONEditor');
  15. const mockHistoryPush = jest.fn();
  16. jest.mock('react-router-dom', () => ({
  17. ...jest.requireActual('react-router-dom'),
  18. useHistory: () => ({
  19. push: mockHistoryPush,
  20. }),
  21. }));
  22. describe('Edit', () => {
  23. containerRendersView(<EditContainer />, Edit);
  24. describe('view', () => {
  25. const pathname = clusterConnectConnectorEditPath(
  26. ':clusterName',
  27. ':connectName',
  28. ':connectorName'
  29. );
  30. const clusterName = 'my-cluster';
  31. const connectName = 'my-connect';
  32. const connectorName = 'my-connector';
  33. const setupWrapper = (props: Partial<EditProps> = {}) => (
  34. <TestRouterWrapper
  35. pathname={pathname}
  36. urlParams={{ clusterName, connectName, connectorName }}
  37. >
  38. <Edit
  39. fetchConfig={jest.fn()}
  40. isConfigFetching={false}
  41. config={connector.config}
  42. updateConfig={jest.fn()}
  43. {...props}
  44. />
  45. </TestRouterWrapper>
  46. );
  47. it('matches snapshot', () => {
  48. const wrapper = create(setupWrapper());
  49. expect(wrapper.toJSON()).toMatchSnapshot();
  50. });
  51. it('matches snapshot when fetching config', () => {
  52. const wrapper = create(setupWrapper({ isConfigFetching: true }));
  53. expect(wrapper.toJSON()).toMatchSnapshot();
  54. });
  55. it('matches snapshot when config has credentials', () => {
  56. const wrapper = create(
  57. setupWrapper({ config: { ...connector.config, password: '******' } })
  58. );
  59. expect(wrapper.toJSON()).toMatchSnapshot();
  60. });
  61. it('fetches config on mount', () => {
  62. const fetchConfig = jest.fn();
  63. mount(setupWrapper({ fetchConfig }));
  64. expect(fetchConfig).toHaveBeenCalledTimes(1);
  65. expect(fetchConfig).toHaveBeenCalledWith(
  66. clusterName,
  67. connectName,
  68. connectorName
  69. );
  70. });
  71. it('calls updateConfig on form submit', async () => {
  72. const updateConfig = jest.fn();
  73. const wrapper = mount(setupWrapper({ updateConfig }));
  74. await act(async () => {
  75. wrapper.find('form').simulate('submit');
  76. });
  77. expect(updateConfig).toHaveBeenCalledTimes(1);
  78. expect(updateConfig).toHaveBeenCalledWith(
  79. clusterName,
  80. connectName,
  81. connectorName,
  82. connector.config
  83. );
  84. });
  85. it('redirects to connector config view on successful submit', async () => {
  86. const updateConfig = jest.fn().mockResolvedValueOnce(connector);
  87. const wrapper = mount(setupWrapper({ updateConfig }));
  88. await act(async () => {
  89. wrapper.find('form').simulate('submit');
  90. });
  91. expect(mockHistoryPush).toHaveBeenCalledTimes(1);
  92. expect(mockHistoryPush).toHaveBeenCalledWith(
  93. clusterConnectConnectorConfigPath(
  94. clusterName,
  95. connectName,
  96. connectorName
  97. )
  98. );
  99. });
  100. it('does not redirect to connector config view on unsuccessful submit', async () => {
  101. const updateConfig = jest.fn().mockResolvedValueOnce(undefined);
  102. const wrapper = mount(setupWrapper({ updateConfig }));
  103. await act(async () => {
  104. wrapper.find('form').simulate('submit');
  105. });
  106. expect(mockHistoryPush).not.toHaveBeenCalled();
  107. });
  108. });
  109. });