mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Navigation: Sign in button now works correctly when served under a sub path (#62504)
make sure getUrlForPartial always includes the basePath + unit tests
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
import { Location } from 'history';
|
||||
|
||||
import { GrafanaConfig } from '../types';
|
||||
|
||||
import { locationUtil } from './location';
|
||||
|
||||
describe('locationUtil', () => {
|
||||
@@ -29,9 +33,9 @@ describe('locationUtil', () => {
|
||||
describe('when appSubUrl configured', () => {
|
||||
beforeEach(() => {
|
||||
locationUtil.initialize({
|
||||
config: { appSubUrl: '/subUrl' } as any,
|
||||
getVariablesUrlParams: (() => {}) as any,
|
||||
getTimeRangeForUrl: (() => {}) as any,
|
||||
config: { appSubUrl: '/subUrl' } as GrafanaConfig,
|
||||
getVariablesUrlParams: jest.fn(),
|
||||
getTimeRangeForUrl: jest.fn(),
|
||||
});
|
||||
});
|
||||
test('relative url', () => {
|
||||
@@ -65,9 +69,9 @@ describe('locationUtil', () => {
|
||||
describe('when appSubUrl not configured', () => {
|
||||
beforeEach(() => {
|
||||
locationUtil.initialize({
|
||||
config: {} as any,
|
||||
getVariablesUrlParams: (() => {}) as any,
|
||||
getTimeRangeForUrl: (() => {}) as any,
|
||||
config: {} as GrafanaConfig,
|
||||
getVariablesUrlParams: jest.fn(),
|
||||
getTimeRangeForUrl: jest.fn(),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -115,12 +119,102 @@ describe('locationUtil', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUrlForPartial', () => {
|
||||
const mockLocation: Location = {
|
||||
hash: '',
|
||||
pathname: '/',
|
||||
search: '',
|
||||
state: {},
|
||||
};
|
||||
describe('when appSubUrl is not configured', () => {
|
||||
beforeEach(() => {
|
||||
locationUtil.initialize({
|
||||
config: {
|
||||
appSubUrl: '',
|
||||
} as GrafanaConfig,
|
||||
getVariablesUrlParams: jest.fn(),
|
||||
getTimeRangeForUrl: jest.fn(),
|
||||
});
|
||||
});
|
||||
|
||||
it('can add params', () => {
|
||||
expect(locationUtil.getUrlForPartial(mockLocation, { forceLogin: 'true' })).toEqual('/?forceLogin=true');
|
||||
});
|
||||
|
||||
it('can remove params using undefined', () => {
|
||||
expect(
|
||||
locationUtil.getUrlForPartial(
|
||||
{
|
||||
...mockLocation,
|
||||
search: '?a=1',
|
||||
},
|
||||
{ a: undefined }
|
||||
)
|
||||
).toEqual('/');
|
||||
});
|
||||
|
||||
it('can remove params using null', () => {
|
||||
expect(
|
||||
locationUtil.getUrlForPartial(
|
||||
{
|
||||
...mockLocation,
|
||||
search: '?a=1',
|
||||
},
|
||||
{ a: null }
|
||||
)
|
||||
).toEqual('/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when appSubUrl is configured', () => {
|
||||
beforeEach(() => {
|
||||
locationUtil.initialize({
|
||||
config: {
|
||||
appSubUrl: '/subpath',
|
||||
} as GrafanaConfig,
|
||||
getVariablesUrlParams: jest.fn(),
|
||||
getTimeRangeForUrl: jest.fn(),
|
||||
});
|
||||
});
|
||||
|
||||
it('can add params', () => {
|
||||
expect(locationUtil.getUrlForPartial(mockLocation, { forceLogin: 'true' })).toEqual(
|
||||
'/subpath/?forceLogin=true'
|
||||
);
|
||||
});
|
||||
|
||||
it('can remove params using undefined', () => {
|
||||
expect(
|
||||
locationUtil.getUrlForPartial(
|
||||
{
|
||||
...mockLocation,
|
||||
search: '?a=1',
|
||||
},
|
||||
{ a: undefined }
|
||||
)
|
||||
).toEqual('/subpath/');
|
||||
});
|
||||
|
||||
it('can remove params using null', () => {
|
||||
expect(
|
||||
locationUtil.getUrlForPartial(
|
||||
{
|
||||
...mockLocation,
|
||||
search: '?a=1',
|
||||
},
|
||||
{ a: null }
|
||||
)
|
||||
).toEqual('/subpath/');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateSearchParams', () => {
|
||||
beforeEach(() => {
|
||||
locationUtil.initialize({
|
||||
config: {} as any,
|
||||
getVariablesUrlParams: (() => {}) as any,
|
||||
getTimeRangeForUrl: (() => {}) as any,
|
||||
config: {} as GrafanaConfig,
|
||||
getVariablesUrlParams: jest.fn(),
|
||||
getTimeRangeForUrl: jest.fn(),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ const getUrlForPartial = (location: Location<any>, searchParamsToUpdate: Record<
|
||||
searchParams[key] = searchParamsToUpdate[key];
|
||||
}
|
||||
}
|
||||
return urlUtil.renderUrl(location.pathname, searchParams);
|
||||
return assureBaseUrl(urlUtil.renderUrl(location.pathname, searchParams));
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user