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, ShallowWrapper } from 'enzyme';
21 import * as React from 'react';
22 import SelectLegacy from '../../../../components/controls/SelectLegacy';
23 import SimpleModal from '../../../../components/controls/SimpleModal';
24 import { mockQualityProfile } from '../../../../helpers/testMocks';
25 import { AddLanguageModal, AddLanguageModalProps } from '../AddLanguageModal';
27 it('should render correctly', () => {
28 expect(diveIntoSimpleModal(shallowRender())).toMatchSnapshot('default');
31 it('should render select options correctly', () => {
32 return new Promise<void>((resolve, reject) => {
33 const wrapper = shallowRender();
35 const langOnChange = getLanguageSelect(wrapper).props().onChange;
40 langOnChange({ value: 'js' });
42 const render = getProfileSelect(wrapper).props().optionRenderer;
47 expect(render({ value: 'bar', label: 'Profile 1' })).toMatchSnapshot('default');
52 it('should correctly handle changes', () => {
53 const onSubmit = jest.fn();
54 const wrapper = shallowRender({ onSubmit });
56 const langSelect = getLanguageSelect(wrapper);
57 let profileSelect = getProfileSelect(wrapper);
59 // Language select should only have 2; JS is not available. Profile Select
60 // should have none, as no language is selected yet.
61 expect(langSelect.props().options).toHaveLength(2);
62 expect(profileSelect.props().options).toHaveLength(0);
65 const langChange = langSelect.props().onChange;
67 expect(langChange).toBeDefined();
69 langChange!({ value: 'css' });
71 // Should now show 2 available profiles.
72 profileSelect = getProfileSelect(wrapper);
73 expect(profileSelect.props().options).toHaveLength(2);
74 expect(profileSelect.props().options).toEqual(
75 expect.arrayContaining([expect.objectContaining({ disabled: true })])
79 const profileChange = profileSelect.props().onChange;
81 expect(profileChange).toBeDefined();
83 profileChange!({ value: 'css2' });
85 submitSimpleModal(wrapper);
86 expect(onSubmit).toHaveBeenLastCalledWith('css2');
89 function diveIntoSimpleModal(wrapper: ShallowWrapper) {
96 function getLanguageSelect(wrapper: ShallowWrapper) {
97 return diveIntoSimpleModal(wrapper)
102 function getProfileSelect(wrapper: ShallowWrapper) {
103 return diveIntoSimpleModal(wrapper)
108 function submitSimpleModal(wrapper: ShallowWrapper) {
115 function shallowRender(props: Partial<AddLanguageModalProps> = {}) {
116 return shallow<AddLanguageModalProps>(
119 css: { key: 'css', name: 'CSS' },
120 ts: { key: 'ts', name: 'TS' },
121 js: { key: 'js', name: 'JS' }
125 profilesByLanguage={{
127 mockQualityProfile({ key: 'css', name: 'CSS', activeRuleCount: 0 }),
128 mockQualityProfile({ key: 'css2', name: 'CSS 2' })
130 ts: [mockQualityProfile({ key: 'ts', name: 'TS' })],
131 js: [mockQualityProfile({ key: 'js', name: 'JS' })]
133 unavailableLanguages={['js']}