[FIX] OPW 575691: parse server datetime manually
[odoo/odoo.git] / addons / web / static / test / list-utils.js
1 $(document).ready(function () {
2     var openerp,
3         create = function (o) {
4             if (typeof Object.create === 'function') {
5                 return Object.create(o);
6             }
7             function Cls() {}
8             Cls.prototype = o;
9             return new Cls;
10         };
11     module('list-events', {
12         setup: function () {
13             openerp = window.openerp.init();
14             window.openerp.web.list(openerp);
15         }
16     });
17     test('Simple event triggering', function () {
18         var e = create(openerp.web.list.Events), passed = false;
19         e.bind('foo', function () { passed = true; });
20         e.trigger('foo');
21         ok(passed);
22     });
23     test('Bind all', function () {
24         var e = create(openerp.web.list.Events), event = null;
25         e.bind(null, function (ev) { event = ev; });
26         e.trigger('foo');
27         strictEqual(event, 'foo');
28     });
29     test('Propagate trigger params', function () {
30        var e = create(openerp.web.list.Events), p = false;
31         e.bind(null, function (_, param) { p = param });
32         e.trigger('foo', true);
33         strictEqual(p, true)
34     });
35     test('Bind multiple callbacks', function () {
36         var e = create(openerp.web.list.Events), count;
37         e.bind('foo', function () { count++; })
38          .bind('bar', function () { count++; })
39          .bind(null, function () { count++; })
40          .bind('foo', function () { count++; })
41          .bind(null, function () { count++; })
42          .bind(null, function () { count++; });
43
44         count = 0;
45         e.trigger('foo');
46         strictEqual(count, 5);
47
48         count = 0;
49         e.trigger('bar');
50         strictEqual(count, 4);
51
52         count = 0;
53         e.trigger('baz');
54         strictEqual(count, 3);
55     });
56     test('Mixin events', function () {
57         var cls = openerp.web.Class.extend({
58             method: function () { this.trigger('e'); }
59         });
60         cls.include(openerp.web.list.Events);
61         var instance = new cls, triggered = false;
62
63         instance.bind('e', function () { triggered = true; });
64         instance.method();
65
66         ok(triggered);
67     });
68     test('Unbind all handlers', function () {
69         var e = create(openerp.web.list.Events), passed = 0;
70         e.bind('foo', function () { passed++; });
71         e.trigger('foo');
72         strictEqual(passed, 1);
73         e.unbind('foo');
74         e.trigger('foo');
75         strictEqual(passed, 1);
76     });
77     test('Unbind one handler', function () {
78         var e = create(openerp.web.list.Events), p1 = 0, p2 = 0,
79             h1 = function () { p1++; }, h2 = function () { p2++; };
80         e.bind('foo', h1);
81         e.bind('foo', h2);
82         e.trigger('foo');
83         strictEqual(p1, 1);
84         strictEqual(p2, 1);
85         e.unbind('foo', h1);
86         e.trigger('foo');
87         strictEqual(p1, 1);
88         strictEqual(p2, 2);
89     });
90
91     module('list-records', {
92         setup: function () {
93             openerp = window.openerp.init();
94             window.openerp.web.list(openerp);
95         }
96     });
97     test('Basic record initialization', function () {
98         var r = new openerp.web.list.Record({qux: 3});
99         r.set('foo', 1);
100         r.set('bar', 2);
101         strictEqual(r.get('foo'), 1);
102         strictEqual(r.get('bar'), 2);
103         strictEqual(r.get('qux'), 3);
104     });
105     test('Change all the things', function () {
106         var r = new openerp.web.list.Record(), changed = false, field;
107         r.bind('change', function () { changed = true; });
108         r.bind(null, function (e) { field = field || e.split(':')[1]});
109         r.set('foo', 1);
110         strictEqual(r.get('foo'), 1);
111         ok(changed);
112         strictEqual(field, 'foo');
113     });
114     test('Change single field', function () {
115         var r = new openerp.web.list.Record(), changed = 0;
116         r.bind('change:foo', function () { changed++; });
117         r.set('foo', 1);
118         r.set('bar', 1);
119         strictEqual(r.get('foo'), 1);
120         strictEqual(r.get('bar'), 1);
121         strictEqual(changed, 1);
122     });
123
124     module('list-collections-degenerate', {
125         setup: function () {
126             openerp = window.openerp.init();
127             window.openerp.web.list(openerp);
128         }
129     });
130     test('Fetch from collection', function () {
131         var c = new openerp.web.list.Collection();
132         strictEqual(c.length, 0);
133         c.add({id: 1, value: 2});
134         c.add({id: 2, value: 3});
135         c.add({id: 3, value: 5});
136         c.add({id: 4, value: 7});
137         strictEqual(c.length, 4);
138         var r = c.at(2), r2 = c.get(1);
139
140         ok(r instanceof openerp.web.list.Record);
141         strictEqual(r.get('id'), 3);
142         strictEqual(r.get('value'), 5);
143
144         ok(r2 instanceof openerp.web.list.Record);
145         strictEqual(r2.get('id'), 1);
146         strictEqual(r2.get('value'), 2);
147     });
148     test('Add at index', function () {
149         var c = new openerp.web.list.Collection([
150             {id: 1, value: 5},
151             {id: 2, value: 10},
152             {id: 3, value: 20}
153         ]);
154         strictEqual(c.at(1).get('value'), 10);
155         equal(c.at(3), undefined);
156         c.add({id:4, value: 55}, {at: 1});
157         strictEqual(c.at(1).get('value'), 55);
158         strictEqual(c.at(3).get('value'), 20);
159     });
160     test('Remove record', function () {
161         var c = new openerp.web.list.Collection([
162             {id: 1, value: 5},
163             {id: 2, value: 10},
164             {id: 3, value: 20}
165         ]);
166         var record = c.get(2);
167         strictEqual(c.length, 3);
168         c.remove(record);
169         strictEqual(c.length, 2);
170         equal(c.get(2), undefined);
171         strictEqual(c.at(1).get('value'), 20);
172     });
173     test('Remove unbind', function () {
174         var changed = false,
175             c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
176         c.bind('change', function () { changed = true; });
177         var record = c.get(1);
178         c.remove(record);
179         record.set('value', 42);
180         ok(!changed, 'removed records should not trigger events in their ' +
181                      'parent collection');
182     });
183     test('Reset', function () {
184         var event, obj, c = new openerp.web.list.Collection([
185             {id: 1, value: 5},
186             {id: 2, value: 10},
187             {id: 3, value: 20}
188         ]);
189         c.bind(null, function (e, instance) { event = e; obj = instance; });
190         c.reset();
191         strictEqual(c.length, 0);
192         strictEqual(event, 'reset');
193         strictEqual(obj, c);
194         c.add([
195             {id: 1, value: 5},
196             {id: 2, value: 10},
197             {id: 3, value: 20}
198         ]);
199         c.reset([{id: 42, value: 55}]);
200         strictEqual(c.length, 1);
201         strictEqual(c.get(42).get('value'), 55);
202     });
203     test('Reset unbind', function () {
204         var changed = false,
205             c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
206         c.bind('change', function () { changed = true; });
207         var record = c.get(1);
208         c.reset();
209         record.set('value', 42);
210         ok(!changed, 'removed records should not trigger events in their ' +
211                      'parent collection');
212     });
213
214     test('Events propagation', function () {
215         var values = [];
216         var c = new openerp.web.list.Collection([
217             {id: 1, value: 5},
218             {id: 2, value: 10},
219             {id: 3, value: 20}
220         ]);
221         c.bind('change:value', function (e, record, value) {
222             values.push(value);
223         });
224         c.get(1).set('value', 6);
225         c.get(2).set('value', 11);
226         c.get(3).set('value', 21);
227         deepEqual(values, [6, 11, 21]);
228     });
229     test('BTree', function () {
230         var root = new openerp.web.list.Collection(),
231             c = root.proxy('admin'),
232             total = 0;
233         c.add({id: 1, name: "Administrator", login: 'admin'});
234         c.add({id: 3, name: "Demo", login: 'demo'});
235         root.bind('change:wealth', function () {
236             total = (root.get(1).get('wealth') || 0) + (root.get(3).get('wealth') || 0);
237         });
238
239         strictEqual(total, 0);
240         c.at(0).set('wealth', 42);
241         strictEqual(total, 42);
242         c.at(1).set('wealth', 5);
243         strictEqual(total, 47);
244     });
245
246     module('list-hofs', {
247         setup: function () {
248             openerp = window.openerp.init();
249             window.openerp.web.list(openerp);
250         }
251     });
252     test('each, degenerate', function () {
253         var c = new openerp.web.list.Collection([
254             {id: 1, value: 5},
255             {id: 2, value: 10},
256             {id: 3, value: 20}
257         ]), ids = [];
258         c.each(function (record) {
259             ids.push(record.get('id'));
260         });
261         deepEqual(
262             ids, [1, 2, 3],
263             'degenerate collections should be iterated in record order');
264     });
265     test('each, deep', function () {
266         var root = new openerp.web.list.Collection(),
267             ids = [];
268         root.proxy('foo').add([
269             {id: 1, value: 5},
270             {id: 2, value: 10},
271             {id: 3, value: 20}]);
272         root.proxy('bar').add([
273             {id: 10, value: 5},
274             {id: 20, value: 10},
275             {id: 30, value: 20}]);
276         root.each(function (record) {
277             ids.push(record.get('id'));
278         });
279         // No contract on sub-collection iteration order (for now anyway)
280         ids.sort(function (a, b) { return a - b; });
281         deepEqual(
282             ids, [1, 2, 3, 10, 20, 30],
283             'tree collections should be deeply iterated');
284     });
285     test('map, degenerate', function () {
286         var c = new openerp.web.list.Collection([
287             {id: 1, value: 5},
288             {id: 2, value: 10},
289             {id: 3, value: 20}
290         ]);
291         var ids = c.map(function (record) {
292             return record.get('id');
293         });
294         deepEqual(
295             ids, [1, 2, 3],
296             'degenerate collections should be iterated in record order');
297     });
298     test('map, deep', function () {
299         var root = new openerp.web.list.Collection();
300         root.proxy('foo').add([
301             {id: 1, value: 5},
302             {id: 2, value: 10},
303             {id: 3, value: 20}]);
304         root.proxy('bar').add([
305             {id: 10, value: 5},
306             {id: 20, value: 10},
307             {id: 30, value: 20}]);
308         var ids = root.map(function (record) {
309             return record.get('id');
310         });
311         // No contract on sub-collection iteration order (for now anyway)
312         ids.sort(function (a, b) { return a - b; });
313         deepEqual(
314             ids, [1, 2, 3, 10, 20, 30],
315             'tree collections should be deeply iterated');
316     });
317 });