summaryrefslogtreecommitdiff
path: root/scorpion-tests.js
blob: 0bbe721d0197cabe1e878e39f4351d77f0bf65b2 (about) (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*global describe,it,expect,beforeEach,Scorpion, setTimeout*/

describe("deferred", function() {
    var deferred, promise;
    beforeEach(function() {
        deferred = Scorpion.defer();
        promise = deferred.promise;
    });
    this.timeout(100);

    describe("promise", function() {
        it("should have a 'then' method", function() {
            expect(promise.then).not.to.equal(undefined);
        });

        it("should be able to chain 'then' methods", function() {
            var promise1 = promise;
            var promise2 = promise1.then(function(x) {
                return x + 1;
            });
            var promise3 = promise1.then(function(x) {
                return x + 1;
            });
            expect(promise1.then).not.to.equal(undefined);
            expect(promise2.then).not.to.equal(undefined);
            expect(promise3.then).not.to.equal(undefined);
        });
    });

    describe("resolution", function() {

        it("should resolve asynchronously", function(done) {
            var hasRun = false;
            promise.then(function(val) {
                hasRun = true;
                return val;
            });
            deferred.resolve("value");
            expect(hasRun).to.equal(false);
            setTimeout(function() {
                expect(hasRun).to.equal(true);
                done();
            });
        });

        it("should propagate through multiple promises", function(done) {
            var promise2 = promise.then(function(value) {
                return value + 1;
            });
            var promise3 = promise2.then(function(value) {
                return value + 1;
            });
            promise3.then(function(value) {
                expect(value).to.equal(3);
                done();
            });
            deferred.resolve(1);
        });

        it("should call all registered handlers", function(done) {
            var called = 0;
            promise.then(function() {called++;});
            promise.then(function() {called++;});
            promise.then(function() {called++;});
            expect(called).to.equal(0);
            deferred.resolve(null);
            setTimeout(function() {
                expect(called).to.equal(3);
                done();
            });
        });

        it("should call handlers, even when added after resolution", function(done) {
            deferred.resolve(null);
            promise.then(function() {
                done();
            });
        });

        it("should not allow multiple resolution", function() {
            deferred.resolve(1);
            expect(function() {
                deferred.resolve(2);
            }).to.throw();
        });

        it("should not allow rejecting after resolving", function() {
            deferred.resolve(1);
            expect(function() {
                deferred.reject(2);
            }).to.throw();
        });

        it("should handle a returned promise by 'unwrapping' it", function(done) {
            promise.then(function(value) {
                var deferred = Scorpion.defer();
                deferred.resolve(value + 1);
                return deferred.promise;
            }).then(function(value) {
                try {
                    expect(value).to.equal(2);
                    done();
                } catch (e) {
                    done(e);
                }
            });
            deferred.resolve(1);
        });
    });

    describe("rejection", function() {
        it("should reject asynchronously", function(done) {
            var hasRun = false;
            promise.then(null, function(val) {
                hasRun = true;
                return val;
            });
            deferred.reject("value");
            expect(hasRun).to.equal(false);
            setTimeout(function() {
                expect(hasRun).to.equal(true);
                done();
            });
        });


        it("should be turned into resolution by a handler", function(done) {
            var promise2 = promise.then(function(value) {
                return value + 1;
            }, function(value) {
                return value + 100;
            });
            promise2.then(function(value) {
                try {
                    expect(value).to.equal(101);
                    done();
                } catch (e)  {
                    done(e);
                }
            });
            deferred.reject(1);
        });

        it("should call all registered handlers", function(done) {
            var called = 0;
            promise.then(null, function() {called++;});
            promise.then(null, function() {called++;});
            promise.then(null, function() {called++;});
            expect(called).to.equal(0);
            deferred.reject(null);
            setTimeout(function() {
                expect(called).to.equal(3);
                done();
            });
        });

        it("should not allow multiple rejection", function() {
            deferred.reject(1);
            expect(function() {
                deferred.reject(2);
            }).to.throw();
        });

        it("should not allow resolving after rejecting", function() {
            deferred.reject(1);
            expect(function() {
                deferred.resolve(2);
            }).to.throw();
        });

        it("should call handlers, even when added after resolution", function(done) {
            deferred.reject(null);
            promise.then(null, function() {
                done();
            });
        });

        it("should handle a returned promise by 'unwrapping' in the error case", function(done) {
            promise.then(function(value) {
                var deferred = Scorpion.defer();
                deferred.reject(value + 1);
                return deferred.promise;
            }).then(null, function(value) {
                try {
                    expect(value).to.equal(2);
                    done();
                } catch (e) {
                    done(e);
                }
            });
            deferred.resolve(1);
        });
    });

    describe("waitForAll", function() {
        it("should resolve with an array if all successful", function(done) {
            var d1 = Scorpion.defer(),
                d2 = Scorpion.defer(),
                d3 = Scorpion.defer();
            Scorpion
                .waitForAll([d1.promise, d2.promise, d3.promise])
                .then(function(values) {
                    if (values.length == 3 &&
                        values[0] === 0 &&
                        values[1] === 1 &&
                        values[2] === 2) {
                        done();
                    } else {
                        done(new Error("Error in resolved result: " + values));
                    }
                }, done);
            d1.resolve(0);
            d2.resolve(1);
            d3.resolve(2);
        });

        it("should reject with an object of successes/errors if any fail", function(done) {
            var d1 = Scorpion.defer(),
                d2 = Scorpion.defer(),
                d3 = Scorpion.defer();
            Scorpion
                .waitForAll([d1.promise, d2.promise, d3.promise])
                .then(function(values) {
                    done(new Error("incorrectly resolved promise"));
                }, function(e) {
                    if (e.errors && e.values &&
                        e.values[0] === 0 &&
                        e.values[1] === 1 &&
                        e.errors[2] === 2) {
                        done();
                    } else {
                        done(new Error("incorrect reject value"));
                    }
                });
            d1.resolve(0);
            d2.resolve(1);
            d3.reject(2);
        });
    });
});

describe("injector", function() {
    var injector, valuePlugin;
    beforeEach(function() {
        valuePlugin = new Scorpion.ValuePlugin();
        injector = new Scorpion([valuePlugin]);
    });
    this.timeout(100);

    describe("value registration and retrieval", function() {
        it("works", function(done) {
            injector.register("a", function(){return "the value of a";});
            injector.get("a").then(function(value){
                if (value == "the value of a")
                    done();
                else
                    done(new Error("incorrect value for a (" + value + ")"));
            }, done);
        });

        it("with dependencies works", function(done) {
            injector
                .register("a", function(){return "the value of a";})
                .register("b", ["a", function(a){return a.replace(/a/g, "b");}]);
            injector.get("b").then(function(value){
                if (value == "the vblue of b")
                    done();
                else
                    done(new Error("incorrect value for b (" + value + ")"));
            }, done);
        });
    });

    describe("destroy handlers", function() {
        it("work with single gets", function(done) {
            injector.register("a", function(){return "a";});
            expect("a" in valuePlugin.values).to.equal(false);
            var result = injector.get("a");
            result.then(function() {
                if ("a" in valuePlugin.values) {
                    result.destroy().then(function() {
                        if ("a" in valuePlugin.values) {
                            done(new Error("Value found for a after destruction"));
                        } else {
                            done();
                        }
                    });
                } else {
                    done(new Error("No value found for a"));
                }
            }, done);
        });

        it("works with multiple gets", function(done) {
            injector.register("a", function(){return "a";});
            expect("a" in valuePlugin.values).to.equal(false);
            var result = [injector.get("a"), injector.get("a"), injector.get("a")];
            result[1].destroy();
            result[2].destroy();
            result[0].then(function() {
                if ("a" in valuePlugin.values) {
                    result[0].destroy().then(function() {
                        if ("a" in valuePlugin.values) {
                            done(new Error("Value found for a after destruction"));
                        } else {
                            done();
                        }
                    });
                } else {
                    done(new Error("No value found for a"));
                }
            }, done);
        });

        it("works through dependencies", function(done) {
            injector
                .register("a", function(){return "a";})
                .register("b", ["a", function(a){return a + "b";}]);
            expect("a" in valuePlugin.values).to.equal(false);
            var result = injector.get("b");
            result.then(function() {
                if ("a" in valuePlugin.values) {
                    result.destroy().then(function() {
                        if ("a" in valuePlugin.values) {
                            done(new Error("Value found for a after destruction"));
                        } else {
                            done();
                        }
                    });
                } else {
                    done(new Error("No value found for a"));
                }
            }, done);
        });
    });

    describe("cyclic dependency detection", function() {
        it("detects simple cycles", function(done) {
            injector.register("a", ["b", function(b) {return b;}]);
            injector.register("b", ["a", function(a) {return a;}]);
            injector.get("a").then(function() {
                done(new Error("Promise should have been rejected"));
            }, function(e) {
                done();
            });
        });

        it("detects cycles with intermediate nodes", function(done) {
            injector.register("a", ["c", function(c) {return c;}]);
            injector.register("b", ["a", function(a) {return a;}]);
            injector.register("c", ["b", function(b) {return b;}]);
            injector.get("a").then(function() {
                done(new Error("Promise should have been rejected"));
            }, function(e) {
                done();
            });
        });
    });
});