babylon.promise.tests.ts 9.1 KB

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