Browse Source

Finish docmanager defaults tests

Steven Silvester 8 years ago
parent
commit
0580b019e5
2 changed files with 190 additions and 3 deletions
  1. 2 2
      src/docmanager/default.ts
  2. 188 1
      test/src/docmanager/default.spec.ts

+ 2 - 2
src/docmanager/default.ts

@@ -175,7 +175,7 @@ class DocumentModel implements IDocumentModel {
 export
 class TextModelFactory implements IModelFactory {
   /**
-   * The name of the model.
+   * The name of the model type.
    *
    * #### Notes
    * This is a read-only property.
@@ -239,7 +239,7 @@ class TextModelFactory implements IModelFactory {
 export
 class Base64ModelFactory extends TextModelFactory {
   /**
-   * The name of the model.
+   * The name of the model type.
    *
    * #### Notes
    * This is a read-only property.

+ 188 - 1
test/src/docmanager/default.spec.ts

@@ -98,7 +98,7 @@ describe('docmanager/default', () => {
 
     describe('#name', () => {
 
-      it('should get the name of the factory', () => {
+      it('should get the name of the model type', () => {
         let factory = new Base64ModelFactory();
         expect(factory.name).to.be('base64');
       });
@@ -211,42 +211,165 @@ describe('docmanager/default', () => {
 
     describe('#dirty', () => {
 
+      it('should get the dirty state of the document', () => {
+        let model = new DocumentModel();
+        expect(model.dirty).to.be(false);
+      });
+
+      it('should emit `stateChanged` when changed', () => {
+        let model = new DocumentModel();
+        let called = false;
+        model.stateChanged.connect((sender, args) => {
+          expect(sender).to.be(model);
+          expect(args.name).to.be('dirty');
+          expect(args.oldValue).to.be(false);
+          expect(args.newValue).to.be(true);
+          called = true;
+        });
+        model.dirty = true;
+        expect(called).to.be(true);
+      });
+
+      it('should not emit `stateChanged` when not changed', () => {
+        let model = new DocumentModel();
+        let called = false;
+        model.stateChanged.connect(() => { called = true; });
+        model.dirty = false;
+        expect(called).to.be(false);
+      });
+
     });
 
     describe('#readOnly', () => {
 
+      it('should get the read only state of the document', () => {
+        let model = new DocumentModel();
+        expect(model.readOnly).to.be(false);
+      });
+
+      it('should emit `stateChanged` when changed', () => {
+        let model = new DocumentModel();
+        let called = false;
+        model.stateChanged.connect((sender, args) => {
+          expect(sender).to.be(model);
+          expect(args.name).to.be('readOnly');
+          expect(args.oldValue).to.be(false);
+          expect(args.newValue).to.be(true);
+          called = true;
+        });
+        model.readOnly = true;
+        expect(called).to.be(true);
+      });
+
+      it('should not emit `stateChanged` when not changed', () => {
+        let model = new DocumentModel();
+        let called = false;
+        model.stateChanged.connect(() => { called = true; });
+        model.readOnly = false;
+        expect(called).to.be(false);
+      });
+
     });
 
     describe('#defaultKernelName', () => {
 
+      it('should get the default kernel name of the document', () => {
+        let model = new DocumentModel();
+        expect(model.defaultKernelName).to.be('');
+      });
+
+      it('should be read-only', () => {
+        let model = new DocumentModel();
+        expect(() => { model.defaultKernelName = ''; }).to.throwError();
+      });
+
     });
 
     describe('defaultKernelLanguage', () => {
 
+      it('should get the default kernel langauge of the document', () => {
+        let model = new DocumentModel();
+        expect(model.defaultKernelLanguage).to.be('');
+      });
+
+      it('should be set by the constructor arg', () => {
+        let model = new DocumentModel('foo');
+        expect(model.defaultKernelLanguage).to.be('foo');
+      });
+
+      it('should be read-only', () => {
+        let model = new DocumentModel();
+        expect(() => { model.defaultKernelLanguage = ''; }).to.throwError();
+      });
+
     });
 
     describe('#dispose()', () => {
 
+      it('should dispose of the resources held by the document manager', () => {
+        let model = new DocumentModel();
+        model.dispose();
+        expect(model.isDisposed).to.be(true);
+      });
+
+      it('should be safe to call more than once', () => {
+        let model = new DocumentModel();
+        model.dispose();
+        model.dispose();
+        expect(model.isDisposed).to.be(true);
+      });
+
     });
 
     describe('#toString()', () => {
 
+      it('should serialize the model to a string', () => {
+        let model = new DocumentModel();
+        expect(model.toString()).to.be('');
+      });
+
     });
 
     describe('#fromString()', () => {
 
+      it('should deserialize the model from a string', () => {
+        let model = new DocumentModel();
+        model.fromString('foo');
+        expect(model.toString()).to.be('foo');
+      });
+
     });
 
     describe('#toJSON()', () => {
 
+      it('should serialize the model to JSON', () => {
+        let model = new DocumentModel();
+        expect(model.toJSON()).to.be('""');
+      });
+
     });
 
     describe('#fromJSON()', () => {
 
+      it('should deserialize the model from JSON', () => {
+        let model = new DocumentModel();
+        model.fromJSON('"foo"');
+        expect(model.toString()).to.be('foo');
+      });
+
     });
 
     describe('#initialize()', () => {
 
+      it('should clear the dirty flag', () => {
+        let model = new DocumentModel();
+        model.fromString('foo');
+        expect(model.dirty).to.be(true);
+        model.initialize();
+        expect(model.dirty).to.be(false);
+        expect(model.toString()).to.be('foo');
+      });
+
     });
 
   });
@@ -255,26 +378,90 @@ describe('docmanager/default', () => {
 
     describe('#name', () => {
 
+      it('should get the name of the model type', () => {
+        let factory = new TextModelFactory();
+        expect(factory.name).to.be('text');
+      });
+
+      it('should be read-only', () => {
+        let factory = new TextModelFactory();
+        expect(() => { factory.name = ''; }).to.throwError();
+      });
+
     });
 
     describe('#contentsOptions', () => {
 
+      it('should get the contents options used to fetch/save files', () => {
+        let factory = new TextModelFactory();
+        let options = { type: 'file', format: 'text' };
+        expect(factory.contentsOptions).to.eql(options);
+      });
+
+      it('should be read-only', () => {
+        let factory = new TextModelFactory();
+        expect(() => { factory.contentsOptions = null; }).to.throwError();
+      });
+
     });
 
     describe('#isDisposed', () => {
 
+      it('should get whether the factory is disposed', () => {
+        let factory = new TextModelFactory();
+        expect(factory.isDisposed).to.be(false);
+        factory.dispose();
+        expect(factory.isDisposed).to.be(true);
+      });
+
+      it('should be read-only', () => {
+        let factory = new TextModelFactory();
+        expect(() => { factory.isDisposed = false; }).to.throwError();
+      });
+
     });
 
     describe('#dispose()', () => {
 
+      it('should dispose of the resources held by the factory', () => {
+        let factory = new TextModelFactory();
+        factory.dispose();
+        expect(factory.isDisposed).to.be(true);
+      });
+
+      it('should be safe to call multiple times', () => {
+        let factory = new TextModelFactory();
+        factory.dispose();
+        factory.dispose();
+        expect(factory.isDisposed).to.be(true);
+      });
+
     });
 
     describe('#createNew()', () => {
 
+      it('should create a new model', () => {
+        let factory = new TextModelFactory();
+        let model = factory.createNew();
+        expect(model).to.be.a(DocumentModel);
+      });
+
+      it('should accept a language preference', () => {
+        let factory = new TextModelFactory();
+        let model = factory.createNew('foo');
+        expect(model.defaultKernelLanguage).to.be('foo');
+      });
+
     });
 
     describe('#preferredLanguage()', () => {
 
+      it('should get the preferred kernel language given an extension', () => {
+        let factory = new TextModelFactory();
+        expect(factory.preferredLanguage('.py')).to.be('python');
+        expect(factory.preferredLanguage('.jl')).to.be('julia');
+      });
+
     });
 
   });