mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Migration: Migrate org switcher to react (#19607)
* Migration: Migrate org switcher to react * Improve modal overflow behavior * Updated modal backdrop * Renamed type * Modal: Refactoring and reducing duplication
This commit is contained in:
committed by
Torkel Ödegaard
parent
5cd4ffffe3
commit
a093fbb51a
51
public/app/core/specs/OrgSwitcher.test.tsx
Normal file
51
public/app/core/specs/OrgSwitcher.test.tsx
Normal file
@@ -0,0 +1,51 @@
|
||||
import React from 'react';
|
||||
// @ts-ignore
|
||||
import { getBackendSrv } from '@grafana/runtime/src/services/backendSrv';
|
||||
import { OrgSwitcher } from '../components/OrgSwitcher';
|
||||
import { shallow } from 'enzyme';
|
||||
import { OrgRole } from '@grafana/data';
|
||||
|
||||
const getMock = jest.fn(() => Promise.resolve([]));
|
||||
const postMock = jest.fn();
|
||||
|
||||
jest.mock('@grafana/runtime/src/services/backendSrv', () => ({
|
||||
getBackendSrv: () => ({
|
||||
get: getMock,
|
||||
post: postMock,
|
||||
}),
|
||||
}));
|
||||
|
||||
jest.mock('app/core/services/context_srv', () => ({
|
||||
contextSrv: {
|
||||
user: { orgId: 1 },
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('app/core/config', () => {
|
||||
return {
|
||||
appSubUrl: '/subUrl',
|
||||
};
|
||||
});
|
||||
|
||||
let wrapper;
|
||||
let orgSwitcher: OrgSwitcher;
|
||||
|
||||
describe('OrgSwitcher', () => {
|
||||
describe('when switching org', () => {
|
||||
beforeEach(async () => {
|
||||
wrapper = shallow(<OrgSwitcher onDismiss={() => {}} isOpen={true} />);
|
||||
orgSwitcher = wrapper.instance() as OrgSwitcher;
|
||||
orgSwitcher.setWindowLocation = jest.fn();
|
||||
wrapper.update();
|
||||
await orgSwitcher.setCurrentOrg({ name: 'mock org', orgId: 2, role: OrgRole.Viewer });
|
||||
});
|
||||
|
||||
it('should switch orgId in call to backend', () => {
|
||||
expect(postMock).toBeCalledWith('/api/user/using/2');
|
||||
});
|
||||
|
||||
it('should switch orgId in url and redirect to home page', () => {
|
||||
expect(orgSwitcher.setWindowLocation).toBeCalledWith('/subUrl/?orgId=2');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
import { OrgSwitchCtrl } from '../components/org_switcher';
|
||||
// @ts-ignore
|
||||
import q from 'q';
|
||||
|
||||
jest.mock('app/core/services/context_srv', () => ({
|
||||
contextSrv: {
|
||||
user: { orgId: 1 },
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock('app/core/config', () => {
|
||||
return {
|
||||
appSubUrl: '/subUrl',
|
||||
};
|
||||
});
|
||||
|
||||
describe('OrgSwitcher', () => {
|
||||
describe('when switching org', () => {
|
||||
let expectedHref: string;
|
||||
let expectedUsingUrl: string;
|
||||
|
||||
beforeEach(() => {
|
||||
const backendSrvStub: any = {
|
||||
get: (url: string) => {
|
||||
return q.resolve([]);
|
||||
},
|
||||
post: (url: string) => {
|
||||
expectedUsingUrl = url;
|
||||
return q.resolve({});
|
||||
},
|
||||
};
|
||||
|
||||
const orgSwitcherCtrl = new OrgSwitchCtrl(backendSrvStub);
|
||||
|
||||
orgSwitcherCtrl.setWindowLocation = href => (expectedHref = href);
|
||||
|
||||
return orgSwitcherCtrl.setUsingOrg({ orgId: 2 });
|
||||
});
|
||||
|
||||
it('should switch orgId in call to backend', () => {
|
||||
expect(expectedUsingUrl).toBe('/api/user/using/2');
|
||||
});
|
||||
|
||||
it('should switch orgId in url and redirect to home page', () => {
|
||||
expect(expectedHref).toBe('/subUrl/?orgId=2');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user