[ADD] delete button per row
[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.chrome(openerp);
26             // views loader stuff
27             window.openerp.base.views(openerp);
28             window.openerp.base.list(openerp);
29         }
30     });
31
32     asyncTest('render selection checkboxes', 2, function () {
33         var listview = new openerp.base.ListView(
34                 {}, null,
35                 'qunit-fixture', {model: null});
36
37         listview.on_loaded(fvg);
38
39         listview.do_fill_table([{}, {}, {}]).then(function () {
40             ok(are(listview.$element.find('tbody th'),
41                    '.oe-record-selector'));
42             ok(are(listview.$element.find('tbody th input'),
43                    ':checkbox:not([name])'));
44             start();
45         });
46     });
47     asyncTest('render no checkbox if selectable=false', 1, function () {
48         var listview = new openerp.base.ListView(
49                 {}, null,
50                 'qunit-fixture', {model: null}, false,
51                 {selectable: false});
52
53         listview.on_loaded(fvg);
54
55         listview.do_fill_table([{}, {}, {}]).then(function () {
56             equal(listview.$element.find('tbody th').length, 0);
57             start();
58         });
59     });
60     asyncTest('select a bunch of records', 2, function () {
61         var listview = new openerp.base.ListView(
62                 {}, null, 'qunit-fixture', {model: null});
63         listview.on_loaded(fvg);
64
65         listview.do_fill_table([{id: 1}, {id: 2}, {id: 3}]).then(function () {
66             listview.$element.find('tbody th input:eq(2)')
67                              .attr('checked', true);
68             deepEqual(listview.get_selection(), [3]);
69             listview.$element.find('tbody th input:eq(1)')
70                              .attr('checked', true);
71             deepEqual(listview.get_selection(), [2, 3]);
72             start();
73         });
74     });
75     asyncTest('render deletion button if list is deletable', 1, function () {
76         var listview = new openerp.base.ListView(
77                 {}, null, 'qunit-fixture', {model: null});
78
79         listview.on_loaded(fvg);
80
81         listview.do_fill_table([{id: 1}, {id: 2}, {id: 3}]).then(function () {
82             equal(
83                 listview.$element.find('tbody tr td.oe-record-delete button').length,
84                 3);
85             start();
86         });
87     });
88     asyncTest('deletion button should lead on deletion in the dataset',
89               2, function () {
90         var deleted;
91         var listview = new openerp.base.ListView(
92                 {}, null, 'qunit-fixture', {model: null, unlink: function (ids) {
93             deleted = ids;
94         }});
95
96         listview.on_loaded(fvg);
97
98         listview.do_fill_table([{id: 1}, {id: 2}, {id: 3}]).then(function () {
99             listview.$element.find('tbody td.oe-record-delete:eq(2) button').click();
100             deepEqual(deleted, [3]);
101             listview.$element.find('tbody td.oe-record-delete:eq(0) button').click();
102             deepEqual(deleted, [1]);
103             start();
104         });
105     });
106 });