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.

Form-test.tsx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 { change, click, submit } from 'sonar-ui-common/helpers/testUtils';
  23. import Form from '../Form';
  24. it('should render form', async () => {
  25. const onClose = jest.fn();
  26. const onSubmit = jest.fn(() => Promise.resolve());
  27. const wrapper = shallow(
  28. <Form
  29. confirmButtonText="confirmButtonText"
  30. domains={['Coverage', 'Issues']}
  31. header="header"
  32. onClose={onClose}
  33. onSubmit={onSubmit}
  34. types={['INT', 'STRING']}
  35. />
  36. ).dive();
  37. expect(wrapper).toMatchSnapshot();
  38. change(wrapper.find('[name="key"]'), 'foo');
  39. change(wrapper.find('[name="name"]'), 'Foo');
  40. change(wrapper.find('[name="description"]'), 'bar');
  41. wrapper.find('Creatable').prop<Function>('onChange')({ value: 'Coverage' });
  42. submit(wrapper.find('form'));
  43. expect(onSubmit).toBeCalledWith({
  44. description: 'bar',
  45. domain: 'Coverage',
  46. key: 'foo',
  47. name: 'Foo',
  48. type: 'INT'
  49. });
  50. await new Promise(setImmediate);
  51. expect(onClose).toBeCalled();
  52. onClose.mockClear();
  53. click(wrapper.find('ResetButtonLink'));
  54. expect(onClose).toBeCalled();
  55. });
  56. it('should create new domain', () => {
  57. const wrapper = shallow(
  58. <Form
  59. confirmButtonText="confirmButtonText"
  60. domains={['Coverage', 'Issues']}
  61. header="header"
  62. onClose={jest.fn()}
  63. onSubmit={jest.fn()}
  64. types={['INT', 'STRING']}
  65. />
  66. );
  67. const optionsBefore = [
  68. { label: 'Coverage', value: 'Coverage' },
  69. { label: 'Issues', value: 'Issues' }
  70. ];
  71. expect(getSelect().prop('options')).toEqual(optionsBefore);
  72. getSelect().prop<Function>('onChange')({ value: 'Another' });
  73. wrapper.update();
  74. expect(getSelect().prop('options')).toEqual([
  75. ...optionsBefore,
  76. { label: 'Another', value: 'Another' }
  77. ]);
  78. function getSelect() {
  79. return wrapper.dive().find('Creatable');
  80. }
  81. });