123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import expect = require('expect.js');
- import {
- ArrayExt, toArray
- } from '@phosphor/algorithm';
- import {
- CodeCellModel
- } from '@jupyterlab/cells';
- import {
- nbformat
- } from '@jupyterlab/coreutils';
- import {
- NotebookModel
- } from '@jupyterlab/notebook';
- import {
- ModelDB
- } from '@jupyterlab/observables';
- import {
- DEFAULT_CONTENT
- } from '../../notebook-utils';
- import {
- moment
- } from '../../utils';
- describe('@jupyterlab/notebook', () => {
- describe('NotebookModel', () => {
- describe('#constructor()', () => {
- it('should create a notebook model', () => {
- let model = new NotebookModel();
- expect(model).to.be.a(NotebookModel);
- });
- it('should accept an optional language preference', () => {
- let model = new NotebookModel({ languagePreference: 'python' });
- let lang = model.metadata.get('language_info') as nbformat.ILanguageInfoMetadata;
- expect(lang.name).to.be('python');
- });
- it('should add a single code cell by default', () => {
- let model = new NotebookModel();
- expect(model.cells.length).to.be(1);
- expect(model.cells.get(0)).to.be.a(CodeCellModel);
- });
- it('should accept an optional factory', () => {
- let contentFactory = new NotebookModel.ContentFactory({});
- let model = new NotebookModel({ contentFactory });
- expect(model.contentFactory.codeCellContentFactory)
- .to.be(contentFactory.codeCellContentFactory);
- });
- });
- describe('#metadataChanged', () => {
- it('should be emitted when a metadata field changes', () => {
- let model = new NotebookModel();
- let called = false;
- model.metadata.changed.connect((sender, args) => {
- expect(sender).to.be(model.metadata);
- expect(args.key).to.be('foo');
- expect(args.oldValue).to.be(void 0);
- expect(args.newValue).to.be(1);
- called = true;
- });
- model.metadata.set('foo', 1);
- expect(called).to.be(true);
- });
- it('should not be emitted when the value does not change', () => {
- let model = new NotebookModel();
- let called = false;
- model.metadata.set('foo', 1);
- model.metadata.changed.connect(() => { called = true; });
- model.metadata.set('foo', 1);
- expect(called).to.be(false);
- });
- });
- describe('#cells', () => {
- it('should add an empty code cell by default', () => {
- let model = new NotebookModel();
- expect(model.cells.length).to.be(1);
- expect(model.cells.get(0)).to.be.a(CodeCellModel);
- });
- it('should be reset when loading from disk', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- model.cells.push(cell);
- model.fromJSON(DEFAULT_CONTENT);
- expect(ArrayExt.firstIndexOf(toArray(model.cells), cell)).to.be(-1);
- expect(model.cells.length).to.be(6);
- });
- it('should allow undoing a change', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- cell.value.text = 'foo';
- model.cells.push(cell);
- model.fromJSON(DEFAULT_CONTENT);
- model.cells.undo();
- expect(model.cells.length).to.be(2);
- expect(model.cells.get(1).value.text).to.be('foo');
- expect(model.cells.get(1)).to.be(cell); // should be ===.
- });
- context('cells `changed` signal', () => {
- it('should emit a `contentChanged` signal', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- let called = false;
- model.contentChanged.connect(() => { called = true; });
- model.cells.push(cell);
- expect(called).to.be(true);
- });
- it('should set the dirty flag', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- model.cells.push(cell);
- expect(model.dirty).to.be(true);
- });
- it('should add a new code cell when cells are cleared', async () => {
- let model = new NotebookModel();
- model.cells.clear();
- await moment();
- expect(model.cells.length).to.be(1);
- expect(model.cells.get(0)).to.be.a(CodeCellModel);
- });
- });
- describe('cell `changed` signal', () => {
- it('should be called when a cell content changes', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- model.cells.push(cell);
- cell.value.text = 'foo';
- });
- it('should emit the `contentChanged` signal', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- model.cells.push(cell);
- let called = false;
- model.contentChanged.connect(() => { called = true; });
- model.metadata.set('foo', 'bar');
- expect(called).to.be(true);
- });
- it('should set the dirty flag', () => {
- let model = new NotebookModel();
- let cell = model.contentFactory.createCodeCell({});
- model.cells.push(cell);
- model.dirty = false;
- cell.value.text = 'foo';
- expect(model.dirty).to.be(true);
- });
- });
- });
- describe('#contentFactory', () => {
- it('should be the cell model factory used by the model', () => {
- let model = new NotebookModel();
- expect(model.contentFactory.codeCellContentFactory)
- .to.be(NotebookModel.defaultContentFactory.codeCellContentFactory);
- });
- });
- describe('#nbformat', () => {
- it('should get the major version number of the nbformat', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- expect(model.nbformat).to.be(DEFAULT_CONTENT.nbformat);
- });
- });
- describe('#nbformatMinor', () => {
- it('should get the minor version number of the nbformat', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- expect(model.nbformatMinor).to.be(nbformat.MINOR_VERSION);
- });
- });
- describe('#defaultKernelName()', () => {
- it('should get the default kernel name of the document', () => {
- let model = new NotebookModel();
- model.metadata.set('kernelspec', { name: 'python3' });
- expect(model.defaultKernelName).to.be('python3');
- });
- it('should default to an empty string', () => {
- let model = new NotebookModel();
- expect(model.defaultKernelName).to.be('');
- });
- });
- describe('#defaultKernelLanguage', () => {
- it('should get the default kernel language of the document', () => {
- let model = new NotebookModel();
- model.metadata.set('language_info', { name: 'python' });
- expect(model.defaultKernelLanguage).to.be('python');
- });
- it('should default to an empty string', () => {
- let model = new NotebookModel();
- expect(model.defaultKernelLanguage).to.be('');
- });
- it('should be set from the constructor arg', () => {
- let model = new NotebookModel({ languagePreference: 'foo' });
- expect(model.defaultKernelLanguage).to.be('foo');
- });
- });
- describe('#dispose()', () => {
- it('should dispose of the resources held by the model', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- model.dispose();
- expect(model.cells).to.be(null);
- expect(model.isDisposed).to.be(true);
- });
- it('should be safe to call multiple times', () => {
- let model = new NotebookModel();
- model.dispose();
- model.dispose();
- expect(model.isDisposed).to.be(true);
- });
- });
- describe('#toString()', () => {
- it('should serialize the model to a string', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- let text = model.toString();
- let data = JSON.parse(text);
- expect(data.cells.length).to.be(6);
- });
- });
- describe('#fromString()', () => {
- it('should deserialize the model from a string', () => {
- let model = new NotebookModel();
- model.fromString(JSON.stringify(DEFAULT_CONTENT));
- expect(model.cells.length).to.be(6);
- });
- it('should set the dirty flag', () => {
- let model = new NotebookModel();
- model.dirty = false;
- model.fromString(JSON.stringify(DEFAULT_CONTENT));
- expect(model.dirty).to.be(true);
- });
- });
- describe('#toJSON()', () => {
- it('should serialize the model to JSON', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- let data = model.toJSON();
- expect(data.cells.length).to.be(6);
- });
- });
- describe('#fromJSON()', () => {
- it('should serialize the model from JSON', () => {
- let model = new NotebookModel();
- model.fromJSON(DEFAULT_CONTENT);
- expect(model.cells.length).to.be(6);
- expect(model.nbformat).to.be(DEFAULT_CONTENT.nbformat);
- expect(model.nbformatMinor).to.be(nbformat.MINOR_VERSION);
- });
- it('should set the dirty flag', () => {
- let model = new NotebookModel();
- model.dirty = false;
- model.fromJSON(DEFAULT_CONTENT);
- expect(model.dirty).to.be(true);
- });
- });
- describe('#metadata', () => {
- it('should have default values', () => {
- let model = new NotebookModel();
- let metadata = model.metadata;
- expect(metadata.has('kernelspec'));
- expect(metadata.has('language_info'));
- expect(metadata.size).to.be(2);
- });
- it('should set the dirty flag when changed', () => {
- let model = new NotebookModel();
- expect(model.dirty).to.be(false);
- model.metadata.set('foo', 'bar');
- expect(model.dirty).to.be(true);
- });
- it('should emit the `contentChanged` signal', () => {
- let model = new NotebookModel();
- let called = false;
- model.contentChanged.connect(() => { called = true; });
- model.metadata.set('foo', 'bar');
- expect(called).to.be(true);
- });
- it('should emit the `metadataChanged` signal', () => {
- let model = new NotebookModel();
- let called = false;
- model.metadata.changed.connect((sender, args) => {
- expect(sender).to.be(model.metadata);
- expect(args.key).to.be('foo');
- expect(args.oldValue).to.be(void 0);
- expect(args.newValue).to.be('bar');
- called = true;
- });
- model.metadata.set('foo', 'bar');
- expect(called).to.be(true);
- });
- });
- describe('.ContentFactory', () => {
- let factory = new NotebookModel.ContentFactory({});
- context('#codeCellContentFactory', () => {
- it('should be a code cell content factory', () => {
- expect(factory.codeCellContentFactory).to.be(CodeCellModel.defaultContentFactory);
- });
- it('should be settable in the constructor', () => {
- let codeCellContentFactory = new CodeCellModel.ContentFactory();
- factory = new NotebookModel.ContentFactory({ codeCellContentFactory });
- expect(factory.codeCellContentFactory).to.be(codeCellContentFactory);
- });
- });
- context('#createCodeCell()', () => {
- it('should create a new code cell', () => {
- let cell = factory.createCodeCell({});
- expect(cell.type).to.be('code');
- });
- it('should clone an existing code cell', () => {
- let orig = factory.createCodeCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createCodeCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- it('should clone an existing raw cell', () => {
- let orig = factory.createRawCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createCodeCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- });
- context('#createRawCell()', () => {
- it('should create a new raw cell', () => {
- let cell = factory.createRawCell({});
- expect(cell.type).to.be('raw');
- });
- it('should clone an existing raw cell', () => {
- let orig = factory.createRawCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createRawCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- it('should clone an existing code cell', () => {
- let orig = factory.createCodeCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createRawCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- });
- describe('#createMarkdownCell()', () => {
- it('should create a new markdown cell', () => {
- let cell = factory.createMarkdownCell({});
- expect(cell.type).to.be('markdown');
- });
- it('should clone an existing markdown cell', () => {
- let orig = factory.createMarkdownCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createMarkdownCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- it('should clone an existing raw cell', () => {
- let orig = factory.createRawCell({});
- orig.value.text = 'foo';
- let cell = orig.toJSON();
- let newCell = factory.createMarkdownCell({ cell });
- expect(newCell.value.text).to.be('foo');
- });
- });
- describe('#modelDB', () => {
- it('should be undefined by default', () => {
- expect(factory.modelDB).to.be(undefined);
- });
- });
- describe('#clone()', () => {
- it('should create a new content factory with a new IModelDB', () => {
- let modelDB = new ModelDB();
- let factory = new NotebookModel.ContentFactory({ modelDB });
- expect(factory.modelDB).to.be(modelDB);
- let newModelDB = new ModelDB();
- let newFactory = factory.clone(newModelDB);
- expect(newFactory.modelDB).to.be(newModelDB);
- expect(newFactory.codeCellContentFactory)
- .to.be(factory.codeCellContentFactory);
- });
- });
- });
- describe('.defaultContentFactory', () => {
- it('should be a ContentFactory', () => {
- expect(NotebookModel.defaultContentFactory).to.be.a(NotebookModel.ContentFactory);
- });
- });
- });
- });
|