3 * Copyright (C) 2009-2022 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 Radio from '../../../components/controls/Radio';
23 import SelectLegacy from '../../../components/controls/SelectLegacy';
24 import { mockQualityGate } from '../../../helpers/mocks/quality-gates';
25 import { mockCondition } from '../../../helpers/testMocks';
26 import { submit } from '../../../helpers/testUtils';
27 import { MetricKey } from '../../../types/metrics';
28 import { USE_SYSTEM_DEFAULT } from '../constants';
29 import ProjectQualityGateAppRenderer, {
30 ProjectQualityGateAppRendererProps
31 } from '../ProjectQualityGateAppRenderer';
33 it('should render correctly', () => {
34 expect(shallowRender()).toMatchSnapshot('default');
35 expect(shallowRender({ loading: true })).toMatchSnapshot('loading');
36 expect(shallowRender({ submitting: true })).toMatchSnapshot('submitting');
39 currentQualityGate: mockQualityGate({ id: '2', isDefault: true }),
40 selectedQualityGateId: USE_SYSTEM_DEFAULT
42 ).toMatchSnapshot('always use system default');
43 expect(shallowRender({ selectedQualityGateId: '3' })).toMatchSnapshot('show new code warning');
46 selectedQualityGateId: '5'
48 ).toMatchSnapshot('show warning');
51 selectedQualityGateId: USE_SYSTEM_DEFAULT
53 ).toMatchSnapshot('show warning if not using default');
54 expect(shallowRender({ allQualityGates: undefined }).type()).toBeNull(); // no quality gates
57 it('should render select options correctly', () => {
58 return new Promise<void>(resolve => {
59 const wrapper = shallowRender();
60 const render = wrapper.find(SelectLegacy).props().optionRenderer;
62 expect(render).toBeDefined();
64 expect(render!({ value: '1', label: 'Gate 1' })).toMatchSnapshot('default');
69 it('should correctly handle changes', () => {
70 const wrapper = shallowRender();
71 const onSelect = jest.fn(selectedQualityGateId => {
72 wrapper.setProps({ selectedQualityGateId });
74 wrapper.setProps({ onSelect });
80 .onCheck(USE_SYSTEM_DEFAULT);
81 expect(onSelect).toHaveBeenLastCalledWith(USE_SYSTEM_DEFAULT);
88 expect(onSelect).toHaveBeenLastCalledWith('1');
90 wrapper.find(SelectLegacy).props().onChange!({ value: '2' });
91 expect(onSelect).toHaveBeenLastCalledWith('2');
94 it('should correctly handle form submission', () => {
95 const onSubmit = jest.fn();
96 const wrapper = shallowRender({ onSubmit });
97 submit(wrapper.find('form'));
98 expect(onSubmit).toBeCalled();
101 function shallowRender(props: Partial<ProjectQualityGateAppRendererProps> = {}) {
102 const conditions = [mockCondition(), mockCondition({ metric: MetricKey.new_bugs })];
103 const conditionsEmptyOnNew = [mockCondition({ metric: MetricKey.bugs })];
104 return shallow<ProjectQualityGateAppRendererProps>(
105 <ProjectQualityGateAppRenderer
107 mockQualityGate({ conditions }),
108 mockQualityGate({ id: '2', isDefault: true, conditions }),
109 mockQualityGate({ id: '3', isDefault: true, conditions: conditionsEmptyOnNew })
111 currentQualityGate={mockQualityGate({ id: '1' })}
115 selectedQualityGateId="1"