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