[FIX] kinda sorta fix JS tests post-refs (add links to boot.js and core.js, update...
[odoo/odoo.git] / addons / base / static / test / list.js
1 $(document).ready(function () {
2     /**
3      * Tests a jQuery collection against a selector ("ands" the .is() of each
4      * member of the collection, instead of "or"-ing them)
5      *
6      * @param {jQuery} $c a jQuery collection object
7      * @param {String} selector the selector to test the collection against
8      */
9     var are = function ($c, selector) {
10         return ($c.filter(function () { return $(this).is(selector); }).length
11                 === $c.length);
12     };
13
14     var fvg = {fields_view: {
15         'fields': [],
16         'arch': {
17             'attrs': {string: ''}
18         }
19     }};
20
21     var openerp;
22     module("ListView", {
23         setup: function () {
24             openerp = window.openerp.init(true);
25             window.openerp.base.core(openerp);
26             window.openerp.base.chrome(openerp);
27             // views loader stuff
28             window.openerp.base.data(openerp);
29             window.openerp.base.views(openerp);
30             window.openerp.base.list(openerp);
31             window.openerp.base.form(openerp);
32         }
33     });
34
35     test('render selection checkboxes', 2, function () {
36         var listview = new openerp.base.ListView(
37                 null, 'qunit-fixture', {model: null, ids: [null, null, null], index: 0});
38
39         listview.on_loaded(fvg);
40
41         listview.do_fill_table({records: [
42             {data: {id: {value: null}}},
43             {data: {id: {value: null}}},
44             {data: {id: {value: null}}}
45         ]});
46
47         ok(are(listview.$element.find('tbody th'),
48                '.oe-record-selector'));
49         ok(are(listview.$element.find('tbody th input'),
50                ':checkbox:not([name])'));
51     });
52     test('render no checkbox if selectable=false', 1, function () {
53         var listview = new openerp.base.ListView(
54                 null, 'qunit-fixture', {model: null, ids: [null, null, null], index: 0}, false,
55                 {selectable: false});
56
57         listview.on_loaded(fvg);
58
59         listview.do_fill_table({records: [
60                 {data: {id: {value: null}}},
61                 {data: {id: {value: null}}},
62                 {data: {id: {value: null}}}
63         ]});
64         equal(listview.$element.find('tbody th').length, 0);
65     });
66     test('select a bunch of records', 2, function () {
67         var listview = new openerp.base.ListView(
68                 null, 'qunit-fixture', {model: null, ids: [1, 2, 3], index: 0});
69         listview.on_loaded(fvg);
70
71         listview.do_fill_table({records: [
72                 {data: {id: {value: 1}}},
73                 {data: {id: {value: 2}}},
74                 {data: {id: {value: 3}}}
75         ]});
76         // TODO: find less intrusive way to get selection count of list view?
77         listview.$element.find('tbody th input:eq(2)')
78                          .attr('checked', true);
79         deepEqual(listview.list.get_selection(), [3]);
80         listview.$element.find('tbody th input:eq(1)')
81                          .attr('checked', true);
82         deepEqual(listview.list.get_selection(), [2, 3]);
83     });
84     test('render deletion button if list is deletable', 1, function () {
85         var listview = new openerp.base.ListView(
86                 null, 'qunit-fixture', {model: null, ids: [null, null, null], index: 0});
87
88         listview.on_loaded(fvg);
89
90         listview.do_fill_table({records: [
91                 {data: {id: {value: null}}},
92                 {data: {id: {value: null}}},
93                 {data: {id: {value: null}}}
94         ]});
95         equal(
96             listview.$element.find('tbody tr td.oe-record-delete button').length,
97             3);
98     });
99     test('deletion button should lead on deletion in the dataset',
100               2, function () {
101         var deleted;
102         var listview = new openerp.base.ListView(
103                 null, 'qunit-fixture',
104                 {model: null, unlink: function (ids) {
105             deleted = ids;
106         }, ids: [1, 2, 3], index: 0});
107
108         listview.on_loaded(fvg);
109
110         listview.do_fill_table({records: [
111                 {data: {id: {value: 1}}},
112                 {data: {id: {value: 2}}},
113                 {data: {id: {value: 3}}}
114         ]});
115         listview.$element.find('tbody td.oe-record-delete:eq(2) button').click();
116         deepEqual(deleted, [3]);
117         listview.$element.find('tbody td.oe-record-delete:eq(0) button').click();
118         deepEqual(deleted, [1]);
119     });
120     test('multiple records deletion', 1, function () {
121         var deleted;
122         var listview = new openerp.base.ListView(
123                 null, 'qunit-fixture',
124                 {model: null, unlink: function (ids) {
125             deleted = ids;
126         }, ids: [1, 2, 3], index: 0});
127
128         listview.on_loaded(fvg);
129
130         listview.do_fill_table({records: [
131                 {data: {id: {value: 1}}},
132                 {data: {id: {value: 2}}},
133                 {data: {id: {value: 3}}}
134         ]});
135         listview.$element.find('tbody th input:eq(2)')
136                          .attr('checked', true);
137         listview.$element.find('tbody th input:eq(1)')
138                          .attr('checked', true);
139
140         listview.$element.find('.oe-list-delete').click();
141         deepEqual(deleted, [2, 3]);
142     });
143 });