Debian Codegraph @

test/promise/simple.js

raw
1 var failIfThrows = function (done) {
2 'use strict';
3
4 return function (e) {
5 done(e || new Error());
6 };
7 };
8
9 describe('Promise', function () {
10 'use strict';
11
12 specify('sanity check: a fulfilled promise calls its fulfillment handler', function (done) {
13 Promise.resolve(5).then(function (value) {
14 assert.strictEqual(value, 5);
15 }).then(done, failIfThrows(done));
16 });
17
18 specify('directly resolving the promise with itself', function (done) {
19 var resolvePromise;
20 var promise = new Promise(function (resolve) { resolvePromise = resolve; });
21
22 resolvePromise(promise);
23
24 promise.then(
25 function () {
26 assert(false, 'Should not be fulfilled');
27 },
28 function (err) {
29 assert(err instanceof TypeError);
30 }
31 ).then(done, failIfThrows(done));
32 });
33
34 specify('Stealing a resolver and using it to trigger possible reentrancy bug (#83)', function () {
35 var stolenResolver;
36 var StealingPromiseConstructor = function StealingPromiseConstructor(resolver) {
37 stolenResolver = resolver;
38 resolver(function () { }, function () { });
39 };
40
41 var iterable = {};
42 var atAtIterator = '@@iterator'; // on firefox, at least.
43 iterable[atAtIterator] = function () {
44 stolenResolver(null, null);
45 throw new Error(0);
46 };
47
48 assert.doesNotThrow(function () {
49 Promise.all.call(StealingPromiseConstructor, iterable);
50 });
51 });
52
53 specify('resolve with a thenable calls it once', function () {
54 var resolve;
55 var p = new Promise(function (r) { resolve = r; });
56 var count = 0;
57 resolve({
58 then: function () {
59 count += 1;
60 throw new RangeError('reject the promise');
61 }
62 });
63 var a = p.then(function () {})['catch'](function (err) {
64 assert.equal(count, 1);
65 assert.ok(err instanceof RangeError);
66 });
67 var b = p.then(function () {})['catch'](function (err) {
68 assert.equal(count, 1);
69 assert.ok(err instanceof RangeError);
70 });
71 return Promise.all([a, b]);
72 });
73
74 specify('resolve with a thenable that throws on .then, rejects the promise synchronously', function () {
75 var resolve;
76 var p = new Promise(function (r) { resolve = r; });
77 var count = 0;
78 var thenable = Object.defineProperty({}, 'then', {
79 get: function () {
80 count += 1;
81 throw new RangeError('no then for you');
82 }
83 });
84 resolve(thenable);
85 assert.equal(count, 1);
86 var a = p.then(function () {})['catch'](function (err) {
87 assert.equal(count, 1);
88 assert.ok(err instanceof RangeError);
89 });
90 var b = p.then(function () {})['catch'](function (err) {
91 assert.equal(count, 1);
92 assert.ok(err instanceof RangeError);
93 });
94 return Promise.all([a, b]);
95 });
96 });