123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import { ToolbarButton } from '@jupyterlab/apputils';
- import { Context } from '@jupyterlab/docregistry';
- import { initNotebookContext } from '@jupyterlab/testutils';
- import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
- import { toArray } from '@lumino/algorithm';
- import { INotebookModel, NotebookPanel, NotebookWidgetFactory } from '..';
- import * as utils from './utils';
- const rendermime = utils.defaultRenderMime();
- const server = new JupyterServer();
- beforeAll(async () => {
- jest.setTimeout(20000);
- await server.start();
- });
- afterAll(async () => {
- await server.shutdown();
- });
- describe('@jupyterlab/notebook', () => {
- describe('NotebookWidgetFactory', () => {
- let context: Context<INotebookModel>;
- beforeEach(async () => {
- context = await initNotebookContext();
- });
- afterEach(() => {
- context.dispose();
- });
- describe('#constructor()', () => {
- it('should create a notebook widget factory', () => {
- const factory = utils.createNotebookWidgetFactory();
- expect(factory).toBeInstanceOf(NotebookWidgetFactory);
- });
- });
- describe('#isDisposed', () => {
- it('should get whether the factory has been disposed', () => {
- const factory = utils.createNotebookWidgetFactory();
- expect(factory.isDisposed).toBe(false);
- factory.dispose();
- expect(factory.isDisposed).toBe(true);
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources held by the factory', () => {
- const factory = utils.createNotebookWidgetFactory();
- factory.dispose();
- expect(factory.isDisposed).toBe(true);
- });
- it('should be safe to call multiple times', () => {
- const factory = utils.createNotebookWidgetFactory();
- factory.dispose();
- factory.dispose();
- expect(factory.isDisposed).toBe(true);
- });
- });
- describe('#editorConfig', () => {
- it('should be the editor config passed into the constructor', () => {
- const factory = utils.createNotebookWidgetFactory();
- expect(factory.editorConfig).toBe(utils.defaultEditorConfig);
- });
- it('should be settable', () => {
- const factory = utils.createNotebookWidgetFactory();
- const newConfig = { ...utils.defaultEditorConfig };
- factory.editorConfig = newConfig;
- expect(factory.editorConfig).toBe(newConfig);
- });
- });
- describe('#createNew()', () => {
- it('should create a new `NotebookPanel` widget', () => {
- const factory = utils.createNotebookWidgetFactory();
- const panel = factory.createNew(context);
- expect(panel).toBeInstanceOf(NotebookPanel);
- });
- it('should create a clone of the rendermime', () => {
- const factory = utils.createNotebookWidgetFactory();
- const panel = factory.createNew(context);
- expect(panel.content.rendermime).not.toBe(rendermime);
- });
- it('should pass the editor config to the notebook', () => {
- const factory = utils.createNotebookWidgetFactory();
- const panel = factory.createNew(context);
- expect(panel.content.editorConfig).toBe(utils.defaultEditorConfig);
- });
- it('should populate the default toolbar items', () => {
- const factory = utils.createNotebookWidgetFactory();
- const panel = factory.createNew(context);
- const items = toArray(panel.toolbar.names());
- expect(items).toEqual(expect.arrayContaining(['save']));
- expect(items).toEqual(expect.arrayContaining(['restart']));
- });
- it('should populate the customized toolbar items', () => {
- const toolbarFactory = () => [
- { name: 'foo', widget: new ToolbarButton() },
- { name: 'bar', widget: new ToolbarButton() }
- ];
- const factory = utils.createNotebookWidgetFactory(toolbarFactory);
- const panel = factory.createNew(context);
- const panel2 = factory.createNew(context);
- expect(toArray(panel.toolbar.names())).toEqual([
- 'foo',
- 'bar',
- 'toolbar-popup-opener'
- ]);
- expect(toArray(panel2.toolbar.names())).toEqual([
- 'foo',
- 'bar',
- 'toolbar-popup-opener'
- ]);
- expect(toArray(panel.toolbar.children()).length).toBe(3);
- expect(toArray(panel2.toolbar.children()).length).toBe(3);
- });
- it('should clone from the optional source widget', () => {
- const factory = utils.createNotebookWidgetFactory();
- const panel = factory.createNew(context);
- const clone = factory.createNew(panel.context, panel);
- expect(clone).toBeInstanceOf(NotebookPanel);
- expect(clone.content.rendermime).toBe(panel.content.rendermime);
- expect(clone.content.editorConfig).toBe(panel.content.editorConfig);
- expect(clone.content.notebookConfig).toBe(panel.content.notebookConfig);
- });
- });
- });
- });
|