summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resources/examples/apisample13.html
blob: f6cf0eec83186e0a91d050bd7f7f7288321a66fc (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
<!DOCTYPE HTML>
<html>
<head>
<title>Promise Tests</title>
</head>
<body>
<h1>Promise Tests</h1>
<p>This test demonstrates the use of <tt>promise_test</tt>. Assumes ECMAScript 6
Promise support. Some failures are expected.</p>
<div id="log"></div>
<script src="../testharness.js"></script>
<script src="../testharnessreport.js"></script>
<script>
test(
    function() {
        var p = new Promise(function(resolve, reject){});
        assert_true("then" in p);
        assert_equals(typeof Promise.resolve, "function");
        assert_equals(typeof Promise.reject, "function");
    },
    "Promises are supported in your browser");

promise_test(
    function() {
        return Promise.resolve("x")
            .then(
                function(value) {
                    assert_equals(value,
                                  "x",
                                  "Fulfilled promise should pass result to " +
                                  "fulfill reaction.");
                });
    },
    "Promise fulfillment with result");

promise_test(
    function(t) {
        return Promise.reject(new Error("fail"))
            .then(t.unreached_func("Promise should reject"),
                function(reason) {
                    assert_true(
                        reason instanceof Error,
                        "Rejected promise should pass reason to fulfill reaction.");
                    assert_equals(
                        reason.message,
                        "fail",
                        "Rejected promise should pass reason to reject reaction.");
                });
    },
    "Promise rejection with result");

promise_test(
    function() {
        var resolutions = [];
        return Promise.resolve("a")
            .then(
                function(value) {
                    resolutions.push(value);
                    return "b";
                })
            .then(
                function(value) {
                    resolutions.push(value);
                    return "c";
                })
            .then(
                function(value) {
                    resolutions.push(value);
                    assert_array_equals(resolutions, ["a", "b", "c"]);
                });
    },
    "Chain of promise resolutions");

promise_test(
    function(t) {
        var resolutions = [];
        return Promise.resolve("x")
            .then(
                function(value) {
                    assert_true(false, "Expected failure.");
                })
            .then(t.unreached_func("UNEXPECTED FAILURE: Promise should not have resolved."));
    },
    "Assertion failure in a fulfill reaction (should FAIL with an expected failure)");

promise_test(
    function(t) {
        return new Promise(
                function(resolve, reject) {
                    reject(123);
                })
            .then(t.unreached_func("UNEXPECTED FAILURE: Fulfill reaction reached after rejection."),
                  t.unreached_func("Expected failure."));
    },
    "unreached_func as reactor (should FAIL with an expected failure)");

promise_test(
    function() {
        return true;
    },
    "promise_test with function that doesn't return a Promise");

promise_test(function(){},
             "promise_test with function that doesn't return anything");

promise_test(
    function() {
        return Promise.reject("Expected rejection");
    },
    "promise_test with unhandled rejection (should FAIL)");

promise_test(
    function() {
        return Promise.resolve(10)
            .then(
                function(value) {
                    throw Error("Expected exception.");
                });
    },
    "promise_test with unhandled exception in fulfill reaction (should FAIL)");

promise_test(
    function(t) {
        return Promise.reject(10)
            .then(
                t.unreached_func("UNEXPECTED FAILURE: Fulfill reaction reached after rejection."),
                function(value) {
                    throw Error("Expected exception.");
                });
    },
    "promise_test with unhandled exception in reject reaction (should FAIL)");
</script>