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.

App-test.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { waitAndUpdate } from 'sonar-ui-common/helpers/testUtils';
  23. import {
  24. getNewCodePeriod,
  25. resetNewCodePeriod,
  26. setNewCodePeriod
  27. } from '../../../../api/newCodePeriod';
  28. import { mockBranch, mockMainBranch, mockPullRequest } from '../../../../helpers/mocks/branch-like';
  29. import { mockComponent, mockEvent } from '../../../../helpers/testMocks';
  30. import App from '../App';
  31. jest.mock('../../../../api/newCodePeriod', () => ({
  32. getNewCodePeriod: jest.fn().mockResolvedValue({}),
  33. resetNewCodePeriod: jest.fn().mockResolvedValue({}),
  34. setNewCodePeriod: jest.fn().mockResolvedValue({})
  35. }));
  36. it('should render correctly', () => {
  37. expect(shallowRender()).toMatchSnapshot();
  38. });
  39. it('should initialize correctly', async () => {
  40. const wrapper = shallowRender({
  41. branchLikes: [mockBranch(), mockPullRequest(), mockMainBranch()]
  42. });
  43. await waitAndUpdate(wrapper);
  44. expect(wrapper.state().branchList).toHaveLength(2);
  45. expect(wrapper.state().referenceBranch).toBe('master');
  46. });
  47. it('should not display reset button if project setting is not set', () => {
  48. const wrapper = shallowRender();
  49. expect(wrapper.find('Button')).toHaveLength(0);
  50. });
  51. it('should reset the setting correctly', async () => {
  52. const wrapper = shallowRender();
  53. await waitAndUpdate(wrapper);
  54. wrapper.instance().resetSetting();
  55. await waitAndUpdate(wrapper);
  56. expect(wrapper.state('currentSetting')).toBeUndefined();
  57. expect(wrapper.state('selected')).toBeUndefined();
  58. });
  59. it('should save correctly', async () => {
  60. const component = mockComponent();
  61. const wrapper = shallowRender({ component });
  62. await waitAndUpdate(wrapper);
  63. wrapper.setState({ selected: 'NUMBER_OF_DAYS', days: '23' });
  64. wrapper.instance().handleSubmit(mockEvent());
  65. await waitAndUpdate(wrapper);
  66. expect(setNewCodePeriod).toHaveBeenCalledWith({
  67. project: component.key,
  68. type: 'NUMBER_OF_DAYS',
  69. value: '23'
  70. });
  71. expect(wrapper.state('currentSetting')).toEqual(wrapper.state('selected'));
  72. });
  73. it('should handle errors gracefully', async () => {
  74. (getNewCodePeriod as jest.Mock).mockRejectedValue('error');
  75. (setNewCodePeriod as jest.Mock).mockRejectedValue('error');
  76. (resetNewCodePeriod as jest.Mock).mockRejectedValue('error');
  77. const wrapper = shallowRender();
  78. wrapper.setState({ selected: 'PREVIOUS_VERSION' });
  79. await waitAndUpdate(wrapper);
  80. expect(wrapper.state('loading')).toBe(false);
  81. wrapper.instance().resetSetting();
  82. await waitAndUpdate(wrapper);
  83. expect(wrapper.state('saving')).toBe(false);
  84. wrapper.instance().handleSubmit(mockEvent());
  85. await waitAndUpdate(wrapper);
  86. expect(wrapper.state('saving')).toBe(false);
  87. });
  88. function shallowRender(props: Partial<App['props']> = {}) {
  89. return shallow<App>(
  90. <App
  91. branchLikes={[mockMainBranch()]}
  92. branchesEnabled={true}
  93. canAdmin={true}
  94. component={mockComponent()}
  95. {...props}
  96. />
  97. );
  98. }