Debian Codegraph @

addons/web/static/tests/legacy/public/public_widget_tests.js

raw symbols SCIP
1 import publicWidget from "@web/legacy/js/public/public_widget";
2 import testUtils from "@web/../tests/legacy_tests/helpers/test_utils";
3 import { renderToString } from "@web/core/utils/render";
4
5 const Widget = publicWidget.Widget;
6
7 QUnit.module('core', {}, function () {
8
9 QUnit.module('Widget');
10
11 QUnit.test('proxy (String)', function (assert) {
12 assert.expect(1);
13
14 var W = Widget.extend({
15 exec: function () {
16 this.executed = true;
17 }
18 });
19 var w = new W();
20 var fn = w.proxy('exec');
21 fn();
22 assert.ok(w.executed, 'should execute the named method in the right context');
23 w.destroy();
24 });
25
26 QUnit.test('proxy (String)(*args)', function (assert) {
27 assert.expect(2);
28
29 var W = Widget.extend({
30 exec: function (arg) {
31 this.executed = arg;
32 }
33 });
34 var w = new W();
35 var fn = w.proxy('exec');
36 fn(42);
37 assert.ok(w.executed, "should execute the named method in the right context");
38 assert.strictEqual(w.executed, 42, "should be passed the proxy's arguments");
39 w.destroy();
40 });
41
42 QUnit.test('proxy (String), include', function (assert) {
43 assert.expect(1);
44
45 // the proxy function should handle methods being changed on the class
46 // and should always proxy "by name", to the most recent one
47 var W = Widget.extend({
48 exec: function () {
49 this.executed = 1;
50 }
51 });
52 var w = new W();
53 var fn = w.proxy('exec');
54 W.include({
55 exec: function () { this.executed = 2; }
56 });
57
58 fn();
59 assert.strictEqual(w.executed, 2, "should be lazily resolved");
60 w.destroy();
61 });
62
63 QUnit.test('proxy (Function)', function (assert) {
64 assert.expect(1);
65
66 var w = new (Widget.extend({ }))();
67
68 var fn = w.proxy(function () { this.executed = true; });
69 fn();
70 assert.ok(w.executed, "should set the function's context (like Function#bind)");
71 w.destroy();
72 });
73
74 QUnit.test('proxy (Function)(*args)', function (assert) {
75 assert.expect(1);
76
77 var w = new (Widget.extend({ }))();
78
79 var fn = w.proxy(function (arg) { this.executed = arg; });
80 fn(42);
81 assert.strictEqual(w.executed, 42, "should be passed the proxy's arguments");
82 w.destroy();
83 });
84
85 QUnit.test('renderElement, no template, default', function (assert) {
86 assert.expect(7);
87
88 var widget = new (Widget.extend({ }))();
89
90 assert.strictEqual(widget.$el, undefined, "should not have a root element");
91
92 widget.renderElement();
93
94 assert.ok(widget.$el, "should have generated a root element");
95 assert.strictEqual(widget.$el, widget.$el, "should provide $el alias");
96 assert.ok(widget.$el.is(widget.el), "should provide raw DOM alias");
97
98 assert.strictEqual(widget.el.nodeName, 'DIV', "should have generated the default element");
99 assert.strictEqual(widget.el.attributes.length, 0, "should not have generated any attribute");
100 assert.ok(Object.keys(widget.$el.html() || {}).length === 0, "should not have generated any content");
101 widget.destroy();
102 });
103
104 QUnit.test('no template, custom tag', function (assert) {
105 assert.expect(1);
106
107
108 var widget = new (Widget.extend({
109 tagName: 'ul'
110 }))();
111 widget.renderElement();
112
113 assert.strictEqual(widget.el.nodeName, 'UL', "should have generated the custom element tag");
114 widget.destroy();
115 });
116
117 QUnit.test('no template, @id', function (assert) {
118 assert.expect(3);
119
120 var widget = new (Widget.extend({
121 id: 'foo'
122 }))();
123 widget.renderElement();
124
125 assert.strictEqual(widget.el.attributes.length, 1, "should have one attribute");
126 assert.hasAttrValue(widget.$el, 'id', 'foo', "should have generated the id attribute");
127 assert.strictEqual(widget.el.id, 'foo', "should also be available via property");
128 widget.destroy();
129 });
130
131 QUnit.test('no template, @className', function (assert) {
132 assert.expect(2);
133
134 var widget = new (Widget.extend({
135 className: 'oe_some_class'
136 }))();
137 widget.renderElement();
138
139 assert.strictEqual(widget.el.className, 'oe_some_class', "should have the right property");
140 assert.hasAttrValue(widget.$el, 'class', 'oe_some_class', "should have the right attribute");
141 widget.destroy();
142 });
143
144 QUnit.test('no template, bunch of attributes', function (assert) {
145 assert.expect(9);
146
147 var widget = new (Widget.extend({
148 attributes: {
149 'id': 'some_id',
150 'class': 'some_class',
151 'data-foo': 'data attribute',
152 'clark': 'gable',
153 'spoiler': // don't read the next line if you care about Harry Potter...
154 'snape kills dumbledore'
155 }
156 }))();
157 widget.renderElement();
158
159 assert.strictEqual(widget.el.attributes.length, 5, "should have all the specified attributes");
160
161 assert.strictEqual(widget.el.id, 'some_id');
162 assert.hasAttrValue(widget.$el, 'id', 'some_id');
163
164 assert.strictEqual(widget.el.className, 'some_class');
165 assert.hasAttrValue(widget.$el, 'class', 'some_class');
166
167 assert.hasAttrValue(widget.$el, 'data-foo', 'data attribute');
168 assert.strictEqual(widget.$el.data('foo'), 'data attribute');
169
170 assert.hasAttrValue(widget.$el, 'clark', 'gable');
171 assert.hasAttrValue(widget.$el, 'spoiler', 'snape kills dumbledore');
172 widget.destroy();
173 });
174
175 QUnit.test('template', function (assert) {
176 assert.expect(3);
177
178 renderToString.app.addTemplate(
179 "test.widget.template.1",
180 `<ol>
181 <li t-foreach="[0, 1, 2, 3, 4]" t-as="counter" t-key="counter_index" t-attf-class="class-#{counter}">
182 <input/>
183 <t t-esc="counter"/>
184 </li>
185 </ol>`
186 );
187
188 var widget = new (Widget.extend({
189 template: 'test.widget.template.1'
190 }))();
191 widget.renderElement();
192
193 assert.strictEqual(widget.el.nodeName, 'OL');
194 assert.strictEqual(widget.$el.children().length, 5);
195 assert.strictEqual(widget.el.textContent, '01234');
196 widget.destroy();
197 });
198
199 QUnit.test('repeated', async function (assert) {
200 assert.expect(4);
201 var $fix = $( "#qunit-fixture");
202
203 renderToString.app.addTemplate(
204 "test.widget.template.2",
205 `<p>
206 <t t-esc="widget.value"/>
207 </p>`
208 );
209 var widget = new (Widget.extend({
210 template: 'test.widget.template.2'
211 }))();
212 widget.value = 42;
213
214 await widget.appendTo($fix)
215 .then(function () {
216 assert.strictEqual($fix.find('p').text(), '42', "DOM fixture should contain initial value");
217 assert.strictEqual(widget.$el.text(), '42', "should set initial value");
218 widget.value = 36;
219 widget.renderElement();
220 assert.strictEqual($fix.find('p').text(), '36', "DOM fixture should use new value");
221 assert.strictEqual(widget.$el.text(), '36', "should set new value");
222 });
223 widget.destroy();
224 });
225
226
227 QUnit.module('Widgets, with QWeb');
228
229 QUnit.test('basic-alias', function (assert) {
230 assert.expect(1);
231
232 renderToString.app.addTemplate(
233 "test.widget.template.3",
234 `<ol>
235 <li t-foreach="[0,1,2,3,4]" t-as="counter" t-key="counter_index" t-attf-class="class-#{counter}">
236 <input/>
237 <t t-esc="counter"/>
238 </li>
239 </ol>`
240 );
241
242 var widget = new (Widget.extend({
243 template: 'test.widget.template.3'
244 }))();
245 widget.renderElement();
246
247 assert.ok(widget.$('li:eq(3)').is(widget.$el.find('li:eq(3)')),
248 "should do the same thing as calling find on the widget root");
249 widget.destroy();
250 });
251
252
253 QUnit.test('delegate', async function (assert) {
254 assert.expect(5);
255
256 renderToString.app.addTemplate(
257 "test.widget.template.4",
258 `<ol>
259 <li t-foreach="[0,1,2,3,4]" t-as="counter" t-key="counter_index" t-attf-class="class-#{counter}">
260 <input/>
261 <t t-esc="counter"/>
262 </li>
263 </ol>`
264 );
265
266 var a = [];
267 var widget = new (Widget.extend({
268 template: 'test.widget.template.4',
269 events: {
270 'click': function () {
271 a[0] = true;
272 assert.strictEqual(this, widget, "should trigger events in widget");
273 },
274 'click li.class-3': 'class3',
275 'change input': function () { a[2] = true; }
276 },
277 class3: function () { a[1] = true; }
278 }))();
279 widget.renderElement();
280
281 await testUtils.dom.click(widget.$el, {allowInvisible: true});
282 await testUtils.dom.click(widget.$('li:eq(3)'), {allowInvisible: true});
283 await testUtils.fields.editAndTrigger(widget.$('input:last'), 'foo', 'change');
284
285 for(var i=0; i<3; ++i) {
286 assert.ok(a[i], "should pass test " + i);
287 }
288 widget.destroy();
289 });
290
291 QUnit.test('undelegate', async function (assert) {
292 assert.expect(4);
293
294 renderToString.app.addTemplate(
295 "test.widget.template.5",
296 `<ol>
297 <li t-foreach="[0,1,2,3,4]" t-as="counter" t-key="counter_index" t-attf-class="class-#{counter}">
298 <input/>
299 <t t-esc="counter"/>
300 </li>
301 </ol>`
302 );
303
304 var clicked = false;
305 var newclicked = false;
306
307 var widget = new (Widget.extend({
308 template: 'test.widget.template.5',
309 events: { 'click li': function () { clicked = true; } }
310 }))();
311
312 widget.renderElement();
313 widget.$el.on('click', 'li', function () { newclicked = true; });
314
315 await testUtils.dom.clickFirst(widget.$('li'), {allowInvisible: true});
316 assert.ok(clicked, "should trigger bound events");
317 assert.ok(newclicked, "should trigger bound events");
318
319 clicked = newclicked = false;
320 widget._undelegateEvents();
321 await testUtils.dom.clickFirst(widget.$('li'), {allowInvisible: true});
322 assert.ok(!clicked, "undelegate should unbind events delegated");
323 assert.ok(newclicked, "undelegate should only unbind events it created");
324 widget.destroy();
325 });
326
327 QUnit.module('Widget, and async stuff');
328
329 QUnit.test('start is not called when widget is destroyed', function (assert) {
330 assert.expect(0);
331 const $fix = $("#qunit-fixture");
332
333 // Note: willStart is always async
334 const MyWidget = Widget.extend({
335 start: function () {
336 assert.ok(false, 'Should not call start method');
337 },
338 });
339
340 const widget = new MyWidget();
341 widget.appendTo($fix);
342 widget.destroy();
343
344 const divEl = document.createElement('div');
345 $fix[0].appendChild(divEl);
346 const widget2 = new MyWidget();
347 widget2.attachTo(divEl);
348 widget2.destroy();
349 });
350
351 QUnit.test("don't destroy twice widget's children", function (assert) {
352 assert.expect(2);
353
354 var parent = new Widget();
355 new (Widget.extend({
356 destroy: function () {
357 assert.step('destroy');
358 }
359 }))(parent);
360
361 parent.destroy();
362 assert.verifySteps(['destroy'], "child should have been detroyed only once");
363 });
364 });