diff options
Diffstat (limited to 'server/sonar-web/src/main/js/apps/projectBaseline')
23 files changed, 191 insertions, 109 deletions
diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingAnalysis.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingAnalysis.tsx index f31cd308728..0528174c70c 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingAnalysis.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingAnalysis.tsx @@ -32,7 +32,7 @@ export default function BaselineSettingAnalysis({ disabled, onSelect, selected } return ( <RadioCard disabled={disabled} - onClick={() => onSelect('SPECIFIC_ANALYSIS')} + onClick={() => onSelect(NewCodePeriodSettingType.SPECIFIC_ANALYSIS)} selected={selected} title={translate('baseline.specific_analysis')} > diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingDays.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingDays.tsx index 0f356bfd880..ef42354448d 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingDays.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingDays.tsx @@ -41,7 +41,7 @@ export default function BaselineSettingDays(props: Props) { <RadioCard className={className} disabled={disabled} - onClick={() => onSelect('NUMBER_OF_DAYS')} + onClick={() => onSelect(NewCodePeriodSettingType.NUMBER_OF_DAYS)} selected={selected} title={translate('baseline.number_days')} > diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingPreviousVersion.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingPreviousVersion.tsx index 5084aa5a124..e0ccc8487f9 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingPreviousVersion.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingPreviousVersion.tsx @@ -34,7 +34,7 @@ export default function BaselineSettingPreviousVersion(props: Props) { return ( <RadioCard disabled={disabled} - onClick={() => onSelect('PREVIOUS_VERSION')} + onClick={() => onSelect(NewCodePeriodSettingType.PREVIOUS_VERSION)} selected={selected} title={ translate('baseline.previous_version') + (isDefault ? ` (${translate('default')})` : '') diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingReferenceBranch.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingReferenceBranch.tsx index 35600e31718..6604d1e39da 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingReferenceBranch.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BaselineSettingReferenceBranch.tsx @@ -98,7 +98,7 @@ export default function BaselineSettingReferenceBranch(props: BaselineSettingRef <RadioCard className={className} disabled={disabled} - onClick={() => props.onSelect('REFERENCE_BRANCH')} + onClick={() => props.onSelect(NewCodePeriodSettingType.REFERENCE_BRANCH)} selected={selected} title={translate('baseline.reference_branch')} > diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchBaselineSettingModal.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchBaselineSettingModal.tsx index a9236fe447e..0a3ec852329 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchBaselineSettingModal.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchBaselineSettingModal.tsx @@ -60,9 +60,10 @@ export default class BranchBaselineSettingModal extends React.PureComponent<Prop const defaultBranch = otherBranches.length > 0 ? otherBranches[0].name : ''; this.state = { - analysis: this.getValueFromProps('SPECIFIC_ANALYSIS') || '', - days: this.getValueFromProps('NUMBER_OF_DAYS') || '30', - referenceBranch: this.getValueFromProps('REFERENCE_BRANCH') || defaultBranch, + analysis: this.getValueFromProps(NewCodePeriodSettingType.SPECIFIC_ANALYSIS) || '', + days: this.getValueFromProps(NewCodePeriodSettingType.NUMBER_OF_DAYS) || '30', + referenceBranch: + this.getValueFromProps(NewCodePeriodSettingType.REFERENCE_BRANCH) || defaultBranch, saving: false, selected: this.props.branch.newCodePeriod && this.props.branch.newCodePeriod.type, }; @@ -164,7 +165,7 @@ export default class BranchBaselineSettingModal extends React.PureComponent<Prop <BaselineSettingPreviousVersion isDefault={false} onSelect={this.handleSelectSetting} - selected={selected === 'PREVIOUS_VERSION'} + selected={selected === NewCodePeriodSettingType.PREVIOUS_VERSION} /> <BaselineSettingDays days={days} @@ -172,22 +173,22 @@ export default class BranchBaselineSettingModal extends React.PureComponent<Prop isValid={isValid} onChangeDays={this.handleSelectDays} onSelect={this.handleSelectSetting} - selected={selected === 'NUMBER_OF_DAYS'} + selected={selected === NewCodePeriodSettingType.NUMBER_OF_DAYS} /> <BaselineSettingAnalysis onSelect={this.handleSelectSetting} - selected={selected === 'SPECIFIC_ANALYSIS'} + selected={selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS} /> <BaselineSettingReferenceBranch branchList={branchList.map(this.branchToOption)} onChangeReferenceBranch={this.handleSelectReferenceBranch} onSelect={this.handleSelectSetting} referenceBranch={referenceBranch} - selected={selected === 'REFERENCE_BRANCH'} + selected={selected === NewCodePeriodSettingType.REFERENCE_BRANCH} settingLevel="branch" /> </div> - {selected === 'SPECIFIC_ANALYSIS' && ( + {selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS && ( <BranchAnalysisList analysis={analysis} branch={branch.name} diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchList.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchList.tsx index a3a5bf7f2dc..7d9943686e5 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchList.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchList.tsx @@ -24,6 +24,7 @@ import { isBranch, sortBranches } from '../../../helpers/branch-like'; import { translate } from '../../../helpers/l10n'; import { Branch, BranchLike, BranchWithNewCodePeriod } from '../../../types/branch-like'; import { Component, NewCodePeriod } from '../../../types/types'; +import { DEFAULT_GENERAL_SETTING_TYPE } from '../constants'; import BranchBaselineSettingModal from './BranchBaselineSettingModal'; import BranchListRow from './BranchListRow'; @@ -74,7 +75,7 @@ export default class BranchList extends React.PureComponent<Props, State> { if (!newCodePeriod) { return b; } - const { type = 'PREVIOUS_VERSION', value, effectiveValue } = newCodePeriod; + const { type = DEFAULT_GENERAL_SETTING_TYPE, value, effectiveValue } = newCodePeriod; return { ...b, newCodePeriod: { type, value, effectiveValue }, diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchListRow.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchListRow.tsx index 9ed131753c9..ff07fee94dd 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchListRow.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/BranchListRow.tsx @@ -25,7 +25,7 @@ import WarningIcon from '../../../components/icons/WarningIcon'; import DateTimeFormatter from '../../../components/intl/DateTimeFormatter'; import { translate, translateWithParameters } from '../../../helpers/l10n'; import { BranchWithNewCodePeriod } from '../../../types/branch-like'; -import { NewCodePeriod } from '../../../types/types'; +import { NewCodePeriod, NewCodePeriodSettingType } from '../../../types/types'; export interface BranchListRowProps { branch: BranchWithNewCodePeriod; @@ -37,7 +37,7 @@ export interface BranchListRowProps { function renderNewCodePeriodSetting(newCodePeriod: NewCodePeriod) { switch (newCodePeriod.type) { - case 'SPECIFIC_ANALYSIS': + case NewCodePeriodSettingType.SPECIFIC_ANALYSIS: return ( <> {`${translate('baseline.specific_analysis')}: `} @@ -48,11 +48,11 @@ function renderNewCodePeriodSetting(newCodePeriod: NewCodePeriod) { )} </> ); - case 'NUMBER_OF_DAYS': + case NewCodePeriodSettingType.NUMBER_OF_DAYS: return `${translate('baseline.number_days')}: ${newCodePeriod.value}`; - case 'PREVIOUS_VERSION': + case NewCodePeriodSettingType.PREVIOUS_VERSION: return translate('baseline.previous_version'); - case 'REFERENCE_BRANCH': + case NewCodePeriodSettingType.REFERENCE_BRANCH: return `${translate('baseline.reference_branch')}: ${newCodePeriod.value}`; default: return newCodePeriod.type; @@ -65,7 +65,7 @@ function branchInheritsItselfAsReference( ) { return ( !branch.newCodePeriod && - inheritedSetting.type === 'REFERENCE_BRANCH' && + inheritedSetting.type === NewCodePeriodSettingType.REFERENCE_BRANCH && branch.name === inheritedSetting.value ); } @@ -77,7 +77,7 @@ function referenceBranchDoesNotExist( return ( branch.newCodePeriod && branch.newCodePeriod.value && - branch.newCodePeriod.type === 'REFERENCE_BRANCH' && + branch.newCodePeriod.type === NewCodePeriodSettingType.REFERENCE_BRANCH && !existingBranches.includes(branch.newCodePeriod.value) ); } diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/App.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineApp.tsx index 80dbcf8f0eb..025cc21974e 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineApp.tsx @@ -36,6 +36,7 @@ import { Branch, BranchLike } from '../../../types/branch-like'; import { Feature } from '../../../types/features'; import { ParsedAnalysis } from '../../../types/project-activity'; import { Component, NewCodePeriod, NewCodePeriodSettingType } from '../../../types/types'; +import { DEFAULT_GENERAL_SETTING_TYPE } from '../constants'; import '../styles.css'; import { getSettingValue } from '../utils'; import AppHeader from './AppHeader'; @@ -66,11 +67,7 @@ interface State { const DEFAULT_NUMBER_OF_DAYS = '30'; -const DEFAULT_GENERAL_SETTING: { type: NewCodePeriodSettingType } = { - type: 'PREVIOUS_VERSION', -}; - -export class App extends React.PureComponent<Props, State> { +export class ProjectBaselineApp extends React.PureComponent<Props, State> { mounted = false; state: State = { branchList: [], @@ -107,7 +104,8 @@ export class App extends React.PureComponent<Props, State> { const { referenceBranch } = this.state; const defaultDays = - (generalSetting.type === 'NUMBER_OF_DAYS' && generalSetting.value) || DEFAULT_NUMBER_OF_DAYS; + (generalSetting.type === NewCodePeriodSettingType.NUMBER_OF_DAYS && generalSetting.value) || + DEFAULT_NUMBER_OF_DAYS; return { loading: false, @@ -116,10 +114,15 @@ export class App extends React.PureComponent<Props, State> { generalSetting, selected: currentSetting || generalSetting.type, overrideGeneralSetting: Boolean(currentSetting), - days: (currentSetting === 'NUMBER_OF_DAYS' && currentSettingValue) || defaultDays, - analysis: (currentSetting === 'SPECIFIC_ANALYSIS' && currentSettingValue) || '', + days: + (currentSetting === NewCodePeriodSettingType.NUMBER_OF_DAYS && currentSettingValue) || + defaultDays, + analysis: + (currentSetting === NewCodePeriodSettingType.SPECIFIC_ANALYSIS && currentSettingValue) || + '', referenceBranch: - (currentSetting === 'REFERENCE_BRANCH' && currentSettingValue) || referenceBranch, + (currentSetting === NewCodePeriodSettingType.REFERENCE_BRANCH && currentSettingValue) || + referenceBranch, }; } @@ -143,10 +146,12 @@ export class App extends React.PureComponent<Props, State> { ([generalSetting, setting]) => { if (this.mounted) { if (!generalSetting.type) { - generalSetting = DEFAULT_GENERAL_SETTING; + generalSetting = { type: DEFAULT_GENERAL_SETTING_TYPE }; } const currentSettingValue = setting.value; - const currentSetting = setting.inherited ? undefined : setting.type || 'PREVIOUS_VERSION'; + const currentSetting = setting.inherited + ? undefined + : setting.type || DEFAULT_GENERAL_SETTING_TYPE; this.setState( this.getUpdatedState({ @@ -191,8 +196,11 @@ export class App extends React.PureComponent<Props, State> { handleCancel = () => this.setState( - ({ generalSetting = DEFAULT_GENERAL_SETTING, currentSetting, currentSettingValue }) => - this.getUpdatedState({ generalSetting, currentSetting, currentSettingValue }) + ({ + generalSetting = { type: DEFAULT_GENERAL_SETTING_TYPE }, + currentSetting, + currentSettingValue, + }) => this.getUpdatedState({ generalSetting, currentSetting, currentSettingValue }) ); handleSelectSetting = (selected?: NewCodePeriodSettingType) => this.setState({ selected }); @@ -322,4 +330,4 @@ export class App extends React.PureComponent<Props, State> { } } -export default withComponentContext(withAvailableFeatures(withAppStateContext(App))); +export default withComponentContext(withAvailableFeatures(withAppStateContext(ProjectBaselineApp))); diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineSelector.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineSelector.tsx index 76aa0647165..ac0eb18dbef 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineSelector.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/ProjectBaselineSelector.tsx @@ -60,7 +60,7 @@ export interface ProjectBaselineSelectorProps { function renderGeneralSetting(generalSetting: NewCodePeriod) { let setting: string; let description: string; - if (generalSetting.type === 'NUMBER_OF_DAYS') { + if (generalSetting.type === NewCodePeriodSettingType.NUMBER_OF_DAYS) { setting = `${translate('baseline.number_days')} (${translateWithParameters( 'duration.days', generalSetting.value || '?' @@ -137,7 +137,9 @@ export default function ProjectBaselineSelector(props: ProjectBaselineSelectorPr <BaselineSettingPreviousVersion disabled={!overrideGeneralSetting} onSelect={props.onSelectSetting} - selected={overrideGeneralSetting && selected === 'PREVIOUS_VERSION'} + selected={ + overrideGeneralSetting && selected === NewCodePeriodSettingType.PREVIOUS_VERSION + } /> <BaselineSettingDays days={days} @@ -146,7 +148,9 @@ export default function ProjectBaselineSelector(props: ProjectBaselineSelectorPr isValid={isValid} onChangeDays={props.onSelectDays} onSelect={props.onSelectSetting} - selected={overrideGeneralSetting && selected === 'NUMBER_OF_DAYS'} + selected={ + overrideGeneralSetting && selected === NewCodePeriodSettingType.NUMBER_OF_DAYS + } /> {branchesEnabled ? ( <BaselineSettingReferenceBranch @@ -155,18 +159,22 @@ export default function ProjectBaselineSelector(props: ProjectBaselineSelectorPr onChangeReferenceBranch={props.onSelectReferenceBranch} onSelect={props.onSelectSetting} referenceBranch={referenceBranch || ''} - selected={overrideGeneralSetting && selected === 'REFERENCE_BRANCH'} + selected={ + overrideGeneralSetting && selected === NewCodePeriodSettingType.REFERENCE_BRANCH + } settingLevel="project" /> ) : ( <BaselineSettingAnalysis disabled={!overrideGeneralSetting} onSelect={props.onSelectSetting} - selected={overrideGeneralSetting && selected === 'SPECIFIC_ANALYSIS'} + selected={ + overrideGeneralSetting && selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS + } /> )} </div> - {selected === 'SPECIFIC_ANALYSIS' && ( + {selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS && ( <BranchAnalysisList analysis={analysis || ''} branch={branch.name} diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingAnalysis-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingAnalysis-test.tsx index c4b9ec1718b..f6960dbe0fb 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingAnalysis-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingAnalysis-test.tsx @@ -19,6 +19,7 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BaselineSettingAnalysis, { Props } from '../BaselineSettingAnalysis'; it('should render correctly', () => { @@ -30,7 +31,7 @@ it('should callback when clicked', () => { const wrapper = shallowRender({ onSelect, selected: false }); wrapper.find('RadioCard').first().simulate('click'); - expect(onSelect).toHaveBeenCalledWith('SPECIFIC_ANALYSIS'); + expect(onSelect).toHaveBeenCalledWith(NewCodePeriodSettingType.SPECIFIC_ANALYSIS); }); function shallowRender(props: Partial<Props> = {}) { diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingDays-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingDays-test.tsx index 95fcbe423d4..a7aa306c6c6 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingDays-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingDays-test.tsx @@ -19,6 +19,7 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BaselineSettingDays, { Props } from '../BaselineSettingDays'; it('should render correctly', () => { @@ -37,7 +38,7 @@ it('should callback when clicked', () => { const wrapper = shallowRender({ onSelect, selected: false }); wrapper.find('RadioCard').first().simulate('click'); - expect(onSelect).toHaveBeenCalledWith('NUMBER_OF_DAYS'); + expect(onSelect).toHaveBeenCalledWith(NewCodePeriodSettingType.NUMBER_OF_DAYS); }); it('should callback when changing days', () => { diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingPreviousVersion-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingPreviousVersion-test.tsx index b680fb9cb88..9072f671c8d 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingPreviousVersion-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingPreviousVersion-test.tsx @@ -19,6 +19,7 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BaselineSettingPreviousVersion, { Props } from '../BaselineSettingPreviousVersion'; it('should render correctly', () => { @@ -31,7 +32,7 @@ it('should callback when clicked', () => { const wrapper = shallowRender({ onSelect, selected: false }); wrapper.find('RadioCard').first().simulate('click'); - expect(onSelect).toHaveBeenCalledWith('PREVIOUS_VERSION'); + expect(onSelect).toHaveBeenCalledWith(NewCodePeriodSettingType.PREVIOUS_VERSION); }); function shallowRender(props: Partial<Props> = {}) { diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingReferenceBranch-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingReferenceBranch-test.tsx index c240643795d..dd9f28b5a76 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingReferenceBranch-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BaselineSettingReferenceBranch-test.tsx @@ -22,6 +22,7 @@ import * as React from 'react'; import { OptionProps, Props as ReactSelectProps } from 'react-select'; import RadioCard from '../../../../components/controls/RadioCard'; import Select from '../../../../components/controls/Select'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BaselineSettingReferenceBranch, { BaselineSettingReferenceBranchProps, BranchOption, @@ -49,7 +50,7 @@ it('should callback when clicked', () => { const wrapper = shallowRender({ onSelect, selected: false }); wrapper.find(RadioCard).first().simulate('click'); - expect(onSelect).toHaveBeenCalledWith('REFERENCE_BRANCH'); + expect(onSelect).toHaveBeenCalledWith(NewCodePeriodSettingType.REFERENCE_BRANCH); }); it('should callback when changing selection', () => { diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchBaselineSettingModal-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchBaselineSettingModal-test.tsx index 06f71f16c33..8a0e83b8e50 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchBaselineSettingModal-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchBaselineSettingModal-test.tsx @@ -22,6 +22,7 @@ import * as React from 'react'; import { setNewCodePeriod } from '../../../../api/newCodePeriod'; import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like'; import { mockEvent, waitAndUpdate } from '../../../../helpers/testUtils'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BranchBaselineSettingModal from '../BranchBaselineSettingModal'; jest.mock('../../../../api/newCodePeriod', () => ({ @@ -38,7 +39,7 @@ it('should render correctly', () => { it('should display the branch analysis list when necessary', () => { const wrapper = shallowRender(); - wrapper.setState({ selected: 'SPECIFIC_ANALYSIS' }); + wrapper.setState({ selected: NewCodePeriodSettingType.SPECIFIC_ANALYSIS }); expect(wrapper.find('BranchAnalysisList')).toHaveLength(1); }); @@ -51,7 +52,10 @@ it('should save correctly', async () => { component, }); - wrapper.setState({ analysis: 'analysis572893', selected: 'SPECIFIC_ANALYSIS' }); + wrapper.setState({ + analysis: 'analysis572893', + selected: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, + }); await waitAndUpdate(wrapper); wrapper.instance().handleSubmit(mockEvent()); @@ -59,7 +63,7 @@ it('should save correctly', async () => { expect(setNewCodePeriod).toHaveBeenCalledWith({ project: component, - type: 'SPECIFIC_ANALYSIS', + type: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, value: 'analysis572893', branch: 'branchname', }); diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchList-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchList-test.tsx index df935426e90..3fab1ff976f 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchList-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchList-test.tsx @@ -23,6 +23,7 @@ import { listBranchesNewCodePeriod, resetNewCodePeriod } from '../../../../api/n import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like'; import { mockComponent } from '../../../../helpers/mocks/component'; import { waitAndUpdate } from '../../../../helpers/testUtils'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BranchBaselineSettingModal from '../BranchBaselineSettingModal'; import BranchList from '../BranchList'; @@ -35,7 +36,7 @@ const newCodePeriods = [ { projectKey: '', branchKey: 'master', - type: 'NUMBER_OF_DAYS', + type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '27', }, ]; @@ -73,7 +74,9 @@ it('should toggle popup', async () => { expect(nodes).toHaveLength(1); expect(nodes.first().props().branch).toEqual(mockMainBranch()); - wrapper.instance().closeEditModal('master', { type: 'NUMBER_OF_DAYS', value: '23' }); + wrapper + .instance() + .closeEditModal('master', { type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '23' }); expect(wrapper.find('BranchBaselineSettingModal')).toHaveLength(0); expect(wrapper.state().branches.find((b) => b.name === 'master')).toEqual({ @@ -82,7 +85,7 @@ it('should toggle popup', async () => { isMain: true, name: 'master', newCodePeriod: { - type: 'NUMBER_OF_DAYS', + type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '23', }, }); @@ -93,7 +96,7 @@ function shallowRender(props: Partial<BranchList['props']> = {}) { <BranchList branchList={[]} component={mockComponent()} - inheritedSetting={{ type: 'PREVIOUS_VERSION' }} + inheritedSetting={{ type: NewCodePeriodSettingType.PREVIOUS_VERSION }} {...props} /> ); diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchListRow-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchListRow-test.tsx index 66f6593bb16..12f2bb83be1 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchListRow-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/BranchListRow-test.tsx @@ -21,6 +21,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { ActionsDropdownItem } from '../../../../components/controls/ActionsDropdown'; import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import BranchListRow, { BranchListRowProps } from '../BranchListRow'; it('should render correctly', () => { @@ -28,17 +29,23 @@ it('should render correctly', () => { expect( shallowRender({ branch: mockBranch({ name: 'branch-7.3' }), - inheritedSetting: { type: 'REFERENCE_BRANCH', value: 'branch-7.3' }, + inheritedSetting: { type: NewCodePeriodSettingType.REFERENCE_BRANCH, value: 'branch-7.3' }, }) ).toMatchSnapshot('faulty branch'); expect( shallowRender({ - branch: { ...mockBranch(), newCodePeriod: { type: 'NUMBER_OF_DAYS', value: '21' } }, + branch: { + ...mockBranch(), + newCodePeriod: { type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '21' }, + }, }) ).toMatchSnapshot('branch with number of days'); expect( shallowRender({ - branch: { ...mockBranch(), newCodePeriod: { type: 'PREVIOUS_VERSION' } }, + branch: { + ...mockBranch(), + newCodePeriod: { type: NewCodePeriodSettingType.PREVIOUS_VERSION }, + }, }) ).toMatchSnapshot('branch with previous version'); expect( @@ -46,7 +53,7 @@ it('should render correctly', () => { branch: { ...mockBranch(), newCodePeriod: { - type: 'SPECIFIC_ANALYSIS', + type: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, value: 'A85835', effectiveValue: '2018-12-02T13:01:12', }, @@ -55,7 +62,10 @@ it('should render correctly', () => { ).toMatchSnapshot('branch with specific analysis'); expect( shallowRender({ - branch: { ...mockBranch(), newCodePeriod: { type: 'REFERENCE_BRANCH', value: 'master' } }, + branch: { + ...mockBranch(), + newCodePeriod: { type: NewCodePeriodSettingType.REFERENCE_BRANCH, value: 'master' }, + }, }) ).toMatchSnapshot('branch with reference branch'); }); @@ -74,7 +84,10 @@ it('should callback to reset when clicked', () => { const resetToDefault = jest.fn(); const branchName = 'branch-6.5'; const wrapper = shallowRender({ - branch: { ...mockBranch({ name: branchName }), newCodePeriod: { type: 'REFERENCE_BRANCH' } }, + branch: { + ...mockBranch({ name: branchName }), + newCodePeriod: { type: NewCodePeriodSettingType.REFERENCE_BRANCH }, + }, onResetToDefault: resetToDefault, }); diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineApp-test.tsx index aaa7056fe0d..1f368a4060e 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/App-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineApp-test.tsx @@ -28,7 +28,8 @@ import { mockBranch, mockMainBranch, mockPullRequest } from '../../../../helpers import { mockComponent } from '../../../../helpers/mocks/component'; import { mockAppState } from '../../../../helpers/testMocks'; import { mockEvent, waitAndUpdate } from '../../../../helpers/testUtils'; -import { App } from '../App'; +import { NewCodePeriodSettingType } from '../../../../types/types'; +import { ProjectBaselineApp } from '../ProjectBaselineApp'; jest.mock('../../../../api/newCodePeriod', () => ({ getNewCodePeriod: jest.fn().mockResolvedValue({}), @@ -75,12 +76,12 @@ it('should save correctly', async () => { const component = mockComponent(); const wrapper = shallowRender({ component }); await waitAndUpdate(wrapper); - wrapper.setState({ selected: 'NUMBER_OF_DAYS', days: '23' }); + wrapper.setState({ selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, days: '23' }); wrapper.instance().handleSubmit(mockEvent()); await waitAndUpdate(wrapper); expect(setNewCodePeriod).toHaveBeenCalledWith({ project: component.key, - type: 'NUMBER_OF_DAYS', + type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '23', }); expect(wrapper.state('currentSetting')).toEqual(wrapper.state('selected')); @@ -92,7 +93,7 @@ it('should handle errors gracefully', async () => { (resetNewCodePeriod as jest.Mock).mockRejectedValue('error'); const wrapper = shallowRender(); - wrapper.setState({ selected: 'PREVIOUS_VERSION' }); + wrapper.setState({ selected: NewCodePeriodSettingType.PREVIOUS_VERSION }); await waitAndUpdate(wrapper); expect(wrapper.state('loading')).toBe(false); @@ -104,9 +105,9 @@ it('should handle errors gracefully', async () => { expect(wrapper.state('saving')).toBe(false); }); -function shallowRender(props: Partial<App['props']> = {}) { - return shallow<App>( - <App +function shallowRender(props: Partial<ProjectBaselineApp['props']> = {}) { + return shallow<ProjectBaselineApp>( + <ProjectBaselineApp branchLike={mockBranch()} branchLikes={[mockMainBranch()]} appState={mockAppState({ canAdmin: true })} diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineSelector-test.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineSelector-test.tsx index 09b2ecdfe1d..740f830be9c 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineSelector-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/ProjectBaselineSelector-test.tsx @@ -20,6 +20,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { mockBranch, mockMainBranch } from '../../../../helpers/mocks/branch-like'; +import { NewCodePeriodSettingType } from '../../../../types/types'; import ProjectBaselineSelector, { ProjectBaselineSelectorProps } from '../ProjectBaselineSelector'; it('should render correctly', () => { @@ -27,18 +28,21 @@ it('should render correctly', () => { expect( shallowRender({ branchesEnabled: false, - generalSetting: { type: 'NUMBER_OF_DAYS', value: '23' }, + generalSetting: { type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '23' }, }) ).toMatchSnapshot(); expect( - shallowRender({ branchesEnabled: false, generalSetting: { type: 'NUMBER_OF_DAYS', value: '' } }) + shallowRender({ + branchesEnabled: false, + generalSetting: { type: NewCodePeriodSettingType.NUMBER_OF_DAYS, value: '' }, + }) ).toMatchSnapshot(); }); it('should not show save button when unchanged', () => { const wrapper = shallowRender({ - currentSetting: 'PREVIOUS_VERSION', - selected: 'PREVIOUS_VERSION', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, + selected: NewCodePeriodSettingType.PREVIOUS_VERSION, overrideGeneralSetting: true, }); expect(wrapper.find('SubmitButton').parent().hasClass('invisible')).toBe(true); @@ -46,8 +50,8 @@ it('should not show save button when unchanged', () => { it('should show save button when changed', () => { const wrapper = shallowRender({ - currentSetting: 'PREVIOUS_VERSION', - selected: 'NUMBER_OF_DAYS', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, overrideGeneralSetting: true, }); expect(wrapper.find('SubmitButton')).toHaveLength(1); @@ -55,10 +59,10 @@ it('should show save button when changed', () => { it('should show save button when value changed', () => { const wrapper = shallowRender({ - currentSetting: 'NUMBER_OF_DAYS', + currentSetting: NewCodePeriodSettingType.NUMBER_OF_DAYS, currentSettingValue: '23', days: '25', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, overrideGeneralSetting: true, }); expect(wrapper.find('SubmitButton')).toHaveLength(1); @@ -66,10 +70,10 @@ it('should show save button when value changed', () => { it('should disable the save button when saving', () => { const wrapper = shallowRender({ - currentSetting: 'NUMBER_OF_DAYS', + currentSetting: NewCodePeriodSettingType.NUMBER_OF_DAYS, currentSettingValue: '25', saving: true, - selected: 'PREVIOUS_VERSION', + selected: NewCodePeriodSettingType.PREVIOUS_VERSION, overrideGeneralSetting: true, }); @@ -78,9 +82,9 @@ it('should disable the save button when saving', () => { it('should disable the save button when date is invalid', () => { const wrapper = shallowRender({ - currentSetting: 'PREVIOUS_VERSION', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, days: 'hello', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, overrideGeneralSetting: true, }); diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/__snapshots__/App-test.tsx.snap b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/__snapshots__/ProjectBaselineApp-test.tsx.snap index 847b62f98de..847b62f98de 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/__snapshots__/App-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/__snapshots__/ProjectBaselineApp-test.tsx.snap diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/utils-test.ts b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/utils-test.ts index 044e63b9da6..2d53227ef9f 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/utils-test.ts +++ b/server/sonar-web/src/main/js/apps/projectBaseline/components/__tests__/utils-test.ts @@ -17,6 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { NewCodePeriodSettingType } from '../../../../types/types'; import { getSettingValue, validateSetting } from '../../utils'; describe('getSettingValue', () => { @@ -27,19 +28,27 @@ describe('getSettingValue', () => { }; it('should work for Days', () => { - expect(getSettingValue({ ...state, type: 'NUMBER_OF_DAYS' })).toBe(state.days); + expect(getSettingValue({ ...state, type: NewCodePeriodSettingType.NUMBER_OF_DAYS })).toBe( + state.days + ); }); it('should work for Analysis', () => { - expect(getSettingValue({ ...state, type: 'SPECIFIC_ANALYSIS' })).toBe(state.analysis); + expect(getSettingValue({ ...state, type: NewCodePeriodSettingType.SPECIFIC_ANALYSIS })).toBe( + state.analysis + ); }); it('should work for Previous version', () => { - expect(getSettingValue({ ...state, type: 'PREVIOUS_VERSION' })).toBeUndefined(); + expect( + getSettingValue({ ...state, type: NewCodePeriodSettingType.PREVIOUS_VERSION }) + ).toBeUndefined(); }); it('should work for Reference branch', () => { - expect(getSettingValue({ ...state, type: 'REFERENCE_BRANCH' })).toBe(state.referenceBranch); + expect(getSettingValue({ ...state, type: NewCodePeriodSettingType.REFERENCE_BRANCH })).toBe( + state.referenceBranch + ); }); }); @@ -48,68 +57,68 @@ describe('validateSettings', () => { expect(validateSetting({ days: '' })).toEqual({ isChanged: false, isValid: false }); expect( validateSetting({ - currentSetting: 'PREVIOUS_VERSION', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, days: '12', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, }) ).toEqual({ isChanged: true, isValid: true }); expect( validateSetting({ - currentSetting: 'PREVIOUS_VERSION', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, days: 'nope', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, }) ).toEqual({ isChanged: true, isValid: false }); expect( validateSetting({ - currentSetting: 'NUMBER_OF_DAYS', + currentSetting: NewCodePeriodSettingType.NUMBER_OF_DAYS, currentSettingValue: '15', days: '15', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, }) ).toEqual({ isChanged: false, isValid: true }); expect( validateSetting({ - currentSetting: 'NUMBER_OF_DAYS', + currentSetting: NewCodePeriodSettingType.NUMBER_OF_DAYS, currentSettingValue: '15', days: '13', - selected: 'NUMBER_OF_DAYS', + selected: NewCodePeriodSettingType.NUMBER_OF_DAYS, }) ).toEqual({ isChanged: true, isValid: true }); expect( validateSetting({ analysis: 'analysis1', - currentSetting: 'SPECIFIC_ANALYSIS', + currentSetting: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, currentSettingValue: 'analysis1', days: '', - selected: 'SPECIFIC_ANALYSIS', + selected: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, }) ).toEqual({ isChanged: false, isValid: true }); expect( validateSetting({ analysis: 'analysis2', - currentSetting: 'SPECIFIC_ANALYSIS', + currentSetting: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, currentSettingValue: 'analysis1', days: '', - selected: 'SPECIFIC_ANALYSIS', + selected: NewCodePeriodSettingType.SPECIFIC_ANALYSIS, }) ).toEqual({ isChanged: true, isValid: true }); expect( validateSetting({ - currentSetting: 'REFERENCE_BRANCH', + currentSetting: NewCodePeriodSettingType.REFERENCE_BRANCH, currentSettingValue: 'master', days: '', referenceBranch: 'master', - selected: 'REFERENCE_BRANCH', + selected: NewCodePeriodSettingType.REFERENCE_BRANCH, }) ).toEqual({ isChanged: false, isValid: true }); expect( validateSetting({ - currentSetting: 'REFERENCE_BRANCH', + currentSetting: NewCodePeriodSettingType.REFERENCE_BRANCH, currentSettingValue: 'master', days: '', referenceBranch: '', - selected: 'REFERENCE_BRANCH', + selected: NewCodePeriodSettingType.REFERENCE_BRANCH, }) ).toEqual({ isChanged: true, isValid: false }); }); @@ -125,7 +134,7 @@ describe('validateSettings', () => { }); expect( validateSetting({ - currentSetting: 'PREVIOUS_VERSION', + currentSetting: NewCodePeriodSettingType.PREVIOUS_VERSION, days: '', overrideGeneralSetting: false, }) diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/constants.ts b/server/sonar-web/src/main/js/apps/projectBaseline/constants.ts new file mode 100644 index 00000000000..00c2c82e24b --- /dev/null +++ b/server/sonar-web/src/main/js/apps/projectBaseline/constants.ts @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import { NewCodePeriodSettingType } from '../../types/types'; + +export const DEFAULT_GENERAL_SETTING_TYPE: NewCodePeriodSettingType = + NewCodePeriodSettingType.PREVIOUS_VERSION; diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/routes.tsx b/server/sonar-web/src/main/js/apps/projectBaseline/routes.tsx index 5397eae9553..2235fe7aabf 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/routes.tsx +++ b/server/sonar-web/src/main/js/apps/projectBaseline/routes.tsx @@ -19,8 +19,8 @@ */ import React from 'react'; import { Route } from 'react-router-dom'; -import App from './components/App'; +import ProjectBaselineApp from './components/ProjectBaselineApp'; -const routes = () => <Route path="baseline" element={<App />} />; +const routes = () => <Route path="baseline" element={<ProjectBaselineApp />} />; export default routes; diff --git a/server/sonar-web/src/main/js/apps/projectBaseline/utils.ts b/server/sonar-web/src/main/js/apps/projectBaseline/utils.ts index 3f249e2fd77..2ca20b85b1e 100644 --- a/server/sonar-web/src/main/js/apps/projectBaseline/utils.ts +++ b/server/sonar-web/src/main/js/apps/projectBaseline/utils.ts @@ -37,11 +37,11 @@ export function getSettingValue({ type?: NewCodePeriodSettingType; }) { switch (type) { - case 'NUMBER_OF_DAYS': + case NewCodePeriodSettingType.NUMBER_OF_DAYS: return days; - case 'REFERENCE_BRANCH': + case NewCodePeriodSettingType.REFERENCE_BRANCH: return referenceBranch; - case 'SPECIFIC_ANALYSIS': + case NewCodePeriodSettingType.SPECIFIC_ANALYSIS: return analysis; default: return undefined; @@ -74,17 +74,19 @@ export function validateSetting(state: { isChanged = overrideGeneralSetting === false || selected !== currentSetting || - (selected === 'NUMBER_OF_DAYS' && days !== currentSettingValue) || - (selected === 'SPECIFIC_ANALYSIS' && analysis !== currentSettingValue) || - (selected === 'REFERENCE_BRANCH' && referenceBranch !== currentSettingValue); + (selected === NewCodePeriodSettingType.NUMBER_OF_DAYS && days !== currentSettingValue) || + (selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS && + analysis !== currentSettingValue) || + (selected === NewCodePeriodSettingType.REFERENCE_BRANCH && + referenceBranch !== currentSettingValue); } const isValid = overrideGeneralSetting === false || - selected === 'PREVIOUS_VERSION' || - (selected === 'SPECIFIC_ANALYSIS' && analysis.length > 0) || - (selected === 'NUMBER_OF_DAYS' && validateDays(days)) || - (selected === 'REFERENCE_BRANCH' && referenceBranch.length > 0); + selected === NewCodePeriodSettingType.PREVIOUS_VERSION || + (selected === NewCodePeriodSettingType.SPECIFIC_ANALYSIS && analysis.length > 0) || + (selected === NewCodePeriodSettingType.NUMBER_OF_DAYS && validateDays(days)) || + (selected === NewCodePeriodSettingType.REFERENCE_BRANCH && referenceBranch.length > 0); return { isChanged, isValid }; } |