labmenu.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ArrayExt } from '@lumino/algorithm';
  4. import { CommandRegistry } from '@lumino/commands';
  5. import { JupyterLabMenu } from '@jupyterlab/mainmenu';
  6. describe('@jupyterlab/mainmenu', () => {
  7. describe('JupyterLabMenu', () => {
  8. let commands: CommandRegistry;
  9. let menu: JupyterLabMenu;
  10. beforeAll(() => {
  11. commands = new CommandRegistry();
  12. commands.addCommand('run1', {
  13. label: 'Run 1',
  14. execute: () => void 0
  15. });
  16. commands.addCommand('run2', {
  17. label: 'Run 2',
  18. execute: () => void 0
  19. });
  20. commands.addCommand('run3', {
  21. label: 'Run 3',
  22. execute: () => void 0
  23. });
  24. commands.addCommand('run4', {
  25. label: 'Run 4',
  26. execute: () => void 0
  27. });
  28. });
  29. beforeEach(() => {
  30. menu = new JupyterLabMenu({ commands });
  31. });
  32. afterEach(() => {
  33. menu.dispose();
  34. });
  35. describe('#constructor()', () => {
  36. it('should construct a new main menu', () => {
  37. expect(menu).toBeInstanceOf(JupyterLabMenu);
  38. });
  39. it('should accept useSeparators as an option', () => {
  40. const menu1 = new JupyterLabMenu({ commands }, false);
  41. const menu2 = new JupyterLabMenu({ commands }, true);
  42. menu1.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  43. menu2.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  44. expect(menu1.menu.items.length).toBe(2);
  45. expect(menu2.menu.items.length).toBe(4);
  46. });
  47. });
  48. describe('#addGroup()', () => {
  49. it('should add a new group to the menu', () => {
  50. menu.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  51. const idx1 = ArrayExt.findFirstIndex(
  52. menu.menu.items,
  53. m => m.command === 'run1'
  54. );
  55. const idx2 = ArrayExt.findFirstIndex(
  56. menu.menu.items,
  57. m => m.command === 'run2'
  58. );
  59. expect(idx1 === -1).toBe(false);
  60. expect(idx2 === -1).toBe(false);
  61. expect(idx1 > idx2).toBe(false);
  62. });
  63. it('should take a rank as an option', () => {
  64. menu.addGroup([{ command: 'run1' }, { command: 'run2' }], 2);
  65. menu.addGroup([{ command: 'run3' }, { command: 'run4' }], 1);
  66. const idx1 = ArrayExt.findFirstIndex(
  67. menu.menu.items,
  68. m => m.command === 'run1'
  69. );
  70. const idx2 = ArrayExt.findFirstIndex(
  71. menu.menu.items,
  72. m => m.command === 'run2'
  73. );
  74. const idx3 = ArrayExt.findFirstIndex(
  75. menu.menu.items,
  76. m => m.command === 'run3'
  77. );
  78. const idx4 = ArrayExt.findFirstIndex(
  79. menu.menu.items,
  80. m => m.command === 'run4'
  81. );
  82. expect(idx3 < idx4).toBe(true);
  83. expect(idx4 < idx1).toBe(true);
  84. expect(idx1 < idx2).toBe(true);
  85. });
  86. it('should return a disposable that can be used to remove the group', () => {
  87. const group1 = [{ command: 'run1' }, { command: 'run2' }];
  88. const group2 = [{ command: 'run3' }, { command: 'run4' }];
  89. const disposable = menu.addGroup(group1);
  90. menu.addGroup(group2);
  91. disposable.dispose();
  92. const idx1 = ArrayExt.findFirstIndex(
  93. menu.menu.items,
  94. m => m.command === 'run1'
  95. );
  96. const idx2 = ArrayExt.findFirstIndex(
  97. menu.menu.items,
  98. m => m.command === 'run2'
  99. );
  100. const idx3 = ArrayExt.findFirstIndex(
  101. menu.menu.items,
  102. m => m.command === 'run3'
  103. );
  104. const idx4 = ArrayExt.findFirstIndex(
  105. menu.menu.items,
  106. m => m.command === 'run4'
  107. );
  108. expect(idx1).toBe(-1);
  109. expect(idx2).toBe(-1);
  110. expect(idx3 === -1).toBe(false);
  111. expect(idx4 === -1).toBe(false);
  112. });
  113. });
  114. });
  115. });