summaryrefslogtreecommitdiffstats
path: root/dom/promise/tests/test_promise_utils.html
blob: 44777dd965a149c283b0c61b376b1ae95198754b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Test for Promise.all, Promise.race</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="application/javascript"><!--

function promiseUtilitiesDefined() {
  ok(Promise.all, "Promise.all must be defined when Promise is enabled.");
  ok(Promise.race, "Promise.race must be defined when Promise is enabled.");
  runTest();
}

function promiseAllEmptyArray() {
  var p = Promise.all([]);
  ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
  p.then(function(values) {
    ok(Array.isArray(values), "Resolved value should be an array.");
    is(values.length, 0, "Resolved array length should match iterable's length.");
    runTest();
  }, function() {
    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
    runTest();
  });
}

function promiseAllArray() {
  var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
  ok(p instanceof Promise, "Return value of Promise.all should be a Promise.");
  p.then(function(values) {
    ok(Array.isArray(values), "Resolved value should be an array.");
    is(values.length, 3, "Resolved array length should match iterable's length.");
    is(values[0], 1, "Array values should match.");
    ok(values[1] instanceof Date, "Array values should match.");
    is(values[2], "firefox", "Array values should match.");
    runTest();
  }, function() {
    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
    runTest();
  });
}

function promiseAllIterable() {
  function* promiseGen() {
    var i = 3;
    while (--i) {
      yield Promise.resolve(i);
    }

    yield new Promise(function(resolve) {
      setTimeout(resolve, 10);
    });
  }

  Promise.all(promiseGen()).then(function(values) {
    is(values.length, 3, "Resolved array length should match iterable's length.");
    is(values[0], 2, "Array values should match.");
    is(values[1], 1, "Array values should match.");
    is(values[2], undefined, "Array values should match.");
    runTest();
  }, function() {
    ok(false, "Promise.all shouldn't fail when an iterable is passed.");
    runTest();
  });
}

function promiseAllWaitsForAllPromises() {
  var arr = [
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 1), 50);
    }),
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 2), 10);
    }),
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, new Promise(function(resolve2) {
        resolve2(3);
      })), 10);
    }),
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 4), 20);
    }),
  ];

  var p = Promise.all(arr);
  p.then(function(values) {
    ok(Array.isArray(values), "Resolved value should be an array.");
    is(values.length, 4, "Resolved array length should match iterable's length.");
    is(values[0], 1, "Array values should match.");
    is(values[1], 2, "Array values should match.");
    is(values[2], 3, "Array values should match.");
    is(values[3], 4, "Array values should match.");
    runTest();
  }, function() {
    ok(false, "Promise.all shouldn't fail when iterable has no rejected Promises.");
    runTest();
  });
}

function promiseAllRejectFails() {
  var arr = [
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 1), 50);
    }),
    new Promise(function(resolve, reject) {
      setTimeout(reject.bind(undefined, 2), 10);
    }),
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 3), 10);
    }),
    new Promise(function(resolve) {
      setTimeout(resolve.bind(undefined, 4), 20);
    }),
  ];

  var p = Promise.all(arr);
  p.then(function() {
    ok(false, "Promise.all shouldn't resolve when iterable has rejected Promises.");
    runTest();
  }, function(e) {
    ok(true, "Promise.all should reject when iterable has rejected Promises.");
    is(e, 2, "Rejection value should match.");
    runTest();
  });
}

function promiseAllCastError() {
  var p = Promise.all([Promise.resolve(2), { then() {
    throw new ReferenceError("placeholder for nonexistent function call");
  } }]);
  ok(p instanceof Promise, "Should cast to a Promise.");
  p.then(function() {
    ok(false, "promiseAllCastError: should've rejected.");
    runTest();
  }, function(e) {
    ok(e instanceof ReferenceError, "promiseCastThenableError");
    runTest();
  });
}

