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 Select from '../../../../components/controls/Select';
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 correctly handle changes', () => {
32 const onSubmit = jest.fn();
33 const wrapper = shallowRender({ onSubmit });
35 const langSelect = getLanguageSelect(wrapper);
36 let profileSelect = getProfileSelect(wrapper);
38 // Language select should only have 2; JS is not available. Profile Select
39 // should have none, as no language is selected yet.
40 expect(langSelect.props().options).toHaveLength(2);
41 expect(profileSelect.props().options).toHaveLength(0);
44 const langChange = langSelect.props().onChange;
46 expect(langChange).toBeDefined();
48 langChange!({ value: 'css' });
50 // Should now show 2 available profiles.
51 profileSelect = getProfileSelect(wrapper);
52 expect(profileSelect.props().options).toHaveLength(2);
53 expect(profileSelect.props().options).toEqual(
54 expect.arrayContaining([expect.objectContaining({ isDisabled: true })])
58 const profileChange = profileSelect.props().onChange;
60 expect(profileChange).toBeDefined();
62 profileChange!({ value: 'css2' });
64 submitSimpleModal(wrapper);
65 expect(onSubmit).toHaveBeenLastCalledWith('css2');
68 function diveIntoSimpleModal(wrapper: ShallowWrapper) {
69 return wrapper.find(SimpleModal).dive().children();
72 function getLanguageSelect(wrapper: ShallowWrapper) {
73 return diveIntoSimpleModal(wrapper).find(Select).at(0);
76 function getProfileSelect(wrapper: ShallowWrapper) {
77 return diveIntoSimpleModal(wrapper).find(Select).at(1);
80 function submitSimpleModal(wrapper: ShallowWrapper) {
81 wrapper.find(SimpleModal).props().onSubmit();
84 function shallowRender(props: Partial<AddLanguageModalProps> = {}) {
85 return shallow<AddLanguageModalProps>(
88 css: { key: 'css', name: 'CSS' },
89 ts: { key: 'ts', name: 'TS' },
90 js: { key: 'js', name: 'JS' },
96 mockQualityProfile({ key: 'css', name: 'CSS', activeRuleCount: 0 }),
97 mockQualityProfile({ key: 'css2', name: 'CSS 2' }),
99 ts: [mockQualityProfile({ key: 'ts', name: 'TS' })],
100 js: [mockQualityProfile({ key: 'js', name: 'JS' })],
102 unavailableLanguages={['js']}