org_switcher.jest.ts 1.1 KB

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