From ad13f533082d8d41a556683679879eaada9f1220 Mon Sep 17 00:00:00 2001 From: Jonathan Weibel Date: Thu, 1 Oct 2020 11:14:51 +0200 Subject: [PATCH] SONAR-12620 Auto-select the language when creating a Quality Profile --- .../home/CreateProfileForm.tsx | 12 +- .../quality-profiles/home/HomeContainer.tsx | 3 +- .../apps/quality-profiles/home/PageHeader.tsx | 6 +- .../home/__tests__/CreateProfileForm-test.tsx | 18 ++- .../home/__tests__/PageHeader-test.tsx | 8 +- .../CreateProfileForm-test.tsx.snap | 148 +++++++++++++++++- .../__snapshots__/PageHeader-test.tsx.snap | 11 ++ 7 files changed, 189 insertions(+), 17 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/home/CreateProfileForm.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/home/CreateProfileForm.tsx index 0ea80f8cca3..4ac543498ed 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/home/CreateProfileForm.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/home/CreateProfileForm.tsx @@ -23,15 +23,18 @@ import { ResetButtonLink, SubmitButton } from 'sonar-ui-common/components/contro import Modal from 'sonar-ui-common/components/controls/Modal'; import Select from 'sonar-ui-common/components/controls/Select'; import { translate } from 'sonar-ui-common/helpers/l10n'; +import { parseAsOptionalString } from 'sonar-ui-common/helpers/query'; import { changeProfileParent, createQualityProfile, getImporters } from '../../../api/quality-profiles'; +import { Location } from '../../../components/hoc/withRouter'; import { Profile } from '../types'; interface Props { languages: Array<{ key: string; name: string }>; + location: Location; onClose: () => void; onCreate: Function; organization: string | null; @@ -115,10 +118,11 @@ export default class CreateProfileForm extends React.PureComponent render() { const header = translate('quality_profiles.new_profile'); + const languageQueryFilter = parseAsOptionalString(this.props.location.query.language); const languages = sortBy(this.props.languages, 'name'); let profiles: Array<{ label: string; value: string }> = []; - const selectedLanguage = this.state.language || languages[0].key; + const selectedLanguage = this.state.language || languageQueryFilter || languages[0].key; const importers = this.state.importers.filter(importer => importer.languages.includes(selectedLanguage) ); @@ -176,9 +180,9 @@ export default class CreateProfileForm extends React.PureComponent id="create-profile-language" name="language" onChange={this.handleLanguageChange} - options={languages.map(language => ({ - label: language.name, - value: language.key + options={languages.map(l => ({ + label: l.name, + value: l.key }))} value={selectedLanguage} /> diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/home/HomeContainer.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/home/HomeContainer.tsx index b7ff4e359b8..df836bd583e 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/home/HomeContainer.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/home/HomeContainer.tsx @@ -19,6 +19,7 @@ */ import * as React from 'react'; import { Actions } from '../../../api/quality-profiles'; +import { Location } from '../../../components/hoc/withRouter'; import { Profile } from '../types'; import Evolution from './Evolution'; import PageHeader from './PageHeader'; @@ -27,7 +28,7 @@ import ProfilesList from './ProfilesList'; interface Props { actions: Actions; languages: Array<{ key: string; name: string }>; - location: { query: T.Dict }; + location: Location; organization: string | null; profiles: Profile[]; updateProfiles: () => Promise; diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/home/PageHeader.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/home/PageHeader.tsx index bb141e8aee2..a4726522c1c 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/home/PageHeader.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/home/PageHeader.tsx @@ -23,7 +23,7 @@ import { Button } from 'sonar-ui-common/components/controls/buttons'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import { translate } from 'sonar-ui-common/helpers/l10n'; import { Actions } from '../../../api/quality-profiles'; -import { Router, withRouter } from '../../../components/hoc/withRouter'; +import { Location, Router, withRouter } from '../../../components/hoc/withRouter'; import { Profile } from '../types'; import { getProfilePath } from '../utils'; import CreateProfileForm from './CreateProfileForm'; @@ -32,6 +32,7 @@ import RestoreProfileForm from './RestoreProfileForm'; interface Props { actions: Actions; languages: Array<{ key: string; name: string }>; + location: Location; organization: string | null; profiles: Profile[]; router: Pick; @@ -77,7 +78,7 @@ export class PageHeader extends React.PureComponent { }; render() { - const { actions, languages, organization, profiles } = this.props; + const { actions, languages, location, organization, profiles } = this.props; return (

{translate('quality_profiles.page')}

@@ -129,6 +130,7 @@ export class PageHeader extends React.PureComponent { {this.state.createFormOpen && ( jest.clearAllMocks()); @@ -32,7 +32,7 @@ jest.mock('../../../../api/quality-profiles', () => ({ getImporters: jest.fn().mockResolvedValue([ { key: 'key_importer', - languages: ['lang1_importer', 'lang2_importer', 'kr'], + languages: ['lang1_importer', 'lang2_importer', 'js'], name: 'name_importer' } ]) @@ -41,7 +41,11 @@ jest.mock('../../../../api/quality-profiles', () => ({ it('should render correctly', async () => { const wrapper = shallowRender(); await waitAndUpdate(wrapper); - expect(wrapper).toMatchSnapshot(); + + expect(wrapper).toMatchSnapshot('default'); + expect( + wrapper.setProps({ location: mockLocation({ query: { language: 'js' } }) }) + ).toMatchSnapshot('with query filter'); }); it('should handle form submit correctly', async () => { @@ -72,11 +76,15 @@ it('should handle form submit without parent correctly', async () => { function shallowRender(props?: Partial) { return shallow( ); diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/home/__tests__/PageHeader-test.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/home/__tests__/PageHeader-test.tsx index 11990b1789f..d71b11092fb 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/home/__tests__/PageHeader-test.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/home/__tests__/PageHeader-test.tsx @@ -20,7 +20,12 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { click } from 'sonar-ui-common/helpers/testUtils'; -import { mockLanguage, mockQualityProfile, mockRouter } from '../../../../helpers/testMocks'; +import { + mockLanguage, + mockLocation, + mockQualityProfile, + mockRouter +} from '../../../../helpers/testMocks'; import { PageHeader } from '../PageHeader'; it('should render correctly', () => { @@ -46,6 +51,7 @@ function shallowRender(props: Partial = {}) { + +
+ + +
+
+ + create + + + cancel + +
+ + +`; + +exports[`should render correctly: with query filter 1`] = ` + +
+
+

+ quality_profiles.new_profile +

+
+
+
+ + +
+
+ +