Browse Source

Fix tests expecting a code cell.

Ian Rose 6 years ago
parent
commit
b3e680ee88

+ 19 - 29
tests/test-notebook/src/model.spec.ts

@@ -13,11 +13,7 @@ import { NotebookModel } from '@jupyterlab/notebook';
 
 import { ModelDB } from '@jupyterlab/observables';
 
-import {
-  signalToPromise,
-  NBTestUtils,
-  acceptDialog
-} from '@jupyterlab/testutils';
+import { acceptDialog, NBTestUtils } from '@jupyterlab/testutils';
 
 describe('@jupyterlab/notebook', () => {
   describe('NotebookModel', () => {
@@ -35,12 +31,6 @@ describe('@jupyterlab/notebook', () => {
         expect(lang.name).to.equal('python');
       });
 
-      it('should add a single code cell by default', () => {
-        const model = new NotebookModel();
-        expect(model.cells.length).to.equal(1);
-        expect(model.cells.get(0)).to.be.an.instanceof(CodeCellModel);
-      });
-
       it('should accept an optional factory', () => {
         const contentFactory = new NotebookModel.ContentFactory({});
         const model = new NotebookModel({ contentFactory });
@@ -78,12 +68,6 @@ describe('@jupyterlab/notebook', () => {
     });
 
     describe('#cells', () => {
-      it('should add an empty code cell by default', () => {
-        const model = new NotebookModel();
-        expect(model.cells.length).to.equal(1);
-        expect(model.cells.get(0)).to.be.an.instanceof(CodeCellModel);
-      });
-
       it('should be reset when loading from disk', () => {
         const model = new NotebookModel();
         const cell = model.contentFactory.createCodeCell({});
@@ -100,7 +84,7 @@ describe('@jupyterlab/notebook', () => {
         model.cells.push(cell);
         model.fromJSON(NBTestUtils.DEFAULT_CONTENT);
         model.cells.undo();
-        expect(model.cells.length).to.equal(2);
+        expect(model.cells.length).to.equal(1);
         expect(model.cells.get(1).value.text).to.equal('foo');
         expect(model.cells.get(1)).to.equal(cell); // should be ===.
       });
@@ -149,17 +133,6 @@ describe('@jupyterlab/notebook', () => {
           model.cells.push(cell);
           expect(model.dirty).to.equal(true);
         });
-
-        it('should add a new code cell when cells are cleared', async () => {
-          const model = new NotebookModel();
-          let promise = signalToPromise(model.cells.changed);
-          model.cells.clear();
-          await promise;
-          expect(model.cells.length).to.equal(0);
-          await signalToPromise(model.cells.changed);
-          expect(model.cells.length).to.equal(1);
-          expect(model.cells.get(0)).to.be.an.instanceof(CodeCellModel);
-        });
       });
 
       describe('cell `changed` signal', () => {
@@ -393,6 +366,23 @@ describe('@jupyterlab/notebook', () => {
         });
       });
 
+      context('#createCell()', () => {
+        it('should create a new code cell', () => {
+          const cell = factory.createCell('code', {});
+          expect(cell.type).to.equal('code');
+        });
+
+        it('should create a new code cell', () => {
+          const cell = factory.createCell('markdown', {});
+          expect(cell.type).to.equal('markdown');
+        });
+
+        it('should create a new code cell', () => {
+          const cell = factory.createCell('raw', {});
+          expect(cell.type).to.equal('raw');
+        });
+      });
+
       context('#createCodeCell()', () => {
         it('should create a new code cell', () => {
           const cell = factory.createCodeCell({});

+ 0 - 7
tests/test-notebook/src/modelfactory.spec.ts

@@ -93,13 +93,6 @@ describe('@jupyterlab/notebook', () => {
         expect(model).to.be.an.instanceof(NotebookModel);
       });
 
-      it('should add an empty code cell by default', () => {
-        const factory = new NotebookModelFactory({});
-        const model = factory.createNew();
-        expect(model.cells.length).to.equal(1);
-        expect(model.cells.get(0).type).to.equal('code');
-      });
-
       it('should accept a language preference', () => {
         const factory = new NotebookModelFactory({});
         const model = factory.createNew('foo');

+ 39 - 1
tests/test-notebook/src/widget.spec.ts

@@ -23,7 +23,11 @@ import { INotebookModel, NotebookModel } from '@jupyterlab/notebook';
 
 import { Notebook, StaticNotebook } from '@jupyterlab/notebook';
 
-import { NBTestUtils, framePromise } from '@jupyterlab/testutils';
+import {
+  NBTestUtils,
+  framePromise,
+  signalToPromise
+} from '@jupyterlab/testutils';
 
 const contentFactory = NBTestUtils.createNotebookFactory();
 const editorConfig = NBTestUtils.defaultEditorConfig;
@@ -253,6 +257,25 @@ describe('@jupyter/notebook', () => {
         expect(widget.widgets.length).to.equal(6);
       });
 
+      it('should add a default cell if the notebook model is empty', () => {
+        const widget = new LogStaticNotebook(options);
+        const model1 = new NotebookModel();
+        expect(model1.cells.length).to.equal(0);
+        widget.model = model1;
+        expect(model1.cells.length).to.equal(1);
+        expect(model1.cells.get(0).type).to.equal('code');
+
+        widget.notebookConfig = {
+          ...widget.notebookConfig,
+          defaultCell: 'markdown'
+        };
+        const model2 = new NotebookModel();
+        expect(model2.cells.length).to.equal(0);
+        widget.model = model2;
+        expect(model2.cells.length).to.equal(1);
+        expect(model2.cells.get(0).type).to.equal('markdown');
+      });
+
       it('should set the mime types of the cell widgets', () => {
         const widget = new LogStaticNotebook(options);
         const model = new NotebookModel();
@@ -310,6 +333,21 @@ describe('@jupyter/notebook', () => {
           widget.model.cells.clear();
           expect(widget.widgets.length).to.equal(0);
         });
+
+        it('should add a new default cell when cells are cleared', async () => {
+          const model = widget.model;
+          widget.notebookConfig = {
+            ...widget.notebookConfig,
+            defaultCell: 'raw'
+          };
+          let promise = signalToPromise(model.cells.changed);
+          model.cells.clear();
+          await promise;
+          expect(model.cells.length).to.equal(0);
+          await signalToPromise(model.cells.changed);
+          expect(model.cells.length).to.equal(1);
+          expect(model.cells.get(0)).to.be.an.instanceof(RawCellModel);
+        });
       });
     });