From d07ba86a6bbc81ecdb9ec7220d663f6797f869ef Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Fri, 23 Sep 2022 10:01:48 +0200 Subject: [PATCH] SONAR-17362 Add getValue and refactor use of getValues for settings --- .../src/main/js/api/mocks/AuditLogsServiceMock.ts | 8 ++++---- .../main/js/api/mocks/AuthenticationServiceMock.ts | 4 +--- server/sonar-web/src/main/js/api/settings.ts | 13 +++++++++++-- .../main/js/app/components/SystemAnnouncement.tsx | 2 +- .../js/apps/audit-logs/components/AuditApp.tsx | 6 +++--- .../components/__tests__/AuditApp-test.tsx | 12 ++++++------ .../sonar-web/src/main/js/apps/marketplace/App.tsx | 8 +++----- .../js/apps/marketplace/__tests__/App-test.tsx | 14 +++++++------- .../components/LifetimeInformation.tsx | 6 +++--- .../__tests__/LifetimeInformation-test.tsx | 8 ++++---- .../projectsManagement/ProjectManagementApp.tsx | 8 ++++---- .../__tests__/ProjectManagementApp-it.tsx | 2 +- .../__tests__/ProjectManagementApp-test.tsx | 6 +++--- .../components/CategoryDefinitionsList.tsx | 2 +- .../js/apps/settings/components/Definition.tsx | 8 +++----- .../__tests__/CategoryDefinitionsList-test.tsx | 6 +++--- .../components/__tests__/Definition-test.tsx | 12 ++++++------ .../authentication/SamlAuthentication.tsx | 6 +++--- .../js/components/tutorials/TutorialSelection.tsx | 6 +++--- .../tutorials/__tests__/TutorialSelection-test.tsx | 12 ++++++------ 20 files changed, 76 insertions(+), 73 deletions(-) diff --git a/server/sonar-web/src/main/js/api/mocks/AuditLogsServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/AuditLogsServiceMock.ts index 10f6f36fdc0..8d52440034d 100644 --- a/server/sonar-web/src/main/js/api/mocks/AuditLogsServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/AuditLogsServiceMock.ts @@ -20,15 +20,15 @@ import { cloneDeep } from 'lodash'; import { HousekeepingPolicy } from '../../apps/audit-logs/utils'; import { SettingValue } from '../../types/settings'; -import { getValues } from '../settings'; +import { getValue } from '../settings'; export default class AuditLogsServiceMock { - settingValue: SettingValue[]; - defaultValues: SettingValue[] = [{ key: 'test', value: HousekeepingPolicy.Weekly }]; + settingValue: SettingValue; + defaultValues: SettingValue = { key: 'test', value: HousekeepingPolicy.Weekly }; constructor() { this.settingValue = cloneDeep(this.defaultValues); - (getValues as jest.Mock).mockImplementation(this.getValuesHandler); + (getValue as jest.Mock).mockImplementation(this.getValuesHandler); } getValuesHandler = () => { diff --git a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts index 17dda673074..c5a5287e6c2 100644 --- a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts @@ -41,9 +41,7 @@ export default class AuthenticationServiceMock { getValuesHandler = (data: { keys: string; component?: string } & BranchParameters) => { if (data.keys) { - return Promise.resolve( - this.settingValues.filter(set => data.keys.split(',').includes(set.key)) - ); + return Promise.resolve(this.settingValues.filter(set => data.keys.includes(set.key))); } return Promise.resolve(this.settingValues); }; diff --git a/server/sonar-web/src/main/js/api/settings.ts b/server/sonar-web/src/main/js/api/settings.ts index c00cc81078b..6fd866a2a30 100644 --- a/server/sonar-web/src/main/js/api/settings.ts +++ b/server/sonar-web/src/main/js/api/settings.ts @@ -36,10 +36,19 @@ export function getDefinitions(component?: string): Promise { + return getValues({ keys: [data.key], component: data.component }).then(([result]) => result); +} + export function getValues( - data: { keys: string; component?: string } & BranchParameters + data: { keys: string[]; component?: string } & BranchParameters ): Promise { - return getJSON('/api/settings/values', data).then((r: SettingValueResponse) => [ + return getJSON('/api/settings/values', { + keys: data.keys.join(','), + component: data.component + }).then((r: SettingValueResponse) => [ ...r.settings, ...r.setSecuredSettings.map(key => ({ key })) ]); diff --git a/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx b/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx index 65e630c76dc..dd0abf833ea 100644 --- a/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx +++ b/server/sonar-web/src/main/js/app/components/SystemAnnouncement.tsx @@ -44,7 +44,7 @@ export default class SystemAnnouncement extends React.PureComponent<{}, State> { getSettings = async () => { const values: SettingValue[] = await getValues({ - keys: [GlobalSettingKeys.DisplaySystemMessage, GlobalSettingKeys.SystemMessage].join(',') + keys: [GlobalSettingKeys.DisplaySystemMessage, GlobalSettingKeys.SystemMessage] }); const settings = keyBy(values, 'key'); diff --git a/server/sonar-web/src/main/js/apps/audit-logs/components/AuditApp.tsx b/server/sonar-web/src/main/js/apps/audit-logs/components/AuditApp.tsx index 30790038dc8..7582b963c92 100644 --- a/server/sonar-web/src/main/js/apps/audit-logs/components/AuditApp.tsx +++ b/server/sonar-web/src/main/js/apps/audit-logs/components/AuditApp.tsx @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; -import { getValues } from '../../../api/settings'; +import { getValue } from '../../../api/settings'; import withAdminPagesOutletContext from '../../../app/components/admin/withAdminPagesOutletContext'; import { AdminPageExtension } from '../../../types/extension'; import { SettingsKey } from '../../../types/settings'; @@ -62,11 +62,11 @@ export class AuditApp extends React.PureComponent { } fetchHouseKeepingPolicy = async () => { - const results = await getValues({ keys: SettingsKey.AuditHouseKeeping }); + const result = await getValue({ key: SettingsKey.AuditHouseKeeping }); this.setState({ housekeepingPolicy: - (results[0]?.value as HousekeepingPolicy | undefined) ?? HousekeepingPolicy.Monthly + (result?.value as HousekeepingPolicy | undefined) ?? HousekeepingPolicy.Monthly }); }; diff --git a/server/sonar-web/src/main/js/apps/audit-logs/components/__tests__/AuditApp-test.tsx b/server/sonar-web/src/main/js/apps/audit-logs/components/__tests__/AuditApp-test.tsx index 5d519095633..21ffca857b0 100644 --- a/server/sonar-web/src/main/js/apps/audit-logs/components/__tests__/AuditApp-test.tsx +++ b/server/sonar-web/src/main/js/apps/audit-logs/components/__tests__/AuditApp-test.tsx @@ -20,7 +20,7 @@ import { subDays } from 'date-fns'; import { shallow } from 'enzyme'; import * as React from 'react'; -import { getValues } from '../../../../api/settings'; +import { getValue } from '../../../../api/settings'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import { AdminPageExtension } from '../../../../types/extension'; import { HousekeepingPolicy, RangeOption } from '../../utils'; @@ -28,7 +28,7 @@ import { AuditApp } from '../AuditApp'; import AuditAppRenderer from '../AuditAppRenderer'; jest.mock('../../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([]) + getValue: jest.fn().mockResolvedValue({}) })); beforeEach(() => { @@ -44,11 +44,11 @@ it('should do nothing if governance is not available', async () => { await waitAndUpdate(wrapper); expect(wrapper.type()).toBeNull(); - expect(getValues).not.toBeCalled(); + expect(getValue).not.toBeCalled(); }); it('should handle housekeeping policy', async () => { - (getValues as jest.Mock).mockResolvedValueOnce([{ value: HousekeepingPolicy.Weekly }]); + (getValue as jest.Mock).mockResolvedValueOnce({ value: HousekeepingPolicy.Weekly }); const wrapper = shallowRender(); @@ -92,12 +92,12 @@ it('should handle update to admin pages', async () => { await waitAndUpdate(wrapper); expect(wrapper.type()).toBeNull(); - expect(getValues).not.toBeCalled(); + expect(getValue).not.toBeCalled(); wrapper.setProps({ adminPages: [{ key: AdminPageExtension.GovernanceConsole, name: 'name' }] }); await waitAndUpdate(wrapper); - expect(getValues).toBeCalled(); + expect(getValue).toBeCalled(); }); function shallowRender(props: Partial = {}) { diff --git a/server/sonar-web/src/main/js/apps/marketplace/App.tsx b/server/sonar-web/src/main/js/apps/marketplace/App.tsx index 2ce22d60b0a..60d03b5b190 100644 --- a/server/sonar-web/src/main/js/apps/marketplace/App.tsx +++ b/server/sonar-web/src/main/js/apps/marketplace/App.tsx @@ -27,7 +27,7 @@ import { getInstalledPluginsWithUpdates, getPluginUpdates } from '../../api/plugins'; -import { getValues, setSimpleSettingValue } from '../../api/settings'; +import { getValue, setSimpleSettingValue } from '../../api/settings'; import Link from '../../components/common/Link'; import Suggestions from '../../components/embed-docs-modal/Suggestions'; import { Location, Router, withRouter } from '../../components/hoc/withRouter'; @@ -111,14 +111,12 @@ export class App extends React.PureComponent { }; fetchRiskConsent = async () => { - const result = await getValues({ keys: SettingsKey.PluginRiskConsent }); + const consent = await getValue({ key: SettingsKey.PluginRiskConsent }); - if (!result || result.length < 1) { + if (consent === undefined) { return; } - const [consent] = result; - this.setState({ riskConsent: consent.value as RiskConsent | undefined }); }; diff --git a/server/sonar-web/src/main/js/apps/marketplace/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/marketplace/__tests__/App-test.tsx index eb6876198a8..3f70f9d3676 100644 --- a/server/sonar-web/src/main/js/apps/marketplace/__tests__/App-test.tsx +++ b/server/sonar-web/src/main/js/apps/marketplace/__tests__/App-test.tsx @@ -25,7 +25,7 @@ import { getInstalledPluginsWithUpdates, getPluginUpdates } from '../../../api/plugins'; -import { getValues, setSimpleSettingValue } from '../../../api/settings'; +import { getValue, setSimpleSettingValue } from '../../../api/settings'; import { mockLocation, mockRouter } from '../../../helpers/testMocks'; import { waitAndUpdate } from '../../../helpers/testUtils'; import { EditionKey } from '../../../types/editions'; @@ -45,7 +45,7 @@ jest.mock('../../../api/plugins', () => { }); jest.mock('../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([]), + getValue: jest.fn().mockResolvedValue({}), setSimpleSettingValue: jest.fn().mockResolvedValue(true) })); @@ -65,21 +65,21 @@ it('should render correctly', async () => { }); it('should handle accepting the risk', async () => { - (getValues as jest.Mock) - .mockResolvedValueOnce([{ value: RiskConsent.NotAccepted }]) - .mockResolvedValueOnce([{ value: RiskConsent.Accepted }]); + (getValue as jest.Mock) + .mockResolvedValueOnce({ value: RiskConsent.NotAccepted }) + .mockResolvedValueOnce({ value: RiskConsent.Accepted }); const wrapper = shallowRender(); await waitAndUpdate(wrapper); - expect(getValues).toBeCalledWith({ keys: SettingsKey.PluginRiskConsent }); + expect(getValue).toBeCalledWith({ key: SettingsKey.PluginRiskConsent }); wrapper.instance().acknowledgeRisk(); await new Promise(setImmediate); expect(setSimpleSettingValue).toBeCalled(); - expect(getValues).toBeCalledWith({ keys: SettingsKey.PluginRiskConsent }); + expect(getValue).toBeCalledWith({ key: SettingsKey.PluginRiskConsent }); expect(wrapper.state().riskConsent).toBe(RiskConsent.Accepted); }); diff --git a/server/sonar-web/src/main/js/apps/projectBranches/components/LifetimeInformation.tsx b/server/sonar-web/src/main/js/apps/projectBranches/components/LifetimeInformation.tsx index aabaf35c730..a6a0de260a4 100644 --- a/server/sonar-web/src/main/js/apps/projectBranches/components/LifetimeInformation.tsx +++ b/server/sonar-web/src/main/js/apps/projectBranches/components/LifetimeInformation.tsx @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; -import { getValues } from '../../../api/settings'; +import { getValue } from '../../../api/settings'; import withAppStateContext from '../../../app/components/app-state/withAppStateContext'; import { AppState } from '../../../types/appstate'; import { SettingsKey } from '../../../types/settings'; @@ -47,12 +47,12 @@ export class LifetimeInformation extends React.PureComponent { } fetchBranchAndPullRequestLifetimeSetting() { - getValues({ keys: SettingsKey.DaysBeforeDeletingInactiveBranchesAndPRs }).then( + getValue({ key: SettingsKey.DaysBeforeDeletingInactiveBranchesAndPRs }).then( settings => { if (this.mounted) { this.setState({ loading: false, - branchAndPullRequestLifeTimeInDays: settings.length > 0 ? settings[0].value : undefined + branchAndPullRequestLifeTimeInDays: settings?.value }); } }, diff --git a/server/sonar-web/src/main/js/apps/projectBranches/components/__tests__/LifetimeInformation-test.tsx b/server/sonar-web/src/main/js/apps/projectBranches/components/__tests__/LifetimeInformation-test.tsx index f34d9e24983..17d4bd8d3ad 100644 --- a/server/sonar-web/src/main/js/apps/projectBranches/components/__tests__/LifetimeInformation-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectBranches/components/__tests__/LifetimeInformation-test.tsx @@ -19,14 +19,14 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; -import { getValues } from '../../../../api/settings'; +import { getValue } from '../../../../api/settings'; import { mockAppState } from '../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import { SettingsKey } from '../../../../types/settings'; import { LifetimeInformation } from '../LifetimeInformation'; jest.mock('../../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([{ value: '45' }]) + getValue: jest.fn().mockResolvedValue({ value: '45' }) })); it('should render correctly', async () => { @@ -35,8 +35,8 @@ it('should render correctly', async () => { await waitAndUpdate(wrapper); - expect(getValues).toHaveBeenCalledWith({ - keys: SettingsKey.DaysBeforeDeletingInactiveBranchesAndPRs + expect(getValue).toHaveBeenCalledWith({ + key: SettingsKey.DaysBeforeDeletingInactiveBranchesAndPRs }); expect(wrapper).toMatchSnapshot('after_fetching_data'); }); diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/ProjectManagementApp.tsx b/server/sonar-web/src/main/js/apps/projectsManagement/ProjectManagementApp.tsx index 020448c3f73..c333b036058 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/ProjectManagementApp.tsx +++ b/server/sonar-web/src/main/js/apps/projectsManagement/ProjectManagementApp.tsx @@ -22,7 +22,7 @@ import * as React from 'react'; import { Helmet } from 'react-helmet-async'; import { getComponents, Project } from '../../api/components'; import { changeProjectDefaultVisibility } from '../../api/permissions'; -import { getValues } from '../../api/settings'; +import { getValue } from '../../api/settings'; import withCurrentUserContext from '../../app/components/current-user/withCurrentUserContext'; import ListFooter from '../../components/controls/ListFooter'; import Suggestions from '../../components/embed-docs-modal/Suggestions'; @@ -90,10 +90,10 @@ export class ProjectManagementApp extends React.PureComponent { } fetchDefaultProjectVisibility = async () => { - const results = await getValues({ keys: SettingsKey.DefaultProjectVisibility }); + const results = await getValue({ key: SettingsKey.DefaultProjectVisibility }); - if (this.mounted && results.length > 0 && results[0].value) { - this.setState({ defaultProjectVisibility: results[0].value as Visibility }); + if (this.mounted && results?.value !== undefined) { + this.setState({ defaultProjectVisibility: results.value as Visibility }); } }; diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-it.tsx b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-it.tsx index 7c7aa6f02bd..0c39eeebfc6 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-it.tsx +++ b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-it.tsx @@ -36,7 +36,7 @@ jest.mock('../../../api/components', () => ({ })); jest.mock('../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([{ value: 'public' }]) + getValue: jest.fn().mockResolvedValue({ value: 'public' }) })); const components = mockComponents(11); diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-test.tsx b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-test.tsx index 086e4b53644..acde1180943 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectManagementApp-test.tsx @@ -21,7 +21,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { getComponents } from '../../../api/components'; import { changeProjectDefaultVisibility } from '../../../api/permissions'; -import { getValues } from '../../../api/settings'; +import { getValue } from '../../../api/settings'; import { mockLoggedInUser } from '../../../helpers/testMocks'; import { waitAndUpdate } from '../../../helpers/testUtils'; import { ProjectManagementApp, Props } from '../ProjectManagementApp'; @@ -41,7 +41,7 @@ jest.mock('../../../api/permissions', () => ({ })); jest.mock('../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([{ value: 'public' }]) + getValue: jest.fn().mockResolvedValue({ value: 'public' }) })); const defaultSearchParameters = { @@ -58,7 +58,7 @@ it('fetches all projects on mount', async () => { const wrapper = shallowRender(); await waitAndUpdate(wrapper); expect(getComponents).lastCalledWith({ ...defaultSearchParameters, qualifiers: 'TRK' }); - expect(getValues).toBeCalled(); + expect(getValue).toBeCalled(); expect(wrapper.state().defaultProjectVisibility).toBe('public'); }); diff --git a/server/sonar-web/src/main/js/apps/settings/components/CategoryDefinitionsList.tsx b/server/sonar-web/src/main/js/apps/settings/components/CategoryDefinitionsList.tsx index 683fd3404c0..107183e40b7 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/CategoryDefinitionsList.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/CategoryDefinitionsList.tsx @@ -67,7 +67,7 @@ export default class CategoryDefinitionsList extends React.PureComponent definition.category.toLowerCase() === category.toLowerCase() ); - const keys = categoryDefinitions.map(definition => definition.key).join(','); + const keys = categoryDefinitions.map(definition => definition.key); const values: SettingValue[] = await getValues({ keys, diff --git a/server/sonar-web/src/main/js/apps/settings/components/Definition.tsx b/server/sonar-web/src/main/js/apps/settings/components/Definition.tsx index 34e1dcc04bb..1bf9c91506d 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/Definition.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/Definition.tsx @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; -import { getValues, resetSettingValue, setSettingValue } from '../../../api/settings'; +import { getValue, resetSettingValue, setSettingValue } from '../../../api/settings'; import { translate, translateWithParameters } from '../../../helpers/l10n'; import { parseError } from '../../../helpers/request'; import { ExtendedSettingDefinition, SettingType, SettingValue } from '../../../types/settings'; @@ -78,8 +78,7 @@ export default class Definition extends React.PureComponent { try { await resetSettingValue({ keys: definition.key, component: component?.key }); - const result = await getValues({ keys: definition.key, component: component?.key }); - const settingValue = result[0]; + const settingValue = await getValue({ key: definition.key, component: component?.key }); this.setState({ changedValue: undefined, @@ -164,8 +163,7 @@ export default class Definition extends React.PureComponent { try { await setSettingValue(definition, changedValue, component?.key); - const result = await getValues({ keys: definition.key, component: component?.key }); - const settingValue = result[0]; + const settingValue = await getValue({ key: definition.key, component: component?.key }); this.setState({ changedValue: undefined, diff --git a/server/sonar-web/src/main/js/apps/settings/components/__tests__/CategoryDefinitionsList-test.tsx b/server/sonar-web/src/main/js/apps/settings/components/__tests__/CategoryDefinitionsList-test.tsx index 1a2c7b5660b..105a5569df4 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/__tests__/CategoryDefinitionsList-test.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/__tests__/CategoryDefinitionsList-test.tsx @@ -45,7 +45,7 @@ it('should load settings values', async () => { await waitAndUpdate(wrapper); - expect(getValues).toBeCalledWith({ keys: 'yes,yesagain', component: undefined }); + expect(getValues).toBeCalledWith({ keys: ['yes', 'yesagain'], component: undefined }); expect(wrapper.state().settings).toEqual([ { definition: definitions[0], settingValue: settings[0] }, @@ -63,13 +63,13 @@ it('should reload on category change', async () => { await waitAndUpdate(wrapper); - expect(getValues).toBeCalledWith({ keys: 'yes,yesagain', component: 'comp-key' }); + expect(getValues).toBeCalledWith({ keys: ['yes', 'yesagain'], component: 'comp-key' }); wrapper.setProps({ category: 'other' }); await waitAndUpdate(wrapper); - expect(getValues).toBeCalledWith({ keys: 'nope', component: 'comp-key' }); + expect(getValues).toBeCalledWith({ keys: ['nope'], component: 'comp-key' }); }); function shallowRender(props: Partial = {}) { diff --git a/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx b/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx index f877ba5dfb4..65af2b65543 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/__tests__/Definition-test.tsx @@ -19,14 +19,14 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; -import { getValues, resetSettingValue, setSettingValue } from '../../../../api/settings'; +import { getValue, resetSettingValue, setSettingValue } from '../../../../api/settings'; import { mockDefinition, mockSettingValue } from '../../../../helpers/mocks/settings'; import { waitAndUpdate } from '../../../../helpers/testUtils'; import { SettingType } from '../../../../types/settings'; import Definition from '../Definition'; jest.mock('../../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([]), + getValue: jest.fn().mockResolvedValue({}), resetSettingValue: jest.fn().mockResolvedValue(undefined), setSettingValue: jest.fn().mockResolvedValue(undefined) })); @@ -120,7 +120,7 @@ describe('handleSave', () => { it('should save and update setting value', async () => { const settingValue = mockSettingValue(); - (getValues as jest.Mock).mockResolvedValueOnce([settingValue]); + (getValue as jest.Mock).mockResolvedValueOnce(settingValue); const definition = mockDefinition(); const wrapper = shallowRender({ definition }); @@ -133,7 +133,7 @@ describe('handleSave', () => { await waitAndUpdate(wrapper); expect(setSettingValue).toBeCalledWith(definition, 'new value', undefined); - expect(getValues).toBeCalledWith({ keys: definition.key, component: undefined }); + expect(getValue).toBeCalledWith({ key: definition.key, component: undefined }); expect(wrapper.state().changedValue).toBeUndefined(); expect(wrapper.state().loading).toBe(false); expect(wrapper.state().success).toBe(true); @@ -146,7 +146,7 @@ describe('handleSave', () => { it('should reset and update setting value', async () => { const settingValue = mockSettingValue(); - (getValues as jest.Mock).mockResolvedValueOnce([settingValue]); + (getValue as jest.Mock).mockResolvedValueOnce(settingValue); const definition = mockDefinition(); const wrapper = shallowRender({ definition }); @@ -157,7 +157,7 @@ it('should reset and update setting value', async () => { await waitAndUpdate(wrapper); expect(resetSettingValue).toBeCalledWith({ keys: definition.key, component: undefined }); - expect(getValues).toBeCalledWith({ keys: definition.key, component: undefined }); + expect(getValue).toBeCalledWith({ key: definition.key, component: undefined }); expect(wrapper.state().changedValue).toBeUndefined(); expect(wrapper.state().loading).toBe(false); expect(wrapper.state().success).toBe(true); diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthentication.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthentication.tsx index f6070383c10..1196ed4309c 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthentication.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/SamlAuthentication.tsx @@ -86,7 +86,7 @@ class SamlAuthentication extends React.PureComponent< componentDidMount() { const { definitions } = this.props; - const keys = definitions.map(definition => definition.key).join(','); + const keys = definitions.map(definition => definition.key); // Added setTimeout to make sure the component gets updated before scrolling setTimeout(() => { if (location.hash) { @@ -134,7 +134,7 @@ class SamlAuthentication extends React.PureComponent< }); }; - async loadSettingValues(keys: string) { + async loadSettingValues(keys: string[]) { const { settingValue, securedFieldsSubmitted } = this.state; const values = await getValues({ keys @@ -212,7 +212,7 @@ class SamlAuthentication extends React.PureComponent< } this.setState({ success: dirtyFields.length !== dataWithError.length }); }); - await this.loadSettingValues(dirtyFields.join(',')); + await this.loadSettingValues(dirtyFields); this.setState({ submitting: false, dirtyFields: [] }); }; diff --git a/server/sonar-web/src/main/js/components/tutorials/TutorialSelection.tsx b/server/sonar-web/src/main/js/components/tutorials/TutorialSelection.tsx index ae972acfd0e..9888924b8e9 100644 --- a/server/sonar-web/src/main/js/components/tutorials/TutorialSelection.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/TutorialSelection.tsx @@ -20,7 +20,7 @@ import * as React from 'react'; import { getAlmSettingsNoCatch } from '../../api/alm-settings'; import { getScannableProjects } from '../../api/components'; -import { getValues } from '../../api/settings'; +import { getValue } from '../../api/settings'; import { getHostUrl } from '../../helpers/urls'; import { hasGlobalPermission } from '../../helpers/users'; import { AlmSettingsInstance, ProjectAlmBindingResponse } from '../../types/alm-settings'; @@ -102,8 +102,8 @@ export class TutorialSelection extends React.PureComponent { }; fetchBaseUrl = async () => { - const settings = await getValues({ keys: SettingsKey.ServerBaseUrl }).catch(() => undefined); - const baseUrl = settings && settings.find(s => s.key === SettingsKey.ServerBaseUrl)?.value; + const setting = await getValue({ key: SettingsKey.ServerBaseUrl }).catch(() => undefined); + const baseUrl = setting?.value; if (baseUrl && baseUrl.length > 0 && this.mounted) { this.setState({ baseUrl }); } diff --git a/server/sonar-web/src/main/js/components/tutorials/__tests__/TutorialSelection-test.tsx b/server/sonar-web/src/main/js/components/tutorials/__tests__/TutorialSelection-test.tsx index 5cbf3a11a69..7dd92e43fd8 100644 --- a/server/sonar-web/src/main/js/components/tutorials/__tests__/TutorialSelection-test.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/__tests__/TutorialSelection-test.tsx @@ -21,7 +21,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { getAlmSettingsNoCatch } from '../../../api/alm-settings'; import { getScannableProjects } from '../../../api/components'; -import { getValues } from '../../../api/settings'; +import { getValue } from '../../../api/settings'; import { mockAlmSettingsInstance, mockProjectBitbucketBindingResponse @@ -44,7 +44,7 @@ jest.mock('../../../api/alm-settings', () => ({ })); jest.mock('../../../api/settings', () => ({ - getValues: jest.fn().mockResolvedValue([]) + getValue: jest.fn().mockResolvedValue({}) })); jest.mock('../../../api/components', () => ({ @@ -90,14 +90,14 @@ it('should handle selection', () => { }); it('should fetch the correct baseUrl', async () => { - (getValues as jest.Mock) - .mockResolvedValueOnce([{ key: SettingsKey.ServerBaseUrl, value: '' }]) - .mockResolvedValueOnce([{ key: SettingsKey.ServerBaseUrl, value: 'http://sq.example.com' }]) + (getValue as jest.Mock) + .mockResolvedValueOnce({ key: SettingsKey.ServerBaseUrl, value: '' }) + .mockResolvedValueOnce({ key: SettingsKey.ServerBaseUrl, value: 'http://sq.example.com' }) .mockRejectedValueOnce(null); let wrapper = shallowRender(); - expect(getValues).toBeCalled(); + expect(getValue).toBeCalled(); expect(getHostUrl).toBeCalled(); // No baseURL, fallback to the URL in the browser. -- 2.39.5