util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. describe('util', function() {
  2. var testRandom = function(fn) {
  3. var i = 0
  4. , generated = {};
  5. while(i < 25) {
  6. var p = fn();
  7. if (generated[p]) throw new Error('not so random')
  8. generated[p] = 1;
  9. i++;
  10. }
  11. }
  12. describe('.inherits', function() {
  13. it('should make functions inherit properly', function() {
  14. function ctor() {}
  15. function superCtor() {}
  16. superCtor.prototype.test = function() { return 5; }
  17. util.inherits(ctor, superCtor);
  18. expect(new ctor()).to.be.a(superCtor);
  19. expect(new ctor().test()).to.be.equal(5);
  20. });
  21. });
  22. /*
  23. * extend overwrites keys if already exists
  24. * leaves existing keys alone otherwise
  25. */
  26. describe('.extend', function() {
  27. it('should copy the properties of b to a', function() {
  28. var a = {a: 1, b: 2, c: 3, d: 4}
  29. , b = {d: 2};
  30. util.extend(b, a);
  31. expect(b).to.eql(a);
  32. expect(b.d).to.be.equal(4);
  33. b = {z: 2};
  34. util.extend(b, a);
  35. expect(b.z).to.be.equal(2);
  36. });
  37. });
  38. describe('.pack', function() {
  39. it('should be BinaryPack\'s `pack` function', function() {
  40. expect(util.pack).to.be.equal(BinaryPack.pack);
  41. });
  42. });
  43. describe('.unpack', function() {
  44. it('should be BinaryPack\'s `unpack` function', function() {
  45. expect(util.unpack).to.be.equal(BinaryPack.unpack);
  46. });
  47. });
  48. // FF no like
  49. describe('.log', function() {
  50. it('should log with the PeerJS prefix', function(done) {
  51. var consolelog = console.log;
  52. // default is false
  53. expect(util.debug).to.be.equal(false);
  54. util.debug = true;
  55. console.log = function() {
  56. var arg = Array.prototype.slice.call(arguments);
  57. expect(arg.join(' ')).to.be.equal('PeerJS: hi');
  58. done();
  59. }
  60. util.log('hi');
  61. // reset
  62. console.log = consolelog;
  63. util.debug = false;
  64. });
  65. });
  66. describe('.setZeroTimeout', function() {
  67. it('should call the function after a 0s timeout', function(done) {
  68. var isdone = false;
  69. util.setZeroTimeout(function() {
  70. if (isdone) {
  71. done();
  72. }
  73. });
  74. isdone = true;
  75. });
  76. });
  77. describe('.blobToArrayBuffer', function() {
  78. it('should convert a blob to an arraybuffer', function(done) {
  79. var blob = new Blob(['hi']);
  80. util.blobToArrayBuffer(blob, function(result) {
  81. expect(result.byteLength).to.be.equal(2);
  82. expect(result.slice).to.be.a('function');
  83. expect(result instanceof ArrayBuffer).to.be.equal(true);
  84. done();
  85. });
  86. });
  87. });
  88. describe('blobToBinaryString', function() {
  89. it('should convert a blob to a binary string', function(done) {
  90. var blob = new Blob(['hi']);
  91. util.blobToBinaryString(blob, function(result) {
  92. expect(result).to.equal('hi');
  93. done();
  94. });
  95. });
  96. });
  97. describe('.binaryStringToArrayBuffer', function() {
  98. it('should convert a binary string to an arraybuffer', function() {
  99. var ba = util.binaryStringToArrayBuffer('\0\0');
  100. expect(ba.byteLength).to.be.equal(2);
  101. expect(ba.slice).to.be.a('function');
  102. expect(ba instanceof ArrayBuffer).to.be.equal(true);
  103. });
  104. });
  105. describe('.randomToken', function() {
  106. it('should return a random string', function() {
  107. testRandom(util.randomToken);
  108. });
  109. });
  110. });