diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2022-03-03 16:17:30 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-04 20:02:47 +0000 |
commit | bc489aa9897b982a6b85aaa30f713336ce3cdfbf (patch) | |
tree | 5760e387bc1080798631861806ad4c41a04d4058 /server/sonar-web | |
parent | b9059604204d2b3045d8050fd4ffb9a0029a600f (diff) | |
download | sonarqube-bc489aa9897b982a6b85aaa30f713336ce3cdfbf.tar.gz sonarqube-bc489aa9897b982a6b85aaa30f713336ce3cdfbf.zip |
[NO JIRA] Treat some Code Smells
Diffstat (limited to 'server/sonar-web')
11 files changed, 119 insertions, 40 deletions
diff --git a/server/sonar-web/src/main/js/app/components/GlobalContainer.tsx b/server/sonar-web/src/main/js/app/components/GlobalContainer.tsx index d00fbc0cf73..52a5eccf943 100644 --- a/server/sonar-web/src/main/js/app/components/GlobalContainer.tsx +++ b/server/sonar-web/src/main/js/app/components/GlobalContainer.tsx @@ -26,8 +26,8 @@ import GlobalFooter from './GlobalFooter'; import GlobalMessagesContainer from './GlobalMessagesContainer'; import IndexationContextProvider from './indexation/IndexationContextProvider'; import IndexationNotification from './indexation/IndexationNotification'; -import LanguageContextProvider from './languages/LanguagesContextProvider'; -import MetricContextProvider from './metrics/MetricsContextProvider'; +import LanguagesContextProvider from './languages/LanguagesContextProvider'; +import MetricsContextProvider from './metrics/MetricsContextProvider'; import GlobalNav from './nav/global/GlobalNav'; import PromotionNotification from './promotion-notification/PromotionNotification'; import StartupModal from './StartupModal'; @@ -52,15 +52,15 @@ export default function GlobalContainer(props: Props) { <div className="page-container"> <Workspace> <IndexationContextProvider> - <LanguageContextProvider> - <MetricContextProvider> + <LanguagesContextProvider> + <MetricsContextProvider> <GlobalNav location={props.location} /> <GlobalMessagesContainer /> <IndexationNotification /> <UpdateNotification dismissable={true} /> {props.children} - </MetricContextProvider> - </LanguageContextProvider> + </MetricsContextProvider> + </LanguagesContextProvider> </IndexationContextProvider> </Workspace> </div> diff --git a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalContainer-test.tsx.snap b/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalContainer-test.tsx.snap index 58f58f0bfd2..cf997f458f2 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalContainer-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalContainer-test.tsx.snap @@ -17,8 +17,8 @@ exports[`should render correctly 1`] = ` > <Workspace> <withAppStateContext(IndexationContextProvider)> - <LanguageContextProvider> - <MetricContextProvider> + <LanguagesContextProvider> + <MetricsContextProvider> <Connect(GlobalNav) location={ Object { @@ -38,8 +38,8 @@ exports[`should render correctly 1`] = ` dismissable={true} /> <ChildComponent /> - </MetricContextProvider> - </LanguageContextProvider> + </MetricsContextProvider> + </LanguagesContextProvider> </withAppStateContext(IndexationContextProvider)> </Workspace> </div> diff --git a/server/sonar-web/src/main/js/app/components/languages/LanguagesContextProvider.tsx b/server/sonar-web/src/main/js/app/components/languages/LanguagesContextProvider.tsx index 9e83ad94454..72b531e6b60 100644 --- a/server/sonar-web/src/main/js/app/components/languages/LanguagesContextProvider.tsx +++ b/server/sonar-web/src/main/js/app/components/languages/LanguagesContextProvider.tsx @@ -27,7 +27,7 @@ interface State { languages: Languages; } -export default class LanguageContextProvider extends React.PureComponent<{}, State> { +export default class LanguagesContextProvider extends React.PureComponent<{}, State> { mounted = false; state: State = { languages: {} diff --git a/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx index feb859ac1d8..a25d44bda93 100644 --- a/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx @@ -21,7 +21,7 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { getLanguages } from '../../../../api/languages'; import { waitAndUpdate } from '../../../../helpers/testUtils'; -import LanguageContextProvider from '../LanguagesContextProvider'; +import LanguagesContextProvider from '../LanguagesContextProvider'; jest.mock('../../../../api/languages', () => ({ getLanguages: jest.fn().mockResolvedValue({}) @@ -38,9 +38,9 @@ it('should call language', async () => { }); function shallowRender() { - return shallow<LanguageContextProvider>( - <LanguageContextProvider> + return shallow<LanguagesContextProvider>( + <LanguagesContextProvider> <div /> - </LanguageContextProvider> + </LanguagesContextProvider> ); } diff --git a/server/sonar-web/src/main/js/app/components/metrics/MetricsContextProvider.tsx b/server/sonar-web/src/main/js/app/components/metrics/MetricsContextProvider.tsx index 8e3e2395b77..0567e7da9f9 100644 --- a/server/sonar-web/src/main/js/app/components/metrics/MetricsContextProvider.tsx +++ b/server/sonar-web/src/main/js/app/components/metrics/MetricsContextProvider.tsx @@ -28,7 +28,7 @@ interface State { metrics: Dict<Metric>; } -export default class MetricContextProvider extends React.PureComponent<{}, State> { +export default class MetricsContextProvider extends React.PureComponent<{}, State> { mounted = false; state: State = { metrics: {} diff --git a/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx index 753d73b2e52..be30d4fa999 100644 --- a/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx @@ -23,7 +23,7 @@ import * as React from 'react'; import { getAllMetrics } from '../../../../api/metrics'; import { mockMetric } from '../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../helpers/testUtils'; -import MetricContextProvider from '../MetricsContextProvider'; +import MetricsContextProvider from '../MetricsContextProvider'; jest.mock('../../../../api/metrics', () => ({ getAllMetrics: jest.fn().mockResolvedValue({}) @@ -40,9 +40,9 @@ it('should call metric', async () => { }); function shallowRender() { - return shallow<MetricContextProvider>( - <MetricContextProvider> + return shallow<MetricsContextProvider>( + <MetricsContextProvider> <div /> - </MetricContextProvider> + </MetricsContextProvider> ); } diff --git a/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.tsx b/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.tsx index 60b16b58b1f..defa1340c6f 100644 --- a/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.tsx +++ b/server/sonar-web/src/main/js/apps/component-measures/components/MeasureContent.tsx @@ -287,13 +287,10 @@ export default class MeasureContent extends React.PureComponent<Props, State> { getDefaultShowBestMeasures() { const { asc, view } = this.props; - if (asc !== undefined && view === 'list') { + if ((asc !== undefined && view === 'list') || view === 'tree') { return true; - } else if (view === 'tree') { - return true; - } else { - return false; } + return false; } renderMeasure() { diff --git a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.tsx b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.tsx index a0220255e26..a41482e9796 100644 --- a/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.tsx +++ b/server/sonar-web/src/main/js/apps/projectActivity/components/ProjectActivityAppContainer.tsx @@ -72,6 +72,9 @@ export interface State { export const PROJECT_ACTIVITY_GRAPH = 'sonar_project_activity.graph'; +const ACTIVITY_PAGE_SIZE_FIRST_BATCH = 100; +const ACTIVITY_PAGE_SIZE = 500; + export default class ProjectActivityAppContainer extends React.PureComponent<Props, State> { mounted = false; @@ -129,8 +132,8 @@ export default class ProjectActivityAppContainer extends React.PureComponent<Pro this.mounted = false; } - addCustomEvent = (analysis: string, name: string, category?: string) => { - return createEvent(analysis, name, category).then(({ analysis, ...event }) => { + addCustomEvent = (analysisKey: string, name: string, category?: string) => { + return createEvent(analysisKey, name, category).then(({ analysis, ...event }) => { if (this.mounted) { this.setState(actions.addCustomEvent(analysis, event)); } @@ -141,8 +144,8 @@ export default class ProjectActivityAppContainer extends React.PureComponent<Pro return this.addCustomEvent(analysis, version, 'VERSION'); }; - changeEvent = (event: string, name: string) => { - return changeEvent(event, name).then(({ analysis, ...event }) => { + changeEvent = (eventKey: string, name: string) => { + return changeEvent(eventKey, name).then(({ analysis, ...event }) => { if (this.mounted) { this.setState(actions.changeEvent(analysis, event)); } @@ -248,7 +251,7 @@ export default class ProjectActivityAppContainer extends React.PureComponent<Pro ProjectActivityStatuses.STATUS_LIVE_MEASURE_COMPUTE ], nextPage, - 500 + ACTIVITY_PAGE_SIZE ).then(result => { if (!prevResult) { return this.loadAllActivities(project, result); @@ -288,7 +291,7 @@ export default class ProjectActivityAppContainer extends React.PureComponent<Pro ProjectActivityStatuses.STATUS_LIVE_MEASURE_COMPUTE ], 1, - 100, + ACTIVITY_PAGE_SIZE_FIRST_BATCH, serializeQuery(query) ), getAllMetrics(), diff --git a/server/sonar-web/src/main/js/apps/projectActivity/components/__tests__/ProjectActivityAppContainer-test.tsx b/server/sonar-web/src/main/js/apps/projectActivity/components/__tests__/ProjectActivityAppContainer-test.tsx index a3d32690e9b..7ebc710a5cb 100644 --- a/server/sonar-web/src/main/js/apps/projectActivity/components/__tests__/ProjectActivityAppContainer-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectActivity/components/__tests__/ProjectActivityAppContainer-test.tsx @@ -19,12 +19,58 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { changeEvent, createEvent } from '../../../../api/projectActivity'; import { mockComponent } from '../../../../helpers/mocks/component'; -import { mockLocation, mockMetric, mockRouter } from '../../../../helpers/testMocks'; +import { + mockAnalysisEvent, + mockLocation, + mockMetric, + mockRouter +} from '../../../../helpers/testMocks'; +import { waitAndUpdate } from '../../../../helpers/testUtils'; import { ComponentQualifier } from '../../../../types/component'; import { MetricKey } from '../../../../types/metrics'; import ProjectActivityAppContainer from '../ProjectActivityAppContainer'; +jest.mock('../../../../helpers/dates', () => ({ + parseDate: jest.fn(date => `PARSED:${date}`) +})); + +jest.mock('../../../../api/time-machine', () => { + const { mockPaging } = jest.requireActual('../../../../helpers/testMocks'); + return { + getAllTimeMachineData: jest.fn().mockResolvedValue({ + measures: [ + { + metric: 'bugs', + history: [{ date: '2022-01-01', value: '10' }] + } + ], + paging: mockPaging({ total: 1 }) + }) + }; +}); + +jest.mock('../../../../api/metrics', () => { + const { mockMetric } = jest.requireActual('../../../../helpers/testMocks'); + return { + getAllMetrics: jest.fn().mockResolvedValue([mockMetric()]) + }; +}); + +jest.mock('../../../../api/projectActivity', () => { + const { mockAnalysis, mockPaging } = jest.requireActual('../../../../helpers/testMocks'); + return { + ...jest.requireActual('../../../../api/projectActivity'), + createEvent: jest.fn(), + changeEvent: jest.fn(), + getProjectActivity: jest.fn().mockResolvedValue({ + analyses: [mockAnalysis({ key: 'foo' })], + paging: mockPaging({ total: 1 }) + }) + }; +}); + it('should render correctly', () => { expect(shallowRender()).toMatchSnapshot(); }); @@ -47,6 +93,33 @@ it('should filter metric correctly', () => { expect(metrics).toHaveLength(1); }); +it('should correctly create and update custom events', async () => { + const analysisKey = 'foo'; + const name = 'bar'; + const newName = 'baz'; + const event = mockAnalysisEvent({ name }); + (createEvent as jest.Mock).mockResolvedValueOnce({ analysis: analysisKey, ...event }); + (changeEvent as jest.Mock).mockResolvedValueOnce({ + analysis: analysisKey, + ...event, + name: newName + }); + + const wrapper = shallowRender(); + await waitAndUpdate(wrapper); + const instance = wrapper.instance(); + + instance.addCustomEvent(analysisKey, name); + expect(createEvent).toHaveBeenCalledWith(analysisKey, name, undefined); + await waitAndUpdate(wrapper); + expect(wrapper.state().analyses[0].events[0]).toEqual(event); + + instance.changeEvent(event.key, newName); + expect(changeEvent).toHaveBeenCalledWith(event.key, newName); + await waitAndUpdate(wrapper); + expect(wrapper.state().analyses[0].events[0]).toEqual({ ...event, name: newName }); +}); + function shallowRender(props: Partial<ProjectActivityAppContainer['props']> = {}) { return shallow<ProjectActivityAppContainer>( <ProjectActivityAppContainer diff --git a/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts b/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts index 9661ddc8a29..62a21954818 100644 --- a/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts +++ b/server/sonar-web/src/main/js/apps/projects/__tests__/utils-test.ts @@ -138,3 +138,11 @@ describe('fetchProjects', () => { }); }); }); + +describe('defineMetrics', () => { + it('returns the correct list of metrics', () => { + expect(utils.defineMetrics({ view: 'leak' })).toBe(utils.LEAK_METRICS); + expect(utils.defineMetrics({ view: 'overall' })).toBe(utils.METRICS); + expect(utils.defineMetrics({})).toBe(utils.METRICS); + }); +}); diff --git a/server/sonar-web/src/main/js/apps/projects/utils.ts b/server/sonar-web/src/main/js/apps/projects/utils.ts index 58916044832..b0142611e10 100644 --- a/server/sonar-web/src/main/js/apps/projects/utils.ts +++ b/server/sonar-web/src/main/js/apps/projects/utils.ts @@ -86,7 +86,7 @@ export const VIEWS = [ const PAGE_SIZE = 50; -const METRICS = [ +export const METRICS = [ MetricKey.alert_status, MetricKey.bugs, MetricKey.reliability_rating, @@ -103,7 +103,7 @@ const METRICS = [ MetricKey.projects ]; -const LEAK_METRICS = [ +export const LEAK_METRICS = [ MetricKey.alert_status, MetricKey.new_bugs, MetricKey.new_reliability_rating, @@ -190,13 +190,11 @@ export function fetchProjects(query: Query, isFavorite: boolean, pageIndex = 1) }); } -function defineMetrics(query: Query): string[] { - switch (query.view) { - case 'leak': - return LEAK_METRICS; - default: - return METRICS; +export function defineMetrics(query: Query): string[] { + if (query.view === 'leak') { + return LEAK_METRICS; } + return METRICS; } function defineFacets(query: Query): string[] { |