From 35625522ce5ef18b877f2828e18fff9ab5218a5c Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Wed, 27 Mar 2019 10:18:27 +0100 Subject: [PATCH] Fix potential bug in MetaQualityProfiles, add tests --- .../overview/meta/MetaQualityProfiles.tsx | 11 +- .../__tests__/MetaQualityProfiles-test.tsx | 60 ++++++++ .../MetaQualityProfiles-test.tsx.snap | 128 ++++++++++++++++++ 3 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaQualityProfiles-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/overview/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap diff --git a/server/sonar-web/src/main/js/apps/overview/meta/MetaQualityProfiles.tsx b/server/sonar-web/src/main/js/apps/overview/meta/MetaQualityProfiles.tsx index f5e666d5b87..ff3850cb1f4 100644 --- a/server/sonar-web/src/main/js/apps/overview/meta/MetaQualityProfiles.tsx +++ b/server/sonar-web/src/main/js/apps/overview/meta/MetaQualityProfiles.tsx @@ -41,7 +41,7 @@ interface State { deprecatedByKey: T.Dict; } -class MetaQualityProfiles extends React.PureComponent { +export class MetaQualityProfiles extends React.PureComponent { mounted = false; state: State = { deprecatedByKey: {} }; @@ -55,15 +55,16 @@ class MetaQualityProfiles extends React.PureComponent !p.deleted) - .map(profile => this.loadDeprecatedRulesForProfile(profile.key)); + const existingProfiles = this.props.profiles.filter(p => !p.deleted); + const requests = existingProfiles.map(profile => + this.loadDeprecatedRulesForProfile(profile.key) + ); Promise.all(requests).then( responses => { if (this.mounted) { const deprecatedByKey: T.Dict = {}; responses.forEach((count, i) => { - const profileKey = this.props.profiles[i].key; + const profileKey = existingProfiles[i].key; deprecatedByKey[profileKey] = count; }); this.setState({ deprecatedByKey }); diff --git a/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaQualityProfiles-test.tsx b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaQualityProfiles-test.tsx new file mode 100644 index 00000000000..034aa685747 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/MetaQualityProfiles-test.tsx @@ -0,0 +1,60 @@ +/* + * 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 * as React from 'react'; +import { shallow } from 'enzyme'; +import { MetaQualityProfiles } from '../MetaQualityProfiles'; +import { mockLanguage, mockQualityProfile } from '../../../../helpers/testMocks'; +import { searchRules } from '../../../../api/rules'; +import { waitAndUpdate } from '../../../../helpers/testUtils'; + +jest.mock('../../../../api/rules', () => { + return { + searchRules: jest.fn().mockResolvedValue({ + total: 10 + }) + }; +}); + +it('should render correctly', async () => { + const wrapper = shallowRender(); + expect(wrapper).toMatchSnapshot(); + + await waitAndUpdate(wrapper); + expect(wrapper).toMatchSnapshot(); + expect(wrapper.find('.overview-deprecated-rules').exists()).toBe(true); + expect(wrapper.find('.overview-deleted-profile').exists()).toBe(true); + expect(searchRules).toBeCalled(); +}); + +function shallowRender(props: Partial = {}) { + return shallow( + + ); +} diff --git a/server/sonar-web/src/main/js/apps/overview/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap new file mode 100644 index 00000000000..aeef836890a --- /dev/null +++ b/server/sonar-web/src/main/js/apps/overview/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap @@ -0,0 +1,128 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render correctly 1`] = ` + +

+ overview.quality_profiles +

+
    + +
  • +
    + + (js) + + name +
    +
  • +
    +
  • +
    + + (CSS) + + + name + +
    +
  • +
+
+`; + +exports[`should render correctly 2`] = ` + +

+ overview.quality_profiles +

+
    + +
  • +
    + + (js) + + name +
    +
  • +
    + +
  • +
    + + (CSS) + + + name + +
    +
  • +
    +
+
+`; -- 2.39.5