Michelle Bu 12 gadi atpakaļ
vecāks
revīzija
b38918dd0f
6 mainītis faili ar 216 papildinājumiem un 146 dzēšanām
  1. 24 12
      test/connectionmanager.js
  2. 74 54
      test/dataconnection.js
  3. 10 1
      test/peer.js
  4. 18 9
      test/socket.js
  5. 1 1
      test/test.html
  6. 89 69
      test/util.js

+ 24 - 12
test/connectionmanager.js

@@ -1,29 +1,41 @@
 describe('ConnectionManager', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
-  it('_setupDataChannel');
+  describe('#_setupDataChannel', function() {
+  });
 
-  it('_makeOffer');
+  describe('#_makeOffer', function() {
+  });
 
-  it('_makeAnswer');
+  describe('#_makeAnswer', function() {
+  });
 
-  it('_cleanup');
+  describe('#_cleanup', function() {
+  });
 
-  it('_attachConnectionListeners');
+  describe('#_attachConnectionListeners', function() {
+  });
 
-  it('handleSDP');
+  describe('#handleSDP', function() {
+  });
 
-  it('handleCandidate');
+  describe('#handleCandidate', function() {
+  });
 
-  it('handleLeave');
+  describe('#handleLeave', function() {
+  });
 
-  it('close');
+  describe('#close', function() {
+  });
 
-  it('connect');
+  describe('#connect', function() {
+  });
 
-  it('update');
+  describe('#update', function() {
+  });
 
 });

+ 74 - 54
test/dataconnection.js

@@ -15,88 +15,108 @@ describe('DataConnection', function() {
       },
       // Only sends to peer's dc.
       send: function(msg) {
-        pdc._dc.onmessage({ data: msg });
+        pdc._handleDataMessage({ data: msg });
       }
     };
 
