widgetfactory.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { toArray } from '@lumino/algorithm';
  4. import { ToolbarButton } from '@jupyterlab/apputils';
  5. import { DocumentRegistry, Context } from '@jupyterlab/docregistry';
  6. import { INotebookModel, NotebookPanel, NotebookWidgetFactory } from '../src';
  7. import { initNotebookContext } from '@jupyterlab/testutils';
  8. import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
  9. import * as utils from './utils';
  10. const contentFactory = utils.createNotebookPanelFactory();
  11. const rendermime = utils.defaultRenderMime();
  12. const server = new JupyterServer();
  13. beforeAll(async () => {
  14. jest.setTimeout(20000);
  15. await server.start();
  16. });
  17. afterAll(async () => {
  18. await server.shutdown();
  19. });
  20. function createFactory(
  21. toolbarFactory?: (widget: NotebookPanel) => DocumentRegistry.IToolbarItem[]
  22. ): NotebookWidgetFactory {
  23. return new NotebookWidgetFactory({
  24. name: 'notebook',
  25. fileTypes: ['notebook'],
  26. rendermime,
  27. toolbarFactory,
  28. contentFactory,
  29. mimeTypeService: utils.mimeTypeService,
  30. editorConfig: utils.defaultEditorConfig
  31. });
  32. }
  33. describe('@jupyterlab/notebook', () => {
  34. describe('NotebookWidgetFactory', () => {
  35. let context: Context<INotebookModel>;
  36. beforeEach(async () => {
  37. context = await initNotebookContext();
  38. });
  39. afterEach(() => {
  40. context.dispose();
  41. });
  42. describe('#constructor()', () => {
  43. it('should create a notebook widget factory', () => {
  44. const factory = createFactory();
  45. expect(factory).toBeInstanceOf(NotebookWidgetFactory);
  46. });
  47. });
  48. describe('#isDisposed', () => {
  49. it('should get whether the factory has been disposed', () => {
  50. const factory = createFactory();
  51. expect(factory.isDisposed).toBe(false);
  52. factory.dispose();
  53. expect(factory.isDisposed).toBe(true);
  54. });
  55. });
  56. describe('#dispose()', () => {
  57. it('should dispose of the resources held by the factory', () => {
  58. const factory = createFactory();
  59. factory.dispose();
  60. expect(factory.isDisposed).toBe(true);
  61. });
  62. it('should be safe to call multiple times', () => {
  63. const factory = createFactory();
  64. factory.dispose();
  65. factory.dispose();
  66. expect(factory.isDisposed).toBe(true);
  67. });
  68. });
  69. describe('#editorConfig', () => {
  70. it('should be the editor config passed into the constructor', () => {
  71. const factory = createFactory();
  72. expect(factory.editorConfig).toBe(utils.defaultEditorConfig);
  73. });
  74. it('should be settable', () => {
  75. const factory = createFactory();
  76. const newConfig = { ...utils.defaultEditorConfig };
  77. factory.editorConfig = newConfig;
  78. expect(factory.editorConfig).toBe(newConfig);
  79. });
  80. });
  81. describe('#createNew()', () => {
  82. it('should create a new `NotebookPanel` widget', () => {
  83. const factory = createFactory();
  84. const panel = factory.createNew(context);
  85. expect(panel).toBeInstanceOf(NotebookPanel);
  86. });
  87. it('should create a clone of the rendermime', () => {
  88. const factory = createFactory();
  89. const panel = factory.createNew(context);
  90. expect(panel.content.rendermime).not.toBe(rendermime);
  91. });
  92. it('should pass the editor config to the notebook', () => {
  93. const factory = createFactory();
  94. const panel = factory.createNew(context);
  95. expect(panel.content.editorConfig).toBe(utils.defaultEditorConfig);
  96. });
  97. it('should populate the default toolbar items', () => {
  98. const factory = createFactory();
  99. const panel = factory.createNew(context);
  100. const items = toArray(panel.toolbar.names());
  101. expect(items).toEqual(expect.arrayContaining(['save']));
  102. expect(items).toEqual(expect.arrayContaining(['restart']));
  103. expect(items).toEqual(expect.arrayContaining(['kernelStatus']));
  104. });
  105. it('should populate the customized toolbar items', () => {
  106. const toolbarFactory = () => [
  107. { name: 'foo', widget: new ToolbarButton() },
  108. { name: 'bar', widget: new ToolbarButton() }
  109. ];
  110. const factory = createFactory(toolbarFactory);
  111. const panel = factory.createNew(context);
  112. const panel2 = factory.createNew(context);
  113. expect(toArray(panel.toolbar.names())).toEqual(['foo', 'bar']);
  114. expect(toArray(panel2.toolbar.names())).toEqual(['foo', 'bar']);
  115. expect(toArray(panel.toolbar.children()).length).toBe(2);
  116. expect(toArray(panel2.toolbar.children()).length).toBe(2);
  117. });
  118. it('should clone from the optional source widget', () => {
  119. const factory = createFactory();
  120. const panel = factory.createNew(context);
  121. const clone = factory.createNew(panel.context, panel);
  122. expect(clone).toBeInstanceOf(NotebookPanel);
  123. expect(clone.content.rendermime).toBe(panel.content.rendermime);
  124. expect(clone.content.editorConfig).toBe(panel.content.editorConfig);
  125. expect(clone.content.notebookConfig).toBe(panel.content.notebookConfig);
  126. });
  127. });
  128. });
  129. });