metrics_panel_ctrl.jest.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. jest.mock('app/core/core', () => ({}));
  2. jest.mock('app/core/config', () => {
  3. return {
  4. exploreEnabled: true,
  5. panels: {
  6. test: {
  7. id: 'test',
  8. name: 'test',
  9. },
  10. },
  11. };
  12. });
  13. import q from 'q';
  14. import { PanelModel } from 'app/features/dashboard/panel_model';
  15. import { MetricsPanelCtrl } from '../metrics_panel_ctrl';
  16. describe('MetricsPanelCtrl', () => {
  17. let ctrl;
  18. beforeEach(() => {
  19. ctrl = setupController();
  20. });
  21. describe('when getting additional menu items', () => {
  22. let additionalItems;
  23. describe('and has no datasource set', () => {
  24. beforeEach(() => {
  25. additionalItems = ctrl.getAdditionalMenuItems();
  26. });
  27. it('should not return any items', () => {
  28. expect(additionalItems.length).toBe(0);
  29. });
  30. });
  31. describe('and has datasource set that supports explore and user has powers', () => {
  32. beforeEach(() => {
  33. ctrl.contextSrv = { isEditor: true };
  34. ctrl.datasource = { supportsExplore: true };
  35. additionalItems = ctrl.getAdditionalMenuItems();
  36. });
  37. it('should not return any items', () => {
  38. expect(additionalItems.length).toBe(1);
  39. });
  40. });
  41. });
  42. });
  43. function setupController() {
  44. const injectorStub = {
  45. get: type => {
  46. switch (type) {
  47. case '$q': {
  48. return q;
  49. }
  50. default: {
  51. return jest.fn();
  52. }
  53. }
  54. },
  55. };
  56. const scope = {
  57. panel: { events: [] },
  58. appEvent: jest.fn(),
  59. onAppEvent: jest.fn(),
  60. $on: jest.fn(),
  61. colors: [],
  62. };
  63. MetricsPanelCtrl.prototype.panel = new PanelModel({ type: 'test' });
  64. return new MetricsPanelCtrl(scope, injectorStub);
  65. }