aboutsummaryrefslogtreecommitdiff
path: root/test/promises.tests.js
blob: a6e35acb618c02a035d96b0141bb93d37b2a7af8 (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
var assert = require("assert");

var promises = require("../lib/promises");
var test = require("./test")(module);

test("try", {
    "when function succeeds with non-promise then promise is resolved": function() {
        return promises.try(function() {
            return "success";
        }).then(function(result) {
            assert.strictEqual(result, "success");
        });
    },

    "when function succeeds with promise then promise is resolved": function() {
        return promises.try(function() {
            return promises.resolve("success");
        }).then(function(result) {
            assert.strictEqual(result, "success");
        });
    },

    "when function throws error then promise is rejected": function() {
        return promises.try(function() {
            throw new Error("failure");
        }).then(
            function() {
                assert.fail("Expected rejection");
            },
            function(error) {
                assert.strictEqual(error.message, "failure");
            }
        );
    },

    "when function fails with promise then promise is rejected": function() {
        return promises.try(function() {
            return promises.reject(new Error("failure"));
        }).then(
            function() {
                assert.fail("Expected rejection");
            },
            function(error) {
                assert.strictEqual(error.message, "failure");
            }
        );
    }
});

test("props", {
    "props({}) resolve to {}": function() {
        return promises.props({}).then(function(result) {
            assert.deepStrictEqual(result, {});
        });
    },

    "when object has promise values then props returns with all values resolved": function() {
        return promises.props({
            a: promises.resolve(1),
            b: promises.resolve(2),
            c: promises.resolve(3)
        }).then(function(result) {
            assert.deepStrictEqual(result, {
                a: 1,
                b: 2,
                c: 3
            });
        });
    },

    "when object has non-promise values then props returns with same values": function() {
        return promises.props({
            a: 1,
            b: 2,
            c: 3
        }).then(function(result) {
            assert.deepStrictEqual(result, {
                a: 1,
                b: 2,
                c: 3
            });
        });
    },

    "when value is rejected promise then props is rejected promise": function() {
        return promises.props({
            a: promises.resolve(1),
            b: promises.reject(new Error("failure")),
            c: promises.resolve(3)
        }).then(
            function(result) {
                assert.fail("Expected rejection");
            },
            function(error) {
                assert.strictEqual(error.message, "failure");
            }
        );
    },

    "props(...).also(...) can be used to add props": function() {
        return promises.props({
            a: promises.resolve(1)
        }).also(function(result) {
            return {
                b: result.a + 1
            };
        }).then(function(result) {
            assert.deepStrictEqual(result, {
                a: 1,
                b: 2
            });
        });
    },

    "also can be used multiple times": function() {
        return promises.props({
            a: promises.resolve(1)
        }).also(function(result) {
            return {
                b: result.a + 1
            };
        }).also(function(result) {
            return {
                c: result.b + 1
            };
        }).then(function(result) {
            assert.deepStrictEqual(result, {
                a: 1,
                b: 2,
                c: 3
            });
        });
    }
});