3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 import { shallow } from 'enzyme';
21 import * as React from 'react';
22 import { setNewCodePeriod } from '../../../../api/newCodePeriod';
23 import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like';
24 import { mockEvent, waitAndUpdate } from '../../../../helpers/testUtils';
25 import BranchBaselineSettingModal from '../BranchBaselineSettingModal';
27 jest.mock('../../../../api/newCodePeriod', () => ({
28 setNewCodePeriod: jest.fn().mockResolvedValue({}),
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot('only one branch');
34 shallowRender({ branchList: [mockMainBranch(), mockBranch()], branch: mockMainBranch() })
35 ).toMatchSnapshot('multiple branches');
38 it('should display the branch analysis list when necessary', () => {
39 const wrapper = shallowRender();
41 wrapper.setState({ selected: 'SPECIFIC_ANALYSIS' });
43 expect(wrapper.find('BranchAnalysisList')).toHaveLength(1);
46 it('should save correctly', async () => {
47 const branch = mockMainBranch({ name: 'branchname' });
48 const component = 'compKey';
49 const wrapper = shallowRender({
54 wrapper.setState({ analysis: 'analysis572893', selected: 'SPECIFIC_ANALYSIS' });
55 await waitAndUpdate(wrapper);
57 wrapper.instance().handleSubmit(mockEvent());
58 await new Promise(setImmediate);
60 expect(setNewCodePeriod).toHaveBeenCalledWith({
62 type: 'SPECIFIC_ANALYSIS',
63 value: 'analysis572893',
68 it('should disable the save button when saving', () => {
69 const wrapper = shallowRender();
71 wrapper.setState({ saving: true });
73 expect(wrapper.find('SubmitButton').first().prop('disabled')).toBe(true);
76 it('should disable the save button when date is invalid', () => {
77 const wrapper = shallowRender();
79 wrapper.setState({ days: 'asdf' });
81 expect(wrapper.find('SubmitButton').first().prop('disabled')).toBe(true);
84 function shallowRender(props: Partial<BranchBaselineSettingModal['props']> = {}) {
85 return shallow<BranchBaselineSettingModal>(
86 <BranchBaselineSettingModal
87 branch={mockMainBranch()}
88 branchList={[mockMainBranch()]}