-    it('constructor', function() {
-      // Test without 'new' keyword.
-      dc = DataConnection('peer', null,
-        { serialization: 'json',
-          metadata: { message: 'I\'m testing!'},
-          reliable: true });
+    describe('constructor', function() {
+      it('should accept options properly', function() {
+        // Test without 'new' keyword.
+        dc = DataConnection('peer', null,
+          { serialization: 'json',
+            metadata: { message: 'I\'m testing!'} });
 
-      expect(dc.peer).to.be('peer');
-      expect(dc.serialization).to.be('json');
-      expect(dc.metadata.message).to.be('I\'m testing!');
+        expect(dc.peer).to.be('peer');
+        expect(dc.serialization).to.be('json');
+        expect(dc.metadata.message).to.be('I\'m testing!');
 
-      expect(dc._dc).to.be(null);
-      dc._dc = new DataChannelStub();
+        expect(dc._dc).to.be(null);
+        dc._dc = new DataChannelStub();
+      });
     });
 
     it('inherits from EventEmitter');
 
-    it('_configureDataChannel', function() {
-      dc._configureDataChannel();
-
-      if (util.browserisms === 'Firefox') {
-        expect(dc._dc.binaryType).to.be('arraybuffer');
-      } else {
-        expect(dc._reliable).not.to.be(undefined);
-      }
+    before(function() {
+      dc = DataConnection('peer', null,
+        { serialization: 'json',
+          metadata: { message: 'I\'m testing!'} });
+      dc._dc = new DataChannelStub();
     });
 
-    it('should fire an `open` event', function(done) {
-      dc.on('open', function() {
-        expect(dc.open).to.be(true)
-        done();
+    describe('#_configureDataChannel', function() {
+      it('should set the correct binaryType', function() {
+        dc._configureDataChannel();
+
+        if (util.browserisms === 'Firefox') {
+          expect(dc._dc.binaryType).to.be('arraybuffer');
+        } else {
+          expect(dc._reliable).to.be(undefined);
+        }
+      });
+
+      it('should fire an `open` event', function(done) {
+        dc.on('open', function() {
+          expect(dc.open).to.be(true)
+          done();
+        });
+        dc._dc.onopen();
       });
-      dc._dc.onopen();
     });
 
-    it('_handleDataMessage', function() {
-       
+    describe('#_handleDataMessage', function() {
+
     });
 
-    it('addDC', function() {
-      pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
-      pdc.addDC(new DataChannelStub());
+    describe('#addDC', function() {
+      it('should add a DataConnection properly', function() {
+        pdc = new DataConnection('ignore', null, { serialization: 'json', reliable: true });
+        pdc.addDC(new DataChannelStub());
 
-      expect(pdc._dc).not.to.be(null);
+        expect(pdc._dc).not.to.be(null);
+      });
     });
 
-    it('send', function(done) {
-      pdc.on('data', function(data) {
-        expect(data.hello).to.be('peer-tester');
-        done();
+    describe('#send', function() {
+      it('should send data to the peer', function(done) {
+        pdc = new DataConnection('ignore', null, { serialization: 'json' });
+        pdc.on('data', function(data) {
+          expect(data.hello).to.be('peer-tester');
+          done();
+        });
+        dc.send({ hello: 'peer-tester' });
       });
-      dc.send({ hello: 'peer-tester' });
     });
 
-    it('_cleanup', function(done) {
-      var first = true;
-      dc.on('close', function() {
-        expect(dc.open).to.be(false)
+    describe('#_cleanup', function() {
+      it('should emit a `close` event', function(done) {
+        var first = true;
+        dc.on('close', function() {
+          expect(dc.open).to.be(false)
 
-        // Calling it twice should be fine.
-        if (first) {
-          first = false;
-          dc._cleanup();
-        }
+          // Calling it twice should be fine.
+          if (first) {
+            first = false;
+            dc._cleanup();
+          }
 
-        done();
-      });
+          done();
+        });
 
-      dc._cleanup();
+        dc._cleanup();
+      });
     });
 
-    it('close', function() {
-      dc._cleanup = function() {
-        throw Error();
-      }
+    // Hacks hacks
+    describe('#close', function() {
+      it('should not call _cleanup', function() {
+        dc._cleanup = function() {
+          throw Error();
+        }
 
-      // Should not call _cleanup again.
-      dc.close();
+        // Should not call _cleanup again.
+        dc.close();
+      });
     });
 
 

+ 10 - 1
test/peer.js

@@ -1,7 +1,16 @@
 describe('Peer', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
+  describe('.browser', function() {
+    it('should be the current browser', function() {
+      var browser = window.mozRTCPeerConnection ? 'Firefox' : 'Unknown';
+      browser = window.webkitRTCPeerConnection ? 'Webkit' : browser;
+      expect(Peer.browser).to.be(browser);
+    });
+  });
+
 });

+ 18 - 9
test/socket.js

@@ -1,23 +1,32 @@
 describe('Socket', function() {
 
-  it('constructor');
+  describe('constructor', function() {
+  });
 
   it('inherits from EventEmitter');
 
-  it('start');
+  describe('#start', function() {
+  });
 
-  it('_startWebSocket');
+  describe('#_startWebSocket', function() {
+  });
 
-  it('_startXhrStream');
+  describe('#_startXhrStream', function() {
+  });
 
-  it('_handleStream');
+  describe('#_handleStream', function() {
+  });
 
-  it('_setHTTPTimeout');
+  describe('#_setHTTPTimeout', function() {
+  });
 
-  it('send');
+  describe('#send', function() {
+  });
 
-  it('close');
+  describe('#close', function() {
+  });
 
-  it('_wsOpen');
+  describe('#_wsOpen', function() {
+  });
 
 });

+ 1 - 1
test/test.html

@@ -15,9 +15,9 @@
   <script src="../lib/util.js"></script>
   <script src="../lib/adapter.js"></script>
   <script src="../lib/dataconnection.js"></script>
-  <script src="../lib/connectionmanager.js"></script>
   <script src="../lib/peer.js"></script>
   <script src="../lib/socket.js"></script>
+  <script src="../lib/connectionmanager.js"></script>
   <script src="../deps/reliable/lib/reliable.js"></script>
 
   <script src="../node_modules/expect.js/expect.js"></script>

+ 89 - 69
test/util.js

@@ -11,92 +11,112 @@ describe('util', function() {
     }
   }
 
-  it('inherits', function() {
-    function ctor() {}
-    function superCtor() {}
-    superCtor.prototype.test = function() { return 5; }
-    util.inherits(ctor, superCtor);
-    expect(new ctor()).to.be.a(superCtor);
-    expect(new ctor().test()).to.be.equal(5);
-  })
+  describe('.inherits', function() {
+    it('should make functions inherit properly', function() {
+      function ctor() {}
+      function superCtor() {}
+      superCtor.prototype.test = function() { return 5; }
+      util.inherits(ctor, superCtor);
+      expect(new ctor()).to.be.a(superCtor);
+      expect(new ctor().test()).to.be.equal(5);
+    });
+  });
 
   /*
    *  extend overwrites keys if already exists
    *  leaves existing keys alone otherwise
    */
-  it('extend', function() {
-    var a = {a: 1, b: 2, c: 3, d: 4}
-      , b = {d: 2};
-    util.extend(b, a);
-    expect(b).to.eql(a);
-    expect(b.d).to.be.equal(4);
-    b = {z: 2};
-    util.extend(b, a);
-    expect(b.z).to.be.equal(2);
-  })
+  describe('.extend', function() {
+    it('should copy the properties of b to a', function() {
+      var a = {a: 1, b: 2, c: 3, d: 4}
+        , b = {d: 2};
+      util.extend(b, a);
+      expect(b).to.eql(a);
+      expect(b.d).to.be.equal(4);
+      b = {z: 2};
+      util.extend(b, a);
+      expect(b.z).to.be.equal(2);
+    });
+  });
 
-  it('pack', function() {
-    expect(util.pack).to.be.equal(BinaryPack.pack);
-  })
+  describe('.pack', function() {
+    it('should be BinaryPack\'s `pack` function', function() {
+      expect(util.pack).to.be.equal(BinaryPack.pack);
+    });
+  });
 
-  it('unpack', function() {
-    expect(util.unpack).to.be.equal(BinaryPack.unpack);
-  })
+  describe('.unpack', function() {
+    it('should be BinaryPack\'s `unpack` function', function() {
+      expect(util.unpack).to.be.equal(BinaryPack.unpack);
+    });
+  });
 
   // FF no like
-  it('log', function(done) {
-    var consolelog = console.log;
-    // default is false
-    expect(util.debug).to.be.equal(false);
-    util.debug = true;
-    console.log = function() {
-      var arg = Array.prototype.slice.call(arguments);
-      expect(arg.join(' ')).to.be.equal('PeerJS:  hi');
-      done();
-    }
-    util.log('hi');
-    // reset
-    console.log = consolelog;
-    util.debug = false;
-  })
-
-  it('setZeroTimeout', function(done) {
-    var isdone = false;
-    util.setZeroTimeout(function() {
-      if (isdone) {
+  describe('.log', function() {
+    it('should log with the PeerJS prefix', function(done) {
+      var consolelog = console.log;
+      // default is false
+      expect(util.debug).to.be.equal(false);
+      util.debug = true;
+      console.log = function() {
+        var arg = Array.prototype.slice.call(arguments);
+        expect(arg.join(' ')).to.be.equal('PeerJS:  hi');
         done();
       }
+      util.log('hi');
+      // reset
+      console.log = consolelog;
+      util.debug = false;
     });
-    isdone = true;
-  })
+  });
 
-  it('blobToArrayBuffer', function(done) {
-    var blob = new Blob(['hi']);
-    util.blobToArrayBuffer(blob, function(result) {
-      expect(result.byteLength).to.be.equal(2);
-      expect(result.slice).to.be.a('function');
-      expect(result instanceof ArrayBuffer).to.be.equal(true);
-      done();
+  describe('.setZeroTimeout', function() {
+    it('should call the function after a 0s timeout', function(done) {
+      var isdone = false;
+      util.setZeroTimeout(function() {
+        if (isdone) {
+          done();
+        }
+      });
+      isdone = true;
     });
-  })
+  });
 
-  it('blobToBinaryString', function(done) {
-    var blob = new Blob(['hi']);
-    util.blobToBinaryString(blob, function(result) {
-      expect(result).to.equal('hi');
-      done();
+  describe('.blobToArrayBuffer', function() {
+    it('should convert a blob to an arraybuffer', function(done) {
+      var blob = new Blob(['hi']);
+      util.blobToArrayBuffer(blob, function(result) {
+        expect(result.byteLength).to.be.equal(2);
+        expect(result.slice).to.be.a('function');
+        expect(result instanceof ArrayBuffer).to.be.equal(true);
+        done();
+      });
+    });
+  });
+
+  describe('blobToBinaryString', function() {
+    it('should convert a blob to a binary string', function(done) {
+      var blob = new Blob(['hi']);
+      util.blobToBinaryString(blob, function(result) {
+        expect(result).to.equal('hi');
+        done();
+      });
     });
-  })
+  });
 
-  it('binaryStringToArrayBuffer', function() {
-    var ba = util.binaryStringToArrayBuffer('\0\0');
-    expect(ba.byteLength).to.be.equal(2);
-    expect(ba.slice).to.be.a('function');
-    expect(ba instanceof ArrayBuffer).to.be.equal(true);
-  })
+  describe('.binaryStringToArrayBuffer', function() {
+    it('should convert a binary string to an arraybuffer', function() {
+      var ba = util.binaryStringToArrayBuffer('\0\0');
+      expect(ba.byteLength).to.be.equal(2);
+      expect(ba.slice).to.be.a('function');
+      expect(ba instanceof ArrayBuffer).to.be.equal(true);
+    });
+  });
 
-  it('randomToken', function() {
-    testRandom(util.randomToken);
-  })
+  describe('.randomToken', function() {
+    it('should return a random string', function() {
+      testRandom(util.randomToken);
+    });
+  });
 
 });