From 4380a1d1594cb112f3d3e17b99f4a801c3d2fda8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 11 Apr 2019 11:18:49 +0200 Subject: SONARCLOUD-520 change location of homepage data file --- .../src/main/js/apps/about/sonarcloud/Home.tsx | 38 ++++++++++++++++++---- .../apps/about/sonarcloud/__tests__/Home-test.tsx | 30 ++++++++++++++++- .../__tests__/__snapshots__/Home-test.tsx.snap | 37 +++++++++++++++++++++ .../src/main/js/apps/about/sonarcloud/utils.ts | 10 +++--- 4 files changed, 104 insertions(+), 11 deletions(-) (limited to 'server/sonar-web/src') diff --git a/server/sonar-web/src/main/js/apps/about/sonarcloud/Home.tsx b/server/sonar-web/src/main/js/apps/about/sonarcloud/Home.tsx index fd78f1cc580..6f003a2dffb 100644 --- a/server/sonar-web/src/main/js/apps/about/sonarcloud/Home.tsx +++ b/server/sonar-web/src/main/js/apps/about/sonarcloud/Home.tsx @@ -19,6 +19,7 @@ */ import * as React from 'react'; import Helmet from 'react-helmet'; +import { connect } from 'react-redux'; import { FixedNavBar, TopNavBar } from './components/NavBars'; import FeaturedProjects from './components/FeaturedProjects'; import Footer from './components/Footer'; @@ -26,32 +27,48 @@ import { Languages } from './components/Languages'; import LoginButtons from './components/LoginButtons'; import Statistics from './components/Statistics'; import { requestHomepageData, HomepageData, FeaturedProject } from './utils'; +import { getGlobalSettingValue, Store } from '../../../store/rootReducer'; import { addWhitePageClass, removeWhitePageClass } from '../../../helpers/pages'; import { getBaseUrl } from '../../../helpers/urls'; import './new_style.css'; +interface Props { + homePageDataUrl?: string; +} + interface State { data?: HomepageData; } -export default class Home extends React.PureComponent<{}, State> { +export class Home extends React.PureComponent { + mounted = false; state: State = {}; componentDidMount() { + this.mounted = true; addWhitePageClass(); this.fetchData(); } componentWillUnmount() { removeWhitePageClass(); + this.mounted = false; } fetchData = () => { - requestHomepageData() - .then(data => this.setState({ data })) - .catch(() => { - /* Fail silently */ - }); + const { homePageDataUrl } = this.props; + if (homePageDataUrl) { + requestHomepageData(homePageDataUrl).then( + data => { + if (this.mounted) { + this.setState({ data }); + } + }, + () => { + /* Fail silently */ + } + ); + } }; render() { @@ -84,6 +101,15 @@ export default class Home extends React.PureComponent<{}, State> { } } +const mapStateToProps = (state: Store) => { + const homePageDataUrl = getGlobalSettingValue(state, 'sonar.homepage.url'); + return { + homePageDataUrl: homePageDataUrl && homePageDataUrl.value + }; +}; + +export default connect(mapStateToProps)(Home); + function PageBackgroundHeader() { return (
diff --git a/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/Home-test.tsx b/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/Home-test.tsx index ce5282fe00e..bb3374ffe14 100644 --- a/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/Home-test.tsx +++ b/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/Home-test.tsx @@ -21,6 +21,8 @@ import * as React from 'react'; import { shallow } from 'enzyme'; import Home from '../Home'; import { waitAndUpdate } from '../../../../helpers/testUtils'; +import { mockStore } from '../../../../helpers/testMocks'; +import { requestHomepageData } from '../utils'; jest.mock('../utils', () => { const utils = require.requireActual('../utils'); @@ -53,8 +55,13 @@ jest.mock('../utils', () => { return utils; }); +beforeEach(() => { + jest.clearAllMocks(); +}); + it('should render', async () => { - const wrapper = shallow(); + const wrapper = shallowRender('https://static.sonarcloud.io/homepage.json'); + expect(requestHomepageData).toBeCalled(); await waitAndUpdate(wrapper); expect(wrapper).toMatchSnapshot(); expect(wrapper.find('PageBackgroundHeader').dive()).toMatchSnapshot(); @@ -65,3 +72,24 @@ it('should render', async () => { expect(wrapper.find('Stats').dive()).toMatchSnapshot(); expect(wrapper.find('Projects').dive()).toMatchSnapshot(); }); + +it('should not render real Stats and Projects', () => { + const wrapper = shallowRender(undefined); + expect(requestHomepageData).not.toBeCalled(); + expect(wrapper.find('Stats').dive()).toMatchSnapshot(); + expect(wrapper.find('Projects').dive()).toMatchSnapshot(); +}); + +function shallowRender(homePageDataUrl: string | undefined) { + return shallow(, { + context: { + store: mockStore({ + settingsApp: { + values: { + global: { 'sonar.homepage.url': { key: 'sonar.homepage.url', value: homePageDataUrl } } + } + } + }) + } + }).dive(); +} diff --git a/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/__snapshots__/Home-test.tsx.snap b/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/__snapshots__/Home-test.tsx.snap index 46988fe232d..0527dbd3e26 100644 --- a/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/__snapshots__/Home-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/about/sonarcloud/__tests__/__snapshots__/Home-test.tsx.snap @@ -1,5 +1,42 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should not render real Stats and Projects 1`] = ` +
+
+

+ Over 3,000 Projects +
+ Continuously Analyzed +

+
+
+`; + +exports[`should not render real Stats and Projects 2`] = ` +
+
+
+ Come join the fun, it’s entirely free for open-source projects! +
+
+ +
+
+
+`; + exports[`should render 1`] = `
{ - return fetch('/json/homepage.json').then(response => response.json()); -} - export const LANGUAGES = [ { name: 'Java', file: 'java.svg', width: 65 }, { name: 'JavaScript', file: 'js.svg', width: 60 }, @@ -74,3 +72,7 @@ export const LANGUAGES = [ { name: 'XML', file: 'xml.svg', width: 67 }, { name: 'COBOL', file: 'cobol.svg', width: 65 } ]; + +export function requestHomepageData(url: string): Promise { + return getJSON(url); +} -- cgit v1.2.3