Browse Source

wip default-toolbar tests

Steven Silvester 8 years ago
parent
commit
d9596418e5

+ 9 - 0
test/src/docmanager/mockcontext.ts

@@ -29,6 +29,8 @@ import {
 export
 class MockContext<T extends IDocumentModel> implements IDocumentContext<T> {
 
+  methods: string[] = [];
+
   constructor(model: T) {
     this._model = model;
   }
@@ -84,33 +86,40 @@ class MockContext<T extends IDocumentModel> implements IDocumentContext<T> {
   dispose(): void {
     this._model.dispose();
     this._model = null;
+    this.methods.push('dispose');
   }
 
   changeKernel(options: IKernel.IModel): Promise<IKernel> {
     this._kernel = new MockKernel(options);
     this.kernelChanged.emit(this._kernel);
+    this.methods.push('changeKernel');
     return Promise.resolve(this._kernel);
   }
 
   save(): Promise<void> {
+    this.methods.push('save');
     return Promise.resolve(void 0);
   }
 
   saveAs(path: string): Promise<void> {
     this._path = path;
     this.pathChanged.emit(path);
+    this.methods.push('saveAs');
     return Promise.resolve(void 0);
   }
 
   revert(): Promise<void> {
+    this.methods.push('revert');
     return Promise.resolve(void 0);
   }
 
   listSessions(): Promise<ISession.IModel[]> {
+    this.methods.push('listSessions');
     return Promise.resolve([] as ISession.IModel[]);
   }
 
   addSibling(widget: Widget): IDisposable {
+    this.methods.push('addSibling');
     return void 0;
   }
 

+ 153 - 1
test/src/notebook/notebook/default-toolbar.spec.ts

@@ -4,44 +4,196 @@
 import expect = require('expect.js');
 
 import {
- TooolbarItems
+  MimeData
+} from 'phosphor-dragdrop';
+
+import {
+ CodeCellWidget
+} from '../../../../lib/notebook/cells/widget';
+
+import {
+  JUPYTER_CELL_MIME, NotebookActions
+} from '../../../../lib/notebook/notebook/actions';
+
+import {
+ ToolbarItems
+} from '../../../../lib/notebook/notebook/default-toolbar';
+
+import {
+ NotebookModel
 } from '../../../../lib/notebook/notebook/model';
 
+import {
+ NotebookPanel
+} from '../../../../lib/notebook/notebook/panel';
+
+import {
+  MockContext
+} from '../../docmanager/mockcontext';
+
+import {
+  defaultRenderMime
+} from '../../rendermime/rendermime.spec';
+
+import {
+  DEFAULT_CONTENT
+} from '../utils';
+
+
+/**
+ * Default data.
+ */
+const rendermime = defaultRenderMime();
+const clipboard = new MimeData();
+
 
 describe('notebook/notebook/default-toolbar', () => {
 
   describe('TooolbarItems', () => {
 
+    let panel: NotebookPanel;
+    let context: MockContext<NotebookModel>;
+
+    beforeEach(() => {
+      panel = new NotebookPanel({ rendermime, clipboard });
+      let model = new NotebookModel();
+      model.fromJSON(DEFAULT_CONTENT);
+      context = new MockContext<NotebookModel>(model);
+      context.changeKernel({ name: 'python' });
+      panel.context = context;
+      return panel;
+    });
+
+    afterEach(() => {
+      panel.dispose();
+    });
+
     describe('#createSaveButton()', () => {
 
+      it('should save when clicked', () => {
+        let button = ToolbarItems.createSaveButton(panel);
+        button.attach(document.body);
+        button.node.click();
+        expect(context.methods).to.contain('save');
+        button.dispose();
+      });
+
+      it("should have the `'jp-NBToolbar-save'` class", () => {
+        let button = ToolbarItems.createSaveButton(panel);
+        expect(button.hasClass('jp-NBToolbar-save')).to.be(true);
+      });
+
     });
 
     describe('#createInsertButton()', () => {
 
+      it('should insert below when clicked', () => {
+        let button = ToolbarItems.createInsertButton(panel);
+        button.attach(document.body);
+        button.node.click();
+        expect(panel.content.activeCellIndex).to.be(1);
+        expect(panel.content.activeCell).to.be.a(CodeCellWidget);
+        button.dispose();
+      });
+
+      it("should have the `'jp-NBToolbar-insert'` class", () => {
+        let button = ToolbarItems.createInsertButton(panel);
+        expect(button.hasClass('jp-NBToolbar-insert')).to.be(true);
+      });
+
     });
 
     describe('#createCutButton()', () => {
 
+      it('should cut when clicked', () => {
+        let button = ToolbarItems.createCutButton(panel);
+        let count = panel.content.childCount();
+        button.attach(document.body);
+        button.node.click();
+        expect(panel.content.childCount()).to.be(count - 1);
+        expect(clipboard.hasData(JUPYTER_CELL_MIME)).to.be(true);
+        button.dispose();
+      });
+
+      it("should have the `'jp-NBToolbar-cut'` class", () => {
+        let button = ToolbarItems.createCutButton(panel);
+        expect(button.hasClass('jp-NBToolbar-cut')).to.be(true);
+      });
+
     });
 
     describe('#createCopyButton()', () => {
 
+      it('should copy when clicked', () => {
+        let button = ToolbarItems.createCopyButton(panel);
+        let count = panel.content.childCount();
+        button.attach(document.body);
+        button.node.click();
+        expect(panel.content.childCount()).to.be(count);
+        expect(clipboard.hasData(JUPYTER_CELL_MIME)).to.be(true);
+        button.dispose();
+      });
+
+      it("should have the `'jp-NBToolbar-copy'` class", () => {
+        let button = ToolbarItems.createCopyButton(panel);
+        expect(button.hasClass('jp-NBToolbar-copy')).to.be(true);
+      });
+
     });
 
     describe('#createPasteButton()', () => {
 
+      it('should paste when clicked', () => {
+        let button = ToolbarItems.createPasteButton(panel);
+        let count = panel.content.childCount();
+        button.attach(document.body);
+        NotebookActions.copy(panel.content, clipboard);
+        button.node.click();
+        expect(panel.content.childCount()).to.be(count + 1);
+        button.dispose();
+      });
+
+      it("should have the `'jp-NBToolbar-paste'` class", () => {
+        let button = ToolbarItems.createPasteButton(panel);
+        expect(button.hasClass('jp-NBToolbar-paste')).to.be(true);
+      });
+
     });
 
     describe('#createRunButton()', () => {
 
+      it('should run and advance when clicked', () => {
+
+      });
+
+      it("should have the `'jp-NBToolbar-run'` class", () => {
+
+      });
+
     });
 
     describe('#createInterruptButton()', () => {
 
+      it('should interrupt the kernel when clicked', () => {
+
+      });
+
+      it("should have the `'jp-NBToolbar-interrupt'` class", () => {
+
+      });
+
     });
 
     describe('#createRestartButton()', () => {
 
+      it('should restart the kernel when clicked', () => {
+
+      });
+
+      it("should have the `'jp-NBToolbar-restart'` class", () => {
+
+      });
+
     });
 
     describe('#createCellTypeItem()', () => {

+ 4 - 5
test/src/notebook/notebook/panel.spec.ts

@@ -35,10 +35,6 @@ import {
   INotebookModel, NotebookModel
 } from '../../../../lib/notebook/notebook/model';
 
-import {
-  nbformat
-} from '../../../../lib/notebook/notebook/nbformat';
-
 import {
   NotebookPanel
 } from '../../../../lib/notebook/notebook/panel';
@@ -59,13 +55,16 @@ import {
   defaultRenderMime
 } from '../../rendermime/rendermime.spec';
 
+import {
+  DEFAULT_CONTENT
+} from '../utils';
+
 
 /**
  * Default data.
  */
 const rendermime = defaultRenderMime();
 const clipboard = new MimeData();
-const DEFAULT_CONTENT: nbformat.INotebookContent = require('../../../../examples/notebook/test.ipynb') as nbformat.INotebookContent;
 
 
 class LogNotebookPanel extends NotebookPanel {