Debian Codegraph @

test/promise/subclass.js

raw
1 describe('Support user subclassing of Promise', function () {
2 'use strict';
3
4 it('should work if you do it right', function (done) {
5 // This is the "correct" es6-compatible way.
6 // (Thanks, @domenic and @zloirock!)
7 var MyPromise = function (executor) {
8 var self = new Promise(executor);
9 Object.setPrototypeOf(self, MyPromise.prototype);
10 self.mine = 'yeah';
11 return self;
12 };
13 if (!Object.setPrototypeOf) { return done(); } // skip test if on IE < 11
14 Object.setPrototypeOf(MyPromise, Promise);
15 MyPromise.prototype = Object.create(Promise.prototype, {
16 constructor: { value: MyPromise }
17 });
18
19 // let's try it!
20 var p1 = MyPromise.resolve(5);
21 assert.strictEqual(p1.mine, 'yeah');
22 p1 = p1.then(function (x) {
23 assert.strictEqual(x, 5);
24 });
25 assert.strictEqual(p1.mine, 'yeah');
26
27 var p2 = new MyPromise(function (r) { r(6); });
28 assert.strictEqual(p2.mine, 'yeah');
29 p2 = p2.then(function (x) {
30 assert.strictEqual(x, 6);
31 });
32 assert.strictEqual(p2.mine, 'yeah');
33
34 var p3 = MyPromise.all([p1, p2]);
35 assert.strictEqual(p3.mine, 'yeah');
36 p3.then(function () { done(); }, done);
37 });
38
39 it("should throw if you don't inherit at all", function () {
40 var MyPromise = function () {};
41 assert['throws'](function () {
42 Promise.all.call(MyPromise, []);
43 }, TypeError);
44 });
45 });