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:
Ashley Harrison 2023-01-31 09:27:40 +00:00 committed by GitHub
parent 439dd062da
commit fd2641a542
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 21 deletions

View File

@ -719,17 +719,6 @@ exports[`better eslint`] = {
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"]
],
"packages/grafana-data/src/utils/location.test.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],
[0, 0, 0, "Unexpected any. Specify a different type.", "2"],
[0, 0, 0, "Unexpected any. Specify a different type.", "3"],
[0, 0, 0, "Unexpected any. Specify a different type.", "4"],
[0, 0, 0, "Unexpected any. Specify a different type.", "5"],
[0, 0, 0, "Unexpected any. Specify a different type.", "6"],
[0, 0, 0, "Unexpected any. Specify a different type.", "7"],
[0, 0, 0, "Unexpected any. Specify a different type.", "8"]
],
"packages/grafana-data/src/utils/location.ts:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Unexpected any. Specify a different type.", "1"],

View File

@ -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(),
});
});

View File

@ -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));
};
/**