From a0c5057dbacef8e8f1841245c58ad4f52da57ef9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 11 Jun 2012 13:32:38 +0200 Subject: [PATCH] [TEST] Widget#renderElement with no template specified, fix Widget#make's handling of attributes bzr revid: xmo@openerp.com-20120611113238-wz516g9tl0xp1igd --- addons/web/static/src/js/corelib.js | 4 +- addons/web/static/test/Widget.js | 68 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/js/corelib.js b/addons/web/static/src/js/corelib.js index 61464a58930..83586ec57d4 100644 --- a/addons/web/static/src/js/corelib.js +++ b/addons/web/static/src/js/corelib.js @@ -762,8 +762,8 @@ instance.web.Widget = instance.web.Class.extend(instance.web.WidgetMixin, { */ make: function (tagName, attributes, content) { var el = document.createElement(tagName); - if (attributes) { - $(el).attrs(attributes); + if (!_.isEmpty(attributes)) { + $(el).attr(attributes); } if (content) { $(el).html(content); diff --git a/addons/web/static/test/Widget.js b/addons/web/static/test/Widget.js index dc86e99b38a..51a5182845d 100644 --- a/addons/web/static/test/Widget.js +++ b/addons/web/static/test/Widget.js @@ -64,4 +64,72 @@ $(document).ready(function () { fn(42); equal(w.executed, 42, "should be passed the proxy's arguments"); }); + + module('Widget.renderElement', mod); + test('no template, default', function () { + var w = new (instance.web.Widget.extend({ })); + + ok(!w.$el, "should not initially have a root element"); + w.renderElement(); + ok(w.$el, "should have generated a root element"); + strictEqual(w.$element, w.$el, "should provide $element alias"); + ok(w.$el.is(w.el), "should provide raw DOM alias"); + + equal(w.el.nodeName, 'DIV', "should have generated the default element"); + equal(w.el.attributes.length, 0, "should not have generated any attribute"); + ok(_.isEmpty(w.$el.html(), "should not have generated any content")); + }); + test('no template, custom tag', function () { + var w = new (instance.web.Widget.extend({ + tagName: 'ul' + })); + w.renderElement(); + + equal(w.el.nodeName, 'UL', "should have generated the custom element tag"); + }); + test('no template, @id', function () { + var w = new (instance.web.Widget.extend({ + id: 'foo' + })); + w.renderElement(); + + equal(w.el.attributes.length, 1, "should have one attribute"); + equal(w.$el.attr('id'), 'foo', "should have generated the id attribute"); + equal(w.el.id, 'foo', "should also be available via property"); + }); + test('no template, @className', function () { + var w = new (instance.web.Widget.extend({ + className: 'oe_some_class' + })); + w.renderElement(); + + equal(w.el.className, 'oe_some_class', "should have the right property"); + equal(w.$el.attr('class'), 'oe_some_class', "should have the right attribute"); + }); + test('no template, bunch of attributes', function () { + var w = new (instance.web.Widget.extend({ + attributes: { + 'id': 'some_id', + 'class': 'some_class', + 'data-foo': 'data attribute', + 'clark': 'gable', + 'spoiler': 'snape kills dumbledore' + } + })); + w.renderElement(); + + equal(w.el.attributes.length, 5, "should have all the specified attributes"); + + equal(w.el.id, 'some_id'); + equal(w.$el.attr('id'), 'some_id'); + + equal(w.el.className, 'some_class'); + equal(w.$el.attr('class'), 'some_class'); + + equal(w.$el.attr('data-foo'), 'data attribute'); + equal(w.$el.data('foo'), 'data attribute'); + + equal(w.$el.attr('clark'), 'gable'); + equal(w.$el.attr('spoiler'), 'snape kills dumbledore'); + }); });