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.

AlmIntegration-test.tsx 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  23. import {
  24. countBindedProjects,
  25. deleteConfiguration,
  26. getAlmDefinitions
  27. } from '../../../../../api/alm-settings';
  28. import { AlmKeys } from '../../../../../types/alm-settings';
  29. import { AlmIntegration } from '../AlmIntegration';
  30. jest.mock('../../../../../api/alm-settings', () => ({
  31. countBindedProjects: jest.fn().mockResolvedValue(0),
  32. deleteConfiguration: jest.fn().mockResolvedValue(undefined),
  33. getAlmDefinitions: jest.fn().mockResolvedValue({ github: [] })
  34. }));
  35. beforeEach(() => {
  36. jest.clearAllMocks();
  37. });
  38. it('should render correctly', () => {
  39. expect(shallowRender()).toMatchSnapshot();
  40. });
  41. it('should handle alm selection', async () => {
  42. const wrapper = shallowRender();
  43. wrapper.setState({ currentAlm: AlmKeys.Azure });
  44. wrapper.instance().handleSelectAlm(AlmKeys.GitHub);
  45. await waitAndUpdate(wrapper);
  46. expect(wrapper.state().currentAlm).toBe(AlmKeys.GitHub);
  47. });
  48. it('should handle delete', async () => {
  49. const toBeDeleted = '45672';
  50. (countBindedProjects as jest.Mock).mockResolvedValueOnce(7);
  51. const wrapper = shallowRender();
  52. wrapper.instance().handleDelete(toBeDeleted);
  53. await waitAndUpdate(wrapper);
  54. expect(wrapper.state().projectCount).toBe(7);
  55. expect(wrapper.state().definitionKeyForDeletion).toBe(toBeDeleted);
  56. });
  57. it('should delete configuration', async () => {
  58. (deleteConfiguration as jest.Mock).mockResolvedValueOnce(undefined);
  59. const wrapper = shallowRender();
  60. wrapper.instance().deleteConfiguration('8345678');
  61. await waitAndUpdate(wrapper);
  62. expect(wrapper.state().projectCount).toBeUndefined();
  63. expect(wrapper.state().definitionKeyForDeletion).toBeUndefined();
  64. });
  65. it('should fetch settings', async () => {
  66. const wrapper = shallowRender();
  67. await wrapper
  68. .instance()
  69. .fetchPullRequestDecorationSetting()
  70. .then(() => {
  71. expect(getAlmDefinitions).toBeCalled();
  72. expect(wrapper.state().definitions).toEqual({ github: [] });
  73. expect(wrapper.state().loading).toBe(false);
  74. });
  75. });
  76. function shallowRender() {
  77. return shallow<AlmIntegration>(<AlmIntegration appState={{ branchesEnabled: true }} />);
  78. }