diff --git a/public/app/core/components/sidemenu/SignIn.test.tsx b/public/app/core/components/sidemenu/SignIn.test.tsx index 4aa50de5fc4..907b801f301 100644 --- a/public/app/core/components/sidemenu/SignIn.test.tsx +++ b/public/app/core/components/sidemenu/SignIn.test.tsx @@ -2,13 +2,9 @@ import React from 'react'; import { shallow } from 'enzyme'; import { SignIn } from './SignIn'; -jest.mock('../../config', () => ({ - appUrl: 'http://localhost:3000/', -})); - describe('Render', () => { it('should render component', () => { - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper).toMatchSnapshot(); }); diff --git a/public/app/core/components/sidemenu/SignIn.tsx b/public/app/core/components/sidemenu/SignIn.tsx index 817f0433554..5400c653417 100644 --- a/public/app/core/components/sidemenu/SignIn.tsx +++ b/public/app/core/components/sidemenu/SignIn.tsx @@ -1,18 +1,13 @@ import React, { FC } from 'react'; -import config from 'app/core/config'; + import { connectWithStore } from 'app/core/utils/connectWithReduxStore'; import { StoreState } from 'app/types'; import { Icon } from '@grafana/ui'; - -const getForcedLoginUrl = (url: string) => { - const urlObj = new URL(url, config.appUrl); - let params = urlObj.searchParams; - params.set('forceLogin', 'true'); - return urlObj.toString(); -}; +import { getForcedLoginUrl } from './utils'; export const SignIn: FC = ({ url }) => { const forcedLoginUrl = getForcedLoginUrl(url); + return (
diff --git a/public/app/core/components/sidemenu/__snapshots__/SignIn.test.tsx.snap b/public/app/core/components/sidemenu/__snapshots__/SignIn.test.tsx.snap index 38a5428f75e..69fc93a90c7 100644 --- a/public/app/core/components/sidemenu/__snapshots__/SignIn.test.tsx.snap +++ b/public/app/core/components/sidemenu/__snapshots__/SignIn.test.tsx.snap @@ -6,7 +6,7 @@ exports[`Render should render component 1`] = ` >
    { + it.each` + appSubUrl | url | expected + ${''} | ${'/whatever?a=1&b=2'} | ${'/whatever?a=1&b=2&forceLogin=true'} + ${'/grafana'} | ${'/whatever?a=1&b=2'} | ${'/grafana/whatever?a=1&b=2&forceLogin=true'} + ${'/grafana/test'} | ${'/whatever?a=1&b=2'} | ${'/grafana/test/whatever?a=1&b=2&forceLogin=true'} + ${'/grafana'} | ${''} | ${'/grafana?forceLogin=true'} + ${'/grafana'} | ${'/whatever'} | ${'/grafana/whatever?forceLogin=true'} + ${'/grafana'} | ${'/whatever/'} | ${'/grafana/whatever/?forceLogin=true'} + `( + "when appUrl set to '$appUrl' and appSubUrl set to '$appSubUrl' then result should be '$expected'", + ({ appSubUrl, url, expected }) => { + updateConfig({ + appSubUrl, + }); + + const result = getForcedLoginUrl(url); + + expect(result).toBe(expected); + } + ); +}); diff --git a/public/app/core/components/sidemenu/utils.ts b/public/app/core/components/sidemenu/utils.ts new file mode 100644 index 00000000000..1ef6a823e26 --- /dev/null +++ b/public/app/core/components/sidemenu/utils.ts @@ -0,0 +1,8 @@ +import { getConfig } from 'app/core/config'; + +export const getForcedLoginUrl = (url: string) => { + const queryParams = new URLSearchParams(url.split('?')[1]); + queryParams.append('forceLogin', 'true'); + + return `${getConfig().appSubUrl}${url.split('?')[0]}?${queryParams.toString()}`; +};