shortcuts.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { ISettingRegistry, SettingRegistry } from '@jupyterlab/settingregistry';
  4. import * as plugin from '@jupyterlab/shortcuts-extension';
  5. import { IDataConnector } from '@jupyterlab/statedb';
  6. import { CommandRegistry } from '@lumino/commands';
  7. import { Platform } from '@lumino/domutils';
  8. import pluginSchema from '../schema/shortcuts.json';
  9. describe('@jupyterlab/shortcut-extension', () => {
  10. const pluginId = 'test-plugin:settings';
  11. let dummySettings: ISettingRegistry.IPlugin;
  12. beforeEach(() => {
  13. dummySettings = {
  14. data: {
  15. composite: {},
  16. user: {}
  17. },
  18. id: plugin.default.id,
  19. raw: '{}',
  20. schema: pluginSchema as any,
  21. version: 'test'
  22. };
  23. });
  24. describe('shorcuts list', () => {
  25. it('should ignored Cmd on non-Mac platform', async () => {
  26. const bar: ISettingRegistry.IPlugin = {
  27. data: {
  28. composite: {},
  29. user: {}
  30. },
  31. id: pluginId,
  32. raw: '{}',
  33. schema: {
  34. 'jupyter.lab.shortcuts': [
  35. {
  36. command: 'notebook:run-cell',
  37. keys: ['Ctrl Enter'],
  38. selector: '.jp-Notebook.jp-mod-editMode'
  39. },
  40. {
  41. command: 'notebook:run-cell',
  42. keys: ['Cmd Enter'],
  43. selector: '.jp-Notebook.jp-mod-editMode'
  44. }
  45. ],
  46. type: 'object'
  47. },
  48. version: 'test'
  49. };
  50. const connector: IDataConnector<
  51. ISettingRegistry.IPlugin,
  52. string,
  53. string,
  54. string
  55. > = {
  56. fetch: jest.fn().mockImplementation((id: string) => {
  57. switch (id) {
  58. case bar.id:
  59. return bar;
  60. case plugin.default.id:
  61. return dummySettings;
  62. default:
  63. return {};
  64. }
  65. }),
  66. list: jest.fn(),
  67. save: jest.fn(),
  68. remove: jest.fn()
  69. };
  70. const settingRegistry = new SettingRegistry({
  71. connector,
  72. timeout: Infinity
  73. });
  74. await settingRegistry.load(bar.id);
  75. await Promise.resolve(
  76. plugin.default.activate(
  77. {
  78. commands: new CommandRegistry()
  79. } as any,
  80. settingRegistry
  81. )
  82. );
  83. const settings = await settingRegistry.load(plugin.default.id);
  84. const shortcuts = (await settings.get('shortcuts')
  85. .composite) as ISettingRegistry.IShortcut[];
  86. expect(shortcuts).toHaveLength(Platform.IS_MAC ? 2 : 1);
  87. });
  88. it('should ignore colliding shortcuts', async () => {
  89. const pluginId = 'test-plugin:settings';
  90. const bar: ISettingRegistry.IPlugin = {
  91. data: {
  92. composite: {},
  93. user: {}
  94. },
  95. id: pluginId,
  96. raw: '{}',
  97. schema: {
  98. 'jupyter.lab.shortcuts': [
  99. {
  100. command: 'notebook:run-cell',
  101. keys: ['Accel Enter'],
  102. selector: '.jp-Notebook.jp-mod-editMode'
  103. },
  104. {
  105. command: 'another-colliding-command',
  106. keys: ['Accel Enter'],
  107. selector: '.jp-Notebook.jp-mod-editMode'
  108. }
  109. ],
  110. type: 'object'
  111. },
  112. version: 'test'
  113. };
  114. const connector: IDataConnector<
  115. ISettingRegistry.IPlugin,
  116. string,
  117. string,
  118. string
  119. > = {
  120. fetch: jest.fn().mockImplementation((id: string) => {
  121. switch (id) {
  122. case bar.id:
  123. return bar;
  124. case plugin.default.id:
  125. return dummySettings;
  126. default:
  127. return {};
  128. }
  129. }),
  130. list: jest.fn(),
  131. save: jest.fn(),
  132. remove: jest.fn()
  133. };
  134. const settingRegistry = new SettingRegistry({
  135. connector
  136. });
  137. await settingRegistry.load(bar.id);
  138. await Promise.resolve(
  139. plugin.default.activate(
  140. {
  141. commands: new CommandRegistry()
  142. } as any,
  143. settingRegistry
  144. )
  145. );
  146. const settings = await settingRegistry.load(plugin.default.id);
  147. const shortcuts = (await settings.get('shortcuts')
  148. .composite) as ISettingRegistry.IShortcut[];
  149. expect(shortcuts).toHaveLength(1);
  150. });
  151. });
  152. });