// Check that the resolved array is enumerable.
function promiseAllEnumerable() {
  var p = Promise.all([1, new Date(), Promise.resolve("firefox")]);
  p.then(function(v) {
    var count = 0;
    for (let key in v) {
      ++count;
      ok(v[key] === 1 || v[key] instanceof Date || v[key] === "firefox",
         "Enumerated properties don't match.");
    }
    is(count, 3, "Resolved array from Promise.all should be enumerable");
    runTest();
  }, function() {
    ok(false, "promiseAllEnumerable: should've resolved.");
    runTest();
  });
}

function promiseRaceEmpty() {
  var p = Promise.race([]);
  ok(p instanceof Promise, "Should return a Promise.");
  p.then(function() {
    ok(false, "Should not resolve");
  }, function() {
    ok(false, "Should not reject");
  });
  // Per spec, An empty race never resolves or rejects.
  setTimeout(function() {
    ok(true);
    runTest();
  }, 50);
}

function promiseRaceValuesArray() {
  var p = Promise.race([true, new Date(), 3]);
  ok(p instanceof Promise, "Should return a Promise.");
  p.then(function(winner) {
    is(winner, true, "First value should win.");
    runTest();
  }, function(err) {
    ok(false, "Should not fail " + err + ".");
    runTest();
  });
}

function promiseRacePromiseArray() {
  var arr = [
    new Promise(function(resolve) {
      resolve("first");
    }),
    Promise.resolve("second"),
    new Promise(function() {}),
    new Promise(function(resolve) {
      setTimeout(function() {
        setTimeout(function() {
          resolve("fourth");
        }, 0);
      }, 0);
    }),
  ];

  var p = Promise.race(arr);
  p.then(function(winner) {
    is(winner, "first", "First queued resolution should win the race.");
    runTest();
  });
}

function promiseRaceIterable() {
  function* participants() {
    yield new Promise(function(resolve) {
      setTimeout(resolve, 10, 10);
    });
    yield new Promise(function(resolve) {
      setTimeout(resolve, 20, 20);
    });
  }

  Promise.race(participants()).then(function(winner) {
    is(winner, 10, "Winner should be the one that finished earlier.");
    runTest();
  }, function() {
    ok(false, "Promise.race shouldn't throw when an iterable is passed!");
    runTest();
  });
}

function promiseRaceReject() {
  var p = Promise.race([
    Promise.reject(new Error("Fail bad!")),
    new Promise(function(resolve) {
      setTimeout(resolve, 0);
    }),
  ]);

  p.then(function() {
    ok(false, "Should not resolve when winning Promise rejected.");
    runTest();
  }, function(e) {
    ok(true, "Should be rejected");
    ok(e instanceof Error, "Should reject with Error.");
    ok(e.message == "Fail bad!", "Message should match.");
    runTest();
  });
}

function promiseRaceThrow() {
  var p = Promise.race([
    new Promise(function() {
      throw new ReferenceError("placeholder for nonexistent function call");
    }),
    new Promise(function(resolve) {
      setTimeout(resolve, 0);
    }),
  ]);

  p.then(function() {
    ok(false, "Should not resolve when winning Promise had an error.");
    runTest();
  }, function(e) {
    ok(true, "Should be rejected");
    ok(e instanceof ReferenceError, "Should reject with ReferenceError for function nonExistent().");
    runTest();
  });
}

var tests = [
              promiseUtilitiesDefined,
              promiseAllEmptyArray,
              promiseAllArray,
              promiseAllIterable,
              promiseAllWaitsForAllPromises,
              promiseAllRejectFails,
              promiseAllCastError,
              promiseAllEnumerable,

              promiseRaceEmpty,
              promiseRaceValuesArray,
              promiseRacePromiseArray,
              promiseRaceIterable,
              promiseRaceReject,
              promiseRaceThrow,
            ];

function runTest() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");
runTest();
// -->
</script>
</pre>
</body>
</html>