babylon.promise.tests.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * Describes the test suite.
  3. */
  4. describe('Babylon.Promise', function () {
  5. this.timeout(10000);
  6. /**
  7. * Loads the dependencies.
  8. */
  9. before(function (done) {
  10. (BABYLONDEVTOOLS).Loader
  11. .useDist()
  12. .load(function () {
  13. // Force apply promise polyfill for consistent behavior between PhantomJS, IE11, and other browsers.
  14. BABYLON.PromisePolyfill.Apply(true);
  15. done();
  16. });
  17. });
  18. describe('#Composition', () => {
  19. it('should chain promises correctly #1', (done) => {
  20. var tempString = "";
  21. var p1 = new Promise((resolve, reject) => {
  22. tempString = "Initial";
  23. resolve();
  24. }).then(() => {
  25. tempString += " message";
  26. }).then(() => {
  27. throw new Error('Something failed');
  28. }).catch(() => {
  29. tempString += " to check promises";
  30. }).then(() => {
  31. try {
  32. expect(tempString).to.eq("Initial message to check promises");
  33. done();
  34. }
  35. catch (error) {
  36. done(error);
  37. }
  38. });
  39. });
  40. it('should chain promises correctly #2', (done) => {
  41. var tempString = "";
  42. var p1 = new Promise((resolve, reject) => {
  43. tempString = "Initial";
  44. resolve();
  45. }).then(() => {
  46. tempString += " message";
  47. }).then(() => {
  48. tempString += " to check promises";
  49. }).catch(() => {
  50. tempString += " wrong!";
  51. }).then(() => {
  52. try {
  53. expect(tempString).to.eq("Initial message to check promises");
  54. done();
  55. }
  56. catch (error) {
  57. done(error);
  58. }
  59. });
  60. });
  61. it('should chain promises correctly #3', (done) => {
  62. var tempString = "";
  63. function resolveLater(resolve, reject) {
  64. setTimeout(function () {
  65. resolve(10);
  66. }, 1000);
  67. }
  68. function rejectLater(resolve, reject) {
  69. setTimeout(function () {
  70. reject(20);
  71. }, 1000);
  72. }
  73. var p1 = (<any>Promise).resolve('foo');
  74. var p2 = p1.then(function () {
  75. // Return promise here, that will be resolved to 10 after 1 second
  76. return new Promise(resolveLater);
  77. });
  78. p2.then(function (v) {
  79. tempString += 'resolved ' + v; // "resolved", 10
  80. }, function (e) {
  81. // not called
  82. tempString += 'rejected' + e;
  83. });
  84. var p3 = p1.then(function () {
  85. // Return promise here, that will be rejected with 20 after 1 second
  86. return new Promise(rejectLater);
  87. });
  88. p3.then(function (v) {
  89. // not called
  90. tempString += 'resolved ' + v;
  91. }, function (e) {
  92. tempString += 'rejected ' + e; // "rejected", 20
  93. try {
  94. expect(tempString).to.eq("resolved 10rejected 20");
  95. done();
  96. }
  97. catch(error) {
  98. done(error);
  99. }
  100. });
  101. });
  102. it('should chain promises correctly #4', (done) => {
  103. var tempString = "first";
  104. var promise = Promise.resolve().then(() => {
  105. tempString += " third";
  106. }).then(() => {
  107. try {
  108. expect(tempString).to.eq("first second third");
  109. done();
  110. }
  111. catch(error) {
  112. done(error);
  113. }
  114. });
  115. tempString += " second";
  116. });
  117. it('should chain promises correctly #5', (done) => {
  118. var tempString = "";
  119. var promise = new Promise(function (resolve) {
  120. setTimeout(function () {
  121. resolve(44);
  122. }, 100);
  123. });
  124. promise = promise.then(function () {
  125. return 55;
  126. });
  127. promise.then(function (value) {
  128. tempString += "1: " + value;
  129. setTimeout(function () {
  130. promise.then(function (value) {
  131. tempString += " 2: " + value;
  132. try {
  133. expect(tempString).to.eq("1: 55 2: 55");
  134. done();
  135. }
  136. catch(error) {
  137. done(error);
  138. }
  139. });
  140. }, 0);
  141. });
  142. });
  143. it('should chain promises correctly #6', (done) => {
  144. var tempString = "";
  145. var promise = new Promise(function (resolve) {
  146. setTimeout(function () {
  147. resolve(44);
  148. }, 100);
  149. });
  150. promise = promise.then(function () {
  151. return Promise.resolve(55);
  152. });
  153. promise.then(function (value) {
  154. tempString += "1: " + value;
  155. setTimeout(function () {
  156. promise.then(function (value) {
  157. tempString += " 2: " + value;
  158. try {
  159. expect(tempString).to.eq("1: 55 2: 55");
  160. done();
  161. }
  162. catch(error) {
  163. done(error);
  164. }
  165. });
  166. }, 0);
  167. });
  168. });
  169. });
  170. describe('#Promise.all', () => {
  171. it('should agregate promises correctly', (done) => {
  172. var promise1 = Promise.resolve(3);
  173. var promise2 = new Promise(function (resolve, reject) {
  174. setTimeout(resolve, 100, 'foo');
  175. });
  176. var promise3 = Promise.resolve(42);
  177. Promise.all([promise1, promise2, promise3]).then(function (values) {
  178. try {
  179. values.should.deep.equal([3, "foo", 42]);
  180. done();
  181. }
  182. catch(error) {
  183. done(error);
  184. }
  185. });
  186. });
  187. });
  188. describe('#Returning value', () => {
  189. it('should correctly handle returned values', (done) => {
  190. Promise.resolve(1)
  191. .then(number => { return number + 1; })
  192. .then(number => { return number + 1; })
  193. .then(number => {
  194. try {
  195. number.should.be.equal(3);
  196. done();
  197. }
  198. catch(error) {
  199. done(error);
  200. }
  201. });
  202. });
  203. });
  204. describe('#Multiple children', () => {
  205. it('should correctly handle multiple independent "then"', (done) => {
  206. var successValue = 'Success!';
  207. var promise1 = BABYLON.Tools.DelayAsync(500).then(() => successValue);
  208. var sum = 0;
  209. var checkDone = (value: string) => {
  210. sum++;
  211. if (sum === 2) {
  212. try {
  213. expect(value).to.equal(successValue);
  214. done();
  215. }
  216. catch (error) {
  217. done(error);
  218. }
  219. }
  220. };
  221. promise1.then(checkDone);
  222. promise1.then(checkDone);
  223. });
  224. });
  225. describe('#All and then', () => {
  226. it('should correctly handle chaining a returning then after a all', (done) => {
  227. var promise = Promise.all([BABYLON.Tools.DelayAsync(100), BABYLON.Tools.DelayAsync(200)]).then(function () {
  228. return 1;
  229. });
  230. promise.then(function (value) {
  231. try {
  232. expect(value).to.equal(1);
  233. done();
  234. }
  235. catch(error) {
  236. done(error);
  237. }
  238. });
  239. });
  240. });
  241. describe('#Move children', () => {
  242. it('should correctly handle moving children', (done) => {
  243. var callback1Count = 0;
  244. var callback2Count = 0;
  245. Promise.resolve().then(function () {
  246. var promise = Promise.all([BABYLON.Tools.DelayAsync(100), BABYLON.Tools.DelayAsync(200)]).then(function () {
  247. callback1Count++;
  248. });
  249. Promise.all([promise]).then(function () {
  250. callback2Count++;
  251. });
  252. return promise;
  253. }).then(function () {
  254. try {
  255. expect(callback1Count).to.equal(1);
  256. expect(callback2Count).to.equal(1);
  257. done();
  258. }
  259. catch(error) {
  260. done(error);
  261. }
  262. });
  263. });
  264. });
  265. describe('#Error handling', () => {
  266. it('should correctly handle exceptions', (done) => {
  267. var errorValue = 'Failed!';
  268. var promise = new Promise((resolve, reject) => {
  269. throw new Error(errorValue);
  270. }).catch(error => {
  271. try {
  272. expect(error.constructor).to.equal(Error);
  273. expect(error.message).to.equal(errorValue);
  274. done();
  275. }
  276. catch(error) {
  277. done(error);
  278. }
  279. });
  280. });
  281. it('should correctly handle exceptions in a reject handler', (done) => {
  282. var errorValue = 'Failed!';
  283. var promise = new Promise((resolve, reject) => {
  284. throw new Error(errorValue);
  285. }).catch(error => {
  286. throw error;
  287. }).catch(error => {
  288. try {
  289. expect(error.constructor).to.equal(Error);
  290. expect(error.message).to.equal(errorValue);
  291. done();
  292. }
  293. catch(error) {
  294. done(error);
  295. }
  296. });
  297. });
  298. });
  299. });