test_promise_utils.html (8739B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <html> 6 <head> 7 <title>Test for Promise.all, Promise.race</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 </head> 11 <body> 12 <p id="display"></p> 13 <div id="content" style="display: none"> 14 15 </div> 16 <pre id="test"> 17 <script type="application/javascript"><!-- 18 19 function promiseUtilitiesDefined() { 20 ok(Promise.all, "Promise.all must be defined when Promise is enabled."); 21 ok(Promise.race, "Promise.race must be defined when Promise is enabled."); 22 runTest(); 23 } 24 25 function promiseAllEmptyArray() { 26 var p = Promise.all([]); 27 ok(p instanceof Promise, "Return value of Promise.all should be a Promise."); 28 p.then(function(values) { 29 ok(Array.isArray(values), "Resolved value should be an array."); 30 is(values.length, 0, "Resolved array length should match iterable's length."); 31 runTest(); 32 }, function() { 33 ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); 34 runTest(); 35 }); 36 } 37 38 function promiseAllArray() { 39 var p = Promise.all([1, new Date(), Promise.resolve("firefox")]); 40 ok(p instanceof Promise, "Return value of Promise.all should be a Promise."); 41 p.then(function(values) { 42 ok(Array.isArray(values), "Resolved value should be an array."); 43 is(values.length, 3, "Resolved array length should match iterable's length."); 44 is(values[0], 1, "Array values should match."); 45 ok(values[1] instanceof Date, "Array values should match."); 46 is(values[2], "firefox", "Array values should match."); 47 runTest(); 48 }, function() { 49 ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); 50 runTest(); 51 }); 52 } 53 54 function promiseAllIterable() { 55 function* promiseGen() { 56 var i = 3; 57 while (--i) { 58 yield Promise.resolve(i); 59 } 60 61 yield new Promise(function(resolve) { 62 setTimeout(resolve, 10); 63 }); 64 } 65 66 Promise.all(promiseGen()).then(function(values) { 67 is(values.length, 3, "Resolved array length should match iterable's length."); 68 is(values[0], 2, "Array values should match."); 69 is(values[1], 1, "Array values should match."); 70 is(values[2], undefined, "Array values should match."); 71 runTest(); 72 }, function() { 73 ok(false, "Promise.all shouldn't fail when an iterable is passed."); 74 runTest(); 75 }); 76 } 77 78 function promiseAllWaitsForAllPromises() { 79 var arr = [ 80 new Promise(function(resolve) { 81 setTimeout(resolve.bind(undefined, 1), 50); 82 }), 83 new Promise(function(resolve) { 84 setTimeout(resolve.bind(undefined, 2), 10); 85 }), 86 new Promise(function(resolve) { 87 setTimeout(resolve.bind(undefined, new Promise(function(resolve2) { 88 resolve2(3); 89 })), 10); 90 }), 91 new Promise(function(resolve) { 92 setTimeout(resolve.bind(undefined, 4), 20); 93 }), 94 ]; 95 96 var p = Promise.all(arr); 97 p.then(function(values) { 98 ok(Array.isArray(values), "Resolved value should be an array."); 99 is(values.length, 4, "Resolved array length should match iterable's length."); 100 is(values[0], 1, "Array values should match."); 101 is(values[1], 2, "Array values should match."); 102 is(values[2], 3, "Array values should match."); 103 is(values[3], 4, "Array values should match."); 104 runTest(); 105 }, function() { 106 ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises."); 107 runTest(); 108 }); 109 } 110 111 function promiseAllRejectFails() { 112 var arr = [ 113 new Promise(function(resolve) { 114 setTimeout(resolve.bind(undefined, 1), 50); 115 }), 116 new Promise(function(resolve, reject) { 117 setTimeout(reject.bind(undefined, 2), 10); 118 }), 119 new Promise(function(resolve) { 120 setTimeout(resolve.bind(undefined, 3), 10); 121 }), 122 new Promise(function(resolve) { 123 setTimeout(resolve.bind(undefined, 4), 20); 124 }), 125 ]; 126 127 var p = Promise.all(arr); 128 p.then(function() { 129 ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises."); 130 runTest(); 131 }, function(e) { 132 ok(true, "Promise.all should reject when iterable has rejected Promises."); 133 is(e, 2, "Rejection value should match."); 134 runTest(); 135 }); 136 } 137 138 function promiseAllCastError() { 139 var p = Promise.all([Promise.resolve(2), { then() { 140 throw new ReferenceError("placeholder for nonexistent function call"); 141 } }]); 142 ok(p instanceof Promise, "Should cast to a Promise."); 143 p.then(function() { 144 ok(false, "promiseAllCastError: should've rejected."); 145 runTest(); 146 }, function(e) { 147 ok(e instanceof ReferenceError, "promiseCastThenableError"); 148 runTest(); 149 }); 150 } 151 152 // Check that the resolved array is enumerable. 153 function promiseAllEnumerable() { 154 var p = Promise.all([1, new Date(), Promise.resolve("firefox")]); 155 p.then(function(v) { 156 var count = 0; 157 for (let key in v) { 158 ++count; 159 ok(v[key] === 1 || v[key] instanceof Date || v[key] === "firefox", 160 "Enumerated properties don't match."); 161 } 162 is(count, 3, "Resolved array from Promise.all should be enumerable"); 163 runTest(); 164 }, function() { 165 ok(false, "promiseAllEnumerable: should've resolved."); 166 runTest(); 167 }); 168 } 169 170 function promiseRaceEmpty() { 171 var p = Promise.race([]); 172 ok(p instanceof Promise, "Should return a Promise."); 173 p.then(function() { 174 ok(false, "Should not resolve"); 175 }, function() { 176 ok(false, "Should not reject"); 177 }); 178 // Per spec, An empty race never resolves or rejects. 179 setTimeout(function() { 180 ok(true); 181 runTest(); 182 }, 50); 183 } 184 185 function promiseRaceValuesArray() { 186 var p = Promise.race([true, new Date(), 3]); 187 ok(p instanceof Promise, "Should return a Promise."); 188 p.then(function(winner) { 189 is(winner, true, "First value should win."); 190 runTest(); 191 }, function(err) { 192 ok(false, "Should not fail " + err + "."); 193 runTest(); 194 }); 195 } 196 197 function promiseRacePromiseArray() { 198 var arr = [ 199 new Promise(function(resolve) { 200 resolve("first"); 201 }), 202 Promise.resolve("second"), 203 new Promise(function() {}), 204 new Promise(function(resolve) { 205 setTimeout(function() { 206 setTimeout(function() { 207 resolve("fourth"); 208 }, 0); 209 }, 0); 210 }), 211 ]; 212 213 var p = Promise.race(arr); 214 p.then(function(winner) { 215 is(winner, "first", "First queued resolution should win the race."); 216 runTest(); 217 }); 218 } 219 220 function promiseRaceIterable() { 221 function* participants() { 222 yield new Promise(function(resolve) { 223 setTimeout(resolve, 10, 10); 224 }); 225 yield new Promise(function(resolve) { 226 setTimeout(resolve, 20, 20); 227 }); 228 } 229 230 Promise.race(participants()).then(function(winner) { 231 is(winner, 10, "Winner should be the one that finished earlier."); 232 runTest(); 233 }, function() { 234 ok(false, "Promise.race shouldn't throw when an iterable is passed!"); 235 runTest(); 236 }); 237 } 238 239 function promiseRaceReject() { 240 var p = Promise.race([ 241 Promise.reject(new Error("Fail bad!")), 242 new Promise(function(resolve) { 243 setTimeout(resolve, 0); 244 }), 245 ]); 246 247 p.then(function() { 248 ok(false, "Should not resolve when winning Promise rejected."); 249 runTest(); 250 }, function(e) { 251 ok(true, "Should be rejected"); 252 ok(e instanceof Error, "Should reject with Error."); 253 ok(e.message == "Fail bad!", "Message should match."); 254 runTest(); 255 }); 256 } 257 258 function promiseRaceThrow() { 259 var p = Promise.race([ 260 new Promise(function() { 261 throw new ReferenceError("placeholder for nonexistent function call"); 262 }), 263 new Promise(function(resolve) { 264 setTimeout(resolve, 0); 265 }), 266 ]); 267 268 p.then(function() { 269 ok(false, "Should not resolve when winning Promise had an error."); 270 runTest(); 271 }, function(e) { 272 ok(true, "Should be rejected"); 273 ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent()."); 274 runTest(); 275 }); 276 } 277 278 var tests = [ 279 promiseUtilitiesDefined, 280 promiseAllEmptyArray, 281 promiseAllArray, 282 promiseAllIterable, 283 promiseAllWaitsForAllPromises, 284 promiseAllRejectFails, 285 promiseAllCastError, 286 promiseAllEnumerable, 287 288 promiseRaceEmpty, 289 promiseRaceValuesArray, 290 promiseRacePromiseArray, 291 promiseRaceIterable, 292 promiseRaceReject, 293 promiseRaceThrow, 294 ]; 295 296 function runTest() { 297 if (!tests.length) { 298 SimpleTest.finish(); 299 return; 300 } 301 302 var test = tests.shift(); 303 test(); 304 } 305 306 SimpleTest.waitForExplicitFinish(); 307 SimpleTest.requestFlakyTimeout("untriaged"); 308 runTest(); 309 // --> 310 </script> 311 </pre> 312 </body> 313 </html>