[IMP] Form: date and datetime widgets are correctly formatted/parsed
[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('Reset', function () {
174         var event, obj, c = new openerp.web.list.Collection([
175             {id: 1, value: 5},
176             {id: 2, value: 10},
177             {id: 3, value: 20}
178         ]);
179         c.bind(null, function (e, instance) { event = e; obj = instance; });
180         c.reset();
181         strictEqual(c.length, 0);
182         strictEqual(event, 'reset');
183         strictEqual(obj, c);
184         c.add([
185             {id: 1, value: 5},
186             {id: 2, value: 10},
187             {id: 3, value: 20}
188         ]);
189         c.reset([{id: 42, value: 55}]);
190         strictEqual(c.length, 1);
191         strictEqual(c.get(42).get('value'), 55);
192     });
193
194     test('Events propagation', function () {
195         var values = [];
196         var c = new openerp.web.list.Collection([
197             {id: 1, value: 5},
198             {id: 2, value: 10},
199             {id: 3, value: 20}
200         ]);
201         c.bind('change:value', function (e, record, value) {
202             values.push(value);
203         });
204         c.get(1).set('value', 6);
205         c.get(2).set('value', 11);
206         c.get(3).set('value', 21);
207         deepEqual(values, [6, 11, 21]);
208     });
209     test('BTree', function () {
210         var root = new openerp.web.list.Collection(),
211             c = root.proxy('admin'),
212             total = 0;
213         c.add({id: 1, name: "Administrator", login: 'admin'});
214         c.add({id: 3, name: "Demo", login: 'demo'});
215         root.bind('change:wealth', function () {
216             total = (root.get(1).get('wealth') || 0) + (root.get(3).get('wealth') || 0);
217         });
218
219         strictEqual(total, 0);
220         c.at(0).set('wealth', 42);
221         strictEqual(total, 42);
222         c.at(1).set('wealth', 5);
223         strictEqual(total, 47);
224     });
225
226     module('list-hofs', {
227         setup: function () {
228             openerp = window.openerp.init();
229             window.openerp.web.list(openerp);
230         }
231     });
232     test('each, degenerate', function () {
233         var c = new openerp.web.list.Collection([
234             {id: 1, value: 5},
235             {id: 2, value: 10},
236             {id: 3, value: 20}
237         ]), ids = [];
238         c.each(function (record) {
239             ids.push(record.get('id'));
240         });
241         deepEqual(
242             ids, [1, 2, 3],
243             'degenerate collections should be iterated in record order');
244     });
245     test('each, deep', function () {
246         var root = new openerp.web.list.Collection(),
247             ids = [];
248         root.proxy('foo').add([
249             {id: 1, value: 5},
250             {id: 2, value: 10},
251             {id: 3, value: 20}]);
252         root.proxy('bar').add([
253             {id: 10, value: 5},
254             {id: 20, value: 10},
255             {id: 30, value: 20}]);
256         root.each(function (record) {
257             ids.push(record.get('id'));
258         });
259         // No contract on sub-collection iteration order (for now anyway)
260         ids.sort(function (a, b) { return a - b; });
261         deepEqual(
262             ids, [1, 2, 3, 10, 20, 30],
263             'tree collections should be deeply iterated');
264     });
265     test('map, degenerate', function () {
266         var c = new openerp.web.list.Collection([
267             {id: 1, value: 5},
268             {id: 2, value: 10},
269             {id: 3, value: 20}
270         ]);
271         var ids = c.map(function (record) {
272             return record.get('id');
273         });
274         deepEqual(
275             ids, [1, 2, 3],
276             'degenerate collections should be iterated in record order');
277     });
278     test('map, deep', function () {
279         var root = new openerp.web.list.Collection();
280         root.proxy('foo').add([
281             {id: 1, value: 5},
282             {id: 2, value: 10},
283             {id: 3, value: 20}]);
284         root.proxy('bar').add([
285             {id: 10, value: 5},
286             {id: 20, value: 10},
287             {id: 30, value: 20}]);
288         var ids = root.map(function (record) {
289             return record.get('id');
290         });
291         // No contract on sub-collection iteration order (for now anyway)
292         ids.sort(function (a, b) { return a - b; });
293         deepEqual(
294             ids, [1, 2, 3, 10, 20, 30],
295             'tree collections should be deeply iterated');
296     });
297 });