org_switcher.test.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { OrgSwitchCtrl } from '../components/org_switcher';
  2. // @ts-ignore
  3. import q from 'q';
  4. jest.mock('app/core/services/context_srv', () => ({
  5. contextSrv: {
  6. user: { orgId: 1 },
  7. },
  8. }));
  9. jest.mock('app/core/config', () => {
  10. return {
  11. appSubUrl: '/subUrl',
  12. };
  13. });
  14. describe('OrgSwitcher', () => {
  15. describe('when switching org', () => {
  16. let expectedHref: string;
  17. let expectedUsingUrl: string;
  18. beforeEach(() => {
  19. const backendSrvStub: any = {
  20. get: (url: string) => {
  21. return q.resolve([]);
  22. },
  23. post: (url: string) => {
  24. expectedUsingUrl = url;
  25. return q.resolve({});
  26. },
  27. };
  28. const orgSwitcherCtrl = new OrgSwitchCtrl(backendSrvStub);
  29. orgSwitcherCtrl.setWindowLocation = href => (expectedHref = href);
  30. return orgSwitcherCtrl.setUsingOrg({ orgId: 2 });
  31. });
  32. it('should switch orgId in call to backend', () => {
  33. expect(expectedUsingUrl).toBe('/api/user/using/2');
  34. });
  35. it('should switch orgId in url and redirect to home page', () => {
  36. expect(expectedHref).toBe('/subUrl/?orgId=2');
  37. });
  38. });
  39. });