getPanelMenu.test.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { PanelMenuItem } from '@grafana/data';
  2. import config from 'app/core/config';
  3. import * as actions from 'app/features/explore/state/main';
  4. import { setStore } from 'app/store/store';
  5. import { describe } from '../../../../test/lib/common';
  6. import { DashboardModel, PanelModel } from '../state';
  7. import { getPanelMenu } from './getPanelMenu';
  8. jest.mock('app/core/services/context_srv', () => ({
  9. contextSrv: {
  10. hasAccessToExplore: () => true,
  11. },
  12. }));
  13. describe('getPanelMenu', () => {
  14. it('should return the correct panel menu items', () => {
  15. const panel = new PanelModel({});
  16. const dashboard = new DashboardModel({});
  17. const menuItems = getPanelMenu(dashboard, panel);
  18. expect(menuItems).toMatchInlineSnapshot(`
  19. Array [
  20. Object {
  21. "iconClassName": "eye",
  22. "onClick": [Function],
  23. "shortcut": "v",
  24. "text": "View",
  25. },
  26. Object {
  27. "iconClassName": "edit",
  28. "onClick": [Function],
  29. "shortcut": "e",
  30. "text": "Edit",
  31. },
  32. Object {
  33. "iconClassName": "share-alt",
  34. "onClick": [Function],
  35. "shortcut": "p s",
  36. "text": "Share",
  37. },
  38. Object {
  39. "iconClassName": "compass",
  40. "onClick": [Function],
  41. "shortcut": "x",
  42. "text": "Explore",
  43. },
  44. Object {
  45. "iconClassName": "info-circle",
  46. "onClick": [Function],
  47. "shortcut": "i",
  48. "subMenu": Array [
  49. Object {
  50. "onClick": [Function],
  51. "text": "Panel JSON",
  52. },
  53. ],
  54. "text": "Inspect",
  55. "type": "submenu",
  56. },
  57. Object {
  58. "iconClassName": "cube",
  59. "onClick": [Function],
  60. "subMenu": Array [
  61. Object {
  62. "onClick": [Function],
  63. "shortcut": "p d",
  64. "text": "Duplicate",
  65. },
  66. Object {
  67. "onClick": [Function],
  68. "text": "Copy",
  69. },
  70. Object {
  71. "onClick": [Function],
  72. "text": "Create library panel",
  73. },
  74. ],
  75. "text": "More...",
  76. "type": "submenu",
  77. },
  78. Object {
  79. "text": "",
  80. "type": "divider",
  81. },
  82. Object {
  83. "iconClassName": "trash-alt",
  84. "onClick": [Function],
  85. "shortcut": "p r",
  86. "text": "Remove",
  87. },
  88. ]
  89. `);
  90. });
  91. describe('when panel is in view mode', () => {
  92. it('should return the correct panel menu items', () => {
  93. const getExtendedMenu = () => [{ text: 'Toggle legend', shortcut: 'p l', click: jest.fn() }];
  94. const ctrl: any = { getExtendedMenu };
  95. const scope: any = { $$childHead: { ctrl } };
  96. const angularComponent: any = { getScope: () => scope };
  97. const panel = new PanelModel({ isViewing: true });
  98. const dashboard = new DashboardModel({});
  99. const menuItems = getPanelMenu(dashboard, panel, angularComponent);
  100. expect(menuItems).toMatchInlineSnapshot(`
  101. Array [
  102. Object {
  103. "iconClassName": "eye",
  104. "onClick": [Function],
  105. "shortcut": "v",
  106. "text": "View",
  107. },
  108. Object {
  109. "iconClassName": "edit",
  110. "onClick": [Function],
  111. "shortcut": "e",
  112. "text": "Edit",
  113. },
  114. Object {
  115. "iconClassName": "share-alt",
  116. "onClick": [Function],
  117. "shortcut": "p s",
  118. "text": "Share",
  119. },
  120. Object {
  121. "iconClassName": "compass",
  122. "onClick": [Function],
  123. "shortcut": "x",
  124. "text": "Explore",
  125. },
  126. Object {
  127. "iconClassName": "info-circle",
  128. "onClick": [Function],
  129. "shortcut": "i",
  130. "subMenu": Array [
  131. Object {
  132. "onClick": [Function],
  133. "text": "Panel JSON",
  134. },
  135. ],
  136. "text": "Inspect",
  137. "type": "submenu",
  138. },
  139. Object {
  140. "iconClassName": "cube",
  141. "onClick": [Function],
  142. "subMenu": Array [
  143. Object {
  144. "href": undefined,
  145. "onClick": [Function],
  146. "shortcut": "p l",
  147. "text": "Toggle legend",
  148. },
  149. ],
  150. "text": "More...",
  151. "type": "submenu",
  152. },
  153. ]
  154. `);
  155. });
  156. });
  157. describe('onNavigateToExplore', () => {
  158. const testSubUrl = '/testSubUrl';
  159. const testUrl = '/testUrl';
  160. const windowOpen = jest.fn();
  161. let event: any;
  162. let explore: PanelMenuItem;
  163. let navigateSpy: any;
  164. beforeAll(() => {
  165. const panel = new PanelModel({});
  166. const dashboard = new DashboardModel({});
  167. const menuItems = getPanelMenu(dashboard, panel);
  168. explore = menuItems.find((item) => item.text === 'Explore') as PanelMenuItem;
  169. navigateSpy = jest.spyOn(actions, 'navigateToExplore');
  170. window.open = windowOpen;
  171. event = {
  172. ctrlKey: true,
  173. preventDefault: jest.fn(),
  174. };
  175. setStore({ dispatch: jest.fn() } as any);
  176. });
  177. it('should navigate to url without subUrl', () => {
  178. explore.onClick!(event);
  179. const openInNewWindow = navigateSpy.mock.calls[0][1].openInNewWindow;
  180. openInNewWindow(testUrl);
  181. expect(windowOpen).toHaveBeenLastCalledWith(testUrl);
  182. });
  183. it('should navigate to url with subUrl', () => {
  184. config.appSubUrl = testSubUrl;
  185. explore.onClick!(event);
  186. const openInNewWindow = navigateSpy.mock.calls[0][1].openInNewWindow;
  187. openInNewWindow(testUrl);
  188. expect(windowOpen).toHaveBeenLastCalledWith(`${testSubUrl}${testUrl}`);
  189. });
  190. });
  191. });