From f37eb96fa18b2d5e080fc0187d3307125d3c31f2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Fri, 12 Oct 2018 17:03:11 +0200 Subject: [PATCH] SONAR-11289 Display plus menu only for logged in users on SonarCloud --- .../app/components/nav/global/GlobalNav.tsx | 19 +++++++----- .../components/nav/global/GlobalNavPlus.tsx | 4 +-- .../nav/global/__tests__/GlobalNav-test.tsx | 22 +++++++------- .../global/__tests__/GlobalNavPlus-test.tsx | 13 ++++---- .../__snapshots__/GlobalNav-test.tsx.snap | 30 ------------------- .../src/main/js/helpers/testUtils.ts | 3 +- 6 files changed, 34 insertions(+), 57 deletions(-) diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNav.tsx b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNav.tsx index 513ee90f6d9..0eda00d3b95 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNav.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNav.tsx @@ -27,7 +27,7 @@ import GlobalNavUserContainer from './GlobalNavUserContainer'; import Search from '../../search/Search'; import EmbedDocsPopupHelper from '../../embed-docs-modal/EmbedDocsPopupHelper'; import * as theme from '../../../theme'; -import { CurrentUser, AppState } from '../../../types'; +import { CurrentUser, AppState, isLoggedIn } from '../../../types'; import NavBar from '../../../../components/nav/NavBar'; import { lazyLoad } from '../../../../components/lazyLoad'; import { getCurrentUser, getAppState, Store } from '../../../../store/rootReducer'; @@ -35,7 +35,7 @@ import { SuggestionLink } from '../../embed-docs-modal/SuggestionsProvider'; import { isSonarCloud } from '../../../../helpers/system'; import './GlobalNav.css'; -const GlobalNavPlus = lazyLoad(() => import('./GlobalNavPlus')); +const GlobalNavPlus = lazyLoad(() => import('./GlobalNavPlus'), 'GlobalNavPlus'); interface StateProps { appState: Pick; @@ -53,6 +53,7 @@ export class GlobalNav extends React.PureComponent { static contextTypes = { openProjectOnboarding: PropTypes.func }; render() { + const { appState, currentUser } = this.props; return ( {isSonarCloud() ? : } @@ -62,12 +63,14 @@ export class GlobalNav extends React.PureComponent {
    {isSonarCloud() && } - - + + {isLoggedIn(currentUser) && ( + + )}
diff --git a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx index 9da8d2ac45d..99d30116560 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/GlobalNavPlus.tsx @@ -22,7 +22,7 @@ import { Link, withRouter, WithRouterProps } from 'react-router'; import CreateFormShim from '../../../../apps/portfolio/components/CreateFormShim'; import Dropdown from '../../../../components/controls/Dropdown'; import PlusIcon from '../../../../components/icons-components/PlusIcon'; -import { AppState, hasGlobalPermission, CurrentUser } from '../../../types'; +import { AppState, hasGlobalPermission, LoggedInUser } from '../../../types'; import { getPortfolioAdminUrl, getPortfolioUrl } from '../../../../helpers/urls'; import { getExtensionStart } from '../../extensions/utils'; import { isSonarCloud } from '../../../../helpers/system'; @@ -31,7 +31,7 @@ import { getComponentNavigation } from '../../../../api/nav'; interface Props { appState: Pick; - currentUser: CurrentUser; + currentUser: LoggedInUser; openProjectOnboarding: () => void; } diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNav-test.tsx b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNav-test.tsx index 3410e31581f..99130935150 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNav-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNav-test.tsx @@ -30,7 +30,6 @@ const appState: GlobalNav['props']['appState'] = { organizationsEnabled: false, qualifiers: [] }; -const currentUser = { isLoggedIn: false }; const location = { pathname: '' }; it('should render for SonarQube', () => { @@ -43,14 +42,15 @@ it('should render for SonarCloud', () => { function runTest(mockedIsSonarCloud: boolean) { (isSonarCloud as jest.Mock).mockImplementation(() => mockedIsSonarCloud); - expect( - shallow( - - ) - ).toMatchSnapshot(); + const wrapper = shallow( + + ); + expect(wrapper).toMatchSnapshot(); + wrapper.setProps({ currentUser: { isLoggedIn: true } }); + expect(wrapper.find('GlobalNavPlus').exists()).toBe(true); } diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx index b4a77bf8216..37f845fd4cc 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/GlobalNavPlus-test.tsx @@ -22,6 +22,7 @@ import { shallow, ShallowWrapper } from 'enzyme'; import { GlobalNavPlus } from '../GlobalNavPlus'; import { isSonarCloud } from '../../../../../helpers/system'; import { click, mockRouter } from '../../../../../helpers/testUtils'; +import { LoggedInUser } from '../../../../types'; jest.mock('../../../../../helpers/system', () => ({ isSonarCloud: jest.fn() @@ -71,14 +72,16 @@ it('should display create application', () => { function getWrapper(props = {}, globalPermissions?: string[]) { return shallow( + // @ts-ignore avoid passing everything from WithRouterProps diff --git a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNav-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNav-test.tsx.snap index f785691d515..62c2757d57b 100644 --- a/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNav-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/global/__tests__/__snapshots__/GlobalNav-test.tsx.snap @@ -56,21 +56,6 @@ exports[`should render for SonarCloud 1`] = ` } } /> - -