Promise constructor should throw when not called with "new"
[WebKit-https.git] / LayoutTests / js / script-tests / Promise-types.js
1 description("Promises - Test basic types / exceptions.");
2
3 // Promise
4 debug("");
5 debug("Promises");
6 debug("");
7
8 // Promises should be of type Promise.
9
10 var aPromise = new Promise(function(resolve, reject) { resolve(1); });
11
12 debug("aPromise = new Promise(...)")
13 shouldBeType("aPromise", "Promise");
14 shouldBe("String(aPromise)", "'[object Promise]'");
15
16 shouldBeDefined("aPromise.then");
17 shouldBeType("aPromise.then", "Function");
18 shouldBe("aPromise.then.length", "2");
19 shouldBeDefined("aPromise.catch");
20 shouldBeType("aPromise.catch", "Function");
21 shouldBe("aPromise.catch.length", "1");
22
23 debug("aPromise2 = Promise(...)")
24 shouldThrow(`Promise(function(resolve, reject) { resolve(1); })`);
25
26 // Promise constructor
27 debug("");
28 debug("Promise constructor");
29 debug("");
30
31 // Need at least one parameter.
32 shouldBe("Promise.length", "1");
33 shouldThrow("new Promise()");
34 shouldThrow("Promise()");
35
36 // Parameter must be a function.
37 shouldThrow("new Promise(1)", "'TypeError: Promise constructor takes a function argument'");
38 shouldThrow("new Promise('hello')", "'TypeError: Promise constructor takes a function argument'");
39 shouldThrow("new Promise([])", "'TypeError: Promise constructor takes a function argument'");
40 shouldThrow("new Promise({})", "'TypeError: Promise constructor takes a function argument'");
41 shouldThrow("new Promise(null)", "'TypeError: Promise constructor takes a function argument'");
42 shouldThrow("new Promise(undefined)", "'TypeError: Promise constructor takes a function argument'");
43
44 shouldThrow("Promise(1)", "'TypeError: Type error'");
45 shouldThrow("Promise('hello')", "'TypeError: Type error'");
46 shouldThrow("Promise([])", "'TypeError: Type error'");
47 shouldThrow("Promise({})", "'TypeError: Type error'");
48 shouldThrow("Promise(null)", "'TypeError: Type error'");
49 shouldThrow("Promise(undefined)", "'TypeError: Type error'");
50
51 // Promise statics
52 debug("");
53 debug("Promise statics");
54 debug("");
55
56
57 // Need at least one parameter.
58 shouldBeType("Promise.resolve", "Function");
59 shouldBe("Promise.resolve.length", "1");
60 shouldNotThrow("Promise.resolve(1)");
61
62 shouldBeType("Promise.reject", "Function");
63 shouldBe("Promise.reject.length", "1");
64 shouldNotThrow("Promise.reject(1)");
65
66 // Should return Promise objects.
67 shouldBeType("Promise.resolve(1)", "Promise");
68 shouldBeType("Promise.reject(1)", "Promise");
69
70