celltoolbartracker.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. import {
  6. createToolbarFactory,
  7. ToolbarWidgetRegistry
  8. } from '@jupyterlab/apputils';
  9. import { CellBarExtension, CellToolbarTracker } from '@jupyterlab/cell-toolbar';
  10. import { NotebookPanel } from '@jupyterlab/notebook';
  11. import { ISettingRegistry, SettingRegistry } from '@jupyterlab/settingregistry';
  12. import { IDataConnector } from '@jupyterlab/statedb';
  13. import { NBTestUtils } from '@jupyterlab/testutils';
  14. import { ITranslator } from '@jupyterlab/translation';
  15. import { CommandRegistry } from '@lumino/commands';
  16. import { Widget } from '@lumino/widgets';
  17. function testToolbarFactory() {
  18. const pluginId = '@jupyterlab/cell-toolbar';
  19. const toolbarRegistry = new ToolbarWidgetRegistry({
  20. defaultFactory: jest.fn().mockImplementation(() => new Widget())
  21. });
  22. const bar: ISettingRegistry.IPlugin = {
  23. data: {
  24. composite: {},
  25. user: {}
  26. },
  27. id: pluginId,
  28. raw: '{}',
  29. schema: {
  30. 'jupyter.lab.toolbars': {
  31. dummyFactory: [
  32. {
  33. name: 'insert',
  34. command: 'notebook:insert-cell-below',
  35. rank: 20
  36. },
  37. { name: 'spacer', type: 'spacer', rank: 100 },
  38. { name: 'cut', command: 'notebook:cut-cell', rank: 21 },
  39. {
  40. name: 'clear-all',
  41. command: 'notebook:clear-all-cell-outputs',
  42. rank: 60,
  43. disabled: true
  44. }
  45. ]
  46. },
  47. 'jupyter.lab.transform': true,
  48. properties: {
  49. toolbar: {
  50. type: 'array'
  51. }
  52. },
  53. type: 'object'
  54. },
  55. version: 'test'
  56. };
  57. const connector: IDataConnector<
  58. ISettingRegistry.IPlugin,
  59. string,
  60. string,
  61. string
  62. > = {
  63. fetch: jest.fn().mockImplementation((id: string) => {
  64. switch (id) {
  65. case bar.id:
  66. return bar;
  67. default:
  68. return {};
  69. }
  70. }),
  71. list: jest.fn(),
  72. save: jest.fn(),
  73. remove: jest.fn()
  74. };
  75. const settingRegistry = new SettingRegistry({
  76. connector
  77. });
  78. const factoryName = 'dummyFactory';
  79. const translator: ITranslator = {
  80. load: jest.fn()
  81. };
  82. return createToolbarFactory(
  83. toolbarRegistry,
  84. settingRegistry,
  85. factoryName,
  86. pluginId,
  87. translator
  88. );
  89. }
  90. describe('@jupyterlab/cell-toolbar', () => {
  91. describe('CellBarExtension', () => {
  92. let commands: CommandRegistry;
  93. let panel: NotebookPanel;
  94. let extension: CellBarExtension;
  95. beforeAll(() => {
  96. commands = new CommandRegistry();
  97. commands.addCommand('notebook:move-cell-up', {
  98. execute: args => null
  99. });
  100. commands.addCommand('notebook:move-cell-down', {
  101. execute: args => null
  102. });
  103. extension = new CellBarExtension(commands, testToolbarFactory());
  104. });
  105. afterEach(() => {
  106. if (panel) {
  107. panel.dispose();
  108. }
  109. });
  110. describe('#constructor()', () => {
  111. it('should create a cell toolbar extension', () => {
  112. expect(extension).toBeInstanceOf(CellBarExtension);
  113. });
  114. });
  115. });
  116. describe('CellToolbarTracker', () => {
  117. let commands: CommandRegistry;
  118. let panel: NotebookPanel;
  119. let extension: CellBarExtension;
  120. beforeAll(() => {
  121. commands = new CommandRegistry();
  122. commands.addCommand('notebook:move-cell-up', {
  123. execute: args => null
  124. });
  125. commands.addCommand('notebook:move-cell-down', {
  126. execute: args => null
  127. });
  128. extension = new CellBarExtension(commands, testToolbarFactory());
  129. });
  130. afterEach(() => {
  131. if (panel) {
  132. panel.dispose();
  133. }
  134. });
  135. describe('#createNew()', () => {
  136. it('should create a new cell toolbar tracker', async () => {
  137. const context = await NBTestUtils.createMockContext();
  138. panel = NBTestUtils.createNotebookPanel(context);
  139. await panel.revealed;
  140. const tracker = extension.createNew(panel);
  141. expect(tracker).toBeInstanceOf(CellToolbarTracker);
  142. });
  143. });
  144. });
  145. });