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.

DetailsHeader-test.tsx 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 { click, waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  23. import { setQualityGateAsDefault } from '../../../../api/quality-gates';
  24. import { mockQualityGate } from '../../../../helpers/testMocks';
  25. import DetailsHeader from '../DetailsHeader';
  26. jest.mock('../../../../api/quality-gates', () => ({
  27. setQualityGateAsDefault: jest.fn().mockResolvedValue(null)
  28. }));
  29. beforeEach(jest.clearAllMocks);
  30. it('should render correctly', () => {
  31. expect(shallowRender()).toMatchSnapshot('default');
  32. expect(shallowRender({ qualityGate: mockQualityGate({ isBuiltIn: true }) })).toMatchSnapshot(
  33. 'built-in'
  34. );
  35. expect(
  36. shallowRender({
  37. qualityGate: mockQualityGate({
  38. actions: {
  39. copy: true,
  40. delete: true,
  41. rename: true,
  42. setAsDefault: true
  43. }
  44. })
  45. })
  46. ).toMatchSnapshot('admin actions');
  47. });
  48. it('should allow the QG to be set as the default', async () => {
  49. const onSetDefault = jest.fn();
  50. const refreshItem = jest.fn();
  51. const refreshList = jest.fn();
  52. const qualityGate = mockQualityGate({ id: 1, actions: { setAsDefault: true } });
  53. const wrapper = shallowRender({ onSetDefault, qualityGate, refreshItem, refreshList });
  54. click(wrapper.find('Button#quality-gate-toggle-default'));
  55. expect(setQualityGateAsDefault).toBeCalledWith({ id: 1 });
  56. expect(onSetDefault).toBeCalled();
  57. await waitAndUpdate(wrapper);
  58. expect(refreshItem).toBeCalled();
  59. expect(refreshList).toBeCalled();
  60. jest.clearAllMocks();
  61. wrapper.setProps({ qualityGate: mockQualityGate({ ...qualityGate, isDefault: true }) });
  62. click(wrapper.find('Button#quality-gate-toggle-default'));
  63. expect(setQualityGateAsDefault).not.toBeCalled();
  64. expect(onSetDefault).not.toBeCalled();
  65. await waitAndUpdate(wrapper);
  66. expect(refreshItem).not.toBeCalled();
  67. expect(refreshList).not.toBeCalled();
  68. });
  69. function shallowRender(props: Partial<DetailsHeader['props']> = {}) {
  70. return shallow<DetailsHeader>(
  71. <DetailsHeader
  72. onSetDefault={jest.fn()}
  73. qualityGate={mockQualityGate()}
  74. refreshItem={jest.fn().mockResolvedValue(null)}
  75. refreshList={jest.fn().mockResolvedValue(null)}
  76. {...props}
  77. />
  78. );
  79. }