[FIX] deferred.pipe -> deferred.then
[odoo/odoo.git] / addons / web / static / test / testing.js
1 openerp.testing.section('testing.stack', function (test) {
2     // I heard you like tests, so I put tests in your testing infrastructure,
3     // so you can test what you test
4     var reject = function () {
5         // utility function, rejects a success
6         var args = _.toArray(arguments);
7         return $.Deferred(function (d) {
8             d.reject.apply(d, ["unexpected success"].concat(args));
9         });
10     };
11     test('direct, value, success', {asserts: 1}, function () {
12         var s = openerp.testing.Stack();
13         return s.execute(function () {
14             return 42;
15         }).then(function (val) {
16             strictEqual(val, 42, "should return the handler value");
17         });
18     });
19     test('direct, deferred, success', {asserts: 1}, function () {
20         var s = openerp.testing.Stack();
21         return s.execute(function () {
22             return $.when(42);
23         }).then(function (val) {
24             strictEqual(val, 42, "should return the handler value")
25         });
26     });
27     test('direct, value, error', {asserts: 1}, function () {
28         var s = openerp.testing.Stack();
29         return s.execute(function () {
30             throw new Error("foo");
31         }).then(reject, function (f) {
32             strictEqual(f.message, "foo", "should reject with exception");
33             return $.when();
34         });
35     });
36     test('direct, deferred, failure', {asserts: 1}, function () {
37         var s = openerp.testing.Stack();
38         return s.execute(function () {
39             return $.Deferred(function (d) {
40                 d.reject("failed");
41             });
42         }).then(reject, function (f) {
43             strictEqual(f, "failed", "should propagate failure");
44             return $.when();
45         });
46     });
47
48     test('successful setup', {asserts: 2}, function () {
49         var setup_done = false;
50         var s = openerp.testing.Stack();
51         return s.push(function () {
52             return $.Deferred(function (d) {
53                 setTimeout(function () {
54                     setup_done = true;
55                     d.resolve(2);
56                 }, 50);
57             });
58         }).execute(function () {
59             return 42;
60         }).then(function (val) {
61             ok(setup_done, "should have executed setup");
62             strictEqual(val, 42, "should return executed function value (not setup)");
63         });
64     });
65     test('successful teardown', {asserts: 2}, function () {
66         var teardown = false;
67         var s = openerp.testing.Stack();
68         return s.push(null, function () {
69             return $.Deferred(function (d) {
70                 setTimeout(function () {
71                     teardown = true;
72                     d.resolve(2);
73                 }, 50);
74             });
75         }).execute(function () {
76             return 42;
77         }).then(function (val) {
78             ok(teardown, "should have executed teardown");
79             strictEqual(val, 42, "should return executed function value (not setup)");
80         });
81     });
82     test('successful setup and teardown', {asserts: 3}, function () {
83         var setup = false, teardown = false;
84         var s = openerp.testing.Stack();
85         return s.push(function () {
86             return $.Deferred(function (d) {
87                 setTimeout(function () {
88                     setup = true;
89                     d.resolve(2);
90                 }, 50);
91             });
92         }, function () {
93             return $.Deferred(function (d) {
94                 setTimeout(function () {
95                     teardown = true;
96                     d.resolve(2);
97                 }, 50);
98             });
99         }).execute(function () {
100             return 42;
101         }).then(function (val) {
102             ok(setup, "should have executed setup");
103             ok(teardown, "should have executed teardown");
104             strictEqual(val, 42, "should return executed function value (not setup)");
105         });
106     });
107
108     test('multiple setups', {asserts: 2}, function () {
109         var setups = 0;
110         var s = openerp.testing.Stack();
111         return s.push(function () {
112             setups++;
113         }).push(function () {
114             setups++;
115         }).push(function () {
116             setups++;
117         }).push(function () {
118             setups++;
119         }).execute(function () {
120             return 42;
121         }).then(function (val) {
122             strictEqual(setups, 4, "should have executed all setups of stack");
123             strictEqual(val, 42);
124         });
125     });
126     test('multiple teardowns', {asserts: 2}, function () {
127         var teardowns = 0;
128         var s = openerp.testing.Stack();
129         return s.push(null, function () {
130             teardowns++;
131         }).push(null, function () {
132             teardowns++;
133         }).push(null, function () {
134             teardowns++;
135         }).push(null, function () {
136             teardowns++;
137         }).execute(function () {
138             return 42;
139         }).then(function (val) {
140             strictEqual(teardowns, 4, "should have executed all teardowns of stack");
141             strictEqual(val, 42);
142         });
143     });
144     test('holes in setups', {asserts: 2}, function () {
145         var setups = [];
146         var s = openerp.testing.Stack();
147         return s.push(function () {
148             setups.push(0);
149         }).push().push().push(function () {
150             setups.push(3);
151         }).push(function () {
152             setups.push(4);
153         }).push().push(function () {
154             setups.push(6);
155         }).execute(function () {
156             return 42;
157         }).then(function (val) {
158             deepEqual(setups, [0, 3, 4, 6],
159                 "should have executed setups in correct order");
160             strictEqual(val, 42);
161         });
162     });
163     test('holes in teardowns', {asserts: 2}, function () {
164         var teardowns = [];
165         var s = openerp.testing.Stack();
166         return s.push(null, function () {
167             teardowns.push(0);
168         }).push().push().push(null, function () {
169             teardowns.push(3);
170         }).push(null, function () {
171             teardowns.push(4);
172         }).push().push(null, function () {
173             teardowns.push(6);
174         }).execute(function () {
175             return 42;
176         }).then(function (val) {
177             deepEqual(teardowns, [6, 4, 3, 0],
178                 "should have executed teardowns in correct order");
179             strictEqual(val, 42);
180         });
181
182     });
183
184     test('failed setup', {asserts: 5}, function () {
185         var setup, teardown, teardown2, code;
186         return openerp.testing.Stack().push(function () {
187             setup = true;
188         }, function () {
189             teardown = true;
190         }).push(function () {
191             return $.Deferred().reject("Fail!");
192         }, function () {
193             teardown2 = true;
194         }).execute(function () {
195             code = true;
196             return 42;
197         }).then(reject, function (m) {
198             ok(setup, "should have executed first setup function");
199             ok(teardown, "should have executed first teardown function");
200             ok(!teardown2, "should not have executed second teardown function");
201             strictEqual(m, "Fail!", "should return setup failure message");
202             ok(!code, "should not have executed callback");
203             return $.when();
204         });
205     });
206     test('failed teardown', {asserts: 2}, function () {
207         var teardowns = 0;
208         return openerp.testing.Stack().push(null, function () {
209             teardowns++;
210             return $.Deferred().reject('Fail 1');
211         }).push(null, function () {
212             teardowns++;
213         }).push(null, function () {
214             teardowns++;
215             return $.Deferred().reject('Fail 3');
216         }).execute(function () {
217             return 42;
218         }).then(reject, function (m) {
219             strictEqual(teardowns, 3,
220                 "should have tried executing all teardowns");
221             strictEqual(m, "Fail 3", "should return first failure message");
222                     return $.when();
223         });
224     });
225     test('failed call + teardown', {asserts: 2}, function () {
226         var teardowns = 0;
227         return openerp.testing.Stack().push(null, function () {
228             teardowns++;
229         }).push(null, function () {
230             teardowns++;
231             return $.Deferred().reject('Fail 2');
232         }).execute(function () {
233             return $.Deferred().reject("code");
234         }).then(reject, function (m) {
235             strictEqual(teardowns, 2,
236                 "should have tried executing all teardowns");
237             strictEqual(m, "code", "should return first failure message");
238                     return $.when();
239         });
240     });
241
242     test('arguments passing', {asserts: 9}, function () {
243         var asserter = function (a, b, c) {
244             strictEqual(a, 1);
245             strictEqual(b, "foo");
246             deepEqual(c, {bar: "baz", qux: 42});
247         };
248
249         return openerp.testing.Stack()
250             .push(asserter, asserter)
251             .execute(asserter, 1, "foo", {bar: 'baz', qux: 42});
252     });
253 });