widgetfactory.spec.ts 5.1 KB

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