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 { OptionProps, Props as ReactSelectProps } from 'react-select';
23 import RadioCard from '../../../../components/controls/RadioCard';
24 import Select from '../../../../components/controls/Select';
25 import BaselineSettingReferenceBranch, {
26 BaselineSettingReferenceBranchProps,
29 } from '../BaselineSettingReferenceBranch';
31 it('should render correctly', () => {
32 expect(shallowRender()).toMatchSnapshot('Project level');
33 expect(shallowRender({ settingLevel: 'branch' })).toMatchSnapshot('Branch level');
36 branchList: [{ label: 'master', value: 'master', isMain: true }],
37 settingLevel: 'branch',
39 ).toMatchSnapshot('Branch level - no other branches');
42 it('should not display input when not selected', () => {
43 const wrapper = shallowRender({ selected: false });
44 expect(wrapper.find('SearchSelect')).toHaveLength(0);
47 it('should callback when clicked', () => {
48 const onSelect = jest.fn();
49 const wrapper = shallowRender({ onSelect, selected: false });
51 wrapper.find(RadioCard).first().simulate('click');
52 expect(onSelect).toHaveBeenCalledWith('REFERENCE_BRANCH');
55 it('should callback when changing selection', () => {
56 const onChangeReferenceBranch = jest.fn();
57 const wrapper = shallowRender({ onChangeReferenceBranch });
59 wrapper.find(Select).first().simulate('change', { value: 'branch-6.9' });
60 expect(onChangeReferenceBranch).toHaveBeenCalledWith('branch-6.9');
63 it('should handle an invalid branch', () => {
64 const unknownBranchName = 'branch-unknown';
65 const wrapper = shallowRender({ referenceBranch: unknownBranchName });
67 expect(wrapper.find<ReactSelectProps>(Select).first().props().value).toEqual({
68 label: unknownBranchName,
69 value: unknownBranchName,
75 describe('renderOption', () => {
76 // fake props injected by the Select itself
77 const props = {} as OptionProps<BranchOption, false>;
79 it('should render correctly', () => {
81 renderBranchOption({ ...props, data: { label: 'master', value: 'master', isMain: true } })
82 ).toMatchSnapshot('main');
86 data: { label: 'branch-7.4', value: 'branch-7.4', isMain: false },
88 ).toMatchSnapshot('branch');
92 data: { label: 'disabled', value: 'disabled', isMain: false, isDisabled: true },
94 ).toMatchSnapshot('disabled');
98 data: { label: 'branch-nope', value: 'branch-nope', isMain: false, isInvalid: true },
100 ).toMatchSnapshot("branch doesn't exist");
104 function shallowRender(props: Partial<BaselineSettingReferenceBranchProps> = {}) {
105 const branchOptions = [
106 { label: 'master', value: 'master', isMain: true },
107 { label: 'branch-7.9', value: 'branch-7.9', isMain: false },
111 <BaselineSettingReferenceBranch
112 branchList={branchOptions}
113 settingLevel="project"
114 onChangeReferenceBranch={jest.fn()}
116 referenceBranch="master"