metrics_panel_ctrl.test.ts 2.1 KB

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