Debian Codegraph @

addons/web/static/tests/views/view_dialogs/form_view_dialog.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, edit, press, queryAllTexts, runAllTimers, waitFor } from "@odoo/hoot-dom";
3 import { animationFrame, Deferred } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 fieldInput,
9 fields,
10 getService,
11 mockService,
12 models,
13 mountView,
14 mountViewInDialog,
15 mountWithCleanup,
16 onRpc,
17 } from "@web/../tests/web_test_helpers";
18
19 import { FormViewDialog } from "@web/views/view_dialogs/form_view_dialog";
20 import { WebClient } from "@web/webclient/webclient";
21
22 class Partner extends models.Model {
23 name = fields.Char({ string: "Displayed name" });
24 foo = fields.Char({ string: "Foo" });
25 bar = fields.Boolean({ string: "Bar" });
26 instrument = fields.Many2one({
27 string: "Instruments",
28 relation: "instrument",
29 });
30 method1() {}
31 method2() {}
32 _records = [
33 { id: 1, foo: "blip", name: "blipblip", bar: true },
34 { id: 2, foo: "ta tata ta ta", name: "macgyver", bar: false },
35 { id: 3, foo: "piou piou", name: "Jack O'Neill", bar: true },
36 ];
37 }
38
39 class Instrument extends models.Model {
40 name = fields.Char({ string: "name" });
41 badassery = fields.Many2many({
42 string: "level",
43 relation: "badassery",
44 domain: [["level", "=", "Awsome"]],
45 });
46 }
47
48 class Badassery extends models.Model {
49 level = fields.Char({ string: "level" });
50
51 _records = [{ id: 1, level: "Awsome" }];
52 }
53
54 class Product extends models.Model {
55 name = fields.Char({ string: "name" });
56 partner = fields.One2many({ string: "Doors", relation: "partner" });
57
58 _records = [{ id: 1, name: "The end" }];
59 }
60
61 defineModels([Partner, Instrument, Badassery, Product]);
62
63 test("formviewdialog buttons in footer are positioned properly", async () => {
64 Partner._views.form = /* xml */ `
65 <form string="Partner">
66 <sheet>
67 <group><field name="foo"/></group >
68 <footer><button string="Custom Button" type="object" class="btn-primary"/></footer>
69 </sheet>
70 </form>
71 `;
72
73 await mountWithCleanup(WebClient);
74 getService("dialog").add(FormViewDialog, {
75 resModel: "partner",
76 resId: 1,
77 });
78
79 await animationFrame();
80
81 expect(".modal-body button").toHaveCount(0, { message: "should not have any button in body" });
82 expect(".modal-footer button:visible").toHaveCount(1, {
83 message: "should have only one button in footer",
84 });
85 });
86
87 test("modifiers are considered on multiple <footer/> tags", async () => {
88 Partner._views.form = /* xml */ `
89 <form>
90 <field name="bar"/>
91 <footer invisible="not bar">
92 <button>Hello</button>
93 <button>World</button>
94 </footer>
95 <footer invisible="bar">
96 <button>Foo</button>
97 </footer>
98 </form>
99 `;
100 await mountWithCleanup(WebClient);
101 getService("dialog").add(FormViewDialog, {
102 resModel: "partner",
103 resId: 1,
104 });
105
106 await animationFrame();
107
108 expect(queryAllTexts(".modal-footer button:visible")).toEqual(["Hello", "World"], {
109 message: "only the first button section should be visible",
110 });
111
112 await click(".o_field_boolean input");
113 await animationFrame();
114 expect(queryAllTexts(".modal-footer button:visible")).toEqual(["Foo"], {
115 message: "only the second button section should be visible",
116 });
117 });
118
119 test("formviewdialog buttons in footer are not duplicated", async () => {
120 Partner._fields.poney_ids = fields.One2many({
121 string: "Poneys",
122 relation: "partner",
123 });
124 Partner._records[0].poney_ids = [];
125 Partner._views.form = /* xml */ `
126 <form string="Partner">
127 <field name="poney_ids"><list editable="top"><field name="name"/></list></field>
128 <footer><button string="Custom Button" type="object" class="my_button"/></footer>
129 </form>
130 `;
131 await mountWithCleanup(WebClient);
132 getService("dialog").add(FormViewDialog, {
133 resModel: "partner",
134 resId: 1,
135 });
136 await animationFrame();
137
138 expect(".modal").toHaveCount(1);
139 expect(".modal button.my_button").toHaveCount(1, { message: "should have 1 buttons in modal" });
140
141 await click(".o_field_x2many_list_row_add a");
142 await animationFrame();
143 await press("escape");
144 await animationFrame();
145
146 expect(".modal").toHaveCount(1);
147 expect(".modal button.btn-primary").toHaveCount(1, {
148 message: "should still have 1 buttons in modal",
149 });
150 });
151
152 test.tags("desktop");
153 test("Form dialog and subview with _view_ref contexts", async () => {
154 expect.assertions(2);
155
156 Instrument._records = [{ id: 1, name: "Tromblon", badassery: [1] }];
157 Partner._records[0].instrument = 1;
158 // This is an old test, written before "get_views" (formerly "load_views") automatically
159 // inlines x2many subviews. As the purpose of this test is to assert that the js fetches
160 // the correct sub view when it is not inline (which can still happen in nested form views),
161 // we bypass the inline mecanism of "get_views" by setting widget="many2many" on the field.
162 Instrument._views.form = /* xml */ `
163 <form>
164 <field name="name"/>
165 <field name="badassery" widget="many2many" context="{'list_view_ref': 'some_other_tree_view'}"/>
166 </form>
167 `;
168 Badassery._views.list = /* xml */ `<list><field name="level"/></list>`;
169
170 onRpc(({ kwargs, method, model }) => {
171 if (method === "get_formview_id") {
172 return false;
173 }
174 if (method === "get_views" && model === "instrument") {
175 expect(kwargs.context).toEqual(
176 {
177 allowed_company_ids: [1],
178 lang: "en",
179 list_view_ref: "some_tree_view",
180 tz: "taht",
181 uid: 7,
182 },
183 {
184 message:
185 "1 The correct _view_ref should have been sent to the server, first time",
186 }
187 );
188 }
189 if (method === "get_views" && model === "badassery") {
190 expect(kwargs.context).toEqual(
191 {
192 allowed_company_ids: [1],
193 lang: "en",
194 list_view_ref: "some_other_tree_view",
195 tz: "taht",
196 uid: 7,
197 },
198 {
199 message:
200 "2 The correct _view_ref should have been sent to the server for the subview",
201 }
202 );
203 }
204 });
205
206 await mountViewInDialog({
207 type: "form",
208 resModel: "partner",
209 resId: 1,
210 arch: /* xml */ `
211 <form>
212 <field name="name"/>
213 <field name="instrument" context="{'list_view_ref': 'some_tree_view'}"/>
214 </form>
215 `,
216 });
217 await click('.o_field_widget[name="instrument"] button.o_external_button');
218 await animationFrame();
219 });
220
221 test("click on view buttons in a FormViewDialog", async () => {
222 Partner._views.form = /* xml */ `
223 <form>
224 <field name="foo"/>
225 <button name="method1" type="object" string="Button 1" class="btn1"/>
226 <button name="method2" type="object" string="Button 2" class="btn2" close="1"/>
227 </form>
228 `;
229
230 onRpc(({ method }) => expect.step(method));
231
232 await mountWithCleanup(WebClient);
233 getService("dialog").add(FormViewDialog, {
234 resModel: "partner",
235 resId: 1,
236 });
237 await animationFrame();
238 expect(".o_dialog .o_form_view").toHaveCount(1);
239 expect(".o_dialog .o_form_view button").toHaveCount(2);
240 expect.verifySteps(["get_views", "web_read"]);
241 await click(".o_dialog .o_form_view .btn1");
242 await animationFrame();
243 expect(".o_dialog .o_form_view").toHaveCount(1);
244 expect.verifySteps(["method1", "web_read"]); // should re-read the record
245 await click(".o_dialog .o_form_view .btn2");
246 await animationFrame();
247 expect(".o_dialog .o_form_view").toHaveCount(0);
248 expect.verifySteps(["method2"]); // should not read as we closed
249 });
250
251 test("formviewdialog is not closed when button handlers return a rejected promise", async () => {
252 Partner._views.form = /* xml */ `
253 <form string="Partner">
254 <sheet><group><field name="foo"/></group></sheet>
255 </form>
256 `;
257 let reject;
258 onRpc("web_save", () => {
259 if (reject) {
260 return Promise.reject("rejected");
261 }
262 });
263 await mountWithCleanup(WebClient);
264 getService("dialog").add(FormViewDialog, {
265 resModel: "partner",
266 context: { answer: 42 },
267 });
268
269 await animationFrame();
270 expect(".modal-body button").not.toHaveCount();
271 expect(".modal-footer button:visible").toHaveCount(2);
272
273 // Click "save" inside the dialog (with rejection)
274 expect.errors(1);
275 reject = true;
276 await clickSave();
277
278 expect.verifyErrors(["rejected"]);
279
280 // Close error modal
281 await click(waitFor(".o_error_dialog .btn:contains(Close)"));
282
283 // Click "save" inside the dialog (without rejection)
284 reject = false;
285 await clickSave();
286
287 expect(".modal").not.toHaveCount();
288 });
289
290 test("FormViewDialog with remove button", async () => {
291 Partner._views.form = /* xml */ `<form><field name="foo"/></form>`;
292 await mountWithCleanup(WebClient);
293 getService("dialog").add(FormViewDialog, {
294 resModel: "partner",
295 resId: 1,
296 removeRecord: () => expect.step("remove"),
297 });
298 await animationFrame();
299
300 expect(".o_dialog .o_form_view").toHaveCount(1);
301 expect(".o_dialog .modal-footer .o_form_button_remove").toHaveCount(1);
302 await click(".o_dialog .modal-footer .o_form_button_remove");
303 await animationFrame();
304 expect.verifySteps(["remove"]);
305 expect(".o_dialog .o_form_view").toHaveCount(0);
306 });
307
308 test("Buttons are set as disabled on click", async () => {
309 Partner._views.form = /* xml */ `
310 <form string="Partner">
311 <sheet>
312 <group>
313 <field name="name"/>
314 </group>
315 </sheet>
316 </form>
317 `;
318
319 const def = new Deferred();
320 onRpc("web_save", async () => await def);
321 await mountWithCleanup(WebClient);
322 getService("dialog").add(FormViewDialog, {
323 resModel: "partner",
324 resId: 1,
325 });
326
327 await animationFrame();
328
329 await click(".o_dialog .o_content .o_field_char .o_input");
330 await edit("test");
331 await animationFrame();
332
333 await clickSave();
334
335 expect(".o_dialog .modal-footer .o_form_button_save").toHaveAttribute("disabled", "1");
336
337 def.resolve();
338 await animationFrame();
339 expect(".o_dialog .o_form_view").toHaveCount(0);
340 });
341
342 test("FormViewDialog with discard button", async () => {
343 Partner._views.form = /* xml */ `<form><field name="foo"/></form>`;
344 await mountWithCleanup(WebClient);
345 getService("dialog").add(FormViewDialog, {
346 resModel: "partner",
347 resId: 1,
348 onRecordDiscarded: () => expect.step("discard"),
349 });
350 await animationFrame();
351
352 expect(".o_dialog .o_form_view").toHaveCount(1);
353 expect(".o_dialog .modal-footer .o_form_button_cancel").toHaveCount(1);
354 await click(".o_dialog .modal-footer .o_form_button_cancel");
355 await animationFrame();
356 expect.verifySteps(["discard"]);
357 expect(".o_dialog .o_form_view").toHaveCount(0);
358 });
359
360 test("Save a FormViewDialog when a required field is empty don't close the dialog", async () => {
361 Partner._views.form = /* xml */ `
362 <form string="Partner">
363 <sheet>
364 <group><field name="foo" required="1"/></group>
365 </sheet>
366 <footer>
367 <button name="save" special="save" class="btn-primary"/>
368 </footer>
369 </form>
370 `;
371 await mountWithCleanup(WebClient);
372 getService("dialog").add(FormViewDialog, {
373 resModel: "partner",
374 context: { answer: 42 },
375 });
376
377 await animationFrame();
378
379 await click('.modal button[name="save"]');
380 await animationFrame();
381
382 expect(".modal").toHaveCount(1, { message: "modal should still be opened" });
383 await click("[name='foo'] input");
384 await edit("new");
385 await click('.modal button[name="save"]');
386 await animationFrame();
387 expect(".modal").toHaveCount(0, { message: "modal should be closed" });
388 });
389
390 test("new record has an expand button", async () => {
391 Partner._views.form = /* xml */ `<form><field name="foo"/></form>`;
392 Partner._records = [];
393 onRpc("web_save", () => {
394 expect.step("save");
395 });
396 mockService("action", {
397 doAction(actionRequest) {
398 expect.step([
399 actionRequest.res_id,
400 actionRequest.res_model,
401 actionRequest.type,
402 actionRequest.views,
403 ]);
404 },
405 });
406 await mountWithCleanup(WebClient);
407 getService("dialog").add(FormViewDialog, {
408 resModel: "partner",
409 });
410 await animationFrame();
411 expect(".o_dialog .o_form_view").toHaveCount(1);
412 expect(".o_dialog .modal-header .o_expand_button").toHaveCount(1);
413 await fieldInput("foo").edit("new");
414 await click(".o_dialog .modal-header .o_expand_button");
415 await animationFrame();
416 expect.verifySteps(["save", [1, "partner", "ir.actions.act_window", [[false, "form"]]]]);
417 });
418
419 test("existing record has an expand button", async () => {
420 Partner._views.form = /* xml */ `<form><field name="foo"/></form>`;
421 onRpc("web_save", () => {
422 expect.step("save");
423 });
424 mockService("action", {
425 doAction(actionRequest) {
426 expect.step([
427 actionRequest.res_id,
428 actionRequest.res_model,
429 actionRequest.type,
430 actionRequest.views,
431 actionRequest.context,
432 ]);
433 },
434 });
435 await mountWithCleanup(WebClient);
436 getService("dialog").add(FormViewDialog, {
437 expandedFormRef: "test_partner_form_view",
438 resModel: "partner",
439 resId: 1,
440 context: { key: "val" },
441 });
442 await animationFrame();
443 expect(".o_dialog .o_form_view").toHaveCount(1);
444 expect(".o_dialog .modal-header .o_expand_button").toHaveCount(1);
445 await fieldInput("foo").edit("hola");
446 await click(".o_dialog .modal-header .o_expand_button");
447 await animationFrame();
448 expect.verifySteps([
449 "save",
450 [
451 1,
452 "partner",
453 "ir.actions.act_window",
454 [[false, "form"]],
455 {
456 key: "val",
457 form_view_ref: "test_partner_form_view",
458 },
459 ],
460 ]);
461 });
462
463 test("expand button with save and new", async () => {
464 Instrument._views.form = /* xml */ `<form><field name="name"/></form>`;
465 Instrument._records = [{ id: 1, name: "Violon" }];
466 onRpc("web_save", () => {
467 expect.step("save");
468 });
469 mockService("action", {
470 doAction(actionRequest) {
471 expect.step([
472 actionRequest.res_id,
473 actionRequest.res_model,
474 actionRequest.type,
475 actionRequest.views,
476 ]);
477 },
478 });
479 await mountWithCleanup(WebClient);
480 getService("dialog").add(FormViewDialog, {
481 resModel: "instrument",
482 resId: 1,
483 isToMany: true,
484 });
485 await animationFrame();
486 expect(".o_dialog .o_form_view").toHaveCount(1);
487 expect(".o_dialog .modal-header .o_expand_button").toHaveCount(1);
488 await fieldInput("name").edit("Violoncelle");
489 await click(".o_dialog .modal-footer .o_form_button_save_new");
490 await animationFrame();
491 await fieldInput("name").edit("Flute");
492 await click(".o_dialog .modal-header .o_expand_button");
493 await animationFrame();
494 expect.verifySteps([
495 "save",
496 "save",
497 [2, "instrument", "ir.actions.act_window", [[false, "form"]]],
498 ]);
499 });
500
501 test("FormViewDialog with canExpand set to false", async () => {
502 Partner._views.form = /* xml */ `<form><field name="foo"/></form>`;
503 Partner._records = [];
504 await mountWithCleanup(WebClient);
505 getService("dialog").add(FormViewDialog, {
506 resModel: "partner",
507 canExpand: false,
508 });
509 await animationFrame();
510 expect(".o_dialog .o_form_view").toHaveCount(1);
511 expect(".o_dialog .modal-header .o_expand_button").toHaveCount(0);
512 });
513
514 test.tags("desktop");
515 test("close dialog with escape after modifying a field with onchange (no blur)", async () => {
516 Partner._views.form = `<form><field name="foo"/></form>`;
517 Partner._onChanges.foo = () => {};
518 onRpc("web_save", () => {
519 throw new Error("should not save");
520 });
521
522 await mountWithCleanup(WebClient);
523
524 // must focus something else than body before opening the form view dialog, such that the ui
525 // service has something to focus on dialog close, which will then blur the input and fire the
526 // change event
527 await contains(".o_navbar_apps_menu button").focus();
528 expect(".o_navbar_apps_menu button").toBeFocused();
529
530 getService("dialog").add(FormViewDialog, {
531 resModel: "partner",
532 resId: 1,
533 });
534 await animationFrame();
535 expect(".o_dialog").toHaveCount(1);
536
537 await contains(".o_field_widget[name=foo] input").edit("new value", { confirm: false });
538 await press("escape");
539 await animationFrame();
540 expect(".o_dialog").toHaveCount(0);
541 expect(".o_navbar_apps_menu button").toBeFocused();
542 });
543
544 test.tags("desktop");
545 test("display a dialog if onchange result is a warning from within a dialog", async function () {
546 Instrument._views = {
547 form: `<form><field name="name" /></form>`,
548 };
549
550 onRpc("instrument", "onchange", () => {
551 expect.step("onchange warning");
552 return {
553 value: {
554 name: false,
555 },
556 warning: {
557 title: "Warning",
558 message: "You must first select a partner",
559 type: "dialog",
560 },
561 };
562 });
563 await mountView({
564 type: "form",
565 resModel: "partner",
566 arch: `<form><field name="instrument"/></form>`,
567 resId: 2,
568 });
569
570 await contains(".o_field_widget[name=instrument] input").edit("tralala", { confirm: false });
571 await runAllTimers();
572 await contains(".o_field_widget[name=instrument] .o_m2o_dropdown_option_create_edit").click();
573
574 await waitFor(".modal.o_inactive_modal");
575 expect(".modal").toHaveCount(2);
576 expect(".modal:not(.o_inactive_modal) .modal-body").toHaveText(
577 "You must first select a partner"
578 );
579
580 await contains(".modal:not(.o_inactive_modal) button").click();
581 expect(".modal").toHaveCount(1);
582 expect(".modal:not(.o_inactive_modal) .modal-title").toHaveText("Create Instruments");
583
584 expect.verifySteps(["onchange warning"]);
585 });