org-switcher: should redirect to home page (#10782)

Fixes #10776
This commit is contained in:
Daniel Lee 2018-02-06 10:02:28 +01:00 committed by Torkel Ödegaard
parent 45d66e4b29
commit 5ca03972a8
2 changed files with 12 additions and 11 deletions

View File

@ -1,5 +1,6 @@
import coreModule from 'app/core/core_module'; import coreModule from 'app/core/core_module';
import { contextSrv } from 'app/core/services/context_srv'; import { contextSrv } from 'app/core/services/context_srv';
import config from 'app/core/config';
const template = ` const template = `
<div class="modal-body"> <div class="modal-body">
@ -60,16 +61,11 @@ export class OrgSwitchCtrl {
setUsingOrg(org) { setUsingOrg(org) {
return this.backendSrv.post('/api/user/using/' + org.orgId).then(() => { return this.backendSrv.post('/api/user/using/' + org.orgId).then(() => {
const re = /orgId=\d+/gi; this.setWindowLocation(config.appSubUrl + (config.appSubUrl.endsWith('/') ? '' : '/') + '?orgId=' + org.orgId);
this.setWindowLocationHref(this.getWindowLocationHref().replace(re, 'orgId=' + org.orgId));
}); });
} }
getWindowLocationHref() { setWindowLocation(href: string) {
return window.location.href;
}
setWindowLocationHref(href: string) {
window.location.href = href; window.location.href = href;
} }
} }

View File

@ -7,6 +7,12 @@ jest.mock('app/core/services/context_srv', () => ({
}, },
})); }));
jest.mock('app/core/config', () => {
return {
appSubUrl: '/subUrl',
};
});
describe('OrgSwitcher', () => { describe('OrgSwitcher', () => {
describe('when switching org', () => { describe('when switching org', () => {
let expectedHref; let expectedHref;
@ -25,8 +31,7 @@ describe('OrgSwitcher', () => {
const orgSwitcherCtrl = new OrgSwitchCtrl(backendSrvStub); const orgSwitcherCtrl = new OrgSwitchCtrl(backendSrvStub);
orgSwitcherCtrl.getWindowLocationHref = () => 'http://localhost:3000?orgId=1&from=now-3h&to=now'; orgSwitcherCtrl.setWindowLocation = href => (expectedHref = href);
orgSwitcherCtrl.setWindowLocationHref = href => (expectedHref = href);
return orgSwitcherCtrl.setUsingOrg({ orgId: 2 }); return orgSwitcherCtrl.setUsingOrg({ orgId: 2 });
}); });
@ -35,8 +40,8 @@ describe('OrgSwitcher', () => {
expect(expectedUsingUrl).toBe('/api/user/using/2'); expect(expectedUsingUrl).toBe('/api/user/using/2');
}); });
it('should switch orgId in url', () => { it('should switch orgId in url and redirect to home page', () => {
expect(expectedHref).toBe('http://localhost:3000?orgId=2&from=now-3h&to=now'); expect(expectedHref).toBe('/subUrl/?orgId=2');
}); });
}); });
}); });