diff options
author | Philippe Perrin <philippe.perrin@sonarsource.com> | 2022-01-28 17:39:50 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-03-29 20:03:37 +0000 |
commit | b345bbc09eb8e0bd04951e1db88f56666f5f4c6a (patch) | |
tree | d61378aa709f81a1c6a9729b6c792e404700dfcf /server/sonar-web/src/main/js/app/utils | |
parent | c1ea1e9df27db899c07948ce50e6aa9cceab348a (diff) | |
download | sonarqube-b345bbc09eb8e0bd04951e1db88f56666f5f4c6a.tar.gz sonarqube-b345bbc09eb8e0bd04951e1db88f56666f5f4c6a.zip |
SONAR-15938 Improve code sharing with the license extension
Diffstat (limited to 'server/sonar-web/src/main/js/app/utils')
-rw-r--r-- | server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts | 86 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/app/utils/exportModulesAsGlobals.ts (renamed from server/sonar-web/src/main/js/app/utils/throwGlobalError.ts) | 46 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/app/utils/startReactApp.tsx | 18 |
3 files changed, 30 insertions, 120 deletions
diff --git a/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts b/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts deleted file mode 100644 index 13c5f9f85b4..00000000000 --- a/server/sonar-web/src/main/js/app/utils/__tests__/throwGlobalError-test.ts +++ /dev/null @@ -1,86 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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 getStore from '../getStore'; -import throwGlobalError from '../throwGlobalError'; - -beforeAll(() => { - jest.useFakeTimers(); -}); - -afterAll(() => { - jest.runOnlyPendingTimers(); - jest.useRealTimers(); -}); - -it('should put the error message in the store', async () => { - const response = new Response(); - response.json = jest.fn().mockResolvedValue({ errors: [{ msg: 'error 1' }] }); - - // We need to catch because throwGlobalError rethrows after displaying the message - await throwGlobalError(response) - .then(() => fail('Should throw')) - .catch(() => {}); - - expect(getStore().getState().globalMessages[0]).toMatchObject({ - level: 'ERROR', - message: 'error 1' - }); -}); - -it('should put a default error messsage in the store', async () => { - const response = new Response(); - response.json = jest.fn().mockResolvedValue({}); - - // We need to catch because throwGlobalError rethrows after displaying the message - await throwGlobalError(response) - .then(() => fail('Should throw')) - .catch(() => {}); - - expect(getStore().getState().globalMessages[0]).toMatchObject({ - level: 'ERROR', - message: 'default_error_message' - }); -}); - -it('should handle weird response types', () => { - const response = { weird: 'response type' }; - - return throwGlobalError(response) - .then(() => fail('Should throw')) - .catch(error => { - expect(error).toBe(response); - }); -}); - -it('should unwrap response if necessary', async () => { - const response = new Response(); - response.json = jest.fn().mockResolvedValue({}); - - /* eslint-disable-next-line no-console */ - console.warn = jest.fn(); - - // We need to catch because throwGlobalError rethrows after displaying the message - await throwGlobalError({ response }) - .then(() => fail('Should throw')) - .catch(() => {}); - - /* eslint-disable-next-line no-console */ - expect(console.warn).toHaveBeenCalled(); -}); diff --git a/server/sonar-web/src/main/js/app/utils/throwGlobalError.ts b/server/sonar-web/src/main/js/app/utils/exportModulesAsGlobals.ts index dbb9ee45771..f9351d9ef26 100644 --- a/server/sonar-web/src/main/js/app/utils/throwGlobalError.ts +++ b/server/sonar-web/src/main/js/app/utils/exportModulesAsGlobals.ts @@ -17,29 +17,29 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { parseError } from '../../helpers/request'; -import { addGlobalErrorMessage } from '../../store/globalMessages'; -import getStore from './getStore'; -export default function throwGlobalError(param: Response | any): Promise<Response | any> { - const store = getStore(); +import * as EmotionReact from '@emotion/react'; +import EmotionStyled from '@emotion/styled'; +import * as DateFns from 'date-fns'; +import Lodash from 'lodash'; +import React from 'react'; +import * as ReactDom from 'react-dom'; +import * as ReactIntl from 'react-intl'; +import ReactModal from 'react-modal'; +import * as ReactRouter from 'react-router'; - if (param.response instanceof Response) { - /* eslint-disable-next-line no-console */ - console.warn('DEPRECATED: response should not be wrapped, pass it directly.'); - param = param.response; - } - - if (param instanceof Response) { - return parseError(param) - .then( - message => { - store.dispatch(addGlobalErrorMessage(message)); - }, - () => {} - ) - .then(() => Promise.reject(param)); - } - - return Promise.reject(param); +/* + * Expose dependencies to extensions + */ +export default function exportModulesAsGlobals() { + const w = (window as unknown) as any; + w.EmotionReact = EmotionReact; + w.EmotionStyled = EmotionStyled; + w.DateFns = DateFns; + w.Lodash = Lodash; + w.React = React; + w.ReactDOM = ReactDom; + w.ReactIntl = ReactIntl; + w.ReactModal = ReactModal; + w.ReactRouter = ReactRouter; } diff --git a/server/sonar-web/src/main/js/app/utils/startReactApp.tsx b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx index 0c8dc2ab1d5..fc17431e3fa 100644 --- a/server/sonar-web/src/main/js/app/utils/startReactApp.tsx +++ b/server/sonar-web/src/main/js/app/utils/startReactApp.tsx @@ -21,7 +21,7 @@ import { Location } from 'history'; import { pick } from 'lodash'; import * as React from 'react'; -import ReactDom, { render } from 'react-dom'; +import { render } from 'react-dom'; import { HelmetProvider } from 'react-helmet-async'; import { IntlProvider } from 'react-intl'; import { Provider } from 'react-redux'; @@ -69,16 +69,9 @@ import GlobalContainer from '../components/GlobalContainer'; import { PageContext } from '../components/indexation/PageUnavailableDueToIndexation'; import MigrationContainer from '../components/MigrationContainer'; import NonAdminPagesContainer from '../components/NonAdminPagesContainer'; +import exportModulesAsGlobals from './exportModulesAsGlobals'; import getStore from './getStore'; -/* - * Expose dependencies to extensions - */ -function attachToGlobal() { - window.React = React; - window.ReactDOM = ReactDom; -} - function handleUpdate(this: { state: { location: Location } }) { const { action } = this.state.location; @@ -202,7 +195,10 @@ function renderComponentRoutes() { if (query.types === 'SECURITY_HOTSPOT') { replace({ pathname: '/security_hotspots', - query: { ...pick(query, ['id', 'branch', 'pullRequest']), assignedToMe: false } + query: { + ...pick(query, ['id', 'branch', 'pullRequest']), + assignedToMe: false + } }); } else { query.types = query.types @@ -282,7 +278,7 @@ function renderAdminRoutes() { } export default function startReactApp(lang: string, appState: AppState, currentUser?: CurrentUser) { - attachToGlobal(); + exportModulesAsGlobals(); const el = document.getElementById('content'); |