labmenu.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { JupyterLabMenu } from '@jupyterlab/mainmenu';
  4. import { ArrayExt } from '@lumino/algorithm';
  5. import { CommandRegistry } from '@lumino/commands';
  6. import { Menu } from '@lumino/widgets';
  7. describe('@jupyterlab/mainmenu', () => {
  8. describe('JupyterLabMenu', () => {
  9. let commands: CommandRegistry;
  10. let menu: JupyterLabMenu;
  11. beforeAll(() => {
  12. commands = new CommandRegistry();
  13. commands.addCommand('run1', {
  14. label: 'Run 1',
  15. execute: () => void 0
  16. });
  17. commands.addCommand('run2', {
  18. label: 'Run 2',
  19. execute: () => void 0
  20. });
  21. commands.addCommand('run3', {
  22. label: 'Run 3',
  23. execute: () => void 0
  24. });
  25. commands.addCommand('run4', {
  26. label: 'Run 4',
  27. execute: () => void 0
  28. });
  29. });
  30. beforeEach(() => {
  31. menu = new JupyterLabMenu({ commands });
  32. });
  33. afterEach(() => {
  34. menu.dispose();
  35. });
  36. describe('#constructor()', () => {
  37. it('should construct a new main menu', () => {
  38. expect(menu).toBeInstanceOf(JupyterLabMenu);
  39. expect(menu).toBeInstanceOf(Menu);
  40. });
  41. it('should accept useSeparators as an option', () => {
  42. const menu1 = new JupyterLabMenu({
  43. commands,
  44. includeSeparators: false
  45. });
  46. const menu2 = new JupyterLabMenu({ commands, includeSeparators: true });
  47. menu1.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  48. menu2.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  49. expect(menu1.items.length).toBe(2);
  50. expect(menu2.items.length).toBe(4);
  51. });
  52. it('should accept rank as an option', () => {
  53. const menu = new JupyterLabMenu({ commands, rank: 22 });
  54. expect(menu.rank).toEqual(22);
  55. });
  56. it('should have rank undefined by default', () => {
  57. const menu = new JupyterLabMenu({ commands });
  58. expect(menu.rank).toBeUndefined();
  59. });
  60. });
  61. describe('#rank', () => {
  62. it('should have a read-only rank', () => {
  63. expect(() => {
  64. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  65. // @ts-ignore
  66. menu.rank = 42;
  67. }).toThrowError();
  68. });
  69. });
  70. describe('#addGroup()', () => {
  71. it('should add a new group to the menu', () => {
  72. menu.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  73. const idx1 = ArrayExt.findFirstIndex(
  74. menu.items,
  75. m => m.command === 'run1'
  76. );
  77. const idx2 = ArrayExt.findFirstIndex(
  78. menu.items,
  79. m => m.command === 'run2'
  80. );
  81. expect(idx1 === -1).toBe(false);
  82. expect(idx2 === -1).toBe(false);
  83. expect(idx1 > idx2).toBe(false);
  84. });
  85. it('should take a rank as an option', () => {
  86. menu.addGroup([{ command: 'run1' }, { command: 'run2' }], 2);
  87. menu.addGroup([{ command: 'run3' }, { command: 'run4' }], 1);
  88. const idx1 = ArrayExt.findFirstIndex(
  89. menu.items,
  90. m => m.command === 'run1'
  91. );
  92. const idx2 = ArrayExt.findFirstIndex(
  93. menu.items,
  94. m => m.command === 'run2'
  95. );
  96. const idx3 = ArrayExt.findFirstIndex(
  97. menu.items,
  98. m => m.command === 'run3'
  99. );
  100. const idx4 = ArrayExt.findFirstIndex(
  101. menu.items,
  102. m => m.command === 'run4'
  103. );
  104. expect(idx3 < idx4).toBe(true);
  105. expect(idx4 < idx1).toBe(true);
  106. expect(idx1 < idx2).toBe(true);
  107. });
  108. it('should return a disposable that can be used to remove the group', () => {
  109. const group1 = [{ command: 'run1' }, { command: 'run2' }];
  110. const group2 = [{ command: 'run3' }, { command: 'run4' }];
  111. const disposable = menu.addGroup(group1);
  112. menu.addGroup(group2);
  113. disposable.dispose();
  114. const idx1 = ArrayExt.findFirstIndex(
  115. menu.items,
  116. m => m.command === 'run1'
  117. );
  118. const idx2 = ArrayExt.findFirstIndex(
  119. menu.items,
  120. m => m.command === 'run2'
  121. );
  122. const idx3 = ArrayExt.findFirstIndex(
  123. menu.items,
  124. m => m.command === 'run3'
  125. );
  126. const idx4 = ArrayExt.findFirstIndex(
  127. menu.items,
  128. m => m.command === 'run4'
  129. );
  130. expect(idx1).toBe(-1);
  131. expect(idx2).toBe(-1);
  132. expect(idx3 === -1).toBe(false);
  133. expect(idx4 === -1).toBe(false);
  134. });
  135. });
  136. describe('#addItem()', () => {
  137. it('should use the provided rank to position the item', () => {
  138. menu.addItem({ command: 'run1', rank: 1000 });
  139. menu.addItem({ command: 'run2', rank: 10 });
  140. expect(menu.getRankAt(0)).toEqual(10);
  141. expect(menu.getRankAt(1)).toEqual(1000);
  142. });
  143. it('should append the item at the end if no rank is provided', () => {
  144. menu.addItem({ command: 'run1', rank: 10 });
  145. menu.addItem({ command: 'run3' });
  146. expect(menu.items[1].command).toEqual('run3');
  147. expect(menu.getRankAt(1)).toEqual(100);
  148. menu.addItem({ command: 'run2', rank: 1000 });
  149. // Set a rank to n-1 element if it is higher than the default
  150. menu.addItem({ command: 'run4' });
  151. expect(menu.items[3].command).toEqual('run4');
  152. expect(menu.getRankAt(3)).toEqual(1000);
  153. });
  154. });
  155. });
  156. });