Browse Source

Fix more tests, convert many to use async/await.

Jason Grout 7 years ago
parent
commit
ba2802fa8c

+ 9 - 14
tests/test-notebook/src/actions.spec.ts

@@ -28,7 +28,7 @@ import {
 } from '@jupyterlab/notebook';
 
 import {
-  acceptDialog, createClientSession, dismissDialog
+  acceptDialog, createClientSession, dismissDialog, moment
 } from '../../utils';
 
 import {
@@ -277,17 +277,14 @@ describe('@jupyterlab/notebook', () => {
         expect(widget.activeCellIndex).to.be(widget.widgets.length - 1);
       });
 
-      it('should add a code cell if all cells are deleted', (done) => {
+      it('should add a code cell if all cells are deleted', async () => {
         for (let i = 0; i < widget.widgets.length; i++) {
           widget.select(widget.widgets[i]);
         }
         NotebookActions.deleteCells(widget);
-        requestAnimationFrame(() => {
-          expect(widget.widgets.length).to.be(1);
-          expect(widget.activeCell).to.be.a(CodeCell);
-          done();
-        });
-
+        await moment();
+        expect(widget.widgets.length).to.be(1);
+        expect(widget.activeCell).to.be.a(CodeCell);
       });
 
       it('should be undo-able', () => {
@@ -1106,16 +1103,14 @@ describe('@jupyterlab/notebook', () => {
         expect(widget.widgets[0].model.value.text).to.be(source);
       });
 
-      it('should add a new code cell if all cells were cut', (done) => {
+      it('should add a new code cell if all cells were cut', async () => {
         for (let i = 0; i < widget.widgets.length; i++) {
           widget.select(widget.widgets[i]);
         }
         NotebookActions.cut(widget);
-        requestAnimationFrame(() => {
-          expect(widget.widgets.length).to.be(1);
-          expect(widget.activeCell).to.be.a(CodeCell);
-          done();
-        });
+        await moment();
+        expect(widget.widgets.length).to.be(1);
+        expect(widget.activeCell).to.be.a(CodeCell);
       });
 
     });

+ 2 - 2
tests/test-notebook/src/celltools.spec.ts

@@ -32,7 +32,7 @@ import {
 } from '../../notebook-utils';
 
 import {
-  createNotebookContext
+  createNotebookContext, moment
 } from '../../utils';
 
 class LogTool extends CellTools.Tool {
@@ -123,7 +123,7 @@ describe('@jupyterlab/notebook', () => {
       tabpanel.node.style.height = '800px';
       Widget.attach(tabpanel, document.body);
       // Give the posted messages a chance to be handled.
-      await undefined;
+      await moment();
     });
 
     afterEach(() => {

+ 31 - 18
tests/test-notebook/src/default-toolbar.spec.ts

@@ -7,6 +7,11 @@ import {
   toArray
 } from '@phosphor/algorithm';
 
+
+import {
+  PromiseDelegate
+} from '@phosphor/coreutils';
+
 import {
   Widget
 } from '@phosphor/widgets';
@@ -36,7 +41,7 @@ import {
 } from '@jupyterlab/notebook';
 
 import {
-  createNotebookContext
+  createNotebookContext, moment
 } from '../../utils';
 
 import {
@@ -150,7 +155,7 @@ describe('@jupyterlab/notebook', () => {
         Widget.attach(button, document.body);
         NotebookActions.copy(panel.content);
         button.node.click();
-        await 0;
+        await moment();
         expect(panel.content.widgets.length).to.be(count + 1);
         button.dispose();
       });
@@ -164,26 +169,34 @@ describe('@jupyterlab/notebook', () => {
 
     describe('#createRunButton()', () => {
 
-      it.skip('should run and advance when clicked', (done) => {
+      it('should run and advance when clicked', async () => {
         let button = ToolbarItems.createRunButton(panel);
         let widget = panel.content;
-        let next = widget.widgets[1] as MarkdownCell;
-        widget.select(next);
-        let cell = widget.activeCell as CodeCell;
-        cell.model.outputs.clear();
-        next.rendered = false;
+
+        // Clear and select the first two cells.
+        const codeCell = widget.widgets[0] as CodeCell;
+        codeCell.model.outputs.clear();
+        widget.select(codeCell);
+        const mdCell = widget.widgets[1] as MarkdownCell;
+        mdCell.rendered = false;
+        widget.select(mdCell);
+
         Widget.attach(button, document.body);
-        const session = panel.context.session;
-        panel.context.session.kernel.ready.then(() => {
-          session.statusChanged.connect((sender, status) => {
-            if (status === 'idle' && cell.model.outputs.length > 0) {
-              expect(next.rendered).to.be(true);
-              button.dispose();
-              done();
-            }
-          });
-          button.node.click();
+        await context.ready;
+        await context.session.ready;
+        await context.session.kernel.ready;
+        const p = new PromiseDelegate();
+        context.session.statusChanged.connect((sender, status) => {
+          // Find the right status idle message
+          if (status === 'idle' && codeCell.model.outputs.length > 0) {
+            expect(mdCell.rendered).to.be(true);
+            expect(widget.activeCellIndex).to.equal(2);
+            button.dispose();
+            p.resolve(0);
+          }
         });
+        button.node.click();
+        await p.promise;
       });
 
       it('should have the `\'jp-RunIcon\'` class', () => {

+ 0 - 13
tests/test-notebook/src/panel.spec.ts

@@ -3,10 +3,6 @@
 
 import expect = require('expect.js');
 
-import {
-  ServiceManager
-} from '@jupyterlab/services';
-
 import {
   Context
 } from '@jupyterlab/docregistry';
@@ -33,15 +29,6 @@ import {
  * Default data.
  */
 const contentFactory = createNotebookPanelFactory();
-const options = { rendermime, mimeTypeService, contentFactory };
-
-
-function createPanel(context: Context<INotebookModel>): NotebookPanel {
-  const panel = createNotebookPanel(context);
-  context.model.fromJSON(DEFAULT_CONTENT);
-  return panel;
-}
-
 
 describe('@jupyterlab/notebook', () => {
 

+ 1 - 1
tests/test-notebook/src/tracker.spec.ts

@@ -39,7 +39,7 @@ class TestTracker extends NotebookTracker {
 
 describe('@jupyterlab/notebook', () => {
 
-  describe.only('NotebookTracker', () => {
+  describe('NotebookTracker', () => {
 
     let context: Context<INotebookModel>;
 

+ 77 - 106
tests/test-notebook/src/widget.spec.ts

@@ -32,6 +32,7 @@ import {
   DEFAULT_CONTENT, createNotebookFactory, rendermime, mimeTypeService,
   editorFactory, defaultEditorConfig
 } from '../../notebook-utils';
+import { moment } from '../../utils';
 
 
 const contentFactory = createNotebookFactory();
@@ -152,7 +153,7 @@ function selected(nb: Notebook): number[] {
 }
 
 
-describe('notebook/widget', () => {
+describe('@jupyter/notebook', () => {
 
   describe('StaticNotebook', () => {
 
@@ -284,14 +285,11 @@ describe('notebook/widget', () => {
           widget.dispose();
         });
 
-        it('should handle changes to the model cell list', (done) => {
+        it('should handle changes to the model cell list', async () => {
           widget = createWidget();
           widget.model.cells.clear();
-          // The model should add a single code cell.
-          requestAnimationFrame(() => {
-            expect(widget.widgets.length).to.be(1);
-            done();
-          });
+          await moment();
+          expect(widget.widgets.length).to.be(1);
         });
 
         it('should handle a remove', () => {
@@ -667,28 +665,24 @@ describe('notebook/widget', () => {
         expect(called).to.be(false);
       });
 
-      it('should post an update request', (done) => {
+      it('should post an update request', async () => {
         let widget = createActiveWidget();
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onUpdateRequest');
-          done();
-        });
         widget.mode = 'edit';
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
       });
 
-      it('should deselect all cells if switching to edit mode', (done) => {
+      it('should deselect all cells if switching to edit mode', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
-        requestAnimationFrame(() => {
-          widget.extendContiguousSelectionTo(widget.widgets.length - 1);
-          let selectedRange = Array.from(Array(widget.widgets.length).keys());
-          expect(selected(widget)).to.eql(selectedRange);
-          widget.mode = 'edit';
-          expect(selected(widget)).to.eql([]);
-          widget.dispose();
-          done();
-        });
+        await moment();
+        widget.extendContiguousSelectionTo(widget.widgets.length - 1);
+        let selectedRange = Array.from(Array(widget.widgets.length).keys());
+        expect(selected(widget)).to.eql(selectedRange);
+        widget.mode = 'edit';
+        expect(selected(widget)).to.eql([]);
+        widget.dispose();
       });
 
       it('should unrender a markdown cell when switching to edit mode', () => {
@@ -753,13 +747,11 @@ describe('notebook/widget', () => {
         expect(called).to.be(false);
       });
 
-      it('should post an update request', (done) => {
+      it('should post an update request', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onUpdateRequest');
-          done();
-        });
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
         widget.activeCellIndex = 1;
       });
 
@@ -1127,11 +1119,11 @@ describe('notebook/widget', () => {
 
       let widget: LogNotebook;
 
-      beforeEach((done) => {
+      beforeEach(async () => {
         widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
-        requestAnimationFrame(() => { done(); });
+        await moment();
       });
 
       afterEach(() => {
@@ -1313,87 +1305,76 @@ describe('notebook/widget', () => {
 
     describe('#onAfterAttach()', () => {
 
-      it('should add event listeners', (done) => {
+      it('should add event listeners', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
         let child = widget.widgets[0];
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onAfterAttach');
-          simulate(widget.node, 'mousedown');
-          expect(widget.events).to.contain('mousedown');
-          simulate(widget.node, 'dblclick');
-          expect(widget.events).to.contain('dblclick');
-          simulate(child.node, 'focusin');
-          expect(widget.events).to.contain('focusin');
-          widget.dispose();
-          done();
-        });
+        await moment();
+        expect(widget.methods).to.contain('onAfterAttach');
+        simulate(widget.node, 'mousedown');
+        expect(widget.events).to.contain('mousedown');
+        simulate(widget.node, 'dblclick');
+        expect(widget.events).to.contain('dblclick');
+        simulate(child.node, 'focusin');
+        expect(widget.events).to.contain('focusin');
+        widget.dispose();
       });
 
-      it('should post an update request', (done) => {
+      it('should post an update request', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onAfterAttach');
-          requestAnimationFrame(() => {
-            expect(widget.methods).to.contain('onUpdateRequest');
-            widget.dispose();
-            done();
-          });
-        });
+        await moment();
+        expect(widget.methods).to.contain('onAfterAttach');
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
+        widget.dispose();
       });
 
     });
 
     describe('#onBeforeDetach()', () => {
 
-      it('should remove event listeners', (done) => {
+      it('should remove event listeners', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
         let child = widget.widgets[0];
-        requestAnimationFrame(() => {
-          Widget.detach(widget);
-          expect(widget.methods).to.contain('onBeforeDetach');
-          widget.events = [];
-          simulate(widget.node, 'mousedown');
-          expect(widget.events).to.not.contain('mousedown');
-          simulate(widget.node, 'dblclick');
-          expect(widget.events).to.not.contain('dblclick');
-          simulate(child.node, 'focusin');
-          expect(widget.events).to.not.contain('focusin');
-          widget.dispose();
-          done();
-        });
+        await moment();
+        Widget.detach(widget);
+        expect(widget.methods).to.contain('onBeforeDetach');
+        widget.events = [];
+        simulate(widget.node, 'mousedown');
+        expect(widget.events).to.not.contain('mousedown');
+        simulate(widget.node, 'dblclick');
+        expect(widget.events).to.not.contain('dblclick');
+        simulate(child.node, 'focusin');
+        expect(widget.events).to.not.contain('focusin');
+        widget.dispose();
       });
 
     });
 
     describe('#onActivateRequest()', () => {
 
-      it('should focus the node after an update', (done) => {
+      it('should focus the node after an update', async () => {
         let widget = createActiveWidget();
         Widget.attach(widget, document.body);
         MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
         expect(widget.methods).to.contain('onActivateRequest');
-        requestAnimationFrame(() => {
-          expect(document.activeElement).to.be(widget.node);
-          widget.dispose();
-          done();
-        });
+        await moment();
+        expect(document.activeElement).to.be(widget.node);
+        widget.dispose();
       });
 
-      it('should post an `update-request', (done) => {
+      it('should post an `update-request', async () => {
         let widget = createActiveWidget();
         MessageLoop.sendMessage(widget, Widget.Msg.ActivateRequest);
         expect(widget.methods).to.contain('onActivateRequest');
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onUpdateRequest');
-          widget.dispose();
-          done();
-        });
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
+        widget.dispose();
       });
 
     });
@@ -1402,11 +1383,11 @@ describe('notebook/widget', () => {
 
       let widget: LogNotebook;
 
-      beforeEach((done) => {
+      beforeEach(async () => {
         widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         Widget.attach(widget, document.body);
-        requestAnimationFrame(() => {  done(); });
+        await moment();
       });
 
       afterEach(() => {
@@ -1418,12 +1399,10 @@ describe('notebook/widget', () => {
         expect(widget.hasClass('jp-mod-commandMode')).to.be(true);
       });
 
-      it('should apply the edit class if in edit mode', (done) => {
+      it('should apply the edit class if in edit mode', async () => {
         widget.mode = 'edit';
-        requestAnimationFrame(() => {
-          expect(widget.hasClass('jp-mod-editMode')).to.be(true);
-          done();
-        });
+        await moment();
+        expect(widget.hasClass('jp-mod-editMode')).to.be(true);
       });
 
       it('should add the active class to the active widget', () => {
@@ -1431,38 +1410,32 @@ describe('notebook/widget', () => {
         expect(cell.hasClass('jp-mod-active')).to.be(true);
       });
 
-      it('should set the selected class on the selected widgets', (done) => {
+      it('should set the selected class on the selected widgets', async () => {
         widget.select(widget.widgets[1]);
-        requestAnimationFrame(() => {
-          for (let i = 0; i < 2; i++) {
-            let cell = widget.widgets[i];
-            expect(cell.hasClass('jp-mod-selected')).to.be(true);
-            done();
-          }
-        });
+        await moment();
+        for (let i = 0; i < 2; i++) {
+          let cell = widget.widgets[i];
+          expect(cell.hasClass('jp-mod-selected')).to.be(true);
+        }
       });
 
-      it('should add the multi select class if there is more than one widget', (done) => {
+      it('should add the multi select class if there is more than one widget', async () => {
         widget.select(widget.widgets[1]);
         expect(widget.hasClass('jp-mod-multSelected')).to.be(false);
-        requestAnimationFrame(() => {
-          expect(widget.hasClass('jp-mod-multSelected')).to.be(false);
-          done();
-        });
+        await moment();
+        expect(widget.hasClass('jp-mod-multSelected')).to.be(false);
       });
 
     });
 
     describe('#onCellInserted()', () => {
 
-      it('should post an `update-request', (done) => {
+      it('should post an `update-request', async () => {
         let widget = createActiveWidget();
         widget.model.fromJSON(DEFAULT_CONTENT);
         expect(widget.methods).to.contain('onCellInserted');
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onUpdateRequest');
-          done();
-        });
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
       });
 
       it('should update the active cell if necessary', () => {
@@ -1536,15 +1509,13 @@ describe('notebook/widget', () => {
 
     describe('#onCellRemoved()', () => {
 
-      it('should post an `update-request', (done) => {
+      it('should post an `update-request', async () => {
         let widget = createActiveWidget();
         let cell = widget.model.cells.get(0);
         widget.model.cells.removeValue(cell);
         expect(widget.methods).to.contain('onCellRemoved');
-        requestAnimationFrame(() => {
-          expect(widget.methods).to.contain('onUpdateRequest');
-          done();
-        });
+        await moment();
+        expect(widget.methods).to.contain('onUpdateRequest');
       });
 
       it('should update the active cell if necessary', () => {

+ 9 - 12
tests/test-notebook/src/widgetfactory.spec.ts

@@ -20,7 +20,7 @@ import {
 } from '@jupyterlab/notebook';
 
 import {
-  Context, DocumentWidget
+  Context
 } from '@jupyterlab/docregistry';
 
 import {
@@ -47,23 +47,20 @@ function createFactory(): NotebookWidgetFactory {
 }
 
 
-describe('notebook/notebook/widgetfactory', () => {
+describe('@jupyterlab/notebook', () => {
 
-  let context: Context<INotebookModel>;
+  describe('NotebookWidgetFactory', () => {
+
+    let context: Context<INotebookModel>;
 
-  beforeEach(() => {
-    return createNotebookContext().then(c => {
-      context = c;
+    beforeEach(async () => {
+      context = await createNotebookContext();
     });
-  });
 
-  afterEach(() => {
-    return context.session.shutdown().then(() => {
+    afterEach(async () => {
+      await context.session.shutdown();
       context.dispose();
     });
-  });
-
-  describe('NotebookWidgetFactory', () => {
 
     describe('#constructor()', () => {