client-tests.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. var blobData;
  2. var arrayBufferData;
  3. var binaryData;
  4. var dataUriData;
  5. var urlData;
  6. // Init with Blob
  7. Tinytest.addAsync('cfs-data - client - Init with Blob', function(test, onComplete) {
  8. var blob = new Blob(['Hello World'], {type : 'text/plain'});
  9. blobData = new DataMan(blob);
  10. test.instanceOf(blobData.blob, Blob);
  11. test.equal(blobData.type(), "text/plain");
  12. onComplete();
  13. });
  14. // Init with ArrayBuffer
  15. Tinytest.addAsync('cfs-data - client - Init with ArrayBuffer', function(test, onComplete) {
  16. arrayBufferData = new DataMan(str2ab('Hello World'), "text/plain");
  17. // Should be converted upon init to a Blob
  18. test.instanceOf(arrayBufferData.blob, Blob);
  19. test.equal(arrayBufferData.type(), "text/plain");
  20. onComplete();
  21. });
  22. // Init with Binary
  23. Tinytest.addAsync('cfs-data - client - Init with Binary', function(test, onComplete) {
  24. binaryData = new DataMan(new Uint8Array(str2ab('Hello World')), "text/plain");
  25. // Should be converted upon init to a Blob
  26. test.instanceOf(arrayBufferData.blob, Blob);
  27. test.equal(binaryData.type(), "text/plain");
  28. onComplete();
  29. });
  30. // Init with data URI string
  31. Tinytest.addAsync('cfs-data - client - Init with data URI string', function(test, onComplete) {
  32. var dataUri = 'data:text/plain;base64,SGVsbG8gV29ybGQ='; //'Hello World'
  33. dataUriData = new DataMan(dataUri);
  34. // Should be converted upon init to a Blob
  35. test.instanceOf(dataUriData.blob, Blob);
  36. test.equal(dataUriData.type(), "text/plain"); //should be extracted from data URI
  37. onComplete();
  38. });
  39. // Init with URL string
  40. Tinytest.addAsync('cfs-data - client - Init with URL string', function(test, onComplete) {
  41. urlData = new DataMan(Meteor.absoluteUrl('test'), "text/plain"); //'Hello World'
  42. // URLs are not converted to Blobs upon init
  43. test.equal(urlData.url, Meteor.absoluteUrl('test'));
  44. test.equal(urlData.type(), "text/plain");
  45. onComplete();
  46. });
  47. // getBlob
  48. Tinytest.addAsync('cfs-data - client - getBlob', function(test, onComplete) {
  49. var total = 10, done = 0;
  50. function continueIfDone() {
  51. done++;
  52. if (total === done) {
  53. onComplete();
  54. }
  55. }
  56. function testBlob(error, blob, testType) {
  57. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  58. test.instanceOf(blob, Blob, testType + ' got no blob');
  59. if (blob instanceof Blob) {
  60. var reader = new FileReader();
  61. reader.addEventListener("load", function(event) {
  62. test.equal(reader.result, 'Hello World', testType + ' got back blob with incorrect data');
  63. continueIfDone();
  64. }, false);
  65. reader.addEventListener("error", function(err) {
  66. test.equal(reader.error, null, testType + ' error reading blob as text');
  67. continueIfDone();
  68. }, false);
  69. reader.readAsText(blob, 'utf-8');
  70. } else {
  71. continueIfDone();
  72. }
  73. }
  74. // from Blob
  75. blobData.getBlob(function (error, blob) {
  76. testBlob(error, blob, 'getBlob from Blob');
  77. });
  78. // from Blob (no callback)
  79. testBlob(false, blobData.getBlob(), 'getBlob from Blob');
  80. // from ArrayBuffer
  81. arrayBufferData.getBlob(function (error, blob) {
  82. testBlob(error, blob, 'getBlob from ArrayBuffer');
  83. });
  84. // from ArrayBuffer (no callback)
  85. testBlob(false, arrayBufferData.getBlob(), 'getBlob from ArrayBuffer');
  86. // from binary
  87. binaryData.getBlob(function (error, blob) {
  88. testBlob(error, blob, 'getBlob from binary');
  89. });
  90. // from binary (no callback)
  91. testBlob(false, binaryData.getBlob(), 'getBlob from binary');
  92. // from data URI
  93. dataUriData.getBlob(function (error, blob) {
  94. testBlob(error, blob, 'getBlob from data URI');
  95. });
  96. // from data URI (no callback)
  97. testBlob(false, dataUriData.getBlob(), 'getBlob from data URI');
  98. // from URL
  99. urlData.getBlob(function (error, blob) {
  100. testBlob(error, blob, 'getBlob from URL');
  101. });
  102. // from URL (no callback)
  103. test.throws(function () {
  104. // callback is required for URLs on the client
  105. urlData.getBlob();
  106. });
  107. continueIfDone();
  108. });
  109. // getBinary
  110. Tinytest.addAsync('cfs-data - client - getBinary', function(test, onComplete) {
  111. var total = 5, done = 0;
  112. function continueIfDone() {
  113. done++;
  114. if (total === done) {
  115. onComplete();
  116. }
  117. }
  118. function testBinary(error, binary, testType) {
  119. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  120. test.isTrue(EJSON.isBinary(binary), testType + ' got no binary');
  121. if (EJSON.isBinary(binary)) {
  122. test.equal(bin2str(binary), 'Hello World', testType + ' got back binary with incorrect data');
  123. continueIfDone();
  124. } else {
  125. continueIfDone();
  126. }
  127. }
  128. // from Blob
  129. blobData.getBinary(function (error, binary) {
  130. testBinary(error, binary, 'getBinary from Blob');
  131. });
  132. // from ArrayBuffer
  133. arrayBufferData.getBinary(function (error, binary) {
  134. testBinary(error, binary, 'getBinary from ArrayBuffer');
  135. });
  136. // from binary
  137. binaryData.getBinary(function (error, binary) {
  138. testBinary(error, binary, 'getBinary from binary');
  139. });
  140. // from data URI
  141. dataUriData.getBinary(function (error, binary) {
  142. testBinary(error, binary, 'getBinary from data URI');
  143. });
  144. // from URL
  145. urlData.getBinary(function (error, binary) {
  146. testBinary(error, binary, 'getBinary from URL');
  147. });
  148. });
  149. // getDataUri
  150. Tinytest.addAsync('cfs-data - client - getDataUri', function(test, onComplete) {
  151. var total = 5, done = 0;
  152. function testURI(error, uri, testType) {
  153. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  154. test.equal(typeof uri, "string", testType + ' got no URI string');
  155. test.equal(uri, 'data:text/plain;base64,SGVsbG8gV29ybGQ=', testType + ' got invalid URI');
  156. done++;
  157. if (total === done) {
  158. onComplete();
  159. }
  160. }
  161. // from Blob
  162. blobData.getDataUri(function (error, uri) {
  163. testURI(error, uri, 'getDataUri from Blob');
  164. });
  165. // from ArrayBuffer
  166. arrayBufferData.getDataUri(function (error, uri) {
  167. testURI(error, uri, 'getDataUri from ArrayBuffer');
  168. });
  169. // from binary
  170. binaryData.getDataUri(function (error, uri) {
  171. testURI(error, uri, 'getDataUri from binary');
  172. });
  173. // from data URI
  174. dataUriData.getDataUri(function (error, uri) {
  175. testURI(error, uri, 'getDataUri from data URI');
  176. });
  177. // from URL
  178. urlData.getDataUri(function (error, uri) {
  179. testURI(error, uri, 'getDataUri from URL');
  180. });
  181. });
  182. // size
  183. Tinytest.addAsync('cfs-data - client - size', function(test, onComplete) {
  184. var total = 10, done = 0;
  185. function continueIfDone() {
  186. done++;
  187. if (total === done) {
  188. onComplete();
  189. }
  190. }
  191. function testSize(error, size, testType) {
  192. test.isFalse(!!error, testType + ' got error: ' + (error && error.message));
  193. test.equal(size, 11, testType + ' got wrong size');
  194. continueIfDone();
  195. }
  196. // from Blob
  197. blobData.size(function (error, size) {
  198. testSize(error, size, 'size from Blob');
  199. });
  200. // from Blob (no callback)
  201. testSize(false, blobData.size(), 'size from Blob');
  202. // from ArrayBuffer
  203. arrayBufferData.size(function (error, size) {
  204. testSize(error, size, 'size from ArrayBuffer');
  205. });
  206. // from ArrayBuffer (no callback)
  207. testSize(false, arrayBufferData.size(), 'size from ArrayBuffer');
  208. // from binary
  209. binaryData.size(function (error, size) {
  210. testSize(error, size, 'size from binary');
  211. });
  212. // from binary (no callback)
  213. testSize(false, binaryData.size(), 'size from binary');
  214. // from data URI
  215. dataUriData.size(function (error, size) {
  216. testSize(error, size, 'size from data URI');
  217. });
  218. // from data URI (no callback)
  219. testSize(false, dataUriData.size(), 'size from data URI');
  220. // from URL
  221. urlData.size(function (error, size) {
  222. testSize(error, size, 'size from URL');
  223. });
  224. // from URL (no callback)
  225. test.throws(function () {
  226. // callback is required for URLs on the client
  227. urlData.size();
  228. });
  229. continueIfDone();
  230. });
  231. //Test API:
  232. //test.isFalse(v, msg)
  233. //test.isTrue(v, msg)
  234. //test.equalactual, expected, message, not
  235. //test.length(obj, len)
  236. //test.include(s, v)
  237. //test.isNaN(v, msg)
  238. //test.isUndefined(v, msg)
  239. //test.isNotNull
  240. //test.isNull
  241. //test.throws(func)
  242. //test.instanceOf(obj, klass)
  243. //test.notEqual(actual, expected, message)
  244. //test.runId()
  245. //test.exception(exception)
  246. //test.expect_fail()
  247. //test.ok(doc)
  248. //test.fail(doc)
  249. //test.equal(a, b, msg)