index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var Blob = require('../');
  2. var expect = require('expect.js');
  3. describe('blob', function() {
  4. if (!Blob) {
  5. it('should not have a blob or a blob builder in the global namespace, or blob should not be a constructor function if the module exports false', function() {
  6. try {
  7. var ab = (new Uint8Array(5)).buffer;
  8. global.Blob([ab]);
  9. expect().fail('Blob shouldn\'t be constructable');
  10. } catch (e) {}
  11. var BlobBuilder = global.BlobBuilder
  12. || global.WebKitBlobBuilder
  13. || global.MSBlobBuilder
  14. || global.MozBlobBuilder;
  15. expect(BlobBuilder).to.be(undefined);
  16. });
  17. } else {
  18. it('should encode a proper sized blob when given a string argument', function() {
  19. var b = new Blob(['hi']);
  20. expect(b.size).to.be(2);
  21. });
  22. it('should encode a blob with proper size when given two strings as arguments', function() {
  23. var b = new Blob(['hi', 'hello']);
  24. expect(b.size).to.be(7);
  25. });
  26. it('should encode arraybuffers with right content', function(done) {
  27. var ary = new Uint8Array(5);
  28. for (var i = 0; i < 5; i++) ary[i] = i;
  29. var b = new Blob([ary.buffer]);
  30. var fr = new FileReader();
  31. fr.onload = function() {
  32. var newAry = new Uint8Array(this.result);
  33. for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
  34. done();
  35. };
  36. fr.readAsArrayBuffer(b);
  37. });
  38. it('should encode typed arrays with right content', function(done) {
  39. var ary = new Uint8Array(5);
  40. for (var i = 0; i < 5; i++) ary[i] = i;
  41. var b = new Blob([ary]);
  42. var fr = new FileReader();
  43. fr.onload = function() {
  44. var newAry = new Uint8Array(this.result);
  45. for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
  46. done();
  47. };
  48. fr.readAsArrayBuffer(b);
  49. });
  50. it('should encode sliced typed arrays with right content', function(done) {
  51. var ary = new Uint8Array(5);
  52. for (var i = 0; i < 5; i++) ary[i] = i;
  53. var b = new Blob([ary.subarray(2)]);
  54. var fr = new FileReader();
  55. fr.onload = function() {
  56. var newAry = new Uint8Array(this.result);
  57. for (var i = 0; i < 3; i++) expect(newAry[i]).to.be(i + 2);
  58. done();
  59. };
  60. fr.readAsArrayBuffer(b);
  61. });
  62. it('should encode with blobs', function(done) {
  63. var ary = new Uint8Array(5);
  64. for (var i = 0; i < 5; i++) ary[i] = i;
  65. var b = new Blob([new Blob([ary.buffer])]);
  66. var fr = new FileReader();
  67. fr.onload = function() {
  68. var newAry = new Uint8Array(this.result);
  69. for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
  70. done();
  71. };
  72. fr.readAsArrayBuffer(b);
  73. });
  74. it('should enode mixed contents to right size', function() {
  75. var ary = new Uint8Array(5);
  76. for (var i = 0; i < 5; i++) ary[i] = i;
  77. var b = new Blob([ary.buffer, 'hello']);
  78. expect(b.size).to.be(10);
  79. });
  80. it('should accept mime type', function() {
  81. var b = new Blob(['hi', 'hello'], { type: 'text/html' });
  82. expect(b.type).to.be('text/html');
  83. });
  84. it('should be an instance of constructor', function() {
  85. var b = new Blob(['hi']);
  86. expect(b).to.be.a(Blob);
  87. expect(b).to.be.a(global.Blob);
  88. });
  89. }
  90. });