You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidationModal-test.tsx 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { shallow } from 'enzyme';
  21. import * as React from 'react';
  22. import { waitAndUpdate } from '../../../helpers/testUtils';
  23. import ValidationForm from '../ValidationForm';
  24. import ValidationModal from '../ValidationModal';
  25. it('should render correctly', () => {
  26. const wrapper = shallowRender();
  27. expect(wrapper).toMatchSnapshot();
  28. expect(
  29. wrapper
  30. .find(ValidationForm)
  31. .dive()
  32. .dive()
  33. ).toMatchSnapshot();
  34. });
  35. it('should handle submit', async () => {
  36. const data = { field: 'foo' };
  37. const onSubmit = jest.fn().mockResolvedValue({});
  38. const onClose = jest.fn();
  39. const wrapper = shallowRender({ onClose, onSubmit });
  40. wrapper.instance().handleSubmit(data);
  41. expect(onSubmit).toBeCalledWith(data);
  42. await waitAndUpdate(wrapper);
  43. expect(onClose).toBeCalled();
  44. });
  45. function shallowRender(props: Partial<ValidationModal<{ field: string }>['props']> = {}) {
  46. return shallow<ValidationModal<{ field: string }>>(
  47. <ValidationModal<{ field: string }>
  48. confirmButtonText="confirm"
  49. header="title"
  50. initialValues={{ field: 'foo' }}
  51. isDestructive={true}
  52. isInitialValid={true}
  53. onClose={jest.fn()}
  54. onSubmit={jest.fn()}
  55. validate={jest.fn()}
  56. {...props}>
  57. {props => (
  58. <input
  59. name="field"
  60. onBlur={props.handleBlur}
  61. onChange={props.handleChange}
  62. type="text"
  63. value={props.values.field}
  64. />
  65. )}
  66. </ValidationModal>
  67. );
  68. }