From: Wouter Admiraal Date: Thu, 22 Aug 2019 13:15:54 +0000 (+0200) Subject: SONAR-12333 Cleanup profile inheritance typings, improve coverage X-Git-Tag: 8.0~196 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c69b7b9b257125bc3b24431a7a93f32224c60d71;p=sonarqube.git SONAR-12333 Cleanup profile inheritance typings, improve coverage --- diff --git a/server/sonar-web/src/main/js/api/quality-profiles.ts b/server/sonar-web/src/main/js/api/quality-profiles.ts index b1a5459d4fc..e9f79660f51 100644 --- a/server/sonar-web/src/main/js/api/quality-profiles.ts +++ b/server/sonar-web/src/main/js/api/quality-profiles.ts @@ -101,7 +101,13 @@ export function getProfileProjects( return getJSON('/api/qualityprofiles/projects', data).catch(throwGlobalError); } -export function getProfileInheritance(profileKey: string): Promise { +export function getProfileInheritance( + profileKey: string +): Promise<{ + ancestors: T.ProfileInheritanceDetails[]; + children: T.ProfileInheritanceDetails[]; + profile: T.ProfileInheritanceDetails; +}> { return getJSON('/api/qualityprofiles/inheritance', { profileKey }).catch(throwGlobalError); } diff --git a/server/sonar-web/src/main/js/app/types.d.ts b/server/sonar-web/src/main/js/app/types.d.ts index 09accb78d73..8bfd5c55adc 100644 --- a/server/sonar-web/src/main/js/app/types.d.ts +++ b/server/sonar-web/src/main/js/app/types.d.ts @@ -614,6 +614,14 @@ declare namespace T { }>; } + export interface ProfileInheritanceDetails { + activeRuleCount: number; + isBuiltIn: boolean; + key: string; + name: string; + overridingRuleCount?: number; + } + export interface ProjectLink { id: string; name?: string; diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritance.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritance.tsx index 745583c979b..98c4879c012 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritance.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritance.tsx @@ -33,21 +33,12 @@ interface Props { updateProfiles: () => Promise; } -interface ProfileInheritanceDetails { - activeRuleCount: number; - isBuiltIn: boolean; - key: string; - language: string; - name: string; - overridingRuleCount?: number; -} - interface State { - ancestors?: Array; - children?: Array; + ancestors?: T.ProfileInheritanceDetails[]; + children?: T.ProfileInheritanceDetails[]; formOpen: boolean; loading: boolean; - profile?: ProfileInheritanceDetails; + profile?: T.ProfileInheritanceDetails; } export default class ProfileInheritance extends React.PureComponent { @@ -78,9 +69,11 @@ export default class ProfileInheritance extends React.PureComponent { if (this.mounted) { const { ancestors, children } = r; + ancestors.reverse(); + this.setState({ children, - ancestors: ancestors.reverse(), + ancestors, profile: r.profile, loading: false }); @@ -167,7 +160,6 @@ export default class ProfileInheritance extends React.PureComponent )} diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritanceBox.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritanceBox.tsx index 29c8e6ccc3e..59e2428633a 100644 --- a/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritanceBox.tsx +++ b/server/sonar-web/src/main/js/apps/quality-profiles/details/ProfileInheritanceBox.tsx @@ -30,20 +30,22 @@ interface Props { extendsBuiltIn?: boolean; language: string; organization: string | null; - profile: { - activeRuleCount: number; - isBuiltIn: boolean; - key: string; - language: string; - name: string; - overridingRuleCount?: number; - }; - type: string; + profile: T.ProfileInheritanceDetails; + type?: string; } -export default function ProfileInheritanceBox({ type, displayLink = true, ...props }: Props) { - const { profile, className, extendsBuiltIn } = props; - const offset = 25 * props.depth; +export default function ProfileInheritanceBox(props: Props) { + const { + className, + depth, + extendsBuiltIn, + language, + organization, + profile, + displayLink = true, + type = 'current' + } = props; + const offset = 25 * depth; return ( @@ -52,9 +54,9 @@ export default function ProfileInheritanceBox({ type, displayLink = true, ...pro {displayLink ? ( + organization={organization}> {profile.name} ) : ( diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/ProfileInheritanceBox-test.tsx b/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/ProfileInheritanceBox-test.tsx new file mode 100644 index 00000000000..fc03dde5c7a --- /dev/null +++ b/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/ProfileInheritanceBox-test.tsx @@ -0,0 +1,50 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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 { shallow } from 'enzyme'; +import * as React from 'react'; +import { mockQualityProfileInheritance } from '../../../../helpers/testMocks'; +import ProfileInheritanceBox from '../ProfileInheritanceBox'; + +it('should render correctly', () => { + expect(shallowRender()).toMatchSnapshot(); + expect( + shallowRender({ + depth: 3, + displayLink: true, + profile: mockQualityProfileInheritance({ isBuiltIn: true }) + }) + ).toMatchSnapshot(); + expect(shallowRender({ extendsBuiltIn: true })).toMatchSnapshot(); + expect( + shallowRender({ profile: mockQualityProfileInheritance({ overridingRuleCount: 10 }) }) + ).toMatchSnapshot(); +}); + +function shallowRender(props = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/__snapshots__/ProfileInheritanceBox-test.tsx.snap b/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/__snapshots__/ProfileInheritanceBox-test.tsx.snap new file mode 100644 index 00000000000..934da5e4adb --- /dev/null +++ b/server/sonar-web/src/main/js/apps/quality-profiles/details/__tests__/__snapshots__/ProfileInheritanceBox-test.tsx.snap @@ -0,0 +1,140 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + + +
+ + Foo + +
+ + + quality_profile.x_active_rules.4 + + +

+ quality_profiles.x_overridden_rules.0 +

+ + +`; + +exports[`should render correctly 2`] = ` + + +
+ + Foo + + +
+ + + quality_profile.x_active_rules.4 + + +

+ quality_profiles.x_overridden_rules.0 +

+ + +`; + +exports[`should render correctly 3`] = ` + + +
+ + Foo + + +
+ + + quality_profile.x_active_rules.4 + + +

+ quality_profiles.x_overridden_rules.0 +

+ + +`; + +exports[`should render correctly 4`] = ` + + +
+ + Foo + +
+ + + quality_profile.x_active_rules.4 + + +

+ quality_profiles.x_overridden_rules.10 +

+ + +`; diff --git a/server/sonar-web/src/main/js/helpers/testMocks.ts b/server/sonar-web/src/main/js/helpers/testMocks.ts index 03b6da9b100..95542914134 100644 --- a/server/sonar-web/src/main/js/helpers/testMocks.ts +++ b/server/sonar-web/src/main/js/helpers/testMocks.ts @@ -552,6 +552,19 @@ export function mockQualityProfile(overrides: Partial = {}): Profile { }; } +export function mockQualityProfileInheritance( + overrides: Partial = {} +): T.ProfileInheritanceDetails { + return { + activeRuleCount: 4, + isBuiltIn: false, + key: 'foo', + name: 'Foo', + overridingRuleCount: 0, + ...overrides + }; +} + export function mockQualityGateProjectStatus( overrides: Partial = {} ): T.QualityGateProjectStatus {