Front end of the Slack clone application.

cover.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. 'use strict';
  2. var aplus = require('promises-aplus-tests');
  3. var Promise = require('../lib');
  4. var adapter = {};
  5. var assert = require('assert');
  6. adapter.deferred = function () {
  7. var pending = {};
  8. pending.promise = new Promise(function (resolver, reject) {
  9. pending.resolve = resolver;
  10. pending.reject = reject;
  11. });
  12. return pending;
  13. };
  14. adapter.resolved = function (value) {
  15. return Promise.resolve(value);
  16. }
  17. adapter.rejected = function (reason) {
  18. return Promise.reject(reason);
  19. }
  20. describe('Lie', function () {
  21. it('should work resolving a promise new', function (done) {
  22. new Promise(function (resolve) {
  23. resolve(new Promise(function (resolve) {
  24. resolve(true);
  25. }));
  26. }).then(function (result) {
  27. if (result === true) {
  28. done();
  29. } else {
  30. done(true);
  31. }
  32. });
  33. });
  34. it('should throw if you don\'t pass a function', function (done) {
  35. try {
  36. new Promise(true);
  37. } catch (e) {
  38. if (e instanceof TypeError) {
  39. done();
  40. } else {
  41. done(e);
  42. }
  43. }
  44. });
  45. it('should have a working catch method', function (done) {
  46. new Promise(function () {
  47. throw new Error('boom');
  48. }).catch(function () {
  49. done();
  50. });
  51. });
  52. describe('resolve', function () {
  53. it('should work with true', function (done) {
  54. Promise.resolve(true).then(function (value) {
  55. if (value === true) {
  56. done();
  57. } else {
  58. done(true);
  59. }
  60. });
  61. });
  62. it('should work with false', function (done) {
  63. Promise.resolve(false).then(function (value) {
  64. if (value === false) {
  65. done();
  66. } else {
  67. done(true);
  68. }
  69. });
  70. });
  71. it('should work with null', function (done) {
  72. Promise.resolve(null).then(function (value) {
  73. if (value === null) {
  74. done();
  75. } else {
  76. done(true);
  77. }
  78. });
  79. });
  80. it('should work with undefined', function (done) {
  81. Promise.resolve(undefined).then(function (value) {
  82. if (value === undefined) {
  83. done();
  84. } else {
  85. done(true);
  86. }
  87. });
  88. });
  89. it('should work with 0', function (done) {
  90. Promise.resolve(0).then(function (value) {
  91. value++;
  92. return Promise.resolve(0);
  93. }).then(function (value) {
  94. if (value === 0) {
  95. done();
  96. } else {
  97. done(true);
  98. }
  99. });
  100. });
  101. it('should work with 1', function (done) {
  102. Promise.resolve(1).then(function (value) {
  103. if (value === 1) {
  104. done();
  105. } else {
  106. done(true);
  107. }
  108. });
  109. });
  110. it('should work with \'\'', function (done) {
  111. Promise.resolve('').then(function (value) {
  112. if (value === '') {
  113. done();
  114. } else {
  115. done(true);
  116. }
  117. });
  118. });
  119. it('should work with \'something\'', function (done) {
  120. Promise.resolve('something').then(function (value) {
  121. if (value === 'something') {
  122. done();
  123. } else {
  124. done(true);
  125. }
  126. });
  127. });
  128. });
  129. describe('Promise.all', function () {
  130. //https://github.com/domenic/promises-unwrapping/blob/master/reference-implementation/test/all.js
  131. it('fulfills if passed an empty array', function (done) {
  132. var iterable = [];
  133. Promise.all(iterable).then(function (value) {
  134. assert(Array.isArray(value));
  135. assert.deepEqual(value, []);
  136. done();
  137. });
  138. });
  139. it('fulfills if passed an array of mixed fulfilled promises and values', function (done) {
  140. var iterable = [0, Promise.resolve(1), 2, Promise.resolve(3)];
  141. Promise.all(iterable).then(function (value) {
  142. assert(Array.isArray(value));
  143. assert.deepEqual(value, [0, 1, 2, 3]);
  144. done();
  145. });
  146. });
  147. it('rejects if any passed promise is rejected', function (done) {
  148. var foreverPending = new Promise(function () {});
  149. var error = new Error('Rejected');
  150. var rejected = Promise.reject(error);
  151. var iterable = [foreverPending, rejected];
  152. Promise.all(iterable).then(
  153. function (value) {
  154. assert(false, 'should never get here');
  155. done();
  156. },
  157. function (reason) {
  158. assert.strictEqual(reason, error);
  159. done();
  160. }
  161. );
  162. });
  163. it('resolves foreign thenables', function (done) {
  164. var normal = Promise.resolve(1);
  165. var foreign = { then: function (f) { f(2); } };
  166. var iterable = [normal, foreign];
  167. Promise.all(iterable).then(function (value) {
  168. assert.deepEqual(value, [1, 2]);
  169. done();
  170. });
  171. });
  172. it('does not reject twice', function (done) {
  173. var normal = Promise.resolve(1);
  174. var error = new Error('rejected once');
  175. var two = Promise.reject(error);
  176. var three = new Promise(function (resolve, reject) {
  177. setTimeout(function () {
  178. reject(new Error('rejected twice'));
  179. }, 30);
  180. });
  181. var iterable = [normal, two, three];
  182. Promise.all(iterable).then(function (value) {
  183. assert(false, 'should never get here');
  184. done();
  185. }, function (value) {
  186. assert.deepEqual(value, error);
  187. done();
  188. });
  189. });
  190. it('fulfills when passed an sparse array, giving `undefined` for the omitted values', function (done) {
  191. var iterable = [Promise.resolve(0), , , Promise.resolve(1)];
  192. Promise.all(iterable).then(function (value) {
  193. assert.deepEqual(value, [0, undefined, undefined, 1]);
  194. done();
  195. });
  196. });
  197. it('does not modify the input array', function (done) {
  198. var input = [0, 1];
  199. var iterable = input;
  200. Promise.all(iterable).then(function (value) {
  201. assert.notStrictEqual(input, value);
  202. done();
  203. });
  204. });
  205. it('should reject with a TypeError if given a non-iterable', function (done) {
  206. var notIterable = {};
  207. Promise.all(notIterable).then(
  208. function () {
  209. assert(false, 'should never get here');
  210. done();
  211. },
  212. function (reason) {
  213. assert(reason instanceof TypeError);
  214. done();
  215. }
  216. );
  217. });
  218. });
  219. describe('Promise.race', function () {
  220. //https://github.com/domenic/promises-unwrapping/blob/master/reference-implementation/test/all.js
  221. function delay(value, time, rejectIt) {
  222. return new Promise(function (resolve, reject) {
  223. setTimeout(function () {
  224. if (rejectIt) {
  225. return reject(value);
  226. }
  227. resolve(value);
  228. }, time);
  229. });
  230. }
  231. it('fulfills if passed an empty array', function (done) {
  232. var iterable = [];
  233. Promise.race(iterable).then(function (value) {
  234. assert(Array.isArray(value));
  235. assert.deepEqual(value, []);
  236. done();
  237. });
  238. });
  239. it('fulfills if passed an array of mixed fulfilled promises and values', function (done) {
  240. var iterable = [delay(0, 20), Promise.resolve(1), delay(2, 30), delay(Promise.resolve(3), 20)];
  241. Promise.race(iterable).then(function (value) {
  242. assert.equal(value, 1);
  243. done();
  244. });
  245. });
  246. it('rejects if firsed resolved promise is rejected', function (done) {
  247. var error = new Error('Rejected');
  248. var iterable = [delay(true, 300), delay(error, 20, true)];
  249. Promise.race(iterable).then(
  250. function (value) {
  251. assert(false, 'should never get here');
  252. done();
  253. },
  254. function (reason) {
  255. assert.strictEqual(reason, error);
  256. done();
  257. }
  258. );
  259. });
  260. it('resolves if second resolved promise is rejected', function (done) {
  261. var error = new Error('Rejected');
  262. var iterable = [delay(true, 30), delay(error, 200, true)];
  263. Promise.race(iterable).then(
  264. function (value) {
  265. assert(value, 'should resolve');
  266. done();
  267. },
  268. function (reason) {
  269. assert(false, 'should never get here');
  270. done();
  271. }
  272. );
  273. });
  274. it('resolves foreign thenables', function (done) {
  275. var normal = Promise.resolve(1);
  276. var foreign = { then: function (f) { f(2); } };
  277. var iterable = [delay(Promise.resolve(1), 200), foreign];
  278. Promise.race(iterable).then(function (value) {
  279. assert.deepEqual(value, 2);
  280. done();
  281. });
  282. });
  283. it('fulfills when passed an sparse array, giving `undefined` for the omitted values', function (done) {
  284. var iterable = [delay(Promise.resolve(0), 300), , , delay(Promise.resolve(1), 300)];
  285. Promise.race(iterable).then(function (value) {
  286. assert.equal(value, undefined);
  287. done();
  288. });
  289. });
  290. it('should reject with a TypeError if given a non-iterable', function (done) {
  291. var notIterable = {};
  292. Promise.race(notIterable).then(
  293. function () {
  294. assert(false, 'should never get here');
  295. done();
  296. },
  297. function (reason) {
  298. assert(reason instanceof TypeError);
  299. done();
  300. }
  301. );
  302. });
  303. });
  304. if (!process.browser) {
  305. it('should emit events for unhandled errors', function (done) {
  306. var called = 0;
  307. var err1 = new Error('should be caught');
  308. var err2 = new Error('should be uncaught');
  309. var promise1 = Promise.reject(err1);
  310. var promise2 = Promise.reject(err2);
  311. promise1.catch(function () {});
  312. function onEvent(reason, promise) {
  313. if (!called) {
  314. called++;
  315. assert.equal(err2, reason);
  316. assert.equal(promise2, promise);
  317. setTimeout(function (){
  318. process.removeListener('unhandledRejection', onEvent);
  319. done();
  320. }, 100)
  321. } else {
  322. done(new Error('called more then once'));
  323. }
  324. }
  325. process.on('unhandledRejection', onEvent);
  326. });
  327. }
  328. describe('Promises/A+ Tests', function () {
  329. aplus.mocha(adapter);
  330. });
  331. });