123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import { CodeMirrorEditorFactory } from '@jupyterlab/codemirror';
- import { Context } from '@jupyterlab/docregistry';
- import { ObservableJSON } from '@jupyterlab/observables';
- import { initNotebookContext, sleep } from '@jupyterlab/testutils';
- import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
- import { Message } from '@lumino/messaging';
- import { TabPanel, Widget } from '@lumino/widgets';
- import { simulate } from 'simulate-event';
- import {
- INotebookModel,
- NotebookActions,
- NotebookPanel,
- NotebookTools,
- NotebookTracker
- } from '..';
- import * as utils from './utils';
- class LogTool extends NotebookTools.Tool {
- methods: string[] = [];
- protected onActiveNotebookPanelChanged(msg: Message): void {
- super.onActiveNotebookPanelChanged(msg);
- this.methods.push('onActiveNotebookPanelChanged');
- }
- protected onActiveCellChanged(msg: Message): void {
- super.onActiveCellChanged(msg);
- this.methods.push('onActiveCellChanged');
- }
- protected onSelectionChanged(msg: Message): void {
- super.onSelectionChanged(msg);
- this.methods.push('onSelectionChanged');
- }
- protected onActiveCellMetadataChanged(
- msg: ObservableJSON.ChangeMessage
- ): void {
- super.onActiveCellMetadataChanged(msg);
- this.methods.push('onActiveCellMetadataChanged');
- }
- protected onActiveNotebookPanelMetadataChanged(
- msg: ObservableJSON.ChangeMessage
- ): void {
- super.onActiveNotebookPanelMetadataChanged(msg);
- this.methods.push('onActiveNotebookPanelMetadataChanged');
- }
- }
- class LogKeySelector extends NotebookTools.KeySelector {
- events: string[] = [];
- methods: string[] = [];
- handleEvent(event: Event): void {
- super.handleEvent(event);
- this.events.push(event.type);
- }
- protected onAfterAttach(message: Message): void {
- super.onAfterAttach(message);
- this.methods.push('onAfterAttach');
- }
- protected onBeforeDetach(message: Message): void {
- super.onBeforeDetach(message);
- this.methods.push('onBeforeDetach');
- }
- protected onActiveCellChanged(message: Message): void {
- super.onActiveCellChanged(message);
- this.methods.push('onActiveCellChanged');
- }
- protected onActiveCellMetadataChanged(
- message: ObservableJSON.ChangeMessage
- ): void {
- super.onActiveCellMetadataChanged(message);
- this.methods.push('onActiveCellMetadataChanged');
- }
- protected onValueChanged(): void {
- super.onValueChanged();
- this.methods.push('onValueChanged');
- }
- }
- const server = new JupyterServer();
- beforeAll(async () => {
- jest.setTimeout(20000);
- await server.start();
- });
- afterAll(async () => {
- await server.shutdown();
- });
- describe('@jupyterlab/notebook', () => {
- describe('notebooktools', () => {
- let notebookTools: NotebookTools;
- let tabpanel: TabPanel;
- let tracker: NotebookTracker;
- let context0: Context<INotebookModel>;
- let context1: Context<INotebookModel>;
- let panel0: NotebookPanel;
- let panel1: NotebookPanel;
- beforeEach(async () => {
- context0 = await initNotebookContext();
- panel0 = utils.createNotebookPanel(context0);
- utils.populateNotebook(panel0.content);
- context1 = await initNotebookContext();
- panel1 = utils.createNotebookPanel(context1);
- utils.populateNotebook(panel1.content);
- tracker = new NotebookTracker({ namespace: 'notebook' });
- await tracker.add(panel0);
- await tracker.add(panel1);
- notebookTools = new NotebookTools({ tracker });
- tabpanel = new TabPanel();
- tabpanel.addWidget(panel0);
- tabpanel.addWidget(panel1);
- tabpanel.addWidget(notebookTools);
- tabpanel.node.style.height = '800px';
- Widget.attach(tabpanel, document.body);
- // Give the posted messages a chance to be handled.
- await sleep();
- });
- afterEach(() => {
- tabpanel.dispose();
- notebookTools.dispose();
- panel1.dispose();
- panel0.dispose();
- context1.dispose();
- context0.dispose();
- });
- describe('NotebookTools', () => {
- describe('#constructor()', () => {
- it('should create a notebooktools object', () => {
- expect(notebookTools).toBeInstanceOf(NotebookTools);
- });
- });
- describe('#activeNotebookPanel', () => {
- it('should be the active notebook', () => {
- expect(notebookTools.activeNotebookPanel).toBe(panel1);
- tabpanel.currentIndex = 0;
- simulate(panel0.node, 'focus');
- expect(notebookTools.activeNotebookPanel).toBe(panel0);
- });
- });
- describe('#activeCell', () => {
- it('should be the active cell', () => {
- expect(notebookTools.activeCell).toBe(panel1.content.activeCell);
- tabpanel.currentIndex = 0;
- simulate(panel0.node, 'focus');
- expect(notebookTools.activeCell).toBe(panel0.content.activeCell);
- });
- });
- describe('#selectedCells', () => {
- it('should be the currently selected cells', () => {
- expect(notebookTools.selectedCells).toEqual([
- panel1.content.activeCell
- ]);
- tabpanel.currentIndex = 0;
- simulate(panel0.node, 'focus');
- expect(notebookTools.selectedCells).toEqual([
- panel0.content.activeCell
- ]);
- panel0.content.select(panel0.content.widgets[1]);
- expect(notebookTools.selectedCells.length).toBe(2);
- });
- });
- describe('#addItem()', () => {
- it('should add a cell tool item', () => {
- const tool = new NotebookTools.Tool();
- notebookTools.addItem({ tool });
- tool.dispose();
- });
- it('should accept a rank', () => {
- const tool = new NotebookTools.Tool();
- notebookTools.addItem({ tool, rank: 100 });
- tool.dispose();
- });
- });
- });
- describe('NotebookTools.Tool', () => {
- describe('#constructor', () => {
- it('should create a new base tool', () => {
- const tool = new NotebookTools.Tool();
- expect(tool).toBeInstanceOf(NotebookTools.Tool);
- });
- });
- describe('#parent', () => {
- it('should be the notebooktools object used by the tool', () => {
- const tool = new NotebookTools.Tool({});
- notebookTools.addItem({ tool });
- expect(tool.notebookTools).toBe(notebookTools);
- });
- });
- describe('#onActiveNotebookPanelChanged()', () => {
- it('should be called when the active notebook panel changes', () => {
- const tool = new LogTool({});
- notebookTools.addItem({ tool });
- tool.methods = [];
- simulate(panel0.node, 'focus');
- expect(tool.methods).toContain('onActiveNotebookPanelChanged');
- });
- });
- describe('#onActiveCellChanged()', () => {
- it('should be called when the active cell changes', () => {
- const tool = new LogTool({});
- notebookTools.addItem({ tool });
- tool.methods = [];
- simulate(panel0.node, 'focus');
- expect(tool.methods).toContain('onActiveCellChanged');
- });
- });
- describe('#onSelectionChanged()', () => {
- it('should be called when the selection changes', () => {
- const tool = new LogTool({});
- notebookTools.addItem({ tool });
- tool.methods = [];
- const current = tracker.currentWidget!;
- current.content.select(current.content.widgets[1]);
- expect(tool.methods).toContain('onSelectionChanged');
- });
- });
- describe('#onActiveCellMetadataChanged()', () => {
- it('should be called when the active cell metadata changes', () => {
- const tool = new LogTool({});
- notebookTools.addItem({ tool });
- tool.methods = [];
- const metadata = notebookTools.activeCell!.model.metadata;
- metadata.set('foo', 1);
- metadata.set('foo', 2);
- expect(tool.methods).toContain('onActiveCellMetadataChanged');
- });
- });
- describe('#onActiveNotebookPanelMetadataChanged()', () => {
- it('should be called when the active notebook panel metadata changes', () => {
- const tool = new LogTool({});
- notebookTools.addItem({ tool });
- tool.methods = [];
- const metadata = notebookTools.activeNotebookPanel!.model!.metadata;
- metadata.set('foo', 1);
- metadata.set('foo', 2);
- expect(tool.methods).toContain(
- 'onActiveNotebookPanelMetadataChanged'
- );
- });
- });
- });
- describe('NotebookTools.ActiveCellTool', () => {
- it('should create a new active cell tool', () => {
- const tool = new NotebookTools.ActiveCellTool();
- notebookTools.addItem({ tool });
- expect(tool).toBeInstanceOf(NotebookTools.ActiveCellTool);
- });
- it('should handle a change to the active cell', () => {
- const tool = new NotebookTools.ActiveCellTool();
- notebookTools.addItem({ tool });
- const widget = tracker.currentWidget!;
- widget.content.activeCellIndex++;
- widget.content.activeCell!.model.metadata.set('bar', 1);
- expect(tool.node.querySelector('.jp-InputArea-editor')).toBeTruthy();
- });
- });
- describe('NotebookTools.CellMetadataEditorTool', () => {
- const editorServices = new CodeMirrorEditorFactory();
- const editorFactory = editorServices.newInlineEditor.bind(editorServices);
- it('should create a new metadata editor tool', () => {
- const tool = new NotebookTools.CellMetadataEditorTool({
- editorFactory
- });
- expect(tool).toBeInstanceOf(NotebookTools.CellMetadataEditorTool);
- });
- it('should handle a change to the active cell', () => {
- const tool = new NotebookTools.CellMetadataEditorTool({
- editorFactory
- });
- notebookTools.addItem({ tool });
- const model = tool.editor.model;
- expect(JSON.stringify(model.value.text)).toBeTruthy();
- const widget = tracker.currentWidget!;
- widget.content.activeCellIndex++;
- widget.content.activeCell!.model.metadata.set('bar', 1);
- expect(JSON.stringify(model.value.text)).toContain('bar');
- });
- it('should handle a change to the metadata', () => {
- const tool = new NotebookTools.CellMetadataEditorTool({
- editorFactory
- });
- notebookTools.addItem({ tool });
- const model = tool.editor.model;
- const previous = model.value.text;
- const metadata = notebookTools.activeCell!.model.metadata;
- metadata.set('foo', 1);
- expect(model.value.text).not.toBe(previous);
- });
- });
- describe('NotebookTools.NotebookMetadataEditorTool', () => {
- const editorServices = new CodeMirrorEditorFactory();
- const editorFactory = editorServices.newInlineEditor.bind(editorServices);
- it('should create a new metadata editor tool', () => {
- const tool = new NotebookTools.NotebookMetadataEditorTool({
- editorFactory
- });
- expect(tool).toBeInstanceOf(NotebookTools.NotebookMetadataEditorTool);
- });
- it('should handle a change to the active notebook', () => {
- panel0.model!.metadata.set('panel0', 1);
- panel1.model!.metadata.set('panel1', 1);
- const tool = new NotebookTools.NotebookMetadataEditorTool({
- editorFactory
- });
- notebookTools.addItem({ tool });
- const model = tool.editor.model;
- expect(JSON.stringify(model.value.text)).toBeTruthy();
- simulate(panel0.node, 'focus');
- expect(JSON.stringify(model.value.text)).toContain('panel0');
- expect(JSON.stringify(model.value.text)).not.toContain('panel1');
- simulate(panel1.node, 'focus');
- expect(JSON.stringify(model.value.text)).not.toContain('panel0');
- expect(JSON.stringify(model.value.text)).toContain('panel1');
- });
- it('should handle a change to the metadata', () => {
- const tool = new NotebookTools.NotebookMetadataEditorTool({
- editorFactory
- });
- notebookTools.addItem({ tool });
- const model = tool.editor.model;
- const widget = tracker.currentWidget!;
- expect(JSON.stringify(model.value.text)).not.toContain('newvalue');
- widget.content.model!.metadata.set('newvalue', 1);
- expect(JSON.stringify(model.value.text)).toContain('newvalue');
- });
- });
- describe('NotebookTools.KeySelector', () => {
- let tool: LogKeySelector;
- beforeEach(() => {
- tool = new LogKeySelector({
- key: 'foo',
- title: 'Foo',
- optionValueArray: [
- ['bar', 1],
- ['baz', [1, 2, 'a']]
- ]
- });
- notebookTools.addItem({ tool });
- simulate(panel0.node, 'focus');
- tabpanel.currentIndex = 2;
- });
- afterEach(() => {
- tool.dispose();
- });
- describe('#constructor()', () => {
- it('should create a new key selector', () => {
- expect(tool).toBeInstanceOf(NotebookTools.KeySelector);
- });
- });
- describe('#key', () => {
- it('should be the key used by the selector', () => {
- expect(tool.key).toBe('foo');
- });
- });
- describe('#selectNode', () => {
- it('should be the select node', () => {
- expect(tool.selectNode.localName).toBe('select');
- });
- });
- describe('#handleEvent()', () => {
- describe('change', () => {
- it('should update the metadata', () => {
- const select = tool.selectNode;
- simulate(select, 'focus');
- select.selectedIndex = 1;
- simulate(select, 'change');
- expect(tool.events).toContain('change');
- const metadata = notebookTools.activeCell!.model.metadata;
- expect(metadata.get('foo')).toEqual([1, 2, 'a']);
- });
- });
- describe('focus', () => {
- it('should add the focused class to the wrapper node', () => {
- const select = tool.selectNode;
- simulate(select, 'focus');
- const selector = '.jp-mod-focused';
- expect(tool.node.querySelector(selector)).toBeTruthy();
- });
- });
- describe('blur', () => {
- it('should remove the focused class from the wrapper node', () => {
- const select = tool.selectNode;
- simulate(select, 'focus');
- simulate(select, 'blur');
- const selector = '.jp-mod-focused';
- expect(tool.node.querySelector(selector)).toBeFalsy();
- });
- });
- });
- describe('#onAfterAttach()', () => {
- it('should add event listeners', () => {
- const select = tool.selectNode;
- expect(tool.methods).toContain('onAfterAttach');
- simulate(select, 'focus');
- simulate(select, 'blur');
- select.selectedIndex = 0;
- simulate(select, 'change');
- expect(tool.events).toEqual(['change']);
- });
- });
- describe('#onBeforeDetach()', () => {
- it('should remove event listeners', () => {
- const select = tool.selectNode;
- notebookTools.dispose();
- expect(tool.methods).toContain('onBeforeDetach');
- simulate(select, 'focus');
- simulate(select, 'blur');
- simulate(select, 'change');
- expect(tool.events).toEqual([]);
- });
- });
- describe('#onValueChanged()', () => {
- it('should update the metadata', () => {
- const select = tool.selectNode;
- simulate(select, 'focus');
- select.selectedIndex = 1;
- simulate(select, 'change');
- expect(tool.methods).toContain('onValueChanged');
- const metadata = notebookTools.activeCell!.model.metadata;
- expect(metadata.get('foo')).toEqual([1, 2, 'a']);
- });
- });
- describe('#onActiveCellChanged()', () => {
- it('should update the select value', () => {
- const cell = panel0.content.model!.cells.get(1);
- cell.metadata.set('foo', 1);
- panel0.content.activeCellIndex = 1;
- expect(tool.methods).toContain('onActiveCellChanged');
- expect(tool.selectNode.value).toBe('1');
- });
- });
- describe('#onActiveCellMetadataChanged()', () => {
- it('should update the select value', () => {
- const metadata = notebookTools.activeCell!.model.metadata;
- metadata.set('foo', 1);
- expect(tool.methods).toContain('onActiveCellMetadataChanged');
- expect(tool.selectNode.value).toBe('1');
- });
- });
- });
- describe('NotebookTools.createSlideShowSelector()', () => {
- it('should create a slide show selector', () => {
- const tool = NotebookTools.createSlideShowSelector();
- tool.selectNode.selectedIndex = -1;
- notebookTools.addItem({ tool });
- simulate(panel0.node, 'focus');
- tabpanel.currentIndex = 2;
- expect(tool).toBeInstanceOf(NotebookTools.KeySelector);
- expect(tool.key).toBe('slideshow');
- const select = tool.selectNode;
- expect(select.value).toBe('');
- const metadata = notebookTools.activeCell!.model.metadata;
- expect(metadata.get('slideshow')).toBeUndefined();
- simulate(select, 'focus');
- tool.selectNode.selectedIndex = 1;
- simulate(select, 'change');
- expect(metadata.get('slideshow')).toEqual({
- slide_type: 'slide'
- });
- });
- });
- describe('NotebookTools.createNBConvertSelector()', () => {
- it('should create a raw mimetype selector', () => {
- const optionValueArray: any = [
- [null, '-'],
- ['LaTeX', 'text/latex'],
- ['reST', 'text/restructuredtext'],
- ['HTML', 'text/html'],
- ['Markdown', 'text/markdown'],
- ['Python', 'text/x-python']
- ];
- optionValueArray.push(['None', '-']);
- const tool = NotebookTools.createNBConvertSelector(optionValueArray);
- tool.selectNode.selectedIndex = -1;
- notebookTools.addItem({ tool });
- simulate(panel0.node, 'focus');
- NotebookActions.changeCellType(panel0.content, 'raw');
- tabpanel.currentIndex = 2;
- expect(tool).toBeInstanceOf(NotebookTools.KeySelector);
- expect(tool.key).toBe('raw_mimetype');
- const select = tool.selectNode;
- expect(select.value).toBe('');
- const metadata = notebookTools.activeCell!.model.metadata;
- expect(metadata.get('raw_mimetype')).toBeUndefined();
- simulate(select, 'focus');
- tool.selectNode.selectedIndex = 2;
- simulate(select, 'change');
- expect(metadata.get('raw_mimetype')).toBe('text/restructuredtext');
- });
- it('should have no effect on a code cell', () => {
- const optionValueArray: any = [
- ['None', '-'],
- ['LaTeX', 'text/latex'],
- ['reST', 'text/restructuredtext'],
- ['HTML', 'text/html'],
- ['Markdown', 'text/markdown'],
- ['Python', 'text/x-python']
- ];
- const tool = NotebookTools.createNBConvertSelector(optionValueArray);
- tool.selectNode.selectedIndex = -1;
- notebookTools.addItem({ tool });
- simulate(panel0.node, 'focus');
- NotebookActions.changeCellType(panel0.content, 'code');
- tabpanel.currentIndex = 2;
- expect(tool).toBeInstanceOf(NotebookTools.KeySelector);
- expect(tool.key).toBe('raw_mimetype');
- const select = tool.selectNode;
- expect(select.disabled).toBe(true);
- expect(select.value).toBe('');
- });
- });
- });
- });
|