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, ShallowWrapper } from 'enzyme';
21 import * as React from 'react';
22 import Radio from '../../../../components/controls/Radio';
23 import Select from '../../../../components/controls/Select';
24 import SimpleModal from '../../../../components/controls/SimpleModal';
25 import { mockComponent } from '../../../../helpers/mocks/component';
26 import { mockQualityProfile } from '../../../../helpers/testMocks';
27 import SetQualityProfileModal, { SetQualityProfileModalProps } from '../SetQualityProfileModal';
29 it('should render correctly', () => {
30 expect(shallowRender()).toMatchSnapshot('default');
31 expect(shallowRender({ usesDefault: true })).toMatchSnapshot('inherits system default');
32 expect(shallowRender({ component: mockComponent() })).toMatchSnapshot('needs reanalysis');
35 it('should correctly handle changes', () => {
36 const onSubmit = jest.fn();
37 const wrapper = shallowRender({ onSubmit }, false);
39 diveIntoSimpleModal(wrapper).find(Radio).at(0).props().onCheck('');
40 submitSimpleModal(wrapper);
41 expect(onSubmit).toHaveBeenLastCalledWith(undefined, 'foo');
43 diveIntoSimpleModal(wrapper).find(Radio).at(1).props().onCheck('');
44 diveIntoSimpleModal(wrapper).find(Select).props().onChange({ value: 'bar' });
45 submitSimpleModal(wrapper);
46 expect(onSubmit).toHaveBeenLastCalledWith('bar', 'foo');
48 const change = diveIntoSimpleModal(wrapper).find(Select).props().onChange;
50 expect(change).toBeDefined();
52 change!({ value: 'bar' });
53 submitSimpleModal(wrapper);
54 expect(onSubmit).toHaveBeenLastCalledWith('bar', 'foo');
57 function diveIntoSimpleModal(wrapper: ShallowWrapper) {
58 return wrapper.find(SimpleModal).dive().children();
61 function submitSimpleModal(wrapper: ShallowWrapper) {
62 wrapper.find(SimpleModal).props().onSubmit();
65 function shallowRender(props: Partial<SetQualityProfileModalProps> = {}, dive = true) {
66 const wrapper = shallow<SetQualityProfileModalProps>(
67 <SetQualityProfileModal
69 mockQualityProfile({ key: 'foo', isDefault: true, language: 'js' }),
70 mockQualityProfile({ key: 'bar', language: 'js', activeRuleCount: 0 }),
72 component={mockComponent({ qualityProfiles: [{ key: 'foo', name: 'Foo', language: 'js' }] })}
73 currentProfile={mockQualityProfile({ key: 'foo', language: 'js' })}
81 return dive ? diveIntoSimpleModal(wrapper) : wrapper;