metrics_panel_ctrl.test.ts 2.0 KB

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