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.

CreateApplicationForm-test.tsx 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 SimpleModal from 'sonar-ui-common/components/controls/SimpleModal';
  23. import { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  24. import { createApplication } from '../../../../api/application';
  25. import { mockEvent } from '../../../../helpers/testMocks';
  26. import { ComponentQualifier, Visibility } from '../../../../types/component';
  27. import CreateApplicationForm from '../CreateApplicationForm';
  28. jest.mock('../../../../api/application', () => ({
  29. createApplication: jest.fn().mockResolvedValue({ application: { key: 'foo' } })
  30. }));
  31. beforeEach(jest.clearAllMocks);
  32. it('should render correctly', () => {
  33. const wrapper = shallowRender();
  34. expect(wrapper).toMatchSnapshot('default');
  35. expect(wrapper.find(SimpleModal).dive()).toMatchSnapshot('form');
  36. });
  37. it('should correctly create application on form submit', async () => {
  38. const onCreate = jest.fn();
  39. const wrapper = shallowRender({ onCreate });
  40. const instance = wrapper.instance();
  41. instance.handleDescriptionChange(mockEvent({ currentTarget: { value: 'description' } }));
  42. instance.handleKeyChange(mockEvent({ currentTarget: { value: 'key' } }));
  43. instance.handleNameChange(mockEvent({ currentTarget: { value: 'name' } }));
  44. instance.handleVisibilityChange(Visibility.Private);
  45. wrapper
  46. .find(SimpleModal)
  47. .props()
  48. .onSubmit();
  49. expect(createApplication).toHaveBeenCalledWith('name', 'description', 'key', Visibility.Private);
  50. await waitAndUpdate(wrapper);
  51. expect(onCreate).toHaveBeenCalledWith(
  52. expect.objectContaining({
  53. key: 'foo',
  54. qualifier: ComponentQualifier.Application
  55. })
  56. );
  57. // Can call the WS without any key.
  58. instance.handleKeyChange(mockEvent({ currentTarget: { value: '' } }));
  59. instance.handleFormSubmit();
  60. expect(createApplication).toHaveBeenCalledWith(
  61. 'name',
  62. 'description',
  63. undefined,
  64. Visibility.Private
  65. );
  66. });
  67. function shallowRender(props?: Partial<CreateApplicationForm['props']>) {
  68. return shallow<CreateApplicationForm>(
  69. <CreateApplicationForm onClose={jest.fn()} onCreate={jest.fn()} {...props} />
  70. );
  71. }