metrics_panel_ctrl.test.ts 2.1 KB

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