1 openerp.testing.section('Widget.proxy', {
2 dependencies: ['web.corelib']
4 test('(String)', function (instance) {
5 var W = instance.web.Widget.extend({
11 var fn = w.proxy('exec');
13 ok(w.executed, 'should execute the named method in the right context');
15 test('(String)(*args)', function (instance) {
16 var W = instance.web.Widget.extend({
17 exec: function (arg) {
22 var fn = w.proxy('exec');
24 ok(w.executed, "should execute the named method in the right context");
25 equal(w.executed, 42, "should be passed the proxy's arguments");
27 test('(String), include', function (instance) {
28 // the proxy function should handle methods being changed on the class
29 // and should always proxy "by name", to the most recent one
30 var W = instance.web.Widget.extend({
36 var fn = w.proxy('exec');
38 exec: function () { this.executed = 2; }
42 equal(w.executed, 2, "should be lazily resolved");
45 test('(Function)', function (instance) {
46 var w = new (instance.web.Widget.extend({ }));
48 var fn = w.proxy(function () { this.executed = true; });
50 ok(w.executed, "should set the function's context (like Function#bind)");
52 test('(Function)(*args)', function (instance) {
53 var w = new (instance.web.Widget.extend({ }));
55 var fn = w.proxy(function (arg) { this.executed = arg; });
57 equal(w.executed, 42, "should be passed the proxy's arguments");
60 openerp.testing.section('Widget.renderElement', {
61 dependencies: ['web.corelib'],
62 setup: function (instance) {
63 instance.web.qweb = new QWeb2.Engine();
64 instance.web.qweb.add_template(
66 '<t t-name="test.widget.template">' +
68 '<li t-foreach="5" t-as="counter" ' +
69 't-attf-class="class-#{counter}">' +
71 '<t t-esc="counter"/>' +
75 '<t t-name="test.widget.template-value">' +
76 '<p><t t-esc="widget.value"/></p>' +
81 test('no template, default', function (instance) {
82 var w = new (instance.web.Widget.extend({ }));
84 var $original = w.$el;
85 ok($original, "should initially have a root element");
87 ok(w.$el, "should have generated a root element");
88 ok($original !== w.$el, "should have generated a new root element");
89 strictEqual(w.$el, w.$el, "should provide $el alias");
90 ok(w.$el.is(w.el), "should provide raw DOM alias");
92 equal(w.el.nodeName, 'DIV', "should have generated the default element");
93 equal(w.el.attributes.length, 0, "should not have generated any attribute");
94 ok(_.isEmpty(w.$el.html(), "should not have generated any content"));
96 test('no template, custom tag', function (instance) {
97 var w = new (instance.web.Widget.extend({
102 equal(w.el.nodeName, 'UL', "should have generated the custom element tag");
104 test('no template, @id', function (instance) {
105 var w = new (instance.web.Widget.extend({
110 equal(w.el.attributes.length, 1, "should have one attribute");
111 equal(w.$el.attr('id'), 'foo', "should have generated the id attribute");
112 equal(w.el.id, 'foo', "should also be available via property");
114 test('no template, @className', function (instance) {
115 var w = new (instance.web.Widget.extend({
116 className: 'oe_some_class'
120 equal(w.el.className, 'oe_some_class', "should have the right property");
121 equal(w.$el.attr('class'), 'oe_some_class', "should have the right attribute");
123 test('no template, bunch of attributes', function (instance) {
124 var w = new (instance.web.Widget.extend({
127 'class': 'some_class',
128 'data-foo': 'data attribute',
130 'spoiler': 'snape kills dumbledore'
135 equal(w.el.attributes.length, 5, "should have all the specified attributes");
137 equal(w.el.id, 'some_id');
138 equal(w.$el.attr('id'), 'some_id');
140 equal(w.el.className, 'some_class');
141 equal(w.$el.attr('class'), 'some_class');
143 equal(w.$el.attr('data-foo'), 'data attribute');
144 equal(w.$el.data('foo'), 'data attribute');
146 equal(w.$el.attr('clark'), 'gable');
147 equal(w.$el.attr('spoiler'), 'snape kills dumbledore');
150 test('template', function (instance) {
151 var w = new (instance.web.Widget.extend({
152 template: 'test.widget.template'
156 equal(w.el.nodeName, 'OL');
157 equal(w.$el.children().length, 5);
158 equal(w.el.textContent, '01234');
160 test('repeated', { asserts: 4 }, function (instance, $fix) {
161 var w = new (instance.web.Widget.extend({
162 template: 'test.widget.template-value'
165 return w.appendTo($fix)
167 equal($fix.find('p').text(), '42', "DOM fixture should contain initial value");
168 equal(w.$el.text(), '42', "should set initial value");
171 equal($fix.find('p').text(), '36', "DOM fixture should use new value");
172 equal(w.$el.text(), '36', "should set new value");
176 openerp.testing.section('Widget.$', {
177 dependencies: ['web.corelib'],
178 setup: function (instance) {
179 instance.web.qweb = new QWeb2.Engine();
180 instance.web.qweb.add_template(
182 '<t t-name="test.widget.template">' +
184 '<li t-foreach="5" t-as="counter" ' +
185 't-attf-class="class-#{counter}">' +
187 '<t t-esc="counter"/>' +
194 test('basic-alias', function (instance) {
195 var w = new (instance.web.Widget.extend({
196 template: 'test.widget.template'
200 ok(w.$('li:eq(3)').is(w.$el.find('li:eq(3)')),
201 "should do the same thing as calling find on the widget root");
204 openerp.testing.section('Widget.events', {
205 dependencies: ['web.corelib'],
206 setup: function (instance) {
207 instance.web.qweb = new QWeb2.Engine();
208 instance.web.qweb.add_template(
210 '<t t-name="test.widget.template">' +
212 '<li t-foreach="5" t-as="counter" ' +
213 't-attf-class="class-#{counter}">' +
215 '<t t-esc="counter"/>' +
222 test('delegate', function (instance) {
224 var w = new (instance.web.Widget.extend({
225 template: 'test.widget.template',
227 'click': function () {
229 strictEqual(this, w, "should trigger events in widget")
231 'click li.class-3': 'class3',
232 'change input': function () { a[2] = true; }
234 class3: function () { a[1] = true; }
239 w.$('li:eq(3)').click();
240 w.$('input:last').val('foo').change();
242 for(var i=0; i<3; ++i) {
243 ok(a[i], "should pass test " + i);
246 test('undelegate', function (instance) {
247 var clicked = false, newclicked = false;
248 var w = new (instance.web.Widget.extend({
249 template: 'test.widget.template',
250 events: { 'click li': function () { clicked = true; } }
253 w.$el.on('click', 'li', function () { newclicked = true });
256 ok(clicked, "should trigger bound events");
257 ok(newclicked, "should trigger bound events");
258 clicked = newclicked = false;
260 w.undelegateEvents();
262 ok(!clicked, "undelegate should unbind events delegated");
263 ok(newclicked, "undelegate should only unbind events it created");