source: Dev/trunk/src/client/dojo/tests/when.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 2.5 KB
Line 
1define([
2        "doh/main",
3        "dojo/Deferred",
4        "dojo/promise/Promise",
5        "dojo/when"
6], function(doh, Deferred, Promise, when){
7        var tests = {
8                "when() returns the same promise without callbacks": function(t){
9                        var obj = {};
10                        var promise1 = when(obj);
11                        t.t(promise1 instanceof Promise);
12                        var promise2 = when(this.deferred.promise);
13                        t.t(promise2 instanceof Promise);
14                        t.t(this.deferred.promise === promise2);
15                },
16
17                "when() doesn't convert to promise if errback is passed but no callback": function(t){
18                        var obj = {};
19                        var result = when(obj, null, function(){});
20                        t.t(result === obj);
21                },
22
23                "when() with a result value": function(t){
24                        var obj = {};
25                        var received;
26                        when(obj, function(result){ received = result; });
27                        t.t(received === obj);
28                },
29
30                "when() with a result value, returns result of callback": function(t){
31                        var obj1 = {}, obj2 = {};
32                        var received;
33                        var returned = when(obj1, function(result){
34                                received = result;
35                                return obj2;
36                        });
37                        t.t(received === obj1);
38                        t.t(returned === obj2);
39                },
40
41                "when() with a promise that gets resolved": function(t){
42                        var obj = {};
43                        var received;
44                        when(this.deferred.promise, function(result){ received = result; });
45                        this.deferred.resolve(obj);
46                        t.t(received === obj);
47                },
48
49                "when() with a promise that gets rejected": function(t){
50                        var obj = {};
51                        var received;
52                        when(this.deferred.promise, null, function(result){ received = result; });
53                        this.deferred.reject(obj);
54                        t.t(received === obj);
55                },
56
57                "when() with a promise that gets progress": function(t){
58                        var obj = {};
59                        var received;
60                        when(this.deferred.promise, null, null, function(result){ received = result; });
61                        this.deferred.progress(obj);
62                        t.t(received === obj);
63                },
64
65                "when() with chaining of the result": function(t){
66                        function square(n){ return n * n; }
67
68                        var received;
69                        when(2).then(square).then(square).then(function(n){ received = n; });
70                        t.is(received, 16);
71                },
72
73                "when() converts foreign promises": function(t){
74                        var _callback;
75                        var foreign = { then: function(cb){ _callback = cb; } };
76                        var promise = when(foreign);
77
78                        var obj = {};
79                        var received;
80                        promise.then(function(result){ received = result; });
81                        _callback(obj);
82                        t.t(promise instanceof Promise);
83                        t.t(received === obj);
84                }
85        };
86
87        var wrapped = [];
88        for(var name in tests){
89                wrapped.push({
90                        name: name,
91                        setUp: setUp,
92                        runTest: tests[name]
93                });
94        }
95
96        function setUp(){
97                this.deferred = new Deferred;
98        }
99
100        doh.register("tests.when", wrapped);
101});
Note: See TracBrowser for help on using the repository browser.