labmenu.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { ArrayExt } from '@lumino/algorithm';
  5. import { CommandRegistry } from '@lumino/commands';
  6. import { JupyterLabMenu } from '@jupyterlab/mainmenu';
  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. });
  40. it('should accept useSeparators as an option', () => {
  41. const menu1 = new JupyterLabMenu({ commands }, false);
  42. const menu2 = new JupyterLabMenu({ commands }, true);
  43. menu1.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  44. menu2.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  45. expect(menu1.menu.items.length).toBe(2);
  46. expect(menu2.menu.items.length).toBe(4);
  47. });
  48. });
  49. describe('#addGroup()', () => {
  50. it('should add a new group to the menu', () => {
  51. menu.addGroup([{ command: 'run1' }, { command: 'run2' }]);
  52. const idx1 = ArrayExt.findFirstIndex(
  53. menu.menu.items,
  54. m => m.command === 'run1'
  55. );
  56. const idx2 = ArrayExt.findFirstIndex(
  57. menu.menu.items,
  58. m => m.command === 'run2'
  59. );
  60. expect(idx1 === -1).toBe(false);
  61. expect(idx2 === -1).toBe(false);
  62. expect(idx1 > idx2).toBe(false);
  63. });
  64. it('should take a rank as an option', () => {
  65. menu.addGroup([{ command: 'run1' }, { command: 'run2' }], 2);
  66. menu.addGroup([{ command: 'run3' }, { command: 'run4' }], 1);
  67. const idx1 = ArrayExt.findFirstIndex(
  68. menu.menu.items,
  69. m => m.command === 'run1'
  70. );
  71. const idx2 = ArrayExt.findFirstIndex(
  72. menu.menu.items,
  73. m => m.command === 'run2'
  74. );
  75. const idx3 = ArrayExt.findFirstIndex(
  76. menu.menu.items,
  77. m => m.command === 'run3'
  78. );
  79. const idx4 = ArrayExt.findFirstIndex(
  80. menu.menu.items,
  81. m => m.command === 'run4'
  82. );
  83. expect(idx3 < idx4).toBe(true);
  84. expect(idx4 < idx1).toBe(true);
  85. expect(idx1 < idx2).toBe(true);
  86. });
  87. it('should return a disposable that can be used to remove the group', () => {
  88. const group1 = [{ command: 'run1' }, { command: 'run2' }];
  89. const group2 = [{ command: 'run3' }, { command: 'run4' }];
  90. const disposable = menu.addGroup(group1);
  91. menu.addGroup(group2);
  92. disposable.dispose();
  93. const idx1 = ArrayExt.findFirstIndex(
  94. menu.menu.items,
  95. m => m.command === 'run1'
  96. );
  97. const idx2 = ArrayExt.findFirstIndex(
  98. menu.menu.items,
  99. m => m.command === 'run2'
  100. );
  101. const idx3 = ArrayExt.findFirstIndex(
  102. menu.menu.items,
  103. m => m.command === 'run3'
  104. );
  105. const idx4 = ArrayExt.findFirstIndex(
  106. menu.menu.items,
  107. m => m.command === 'run4'
  108. );
  109. expect(idx1).toBe(-1);
  110. expect(idx2).toBe(-1);
  111. expect(idx3 === -1).toBe(false);
  112. expect(idx4 === -1).toBe(false);
  113. });
  114. });
  115. });
  116. });