Explorar o código

Add document widget tests.

Jason Grout %!s(int64=7) %!d(string=hai) anos
pai
achega
ae8b29b27f

+ 1 - 0
tests/test-docregistry/package.json

@@ -20,6 +20,7 @@
     "@jupyterlab/rendermime": "^0.16.2",
     "@jupyterlab/services": "^2.0.2",
     "@phosphor/algorithm": "^1.1.2",
+    "@phosphor/coreutils": "^1.3.0",
     "@phosphor/disposable": "^1.1.2",
     "@phosphor/messaging": "^1.2.2",
     "@phosphor/widgets": "^1.5.0",

+ 86 - 1
tests/test-docregistry/src/default.spec.ts

@@ -3,19 +3,33 @@
 
 import expect = require('expect.js');
 
+import {
+  UUID
+} from '@phosphor/coreutils';
+
 import {
   Widget
 } from '@phosphor/widgets';
 
 import {
   ABCWidgetFactory, Base64ModelFactory, DocumentModel,
-  DocumentRegistry, DocumentWidget, IDocumentWidget, TextModelFactory
+  DocumentRegistry, DocumentWidget, IDocumentWidget, TextModelFactory, Context
 } from '@jupyterlab/docregistry';
 
+import {
+  ServiceManager
+} from '@jupyterlab/services';
+
 import {
   createFileContext
 } from '../../utils';
 
+function asyncTimer(time: number, value: any) {
+  return new Promise((resolve, reject) => {
+    setTimeout(resolve(value), time);
+  });
+}
+
 
 class WidgetFactory extends ABCWidgetFactory<IDocumentWidget> {
 
@@ -547,5 +561,76 @@ describe('docregistry/default', () => {
 
   });
 
+  describe.only('DocumentWidget', () => {
+
+    let manager: ServiceManager.IManager;
+
+    let context: Context<DocumentRegistry.IModel>;
+    let content: Widget;
+    let widget: DocumentWidget;
+
+    let setup = () => {
+      context = createFileContext(undefined, manager);
+      content = new Widget();
+      widget = new DocumentWidget({ context, content });
+    };
+
+    before(async () => {
+      manager = new ServiceManager();
+      await manager.ready;
+    });
+
+    describe('#constructor', () => {
+      beforeEach(setup);
+
+      it('should set the title for the path', () => {
+        expect(widget.title.label).to.be(context.localPath);
+      });
+
+      it('should update the title when the path changes', async () => {
+        let path = UUID.uuid4() + '.jl';
+        await context.initialize(true);
+        await manager.contents.rename(context.path, path);
+        expect(widget.title.label).to.be(path);
+      });
+
+      it('should add the dirty class when the model is dirty', async () => {
+        await context.initialize(true);
+        await context.ready;
+        context.model.fromString('bar');
+        expect(widget.title.className).to.contain('jp-mod-dirty');
+      });
+
+      it('should store the context', () => {
+        expect(widget.context).to.be(context);
+      });
+
+    });
+
+    describe('#ready', () => {
+      beforeEach(setup);
+
+      it('should resolve after the ready and context ready promises', async () => {
+        let x = Object.create(null);
+        let ready = asyncTimer(300, x);
+        let contextReady = Promise.all([context.ready, x]);
+        let widget = new DocumentWidget({ context, content, ready: ready});
+        expect(widget.isReady).to.be(false);
+
+        // Our promise should resolve before the widget ready promise.
+        expect(await Promise.race([widget.ready, ready])).to.be(x);
+        // The context ready promise should also resolve first.
+        context.initialize(true);
+        expect(await Promise.race([widget.ready, contextReady])).to.eql([undefined, x]);
+        // The widget.ready promise should finally resolve.
+        expect(await widget.ready).to.be(undefined);
+      });
+
+    });
+
+  });
+
+
+
 });
 

+ 3 - 26
tests/test-fileeditor/src/widget.spec.ts

@@ -28,7 +28,7 @@ import {
 } from '@jupyterlab/codemirror';
 
 import {
-  Context, DocumentRegistry, TextModelFactory
+  Context, DocumentRegistry, TextModelFactory, DocumentWidget
 } from '@jupyterlab/docregistry';
 
 import {
@@ -167,29 +167,6 @@ describe('fileeditorcodewrapper', () => {
         }).catch(done);
       });
 
-      it('should set the title for the path', () => {
-        expect(widget.title.label).to.be(context.path);
-      });
-
-      it('should update the title when the path changes', (done) => {
-        let path = uuid() + '.jl';
-        context.pathChanged.connect((sender, args) => {
-          expect(widget.title.label).to.be(path);
-          done();
-        });
-        context.initialize(true).then(() => {
-          return manager.contents.rename(context.path, path);
-        }).catch(done);
-      });
-
-      it('should add the dirty class when the model is dirty', (done) => {
-        context.initialize(true).catch(done);
-        context.ready.then(() => {
-          context.model.fromString('bar');
-          expect(widget.title.className).to.contain('jp-mod-dirty');
-        }).then(done, done);
-      });
-
     });
 
     describe('#context', () => {
@@ -295,8 +272,8 @@ describe('fileeditorcodewrapper', () => {
 
     describe('#createNewWidget()', () => {
 
-      it('should create an editor widget', () => {
-        expect(widgetFactory.createNew(context)).to.be.an(FileEditor);
+      it('should create a document widget', () => {
+        expect(widgetFactory.createNew(context)).to.be.a(DocumentWidget);
       });
 
     });