Debian Codegraph @

addons/web/static/tests/views/fields/many2one_field.test.js

raw
1 import { describe, expect, getFixture, test } from "@odoo/hoot";
2 import {
3 click,
4 middleClick,
5 press,
6 queryAll,
7 queryAllTexts,
8 queryOne,
9 scroll,
10 } from "@odoo/hoot-dom";
11 import { Deferred, animationFrame, runAllTimers } from "@odoo/hoot-mock";
12 import { Component, xml } from "@odoo/owl";
13 import {
14 clickFieldDropdown,
15 clickFieldDropdownItem,
16 clickSave,
17 contains,
18 defineModels,
19 editSearch,
20 fields,
21 getDropdownMenu,
22 getFacetTexts,
23 getService,
24 makeServerError,
25 mockService,
26 models,
27 mountView,
28 mountViewInDialog,
29 mountWithCleanup,
30 onRpc,
31 patchWithCleanup,
32 selectFieldDropdownItem,
33 serverState,
34 toggleMenuItem,
35 toggleSearchBarMenu,
36 validateSearch,
37 } from "@web/../tests/web_test_helpers";
38
39 import { user } from "@web/core/user";
40 import { Record } from "@web/model/record";
41 import { Field } from "@web/views/fields/field";
42 import { Many2XAutocomplete } from "@web/views/fields/relational_utils";
43 import { WebClient } from "@web/webclient/webclient";
44
45 describe.current.tags("desktop");
46
47 class ResPartner extends models.Model {
48 name = fields.Char({ string: "Res Partner Name" });
49
50 _records = [
51 {
52 id: 1,
53 name: "res partner",
54 },
55 ];
56 }
57
58 class Partner extends models.Model {
59 name = fields.Char({ string: "Displayed name" });
60 foo = fields.Char({ default: "My little Foo Value" });
61 bar = fields.Boolean({ default: true });
62 int_field = fields.Integer({ sortable: true });
63 p = fields.One2many({
64 string: "one2many field",
65 relation: "partner",
66 relation_field: "trululu",
67 });
68 turtles = fields.One2many({
69 string: "one2many turtle field",
70 relation: "turtle",
71 relation_field: "turtle_trululu",
72 });
73 trululu = fields.Many2one({ string: "Trululu", relation: "partner" });
74 res_trululu = fields.Many2one({ string: "Res Trululu", relation: "res.partner" });
75 timmy = fields.Many2many({ string: "pokemon", relation: "partner.type" });
76 product_id = fields.Many2one({ string: "Product", relation: "product" });
77 date = fields.Date({ string: "Some Date" });
78 datetime = fields.Datetime({ string: "Datetime Field" });
79 user_id = fields.Many2one({ string: "Users", relation: "res.users" });
80
81 _records = [
82 {
83 id: 1,
84 name: "first record",
85 bar: true,
86 foo: "yop",
87 int_field: 10,
88 p: [],
89 turtles: [2],
90 timmy: [],
91 trululu: 4,
92 res_trululu: 1,
93 user_id: 1,
94 },
95 {
96 id: 2,
97 name: "second record",
98 bar: true,
99 foo: "blip",
100 int_field: 9,
101 p: [],
102 timmy: [],
103 trululu: 1,
104 product_id: 37,
105 date: "2017-01-25",
106 datetime: "2016-12-12 10:55:05",
107 user_id: 1,
108 },
109 {
110 id: 4,
111 name: "aaa",
112 bar: false,
113 },
114 ];
115 }
116
117 class Product extends models.Model {
118 name = fields.Char();
119
120 _records = [
121 {
122 id: 37,
123 name: "xphone",
124 },
125 {
126 id: 41,
127 name: "xpad",
128 },
129 ];
130 }
131
132 class PartnerType extends models.Model {
133 name = fields.Char();
134
135 _records = [
136 { id: 12, name: "gold" },
137 { id: 14, name: "silver" },
138 ];
139 }
140
141 class Turtle extends models.Model {
142 name = fields.Char({ string: "Displayed name" });
143 turtle_foo = fields.Char({ string: "Foo" });
144 turtle_bar = fields.Boolean({ string: "Bar", default: true });
145 turtle_trululu = fields.Many2one({
146 string: "Trululu",
147 relation: "partner",
148 });
149 product_id = fields.Many2one({
150 string: "Product",
151 relation: "product",
152 required: true,
153 });
154 partner_ids = fields.Many2many({ string: "Partner", relation: "partner" });
155
156 _records = [
157 {
158 id: 1,
159 name: "leonardo",
160 turtle_bar: true,
161 turtle_foo: "yop",
162 partner_ids: [],
163 },
164 {
165 id: 2,
166 name: "donatello",
167 turtle_bar: true,
168 turtle_foo: "blip",
169 partner_ids: [2, 4],
170 },
171 {
172 id: 3,
173 name: "raphael",
174 product_id: 37,
175 turtle_bar: false,
176 turtle_foo: "kawa",
177 partner_ids: [],
178 },
179 ];
180 }
181
182 class Users extends models.Model {
183 _name = "res.users";
184 name = fields.Char();
185 partner_ids = fields.One2many({ relation: "partner", relation_field: "user_id" });
186
187 has_group() {
188 return true;
189 }
190
191 _records = [
192 {
193 id: 1,
194 name: "Aline",
195 partner_ids: [1, 2],
196 },
197 {
198 id: 2,
199 name: "Christine",
200 },
201 ];
202 }
203
204 defineModels([ResPartner, Partner, Product, PartnerType, Turtle, Users]);
205
206 test("many2ones in form views", async () => {
207 expect.assertions(2);
208 mockService("action", {
209 doAction(params) {
210 expect(params.res_id).toBe(17);
211 },
212 loadState() {},
213 });
214
215 Partner._views = {
216 form: `
217 <form>
218 <field name="name" />
219 </form>`,
220 };
221
222 onRpc("get_formview_action", function ({ args }) {
223 expect(args[0]).toEqual([4]);
224 return {
225 res_id: 17,
226 type: "ir.actions.act_window",
227 target: "current",
228 res_model: "res.partner",
229 };
230 });
231 onRpc("get_formview_id", function ({ args }) {
232 expect(args[0]).toEqual([4]);
233 return false;
234 });
235 await mountViewInDialog({
236 type: "form",
237 resModel: "partner",
238 resId: 1,
239 arch: `
240 <form>
241 <sheet>
242 <group>
243 <field name="trululu" string="custom label"/>
244 </group>
245 </sheet>
246 </form>`,
247 });
248
249 await contains(".o_external_button:enabled", { visible: false }).click();
250 expect(".o_dialog:not(.o_inactive_modal) .modal-title").toHaveText("Open: custom label");
251
252 // TODO: test that we can edit the record in the dialog, and that
253 // the value is correctly updated on close
254 });
255
256 test("editing a many2one, but not changing anything", async () => {
257 expect.assertions(1);
258
259 Partner._views = {
260 form: `
261 <form>
262 <field name="name" />
263 </form>`,
264 };
265 onRpc("get_formview_id", function ({ args }) {
266 expect(args[0]).toEqual([4]);
267 return false;
268 });
269 onRpc("web_save", () => {
270 throw new Error("should not call write");
271 });
272
273 await mountViewInDialog({
274 type: "form",
275 resModel: "partner",
276 resId: 1,
277 arch: `
278 <form>
279 <sheet>
280 <field name="trululu"/>
281 </sheet>
282 </form>`,
283 });
284
285 // click on the external button (should do an RPC)
286 await contains(".o_external_button", { visible: false }).click();
287 // save and close modal
288 await contains(".modal:eq(1) .o_form_button_save").click();
289 // save form
290 await clickSave();
291 });
292
293 test("context in many2one and default get", async () => {
294 expect.assertions(2);
295
296 Partner._fields.int_field.default = 14;
297 Partner._fields.trululu.default = 2;
298
299 onRpc("onchange", ({ args }) => {
300 const context = args[3].trululu.context;
301 expect(context.blip).toBe(undefined);
302 expect(context.blop).toBe(3);
303 });
304 await mountView({
305 type: "form",
306 resModel: "partner",
307 arch: `
308 <form>
309 <field name="int_field" />
310 <field name="trululu" context="{'blip': int_field, 'blop': 3}" />
311 </form>`,
312 });
313 });
314
315 test("do not send context in unity spec if field is invisible", async () => {
316 expect.assertions(1);
317
318 onRpc("web_read", ({ kwargs }) => {
319 expect(kwargs.specification).toEqual({
320 display_name: {},
321 int_field: {},
322 trululu: {
323 fields: {},
324 },
325 });
326 });
327 await mountView({
328 type: "form",
329 resModel: "partner",
330 resId: 1,
331 arch: `
332 <form>
333 <field name="int_field" />
334 <field name="trululu" invisible="1" context="{'blip': int_field, 'blop': 3}" />
335 </form>`,
336 });
337 });
338
339 test("editing a many2one (with form view opened with external button)", async () => {
340 expect.assertions(4);
341 Partner._views = {
342 form: `
343 <form>
344 <field name="foo" />
345 </form>`,
346 };
347
348 onRpc("get_formview_id", () => false);
349 onRpc("web_save", () => {
350 expect.step("web_save");
351 });
352 onRpc("read", ({ args, model, kwargs }) => {
353 if (model === "partner" && args[0][0] === 4) {
354 expect.step(`read partner: ${args[1]}`);
355 expect(kwargs.context.blip).toBe(10);
356 expect(kwargs.context.blop).toBe(3);
357 }
358 });
359
360 await mountViewInDialog({
361 type: "form",
362 resModel: "partner",
363 resId: 1,
364 arch: `
365 <form>
366 <sheet>
367 <field name="int_field" />
368 <field name="trululu" context="{'blip': int_field, 'blop': 3}"/>
369 </sheet>
370 </form>`,
371 });
372
373 // click on the external button (should do an RPC)
374 await contains(".o_external_button", { visible: false }).click();
375
376 await contains(".o_dialog:not(.o_inactive_modal) .o_field_widget[name='foo'] input").edit(
377 "brandon"
378 );
379
380 // save and close modal
381 await contains(".modal:eq(1) .o_form_button_save").click();
382 expect.verifySteps(["web_save", "read partner: display_name"]);
383 // save form
384 await clickSave();
385 expect.verifySteps([]);
386 });
387
388 test("many2ones in form views with show_address", async () => {
389 onRpc("web_read", ({ kwargs }) => {
390 if (kwargs.specification.trululu.context.show_address) {
391 return [
392 {
393 name: "",
394 trululu: { display_name: "aaa\nStreet\nCity ZIP" },
395 },
396 ];
397 }
398 });
399 await mountView({
400 type: "form",
401 resModel: "partner",
402 resId: 1,
403 arch: `
404 <form>
405 <sheet>
406 <group>
407 <field name="trululu" context="{'show_address': 1}" />
408 </group>
409 </sheet>
410 </form>`,
411 });
412
413 expect("input.o_input").toHaveValue("aaa");
414 expect(".o_field_many2one_extra").toHaveInnerHTML(
415 `<div class="text-truncate" title="Street">Street</div> <div class="text-truncate" title="City ZIP">City ZIP</div>`,
416 { type: "html" }
417 );
418 expect("button.o_external_button").toHaveCount(1);
419 });
420
421 test("many2one show_address in edit", async () => {
422 const namegets = {
423 1: "first record\nFirst\nRecord",
424 2: "second record\nSecond\nRecord",
425 4: "aaa\nAAA\nRecord",
426 };
427 onRpc("web_read", ({ kwargs, parent }) => {
428 if (kwargs.specification.trululu.context.show_address) {
429 const result = parent();
430 result[0].trululu = {
431 id: result[0].trululu.id,
432 display_name: namegets[result[0].trululu.id],
433 };
434 return result;
435 }
436 });
437 onRpc("web_name_search", ({ parent }) => {
438 const result = parent();
439 return result.map(({ id }) => ({
440 id,
441 display_name: namegets[id],
442 __formatted_display_name: namegets[id],
443 }));
444 });
445
446 await mountView({
447 type: "form",
448 resModel: "partner",
449 resId: 1,
450 arch: `
451 <form>
452 <sheet>
453 <group>
454 <field name="trululu" context="{'show_address': 1}" />
455 </group>
456 </sheet>
457 </form>`,
458 });
459
460 expect(".o_field_widget input").toHaveValue("aaa");
461 expect(".o_field_many2one_extra").toHaveInnerHTML(
462 `<div class="text-truncate" title="AAA">AAA</div><div class="text-truncate" title="Record">Record</div>`,
463 { type: "html" }
464 );
465
466 await contains(".o_field_widget input").edit("first record", { confirm: false });
467 await runAllTimers();
468 await contains(".dropdown-menu li").click();
469
470 expect(".o_field_widget input").toHaveValue("first record");
471 expect(".o_field_many2one_extra").toHaveInnerHTML(
472 `<div class="text-truncate" title="First">First</div><div class="text-truncate" title="Record">Record</div>`,
473 { type: "html" }
474 );
475
476 await contains(".o_field_widget input").edit("second record", { confirm: false });
477 await runAllTimers();
478 await contains(".dropdown-menu li").click();
479
480 expect(".o_field_widget input").toHaveValue("second record");
481 expect(".o_field_many2one_extra").toHaveInnerHTML(
482 `<div class="text-truncate" title="Second">Second</div><div class="text-truncate" title="Record">Record</div>`,
483 { type: "html" }
484 );
485 });
486
487 test("show_address works in a view embedded in a view of another type", async () => {
488 expect.assertions(2);
489 Turtle._records[1].turtle_trululu = 2;
490 Turtle._views = {
491 form: `
492 <form>
493 <field name="name" />
494 <field name="turtle_trululu" context="{'show_address': 1}" />
495 </form>`,
496 list: `
497 <list>
498 <field name="name" />
499 </list>`,
500 };
501
502 onRpc("turtle", "web_read", ({ kwargs }) => {
503 expect(kwargs.specification.turtle_trululu.context).toEqual({
504 show_address: 1,
505 });
506 return [
507 {
508 id: 1,
509 name: "donatello",
510 turtle_trululu: {
511 id: 2,
512 display_name: "second record\nrue morgue\nparis 75013",
513 },
514 },
515 ];
516 });
517
518 await mountView({
519 type: "form",
520 resModel: "partner",
521 resId: 1,
522 arch: `
523 <form edit="0">
524 <field name="name" />
525 <field name="turtles" />
526 </form>`,
527 });
528 // click the turtle field, opens a modal with the turtle form view
529 await contains(".o_data_row td.o_data_cell").click();
530
531 expect('[name="turtle_trululu"]').toHaveText("second record\nrue morgue\nparis 75013");
532 });
533
534 test("many2ones in form views with search more", async () => {
535 for (let i = 5; i < 11; i++) {
536 Partner._records.push({ id: i, name: `Partner ${i}` });
537 }
538 Partner._fields.datetime.searchable = true;
539 Partner._views = {
540 search: `
541 <search>
542 <filter name="filter" string="Filter" domain="[[0, '=', 1]]"/>
543 </search>`,
544 list: `
545 <list>
546 <field name="name" />
547 </list>`,
548 };
549
550 await mountView({
551 type: "form",
552 resModel: "partner",
553 resId: 1,
554 arch: `
555 <form>
556 <sheet>
557 <group>
558 <field name="trululu" />
559 </group>
560 </sheet>
561 </form>`,
562 });
563
564 await selectFieldDropdownItem("trululu", "Search more...");
565
566 expect("tr.o_data_row").toHaveCount(9);
567 expect(".o_field_widget[name=trululu] input").toHaveValue("aaa");
568
569 await toggleSearchBarMenu(".modal");
570 await toggleMenuItem("Filter");
571
572 expect("tr.o_data_row").toHaveCount(0);
573 });
574
575 test("many2ones: Open the selection dialog several times using the 'Search more...' button with a context containing 'search_default_...'", async () => {
576 for (let i = 5; i < 11; i++) {
577 Partner._records.push({ id: i, name: `Partner ${i}` });
578 }
579 Partner._fields.name.searchable = true;
580 Partner._views = {
581 search: `
582 <search>
583 <field name="name" />
584 </search>`,
585 list: `
586 <list>
587 <field name="name" />
588 </list>`,
589 };
590
591 await mountView({
592 type: "form",
593 resModel: "partner",
594 resId: 1,
595 arch: `
596 <form>
597 <sheet>
598 <group>
599 <field name="trululu" context="{ 'search_default_name': 'Partner 10'}"/>
600 </group>
601 </sheet>
602 </form>`,
603 });
604
605 await selectFieldDropdownItem("trululu", "Search more...");
606
607 expect(".modal .o_data_row").toHaveCount(1);
608 expect(getFacetTexts(".modal")).toEqual(["Displayed name\nPartner 10"]);
609
610 await contains(".modal .btn-close").click();
611 expect(".modal").toHaveCount(0);
612
613 await selectFieldDropdownItem("trululu", "Search more...");
614 expect(".modal .o_data_row").toHaveCount(1);
615 expect(getFacetTexts(".modal")).toEqual(["Displayed name\nPartner 10"]);
616 });
617
618 test("many2ones in list views: create in dialog keeps the input", async () => {
619 Partner._views = {
620 form: `
621 <form>
622 <field name="name" />
623 </form>`,
624 };
625
626 onRpc("web_save", ({ args }) => {
627 expect.step(`web_save: ${JSON.stringify(args)}`);
628 });
629
630 await mountView({
631 type: "list",
632 resModel: "partner",
633 arch: `
634 <list editable="top">
635 <field name="trululu" />
636 </list>`,
637 });
638
639 await contains(".o_data_cell:eq(0)").click();
640 await contains(".o_field_widget[name=trululu] input").edit("yy", { confirm: false });
641 await runAllTimers();
642 await clickFieldDropdownItem("trululu", "Create and edit...");
643
644 await clickSave();
645 expect.verifySteps([`web_save: [[],{"name":"yy"}]`]);
646 expect(".o_field_widget[name=trululu] input").toHaveValue("yy");
647
648 await contains(getFixture()).click();
649 expect.verifySteps([`web_save: [[1],{"trululu":5}]`]);
650 expect(".o_data_cell[name=trululu]:eq(0)").toHaveText("yy");
651 });
652
653 test("many2ones in list views: create a new record with a context", async () => {
654 expect.assertions(6);
655 onRpc("res.users", "name_create", ({ kwargs }) => {
656 const { context } = kwargs;
657 expect.step("name_create");
658 expect(context.default_test).toBe(1);
659 expect(context.test).toBe(2);
660 expect(context).not.toInclude("default_yop");
661 expect(context.yop).toBe(4);
662 });
663 await mountView({
664 type: "list",
665 resModel: "partner",
666 arch: `
667 <list editable="top">
668 <field name="user_id" context="{'default_test': 1, 'test':2 }" />
669 </list>`,
670 context: {
671 default_yop: 3,
672 yop: 4,
673 },
674 });
675
676 await contains(".o_data_cell:eq(0)").click();
677 await contains(".o_field_widget[name=user_id] input").edit("yy", { confirm: false });
678 await runAllTimers();
679 await clickFieldDropdownItem("user_id", 'Create "yy"');
680 expect(".o_external_button").toHaveCount(1);
681 expect.verifySteps(["name_create"]);
682 });
683
684 test("using a many2one widget must take into account the decorations", async () => {
685 await mountView({
686 type: "list",
687 resModel: "partner",
688 arch: `
689 <list>
690 <field name="user_id" decoration-danger="int_field > 9" widget="many2one"/>
691 <field name="int_field"/>
692 </list>`,
693 });
694
695 expect(".o_list_many2one a.text-danger").toHaveCount(1);
696 expect(".o_data_row").toHaveCount(3);
697 });
698
699 test("onchanges on many2ones trigger when editing record in form view", async () => {
700 expect.assertions(2);
701 Partner._onChanges = {
702 user_id: () => {},
703 };
704 Users._fields.other_field = fields.Char({ string: "Other Field" });
705 Users._views = {
706 form: `
707 <form>
708 <field name="other_field" />
709 </form>`,
710 };
711 onRpc("get_formview_id", () => false);
712 onRpc("onchange", ({ args }) => {
713 expect(args[1].user_id).toBe(1);
714 });
715 onRpc(({ method }) => {
716 expect.step(method);
717 });
718
719 await mountViewInDialog({
720 type: "form",
721 resModel: "partner",
722 resId: 1,
723 arch: `
724 <form>
725 <sheet>
726 <group>
727 <field name="user_id"/>
728 </group>
729 </sheet>
730 </form>`,
731 });
732
733 // open the many2one in form view and change something
734 await contains(".o_external_button", { visible: false }).click();
735 await contains(
736 ".o_dialog:not(.o_inactive_modal) .o_field_widget[name='other_field'] input"
737 ).edit("wood");
738
739 // TODISCUSS ? Same record, don't change the display name (opti ?)
740 // save the modal and make sure an onchange is triggered
741 await contains(".modal:eq(1) .o_form_button_save").click();
742 expect.verifySteps([
743 "get_views",
744 "web_read",
745 "get_formview_id",
746 "get_views",
747 "web_read",
748 "web_save",
749 "read",
750 "onchange",
751 ]);
752 });
753
754 test("edit many2one before onchange is finished should not reset the value", async () => {
755 Partner._onChanges = {
756 name: function (obj) {
757 obj.user_id = 19;
758 },
759 };
760 onRpc("onchange", () => {
761 expect.step("onchange");
762 return def;
763 });
764
765 const def = new Deferred();
766 await mountView({
767 type: "form",
768 resModel: "partner",
769 resId: 1,
770 arch: `
771 <form>
772 <field name="name"/>
773 <field name="user_id"/>
774 </form>`,
775 });
776
777 await contains("[name='name'] input").edit("new name");
778 await contains("[name='user_id'] input").edit("Plop");
779 expect("[name='user_id'] input").toHaveValue("Plop");
780
781 def.resolve();
782 await animationFrame();
783 expect("[name='user_id'] input").toHaveValue("Plop");
784
785 expect.verifySteps(["onchange"]);
786 });
787
788 test("many2one doesn't trigger field_change when being emptied", async () => {
789 await mountView({
790 type: "list",
791 resModel: "partner",
792 arch: `
793 <list multi_edit="1">
794 <field name="trululu"/>
795 </list>`,
796 });
797
798 // Select two records
799 await contains(".o_data_row:eq(0) .o_list_record_selector input").click();
800 await contains(".o_data_row:eq(1) .o_list_record_selector input").click();
801 await contains(".o_data_row .o_data_cell").click();
802 await contains(".o_field_widget[name=trululu] input").clear({ confirm: false });
803 await runAllTimers();
804 expect(".modal").toHaveCount(0);
805
806 await contains(".o_field_widget[name=trululu] .ui-menu-item").click();
807 expect(".modal").toHaveCount(1);
808 });
809
810 test("..._view_ref keys are removed from many2one context on create and edit", async () => {
811 onRpc("get_views", ({ kwargs, method }) => {
812 expect.step(
813 JSON.stringify([method, kwargs.context.default_name, kwargs.context.form_view_ref])
814 );
815 });
816 await mountView({
817 type: "form",
818 resModel: "partner",
819 arch: `
820 <form>
821 <sheet>
822 <group>
823 <field name="trululu"/>
824 </group>
825 </sheet>
826 </form>`,
827 context: {
828 form_view_ref: "test_form_view",
829 },
830 });
831
832 expect.verifySteps(['["get_views",null,"test_form_view"]']);
833 await contains(".o_field_widget[name=trululu] input").edit("ABC", { confirm: false });
834 await runAllTimers();
835 await contains(".o_field_widget[name=trululu] .o_m2o_dropdown_option_create_edit").click();
836 expect.verifySteps(['["get_views",null,null]']);
837 });
838
839 test("empty a many2one field in list view", async () => {
840 onRpc("web_save", ({ args }) => {
841 expect.step("web_save");
842 expect(args[1]).toEqual({ trululu: false });
843 });
844 await mountView({
845 type: "list",
846 resModel: "partner",
847 arch: `
848 <list editable="top">
849 <field name="trululu"/>
850 </list>`,
851 });
852
853 await contains(".o_data_row .o_data_cell").click();
854 await contains(".o_field_widget[name=trululu] input").edit("");
855 expect(".o_data_row .o_field_widget[name=trululu] input").toHaveText("");
856
857 await contains(".o_list_view").click();
858 expect(".o_data_row:eq(0)").toHaveText("");
859
860 expect.verifySteps(["web_save"]);
861 });
862
863 test("focus tracking on a many2one in a list", async () => {
864 Partner._views = {
865 form: `
866 <form>
867 <field name="foo" />
868 </form>`,
869 };
870
871 await mountView({
872 type: "list",
873 resModel: "partner",
874 arch: `
875 <list editable="top">
876 <field name="trululu"/>
877 </list>`,
878 });
879
880 await contains(".o_data_row .o_data_cell").click();
881 expect(".o_data_row .o_data_cell input").toBeFocused();
882
883 await contains(".o_field_widget[name=trululu] input").edit("ABC", { confirm: false });
884 await runAllTimers();
885 await contains(".o_field_widget[name=trululu] .o_m2o_dropdown_option_create_edit").click();
886
887 // At this point, if the focus is correctly registered by the m2o, there
888 // should be only one modal (the "Create" one) and none for saving changes.
889 expect(".modal").toHaveCount(1);
890
891 await contains(".o_form_button_cancel").click();
892
893 expect(".o_data_row .o_data_cell input").toBeFocused();
894 expect(".o_data_row .o_data_cell input").toHaveValue("");
895 });
896
897 test('many2one fields with option "no_open"', async () => {
898 await mountView({
899 type: "form",
900 resModel: "partner",
901 resId: 1,
902 arch: `
903 <form>
904 <sheet>
905 <group>
906 <field name="trululu" options="{'no_open': 1}" />
907 </group>
908 </sheet>
909 </form>`,
910 });
911
912 expect(".o_field_widget[name='trululu'] .o_external_button").toHaveCount(0);
913 });
914
915 test("empty many2one field", async () => {
916 await mountView({
917 type: "form",
918 resModel: "partner",
919 arch: `
920 <form>
921 <sheet>
922 <group>
923 <field name="trululu" />
924 </group>
925 </sheet>
926 </form>`,
927 });
928
929 await contains(".o_field_many2one input").click();
930 expect(".dropdown-menu li.o_m2o_dropdown_option").toHaveCount(1);
931 expect(".dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
932
933 await contains(".o_field_many2one[name='trululu'] input").edit("abc", { confirm: false });
934 await runAllTimers();
935
936 expect(".dropdown-menu li.o_m2o_dropdown_option").toHaveCount(2);
937 expect(".dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
938 expect(".dropdown-menu li.o_m2o_no_result").toHaveCount(0);
939 });
940
941 test("empty readonly many2one field", async () => {
942 await mountView({
943 type: "form",
944 resModel: "partner",
945 arch: `<form><field name="trululu" readonly="1"/></form>`,
946 });
947
948 expect("div.o_field_widget[name=trululu]").toHaveCount(1);
949 expect(".o_field_widget[name=trululu] .o_many2one").toHaveText("");
950 });
951
952 test("empty many2one field with no result", async () => {
953 patchWithCleanup(Many2XAutocomplete.prototype, {
954 getCreationContext(value) {
955 expect(value).toBe("");
956 const context = super.getCreationContext(value);
957 expect(context[`default_${this.props.nameCreateField}`]).toBe(undefined);
958 return context;
959 },
960 });
961 class M2O extends models.Model {
962 m2o = fields.Many2one({ relation: "m2o" });
963 }
964 defineModels([M2O]);
965 await mountView({
966 type: "form",
967 resModel: "m2o",
968 arch: `
969 <form>
970 <sheet>
971 <group>
972 <field name="m2o" />
973 </group>
974 </sheet>
975 </form>`,
976 });
977
978 await contains(".o_field_many2one input").click();
979 expect(".dropdown-menu li.o_m2o_dropdown_option").toHaveCount(1);
980 expect(".dropdown-menu li.o_m2o_dropdown_option").toHaveText("Create...");
981 expect(".dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
982
983 await contains(".dropdown-menu li.o_m2o_dropdown_option").click();
984 expect(".o_dialog").toHaveCount(1);
985 expect(".o_dialog .o_field_many2one[name=m2o] input").toHaveValue("");
986 press("Esc");
987 await animationFrame();
988 expect(".o_dialog").toHaveCount(0);
989
990 await contains(".o_field_many2one input").edit("abc", { confirm: false });
991 await runAllTimers();
992
993 expect(".dropdown-menu li.o_m2o_dropdown_option").toHaveCount(2);
994 expect(".dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
995 expect(".dropdown-menu li.o_m2o_no_result").toHaveCount(0);
996 });
997
998 test("empty many2one field with no result and no create & edit", async () => {
999 class M2O extends models.Model {
1000 m2o = fields.Many2one({ relation: "m2o" });
1001 }
1002 defineModels([M2O]);
1003 await mountView({
1004 type: "form",
1005 resModel: "m2o",
1006 arch: `
1007 <form>
1008 <sheet>
1009 <group>
1010 <field name="m2o" options="{'no_create_edit': 1}"/>
1011 </group>
1012 </sheet>
1013 </form>`,
1014 });
1015
1016 await contains(".o_field_many2one input").click();
1017 expect(".dropdown-menu li").toHaveCount(1);
1018 expect(".dropdown-menu li.o_m2o_start_typing").toHaveCount(1);
1019
1020 await contains(".o_field_many2one input").edit("a", { confirm: false });
1021 await runAllTimers();
1022
1023 expect(".dropdown-menu li").toHaveCount(1);
1024 expect(".dropdown-menu li.o_m2o_dropdown_option_create").toHaveCount(1);
1025 });
1026
1027 test("empty many2one field with node options", async () => {
1028 expect.assertions(2);
1029
1030 await mountView({
1031 type: "form",
1032 resModel: "partner",
1033 arch: `
1034 <form>
1035 <sheet>
1036 <group>
1037 <field name="trululu" options="{'no_create_edit': 1}" />
1038 <field name="product_id" options="{'no_create_edit': 1, 'no_quick_create': 1}" />
1039 </group>
1040 </sheet>
1041 </form>`,
1042 });
1043
1044 await contains(".o_field_many2one[name='trululu'] input").click();
1045 expect(".o_field_many2one[name='trululu'] .dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
1046
1047 await contains(".o_field_many2one[name='product_id'] input").click();
1048 expect(".o_field_many2one[name='product_id'] .dropdown-menu li.o_m2o_start_typing").toHaveCount(
1049 0
1050 );
1051 });
1052
1053 test("many2one with no_create_edit and no_quick_create options should show no records when no result match", async () => {
1054 expect.assertions(2);
1055
1056 await mountView({
1057 type: "form",
1058 resModel: "partner",
1059 arch: `
1060 <form>
1061 <sheet>
1062 <group>
1063 <field name="product_id" options="{'no_create_edit': 1, 'no_quick_create': 1}" />
1064 </group>
1065 </sheet>
1066 </form>`,
1067 });
1068
1069 await contains(".o_field_many2one[name='product_id'] input").click();
1070 expect(".o_field_many2one[name='product_id'] .dropdown-menu li.o_m2o_no_result").toHaveCount(0);
1071 await contains(".o_field_many2one[name='product_id'] input").edit("aze", { confirm: false });
1072 await runAllTimers();
1073 expect(".o_field_many2one[name='product_id'] .dropdown-menu li.o_m2o_no_result").toHaveCount(1);
1074 });
1075
1076 test("many2one in edit mode", async () => {
1077 expect.assertions(17);
1078
1079 // create 10 partners to have the 'Search more' option in the autocomplete dropdown
1080 for (let i = 0; i < 10; i++) {
1081 const id = 20 + i;
1082 Partner._records.push({ id, name: `Partner ${id}` });
1083 }
1084
1085 Partner._views = {
1086 list: `
1087 <list>
1088 <field name="name" />
1089 </list>`,
1090 search: `
1091 <search>
1092 <field name="name" string="Name" />
1093 </search>`,
1094 };
1095
1096 onRpc("partner", "web_save", ({ args }) => {
1097 expect(args[1].trululu).toBe(20);
1098 });
1099
1100 await mountView({
1101 type: "form",
1102 resModel: "partner",
1103 resId: 1,
1104 arch: `
1105 <form>
1106 <sheet>
1107 <group>
1108 <field name="trululu" />
1109 </group>
1110 </sheet>
1111 </form>`,
1112 });
1113
1114 await clickFieldDropdown("trululu");
1115 expect(".o_field_many2one[name='trululu'] .dropdown-menu").toBeVisible();
1116 expect(
1117 ".o_field_many2one[name='trululu'] .dropdown-menu li:not(.o_m2o_dropdown_option)"
1118 ).toHaveCount(8);
1119 expect(".o_field_many2one[name='trululu'] .dropdown-menu li.o_m2o_dropdown_option").toHaveCount(
1120 1
1121 );
1122 expect(".o_field_many2one[name='trululu'] .dropdown-menu li.o_m2o_start_typing").toHaveCount(0);
1123
1124 await contains(".o_field_many2one[name='trululu'] input").click();
1125 expect(".o_field_many2one[name='trululu'] .dropdown-menu").toHaveCount(0);
1126
1127 // change the value of the m2o with a suggestion of the dropdown
1128 await selectFieldDropdownItem("trululu", "first record");
1129 expect(".o_field_many2one[name='trululu'] .dropdown-menu").not.toHaveCount();
1130 expect(".o_field_many2one input").toHaveValue("first record");
1131
1132 // change the value of the m2o with a record in the 'Search more' modal
1133 await clickFieldDropdown("trululu");
1134 // click on 'Search more' (mouseenter required by ui-autocomplete)
1135 await contains(
1136 ".o_field_many2one[name='trululu'] .dropdown-menu .o_m2o_dropdown_option_search_more"
1137 ).click();
1138 expect(".modal .o_list_view").toHaveCount(1);
1139 expect(".modal .o_list_view .o_list_record_selector").toHaveCount(0);
1140 expect(".modal .modal-footer .o_select_button").toHaveCount(0);
1141 expect(queryAll(".modal tbody tr").length).toBeGreaterThan(10, {
1142 message: "list should contain more than 10 records",
1143 });
1144 await contains(".modal .o_searchview_input").edit("P", { confirm: false });
1145 await runAllTimers();
1146 await press("Enter");
1147 await animationFrame();
1148 expect(".modal tbody tr").toHaveCount(10);
1149 // choose a record
1150 await contains(".modal .o_data_cell[data-tooltip='Partner 20']").click();
1151 expect(".modal").toHaveCount(0);
1152 expect(".o_field_many2one[name='trululu'] .dropdown-menu").not.toHaveCount();
1153 expect(".o_field_many2one input").toHaveValue("Partner 20");
1154
1155 // save
1156 await clickSave();
1157 expect(".o_field_many2one input").toHaveValue("Partner 20");
1158 });
1159
1160 test("many2one in non edit mode (with value)", async () => {
1161 await mountView({
1162 type: "form",
1163 resModel: "partner",
1164 resId: 1,
1165 arch: `
1166 <form edit="0">
1167 <field name="res_trululu" />
1168 <field name="trululu" />
1169 </form>`,
1170 });
1171
1172 expect("a.o_form_uri").toHaveCount(2);
1173 expect("div[name=res_trululu] a.o_form_uri").toHaveAttribute("href", "/odoo/res.partner/1");
1174 expect("div[name=trululu] a.o_form_uri").toHaveAttribute("href", "/odoo/m-partner/4");
1175 });
1176
1177 test("many2one in non edit mode (without value)", async () => {
1178 Partner._records[0].trululu = false;
1179
1180 await mountView({
1181 type: "form",
1182 resModel: "partner",
1183 resId: 1,
1184 arch: `
1185 <form edit="0">
1186 <field name="trululu" />
1187 </form>`,
1188 });
1189
1190 // Remove value from many2one and then save, there should be no link anymore
1191 expect("a.o_form_uri").toHaveCount(0);
1192 });
1193
1194 test("many2one with co-model whose name field is a many2one", async () => {
1195 Product._fields.name = fields.Many2one({
1196 string: "User Name",
1197 relation: "res.users",
1198 });
1199 Product._records = [
1200 {
1201 id: 37,
1202 name: 1,
1203 },
1204 {
1205 id: 41,
1206 name: 2,
1207 },
1208 ];
1209 Product._views = {
1210 form: `
1211 <form>
1212 <field name="name" />
1213 </form>`,
1214 };
1215
1216 await mountView({
1217 type: "form",
1218 resModel: "partner",
1219 arch: `
1220 <form>
1221 <field name="product_id" />
1222 </form>`,
1223 });
1224
1225 await contains("div[name=product_id] input").edit("ABC", { confirm: false });
1226 await runAllTimers();
1227 await contains("div[name=product_id] .o_m2o_dropdown_option_create_edit").click();
1228 expect(".modal .o_form_view").toHaveCount(1);
1229
1230 // quick create 'new value'
1231 await contains(".modal div[name=name] input").edit("new value", { confirm: false });
1232 await runAllTimers();
1233 await contains(".modal div[name=name] .o_m2o_dropdown_option").click();
1234 expect(".modal div[name=name] input").toHaveValue("new value");
1235
1236 await contains(".modal .o_form_button_save").click();
1237 expect(".modal .o_form_view").toHaveCount(0);
1238 expect("div[name=product_id] input").toHaveValue("new value");
1239 });
1240
1241 test("many2one searches with correct value", async () => {
1242 onRpc("web_name_search", ({ kwargs }) => {
1243 expect.step(`search: ${kwargs.name}`);
1244 });
1245 await mountView({
1246 type: "form",
1247 resModel: "partner",
1248 resId: 1,
1249 arch: `
1250 <form>
1251 <sheet>
1252 <field name="trululu" />
1253 </sheet>
1254 </form>`,
1255 });
1256
1257 expect(".o_field_many2one input").toHaveValue("aaa");
1258 await contains(".o_field_many2one input").click();
1259 expect.verifySteps(["search: "]);
1260
1261 // unset the many2one -> should search again with ''
1262 await contains(".o_field_many2one input").clear({ confirm: false });
1263 await runAllTimers();
1264 expect.verifySteps(["search: "]);
1265
1266 await contains(".o_field_many2one input").edit("f", { confirm: false });
1267 await runAllTimers();
1268 expect.verifySteps(["search: f"]);
1269
1270 // close and re-open the dropdown -> should search with 'f' again
1271 await contains(".o_field_many2one input").click();
1272 await runAllTimers();
1273 await contains(".o_field_many2one input").click();
1274 await runAllTimers();
1275 expect.verifySteps(["search: f"]);
1276 });
1277
1278 test("no additional searches after no result is found", async () => {
1279 onRpc("web_name_search", ({ kwargs }) => {
1280 expect.step(`search: ${kwargs.name}`);
1281 });
1282 await mountView({
1283 type: "form",
1284 resModel: "partner",
1285 resId: 1,
1286 arch: `
1287 <form>
1288 <sheet>
1289 <field name="trululu" />
1290 </sheet>
1291 </form>`,
1292 });
1293
1294 expect(".o_field_many2one input").toHaveValue("aaa");
1295 await contains(".o_field_many2one input").clear({ confirm: false });
1296 await runAllTimers();
1297 expect.verifySteps(["search: "]);
1298
1299 await contains(".o_field_many2one input").edit("p", { confirm: false });
1300 await runAllTimers();
1301 expect.verifySteps(["search: p"]);
1302 expect(".o_many2one .dropdown-menu li:not(.o_m2o_dropdown_option)").toHaveCount(0, {
1303 message: "no result is found",
1304 });
1305 await contains(".o_field_many2one input").edit("pe", { confirm: false });
1306 await runAllTimers();
1307 // no web_name_search because there was no result for "p"
1308 expect.verifySteps([]);
1309 await contains(".o_field_many2one input").edit("m", { confirm: false });
1310 await runAllTimers();
1311 // request is no longer an extension of "p" (no result), need a web_name_search
1312 expect.verifySteps(["search: m"]);
1313 });
1314
1315 test("do not prevent previously empty searches once domain is changed", async () => {
1316 let count = 0;
1317 onRpc("web_name_search", ({ kwargs }) => {
1318 count++;
1319 if (count < 3) {
1320 expect(kwargs.domain).toEqual([["foo", "=", "yop"]]);
1321 } else {
1322 expect(kwargs.domain).toEqual([["foo", "=", "not yop"]]);
1323 }
1324 expect.step(`search: ${kwargs.name}`);
1325 });
1326 await mountView({
1327 type: "form",
1328 resModel: "partner",
1329 resId: 1,
1330 arch: `
1331 <form>
1332 <field name="foo" />
1333 <field name="trululu" domain="[('foo', '=', foo)]" />
1334 </form>`,
1335 });
1336
1337 expect(".o_field_many2one input").toHaveValue("aaa");
1338 await contains(".o_field_many2one input").clear({ confirm: false });
1339 await runAllTimers();
1340 expect.verifySteps(["search: "]);
1341 await contains(".o_field_many2one input").edit("p", { confirm: false });
1342 await runAllTimers();
1343 expect.verifySteps(["search: p"]);
1344 expect(".o_many2one .dropdown-menu li:not(.o_m2o_dropdown_option)").toHaveCount(0, {
1345 message: "no result is found",
1346 });
1347 await contains(".o_field_many2one input").edit("pe");
1348 await runAllTimers();
1349 // no web_name_search because there was no result for "p"
1350 await contains(".o_field_widget[name='foo'] input").edit("not yop", { confirm: false });
1351 await contains(".o_field_many2one input").edit("pe");
1352 await runAllTimers();
1353 // domain changed so the previously empty search is launched again
1354 expect.verifySteps(["search: pe"]);
1355 });
1356
1357 test("do not prevent previously empty searches once context is changed", async () => {
1358 let count = 0;
1359 onRpc("web_name_search", ({ kwargs }) => {
1360 count++;
1361 if (count < 3) {
1362 expect(kwargs.context.foo).toBe("yop");
1363 } else {
1364 expect(kwargs.context.foo).toBe("not yop");
1365 }
1366 expect.step(`search: ${kwargs.name}`);
1367 });
1368 await mountView({
1369 type: "form",
1370 resModel: "partner",
1371 resId: 1,
1372 arch: `
1373 <form>
1374 <field name="foo" />
1375 <field name="trululu" context="{'foo': foo}" />
1376 </form>`,
1377 });
1378
1379 expect(".o_field_many2one input").toHaveValue("aaa");
1380 await contains(".o_field_many2one input").clear({ confirm: false });
1381 await runAllTimers();
1382 expect.verifySteps(["search: "]);
1383 await contains(".o_field_many2one input").edit("p", { confirm: false });
1384 await runAllTimers();
1385 expect.verifySteps(["search: p"]);
1386 expect(".o_many2one .dropdown-menu li:not(.o_m2o_dropdown_option)").toHaveCount(0, {
1387 message: "no result is found",
1388 });
1389 await contains(".o_field_many2one input").edit("pe");
1390 await runAllTimers();
1391 // no web_name_search because there was no result for "p"
1392 await contains(".o_field_widget[name='foo'] input").edit("not yop", { confirm: false });
1393 await contains(".o_field_many2one input").edit("pe");
1394 await runAllTimers();
1395 // context changed so the previously empty search is launched again
1396 expect.verifySteps(["search: pe"]);
1397 });
1398
1399 test("many2one search with trailing and leading spaces", async () => {
1400 onRpc("web_name_search", ({ kwargs }) => {
1401 expect.step("search: " + kwargs.name);
1402 });
1403 await mountView({
1404 type: "form",
1405 resModel: "partner",
1406 arch: `
1407 <form>
1408 <field name="trululu" />
1409 </form>`,
1410 });
1411
1412 const input = ".o_field_many2one[name='trululu'] input";
1413 await contains(input).click();
1414
1415 expect(".o_field_many2one[name='trululu'] .dropdown-menu").toBeVisible();
1416 expect(
1417 ".o_field_many2one[name='trululu'] .dropdown-menu li:not(.o_m2o_dropdown_option)"
1418 ).toHaveCount(3);
1419
1420 // search with leading spaces
1421 await contains(input).edit(" first", { confirm: false });
1422 await runAllTimers();
1423 expect(
1424 ".o_field_many2one[name='trululu'] .dropdown-menu li:not(.o_m2o_dropdown_option)"
1425 ).toHaveCount(1);
1426
1427 // search with trailing spaces
1428 await contains(input).edit("first ", { confirm: false });
1429 await runAllTimers();
1430 expect(
1431 ".o_field_many2one[name='trululu'] .dropdown-menu li:not(.o_m2o_dropdown_option)"
1432 ).toHaveCount(1);
1433
1434 // search with leading and trailing spaces
1435 await contains(input).edit(" first ", { confirm: false });
1436 await runAllTimers();
1437 expect(
1438 ".o_field_many2one[name='trululu'] .dropdown-menu li:not(.o_m2o_dropdown_option)"
1439 ).toHaveCount(1);
1440
1441 expect.verifySteps(["search: ", "search: first", "search: first", "search: first"]);
1442 });
1443
1444 // Should be removed ?
1445 test("many2one field with option always_reload (edit)", async () => {
1446 onRpc("web_read", ({ parent }) => {
1447 const result = parent();
1448 result[0].trululu = {
1449 ...result[0].trululu,
1450 display_name: "first record\nand some address",
1451 };
1452 return result;
1453 });
1454 await mountView({
1455 type: "form",
1456 resModel: "partner",
1457 resId: 2,
1458 arch: `
1459 <form>
1460 <field name="trululu" />
1461 </form>`,
1462 });
1463
1464 expect(".o_field_widget[name='trululu'] input").toHaveValue("first record");
1465 expect(".o_field_many2one_extra").toHaveCount(1);
1466 expect(".o_field_many2one_extra").toHaveText("and some address");
1467 });
1468
1469 test("many2one field and list navigation", async () => {
1470 await mountView({
1471 type: "list",
1472 resModel: "partner",
1473 arch: `
1474 <list editable="bottom">
1475 <field name="trululu"/>
1476 </list>`,
1477 });
1478
1479 // edit first input, to trigger autocomplete
1480 await contains(".o_data_row .o_data_cell").click();
1481 await contains(".o_data_cell input").clear();
1482
1483 // press keydown, to select first choice
1484 await press("arrowdown");
1485
1486 // we now check that the dropdown is open (and that the focus did not go
1487 // to the next line)
1488 expect(".o_field_many2one").toHaveCount(1);
1489 expect(".o_data_row:eq(0)").toHaveClass("o_selected_row");
1490 expect(".o_data_row:eq(1)").not.toHaveClass("o_selected_row");
1491 });
1492
1493 test("standalone many2one field", async () => {
1494 class Comp extends Component {
1495 static components = { Record, Field };
1496 static template = xml`
1497 <Record resModel="'coucou'" fields="fields" fieldNames="['partner_id']" values="values" mode="'edit'" t-slot-scope="scope">
1498 <Field name="'partner_id'" record="scope.record" canOpen="false" />
1499 </Record>
1500 `;
1501 static props = ["*"];
1502 setup() {
1503 this.fields = {
1504 partner_id: {
1505 display_name: "partner_id",
1506 type: "many2one",
1507 relation: "partner",
1508 },
1509 };
1510 this.values = {
1511 partner_id: [1, "first partner"],
1512 };
1513 }
1514 }
1515
1516 onRpc(({ method }) => {
1517 expect.step(method);
1518 });
1519
1520 await mountWithCleanup(Comp);
1521
1522 await contains(".o_field_widget input").edit("xyzzrot", { confirm: false });
1523 await runAllTimers();
1524 await contains(".o_field_widget .o_m2o_dropdown_option_create").click();
1525 expect(".o_field_widget .o_external_button").toHaveCount(0);
1526 expect.verifySteps(["web_name_search", "name_create"]);
1527 });
1528
1529 test("form: quick create then save directly", async () => {
1530 expect.assertions(3);
1531
1532 const def = new Deferred();
1533 const newRecordId = 5; // with the current records, the created record will be assigned id 5
1534 onRpc("name_create", async () => {
1535 expect.step("name_create");
1536 await def;
1537 });
1538 onRpc("web_save", ({ args }) => {
1539 expect.step("web_save");
1540 expect(args[1].trululu).toBe(newRecordId);
1541 });
1542
1543 await mountView({
1544 type: "form",
1545 resModel: "partner",
1546 arch: '<form><field name="trululu" /></form>',
1547 });
1548
1549 await contains(".o_field_widget[name=trululu] input").edit("b", { confirm: false });
1550 await runAllTimers();
1551 await contains(".o_m2o_dropdown_option_create").click();
1552 await contains(".o_form_button_save").click();
1553
1554 // should wait for the name_create before creating the record
1555 expect.verifySteps(["name_create"]);
1556
1557 def.resolve();
1558 await animationFrame();
1559
1560 expect.verifySteps(["web_save"]);
1561 });
1562
1563 test("form: quick create for field that returns false after name_create call", async () => {
1564 onRpc("name_create", () => {
1565 expect.step("name_create");
1566 // Resolve the name_create call to false. This is possible if
1567 // _rec_name for the model of the field is unassigned.
1568 return false;
1569 });
1570 await mountView({
1571 type: "form",
1572 resModel: "partner",
1573 arch: '<form><field name="trululu" /></form>',
1574 });
1575
1576 await contains(".o_field_widget[name=trululu] input").edit("beam", { confirm: false });
1577 await runAllTimers();
1578 await contains(".o_m2o_dropdown_option_create").click();
1579 expect.verifySteps(["name_create"]);
1580 expect(".o_input_dropdown input").toHaveValue("");
1581 });
1582
1583 test("list: quick create then save directly", async () => {
1584 expect.assertions(8);
1585 const def = new Deferred();
1586 const newRecordId = 5;
1587
1588 onRpc("name_create", async () => {
1589 expect.step("name_create");
1590 await def;
1591 });
1592 onRpc("web_save", ({ args }) => {
1593 expect.step("web_save");
1594 const values = args[1];
1595 expect(values.trululu).toBe(newRecordId);
1596 });
1597
1598 await mountView({
1599 type: "list",
1600 resModel: "partner",
1601 arch: `
1602 <list editable="top">
1603 <field name="trululu" />
1604 </list>`,
1605 });
1606
1607 expect(".o_data_row").toHaveCount(3);
1608
1609 await contains(".o_control_panel_main_buttons .o_list_button_add").click();
1610
1611 expect(".o_data_row").toHaveCount(4);
1612
1613 await contains(".o_field_widget[name=trululu] input").edit("b", { confirm: false });
1614 await runAllTimers();
1615 await contains(".o_m2o_dropdown_option_create").click();
1616
1617 await contains(".o_list_button_save").click();
1618
1619 // should wait for the name_create before creating the record
1620 expect.verifySteps(["name_create"]);
1621 expect(".o_data_row").toHaveCount(4);
1622
1623 def.resolve();
1624 await animationFrame();
1625
1626 expect.verifySteps(["web_save"]);
1627 expect(".o_data_row").toHaveCount(4);
1628 expect(".o_data_row .o_data_cell:eq(0)").toHaveText("b");
1629 });
1630
1631 test("list in form: quick create then save directly", async () => {
1632 expect.assertions(4);
1633
1634 const def = new Deferred();
1635 const newRecordId = 5; // with the current records, the created record will be assigned id 5
1636 onRpc("name_create", async () => {
1637 expect.step("name_create");
1638 await def;
1639 });
1640 onRpc("web_save", ({ args }) => {
1641 expect.step("web_save");
1642 expect(args[1].p[0][2].trululu).toBe(newRecordId);
1643 });
1644
1645 await mountView({
1646 type: "form",
1647 resModel: "partner",
1648 arch: `
1649 <form>
1650 <sheet>
1651 <field name="p">
1652 <list editable="bottom">
1653 <field name="trululu" />
1654 </list>
1655 </field>
1656 </sheet>
1657 </form>`,
1658 });
1659
1660 await contains(".o_field_x2many_list_row_add a").click();
1661
1662 await contains(".o_field_widget[name=trululu] input").edit("b", { confirm: false });
1663 await runAllTimers();
1664 await contains(".o_m2o_dropdown_option_create").click();
1665
1666 await contains(".o_form_button_save").click();
1667
1668 // should wait for the name_create before creating the record
1669 expect.verifySteps(["name_create"]);
1670
1671 await def.resolve();
1672 await animationFrame();
1673
1674 expect.verifySteps(["web_save"]);
1675 expect(".o_data_row .o_data_cell").toHaveText("b");
1676 });
1677
1678 test("name_create in form dialog", async () => {
1679 onRpc("name_create", () => {
1680 expect.step("name_create");
1681 });
1682 await mountView({
1683 type: "form",
1684 resModel: "partner",
1685 arch: `
1686 <form>
1687 <group>
1688 <field name="p">
1689 <list>
1690 <field name="bar"/>
1691 </list>
1692 <form>
1693 <field name="product_id"/>
1694 </form>
1695 </field>
1696 </group>
1697 </form>`,
1698 });
1699
1700 await contains(".o_field_x2many_list_row_add a").click();
1701
1702 await contains(".modal .o_field_widget[name=product_id] input").edit("new record", {
1703 confirm: false,
1704 });
1705 await runAllTimers();
1706 await contains(".modal .o_field_widget[name=product_id] .o_m2o_dropdown_option_create").click();
1707
1708 expect.verifySteps(["name_create"]);
1709 });
1710
1711 test("many2one inside one2many form view, with domain", async () => {
1712 expect.assertions(4);
1713
1714 Partner._fields.trululu.domain = "[['id', '<', 1000]]";
1715 Partner._records = [
1716 { id: 1, name: "a1", p: [1] },
1717 { id: 2, name: "a2" },
1718 { id: 3, name: "a3" },
1719 { id: 4, name: "a4" },
1720 { id: 5, name: "a5" },
1721 { id: 6, name: "a6" },
1722 { id: 7, name: "a7" },
1723 { id: 8, name: "a8" },
1724 { id: 9, name: "a9" },
1725 ];
1726 Partner._views = {
1727 list: '<list><field name="name"/></list>',
1728 };
1729 onRpc("web_name_search", ({ kwargs }) => {
1730 expect(kwargs.domain).toEqual([["id", ">", 1]]);
1731 });
1732 onRpc("web_search_read", ({ kwargs }) => {
1733 expect(kwargs.domain).toEqual([["id", ">", 1]]);
1734 });
1735
1736 await mountView({
1737 type: "form",
1738 resModel: "partner",
1739 arch: `
1740 <form>
1741 <sheet>
1742 <field name="p">
1743 <list>
1744 <field name="name"/>
1745 <field name="trululu" domain="[]"/>
1746 </list>
1747 <form>
1748 <field name="trululu" domain="[['id', '>', 1]]"/>
1749 </form>
1750 </field>
1751 </sheet>
1752 </form>`,
1753 resId: 1,
1754 });
1755 await contains(".o_data_row .o_data_cell").click();
1756 await contains(".o_field_widget[name=trululu] input").click();
1757 await runAllTimers();
1758 await clickFieldDropdownItem("trululu", "Search more...");
1759
1760 expect(".modal .o_list_view").toHaveCount(1);
1761 expect(".modal .o_data_row").toHaveCount(8);
1762 });
1763
1764 test("list in form: quick create then add a new line directly", async () => {
1765 // required many2one inside a one2many list: directly after quick creating
1766 // a new many2one value (before the name_create returns), click on add an item:
1767 // at this moment, the many2one has still no value, and as it is required,
1768 // the row is discarded if a saveLine is requested. However, it should
1769 // wait for the name_create to return before trying to save the line.
1770 expect.assertions(8);
1771 Partner._onChanges = {
1772 trululu: () => {},
1773 };
1774
1775 const def = new Deferred();
1776 const newRecordId = 5; // with the current records, the created record will be assigned id 5
1777 onRpc("name_create", async () => {
1778 await def;
1779 });
1780 onRpc("web_save", ({ args }) => {
1781 expect(args[1].p[0][2].trululu).toBe(newRecordId);
1782 });
1783
1784 await mountView({
1785 type: "form",
1786 resModel: "partner",
1787 arch: `
1788 <form>
1789 <sheet>
1790 <field name="p">
1791 <list editable="bottom">
1792 <field name="trululu" required="1" />
1793 </list>
1794 </field>
1795 </sheet>
1796 </form>`,
1797 });
1798
1799 await contains(".o_field_x2many_list_row_add a").click();
1800
1801 await contains(".o_field_widget[name=trululu] input").edit("b", { confirm: false });
1802 await runAllTimers();
1803 await contains(".o_m2o_dropdown_option_create").click();
1804
1805 await contains(".o_field_x2many_list_row_add a").click();
1806
1807 expect(".o_data_row").toHaveCount(1);
1808 expect(".o_data_row").toHaveClass("o_selected_row");
1809
1810 def.resolve();
1811 await animationFrame();
1812
1813 expect(".o_data_row .o_data_cell:eq(0)").toHaveText("b");
1814 expect(".o_data_row").toHaveCount(2);
1815 expect(".o_data_row:eq(1)").toHaveClass("o_selected_row");
1816
1817 await clickSave();
1818
1819 expect(".o_data_row").toHaveCount(1);
1820 expect(".o_data_row .o_data_cell").toHaveText("b");
1821 });
1822
1823 test("list in form: create with one2many with many2one", async () => {
1824 Partner._fields.p = fields.One2many({
1825 string: "one2many field",
1826 relation: "partner",
1827 relation_field: "trululu",
1828 default: [[0, 0, { name: "new record", p: [] }]],
1829 });
1830 onRpc("read", ({ args }) => {
1831 if (args[1].length === 1 && args[1][0] === "name") {
1832 throw new Error("read(['name']) should not be called");
1833 }
1834 });
1835
1836 await mountView({
1837 type: "form",
1838 resModel: "partner",
1839 arch: `
1840 <form>
1841 <sheet>
1842 <field name="p">
1843 <list editable="bottom">
1844 <field name="name" />
1845 <field name="trululu" />
1846 </list>
1847 </field>
1848 </sheet>
1849 </form>`,
1850 });
1851
1852 expect("td.o_data_cell:eq(0)").toHaveText("new record");
1853 });
1854
1855 test("list in form: create with one2many with many2one (version 2)", async () => {
1856 // This test simulates the exact same scenario as the previous one,
1857 // except that the value for the many2one is explicitely set to false,
1858 // which is stupid, but this happens, so we have to handle it
1859 Partner._fields.p = fields.One2many({
1860 string: "one2many field",
1861 relation: "partner",
1862 relation_field: "trululu",
1863 default: [[0, 0, { name: "new record", trululu: false, p: [] }]],
1864 });
1865 onRpc("read", ({ args }) => {
1866 if (args[1].length === 1 && args[1][0] === "name") {
1867 throw new Error("read(['name']) should not be called");
1868 }
1869 });
1870
1871 await mountView({
1872 type: "form",
1873 resModel: "partner",
1874 arch: `
1875 <form>
1876 <sheet>
1877 <field name="p">
1878 <list editable="bottom">
1879 <field name="name" />
1880 <field name="trululu" />
1881 </list>
1882 </field>
1883 </sheet>
1884 </form>`,
1885 });
1886
1887 expect("td.o_data_cell:eq(0)").toHaveText("new record");
1888 });
1889
1890 test("item not dropped on discard with empty required field (default_get)", async () => {
1891 // This test simulates discarding a record that has been created with
1892 // one of its required field that is empty. When we discard the changes
1893 // on this empty field, it should not assume that this record should be
1894 // abandonned, since it has been added (even though it is a new record).
1895 Partner._fields.p = fields.One2many({
1896 string: "one2many field",
1897 relation: "partner",
1898 relation_field: "trululu",
1899 default: [[0, 0, { name: "new record", trululu: false, p: [] }]],
1900 });
1901
1902 await mountView({
1903 type: "form",
1904 resModel: "partner",
1905 arch: `
1906 <form>
1907 <sheet>
1908 <field name="p">
1909 <list editable="bottom">
1910 <field name="name" />
1911 <field name="trululu" required="1" />
1912 </list>
1913 </field>
1914 </sheet>
1915 </form>`,
1916 });
1917
1918 expect("tr.o_data_row").toHaveCount(1);
1919 expect("td.o_data_cell:eq(0)").toHaveText("new record");
1920 expect("td.o_data_cell:eq(1)").toHaveText("");
1921 await contains(".o_data_row .o_data_cell").click();
1922 expect(".o_selected_row .o_data_cell:eq(1)").toHaveClass("o_required_modifier");
1923
1924 // discard by clicking on body
1925 await contains(getFixture()).click();
1926
1927 expect("tr.o_data_row").toHaveCount(1);
1928 expect("td.o_data_cell:eq(0)").toHaveText("new record");
1929 expect("td.o_data_cell:eq(1)").toHaveText("");
1930
1931 await contains(".o_data_row .o_data_cell").click();
1932 expect(".o_selected_row .o_data_cell:eq(1)").toHaveClass("o_required_modifier");
1933 });
1934
1935 test("list in form: read with unique ids (default_get)", async () => {
1936 Partner._records[0].name = "MyTrululu";
1937 Partner._fields.p = fields.One2many({
1938 string: "one2many field",
1939 relation: "partner",
1940 relation_field: "trululu",
1941 default: [
1942 [0, 0, { trululu: 1, p: [] }],
1943 [0, 0, { trululu: 1, p: [] }],
1944 ],
1945 });
1946 onRpc("read", ({ args }) => {
1947 if (args[1].length === 1 && args[1][0] === "name") {
1948 throw new Error("read(['name']) should not called");
1949 }
1950 });
1951
1952 await mountView({
1953 type: "form",
1954 resModel: "partner",
1955 arch: `
1956 <form>
1957 <sheet>
1958 <field name="p">
1959 <list editable="bottom">
1960 <field name="trululu" />
1961 </list>
1962 </field>
1963 </sheet>
1964 </form>`,
1965 });
1966
1967 expect(queryAllTexts("td.o_data_cell")).toEqual(["MyTrululu", "MyTrululu"]);
1968 });
1969
1970 test("list in form: show name of many2one fields in multi-page (default_get)", async () => {
1971 Partner._fields.p = fields.One2many({
1972 string: "one2many field",
1973 relation: "partner",
1974 relation_field: "trululu",
1975 default: [
1976 [0, 0, { name: "record1", trululu: 1, p: [] }],
1977 [0, 0, { name: "record2", trululu: 2, p: [] }],
1978 ],
1979 });
1980
1981 await mountView({
1982 type: "form",
1983 resModel: "partner",
1984 arch: `
1985 <form>
1986 <sheet>
1987 <field name="p">
1988 <list editable="bottom" limit="1">
1989 <field name="name" />
1990 <field name="trululu" />
1991 </list>
1992 </field>
1993 </sheet>
1994 </form>`,
1995 });
1996
1997 expect(queryAllTexts("td.o_data_cell")).toEqual([
1998 "record1",
1999 "first record",
2000 "record2",
2001 "second record",
2002 ]);
2003 });
2004
2005 test("list in form: item not dropped on discard with empty required field (onchange in default_get)", async () => {
2006 // variant of the test "list in form: discard newly added element with
2007 // empty required field (default_get)", in which the `default_get`
2008 // performs an `onchange` at the same time. This `onchange` may create
2009 // some records, which should not be abandoned on discard, similarly
2010 // to records created directly by `default_get`
2011 Partner._fields.product_id = fields.Many2one({
2012 string: "Product",
2013 relation: "product",
2014 default: 37,
2015 onChange: (obj) => {
2016 if (obj.product_id === 37) {
2017 obj.p = [[0, 0, { name: "entry", trululu: false }]];
2018 }
2019 },
2020 });
2021
2022 await mountView({
2023 type: "form",
2024 resModel: "partner",
2025 arch: `
2026 <form>
2027 <field name="product_id" />
2028 <field name="p">
2029 <list editable="bottom">
2030 <field name="name" />
2031 <field name="trululu" required="1" />
2032 </list>
2033 </field>
2034 </form>`,
2035 });
2036
2037 // check that there is a record in the editable list with empty string as required field
2038 expect(".o_data_row").toHaveCount(1);
2039 expect("td.o_data_cell:eq(0)").toHaveText("entry");
2040 expect("td.o_data_cell.o_required_modifier").toHaveCount(1);
2041 expect("td.o_data_cell.o_required_modifier").toHaveText("");
2042
2043 // click on empty required field in editable list record
2044 await contains("td.o_data_cell.o_required_modifier").click();
2045 // click off so that the required field still stay empty
2046 await contains(getFixture()).click();
2047
2048 // record should not be dropped
2049 expect(".o_data_row").toHaveCount(1);
2050 expect("td.o_data_cell:eq(0)").toHaveText("entry");
2051 expect("td.o_data_cell.o_required_modifier").toHaveText("");
2052 });
2053
2054 test("list in form: item not dropped on discard with empty required field (onchange on list after default_get)", async () => {
2055 // discarding a record from an `onchange` in a `default_get` should not
2056 // abandon the record. This should not be the case for following
2057 // `onchange`, except if an onchange make some changes on the list:
2058 // in particular, if an onchange make changes on the list such that
2059 // a record is added, this record should not be dropped on discard
2060 Partner._onChanges = {
2061 product_id: (obj) => {
2062 if (obj.product_id === 37) {
2063 obj.p = [[0, 0, { name: "entry", trululu: false }]];
2064 }
2065 },
2066 };
2067
2068 await mountView({
2069 type: "form",
2070 resModel: "partner",
2071 arch: `
2072 <form>
2073 <field name="product_id" />
2074 <field name="p">
2075 <list editable="bottom">
2076 <field name="name" />
2077 <field name="trululu" required="1" />
2078 </list>
2079 </field>
2080 </form>`,
2081 });
2082
2083 // check no record in list
2084 expect(".o_data_row").toHaveCount(0);
2085
2086 // select product_id to force on_change in editable list
2087 await contains("div[name=product_id] input").click();
2088 await contains(".ui-menu-item").click();
2089
2090 // check that there is a record in the editable list with empty string as required field
2091 expect(".o_data_row").toHaveCount(1);
2092
2093 expect("td.o_data_cell:eq(0)").toHaveText("entry");
2094 expect("td.o_required_modifier").toHaveCount(1);
2095 expect("td.o_required_modifier").toHaveText("");
2096
2097 // click on empty required field in editable list record
2098 await contains("td.o_required_modifier").click();
2099 // click off so that the required field still stay empty
2100 await contains(getFixture()).click();
2101
2102 // record should not be dropped
2103 expect(".o_data_row").toHaveCount(1);
2104 expect(queryAllTexts("td.o_data_cell")).toEqual(["entry", ""]);
2105 });
2106
2107 test('item dropped on discard with empty required field with "Add an item" (invalid on "ADD")', async () => {
2108 // when a record in a list is added with "Add an item", it should
2109 // always be dropped on discard if some required field are empty
2110 // at the record creation.
2111 await mountView({
2112 type: "form",
2113 resModel: "partner",
2114 arch: `
2115 <form>
2116 <field name="p">
2117 <list editable="bottom">
2118 <field name="name" />
2119 <field name="trululu" required="1" />
2120 </list>
2121 </field>
2122 </form>`,
2123 });
2124
2125 // Click on "Add an item"
2126 await contains(".o_field_x2many_list_row_add a").click();
2127 expect(".o_field_widget.o_required_modifier[name=trululu]").toHaveCount(1);
2128 expect(".o_field_widget.o_required_modifier[name=trululu] input").toHaveValue("");
2129
2130 // click on empty required field in editable list record
2131 await contains(".o_field_widget.o_required_modifier[name=trululu] input").click();
2132 // click off so that the required field still stay empty
2133 await contains(getFixture()).click();
2134
2135 // record should be dropped
2136 expect(".o_data_row").toHaveCount(0);
2137 });
2138
2139 test('item not dropped on discard with empty required field with "Add an item" (invalid on "UPDATE")', async () => {
2140 // when a record in a list is added with "Add an item", it should
2141 // be temporarily added to the list when it is valid (e.g. required
2142 // fields are non-empty). If the record is updated so that the required
2143 // field is empty, and it is discarded, then the record should not be
2144 // dropped.
2145 await mountView({
2146 type: "form",
2147 resModel: "partner",
2148 arch: `
2149 <form>
2150 <field name="p">
2151 <list editable="bottom">
2152 <field name="name" />
2153 <field name="trululu" required="1" />
2154 </list>
2155 </field>
2156 </form>`,
2157 });
2158
2159 expect(".o_data_row").toHaveCount(0);
2160
2161 // Click on "Add an item"
2162 await contains(".o_field_x2many_list_row_add a").click();
2163 expect(".o_data_row").toHaveCount(1);
2164
2165 expect(".o_field_widget.o_required_modifier[name=trululu] input").toHaveCount(1);
2166 expect(".o_field_widget.o_required_modifier[name=trululu] input").toHaveValue("");
2167
2168 // add something to required field and leave edit mode of the record
2169 await contains(".o_field_widget.o_required_modifier[name=trululu] input").click();
2170 await contains("li.ui-menu-item").click();
2171 await contains(getFixture()).click();
2172
2173 expect(".o_data_row").toHaveCount(1);
2174 expect(".o_data_cell:eq(1)").toHaveText("first record");
2175
2176 // leave edit mode of the record
2177 await contains(getFixture()).click();
2178 expect(".o_data_row").toHaveCount(1);
2179 expect(".o_data_cell:eq(1)").toHaveText("first record");
2180 });
2181
2182 // WARNING: this does not seem to be a many2one field test
2183 test("list in form: default_get with x2many create", async () => {
2184 expect.assertions(3);
2185
2186 Partner._fields.timmy = fields.Many2many({
2187 string: "pokemon",
2188 relation: "partner.type",
2189 default: [[0, 0, { name: "brandon is the new timmy" }]],
2190 onChange: (obj) => {
2191 obj.int_field = obj.timmy.length;
2192 },
2193 });
2194
2195 onRpc("web_save", ({ args }) => {
2196 expect(args[1]).toEqual({
2197 int_field: 1,
2198 timmy: [[0, args[1].timmy[0][1], { name: "new value" }]],
2199 });
2200 });
2201
2202 await mountView({
2203 type: "form",
2204 resModel: "partner",
2205 arch: `
2206 <form>
2207 <sheet>
2208 <field name="timmy">
2209 <list editable="bottom">
2210 <field name="name" />
2211 </list>
2212 </field>
2213 <field name="int_field" />
2214 </sheet>
2215 </form>`,
2216 });
2217
2218 expect("td.o_data_cell").toHaveText("brandon is the new timmy");
2219 expect(".o_field_integer input").toHaveValue("1");
2220
2221 // edit the subrecord and save
2222 await contains(".o_data_cell").click();
2223 await contains(".o_data_cell input").edit("new value", { confirm: false });
2224 await clickSave();
2225 });
2226
2227 // WARNING: this does not seem to be a many2one field test
2228 test("list in form: default_get with x2many create and onchange", async () => {
2229 expect.assertions(1);
2230 Partner._fields.turtles = fields.One2many({
2231 string: "one2many turtle field",
2232 relation: "turtle",
2233 relation_field: "turtle_trululu",
2234 default: [
2235 [4, 2],
2236 [4, 3],
2237 ],
2238 });
2239
2240 onRpc("web_save", ({ args }) => {
2241 expect(args[1].turtles).toEqual([
2242 [4, 2],
2243 [4, 3],
2244 ]);
2245 });
2246
2247 await mountView({
2248 type: "form",
2249 resModel: "partner",
2250 arch: `
2251 <form>
2252 <sheet>
2253 <field name="turtles">
2254 <list editable="bottom">
2255 <field name="turtle_foo" />
2256 </list>
2257 </field>
2258 <field name="int_field" />
2259 </sheet>
2260 </form>`,
2261 });
2262
2263 await clickSave();
2264 });
2265
2266 test("list in form: call button in sub view", async () => {
2267 Partner._records[0].p = [2];
2268 Product._views = {
2269 form: `
2270 <form>
2271 <header>
2272 <button name="action" type="action" string="Just do it !" />
2273 <button name="object" type="object" string="Just don't do it !" />
2274 <field name="name" />
2275 </header>
2276 </form>`,
2277 };
2278
2279 const def = new Deferred();
2280 mockService("action", {
2281 doActionButton(params) {
2282 const { name, resModel, resId, resIds } = params;
2283 expect.step(name);
2284 expect(resModel).toBe("product");
2285 expect(resId).toBe(37);
2286 expect(resIds).toEqual([37]);
2287 return def.then(() => {
2288 params.onClose();
2289 });
2290 },
2291 });
2292 onRpc("get_formview_id", () => false);
2293
2294 await mountViewInDialog({
2295 type: "form",
2296 resModel: "partner",
2297 resId: 1,
2298 arch: `
2299 <form>
2300 <field name="p">
2301 <list editable="bottom">
2302 <field name="product_id"/>
2303 </list>
2304 </field>
2305 </form>`,
2306 });
2307
2308 expect(".modal").toHaveCount(1);
2309 await contains("td.o_data_cell").click();
2310 await contains(".o_external_button", { visible: false }).click();
2311 expect(".modal").toHaveCount(2);
2312
2313 const buttons = queryAll(".o_dialog:not(.o_inactive_modal) .o_form_statusbar button");
2314
2315 await contains(buttons[0]).click();
2316 expect.verifySteps(["action"]);
2317 expect(buttons[1]).not.toBeEnabled();
2318
2319 def.resolve();
2320 await animationFrame();
2321
2322 await contains(".modal:eq(1) .o_form_button_cancel").click();
2323 expect(".modal").toHaveCount(1);
2324
2325 await contains(".o_external_button", { visible: false }).click();
2326 expect(".modal").toHaveCount(2);
2327
2328 await contains(".o_dialog:not(.o_inactive_modal) .o_form_statusbar button:eq(1)").click();
2329 expect.verifySteps(["object"]);
2330 });
2331
2332 test("X2Many sequence list in modal", async () => {
2333 Partner._fields.sequence = fields.Integer({
2334 string: "Sequence",
2335 type: "integer",
2336 onChange: (obj) => {
2337 if (obj.id === 2) {
2338 obj.sequence = 1;
2339 expect.step("onchange sequence");
2340 }
2341 },
2342 });
2343 Partner._records[0].sequence = 1;
2344 Partner._records[1].sequence = 2;
2345
2346 Product._fields.turtle_ids = fields.One2many({
2347 string: "Turtles",
2348 relation: "turtle",
2349 });
2350 Product._records[0].turtle_ids = [1];
2351 Product._records[0].name = "leonardo";
2352 Product._records[0].name = "xphone";
2353
2354 Turtle._fields.partnertypes_ids = fields.One2many({
2355 string: "Partner",
2356 relation: "partner",
2357 });
2358 Turtle._fields.type_id = fields.Many2one({
2359 string: "Partner Type",
2360 relation: "partner.type",
2361 });
2362 Turtle._records[0].type_id = 12;
2363
2364 PartnerType._fields.partner_ids = fields.One2many({
2365 string: "Partner",
2366 relation: "partner",
2367 });
2368 PartnerType._records[0].partner_ids = [1, 2];
2369
2370 PartnerType._views = {
2371 form: `
2372 <form>
2373 <field name="partner_ids" />
2374 </form>`,
2375 };
2376 Partner._views = {
2377 list: `
2378 <list>
2379 <field name="name" />
2380 <field name="sequence" widget="handle" />
2381 </list>`,
2382 };
2383
2384 onRpc("partner.type", "get_formview_id", () => false);
2385 onRpc("partner.type", "web_save", () => {
2386 expect.step("partner.type web_save");
2387 });
2388 await mountViewInDialog({
2389 type: "form",
2390 resModel: "product",
2391 resId: 37,
2392 arch: `
2393 <form>
2394 <field name="name" />
2395 <field name="turtle_ids" widget="one2many">
2396 <list editable="bottom">
2397 <field name="type_id"/>
2398 </list>
2399 </field>
2400 </form>`,
2401 });
2402
2403 expect(".modal").toHaveCount(1);
2404
2405 await contains(".o_data_cell").click();
2406 await contains(".o_external_button", { visible: false }).click();
2407
2408 expect(".modal").toHaveCount(2);
2409 expect(".modal:eq(1) .ui-sortable-handle").toHaveCount(2);
2410
2411 await contains(
2412 ".o_dialog:not(.o_inactive_modal) .o_data_row:nth-child(2) .ui-sortable-handle"
2413 ).dragAndDrop(".o_dialog:not(.o_inactive_modal) tbody tr");
2414
2415 // Saving the modal and then the original model
2416 await contains(".modal:eq(1) .o_form_button_save").click();
2417 await clickSave();
2418
2419 expect.verifySteps(["onchange sequence", "partner.type web_save"]);
2420 });
2421
2422 test("autocompletion in a many2one, in form view with a domain", async () => {
2423 expect.assertions(1);
2424 onRpc("web_name_search", ({ kwargs }) => {
2425 expect(kwargs.domain).toEqual([]);
2426 });
2427
2428 await mountView({
2429 type: "form",
2430 resModel: "partner",
2431 resId: 1,
2432 domain: [["trululu", "=", 4]],
2433 arch: '<form><field name="product_id" /></form>',
2434 });
2435 await contains(".o_field_widget[name=product_id] input").click();
2436 });
2437
2438 test("autocompletion in a many2one, in form view with a date field", async () => {
2439 expect.assertions(1);
2440 onRpc("web_name_search", ({ kwargs }) => {
2441 expect(kwargs.domain).toEqual([["bar", "=", true]]);
2442 });
2443
2444 await mountView({
2445 type: "form",
2446 resModel: "partner",
2447 resId: 2,
2448 arch: `
2449 <form>
2450 <field name="bar" />
2451 <field name="date" />
2452 <field name="trululu" domain="[('bar', '=', True)]" />
2453 </form>`,
2454 });
2455 await contains(".o_field_widget[name='trululu'] input").click();
2456 });
2457
2458 test("creating record with many2one with option always_reload", async () => {
2459 Partner._fields.trululu = fields.Many2one({
2460 string: "Trululu",
2461 relation: "partner",
2462 default: 1,
2463 onChange: (obj) => {
2464 obj.trululu = 2; //[2, "second record"];
2465 },
2466 });
2467
2468 onRpc("onchange", ({ parent }) => {
2469 const result = parent();
2470 result.value.trululu = {
2471 ...result.value.trululu,
2472 display_name: "hello world\nso much noise",
2473 };
2474 return result;
2475 });
2476 onRpc(({ method }) => {
2477 expect.step(method);
2478 });
2479
2480 await mountView({
2481 type: "form",
2482 resModel: "partner",
2483 arch: `
2484 <form>
2485 <field name="trululu" />
2486 </form>`,
2487 });
2488
2489 expect(".o_field_widget[name='trululu'] input").toHaveValue("hello world");
2490 expect.verifySteps(["get_views", "onchange"]);
2491 });
2492
2493 test("empty list with sample data and many2one with option always_reload", async () => {
2494 await mountView({
2495 type: "list",
2496 resModel: "partner",
2497 arch: `
2498 <list sample="1">
2499 <field name="product_id" />
2500 </list>`,
2501 context: { search_default_empty: true },
2502 searchViewArch: `
2503 <search>
2504 <filter name="empty" domain="[('id', '&lt;', 0)]"/>
2505 </search>`,
2506 });
2507
2508 expect(".o_list_view .o_content").toHaveClass("o_view_sample_data");
2509 expect(".o_list_table").toHaveCount(1);
2510 expect(".o_data_row").toHaveCount(10);
2511 expect("thead tr th").toHaveCount(2);
2512 });
2513
2514 test("selecting a many2one, then discarding", async () => {
2515 await mountView({
2516 type: "form",
2517 resModel: "partner",
2518 resId: 1,
2519 arch: '<form><field name="product_id" /></form>',
2520 });
2521 expect(".o_field_widget[name='product_id'] input").toHaveValue("");
2522
2523 await contains(".o_field_widget[name='product_id'] input").click();
2524 await contains(".o_field_widget[name='product_id'] .dropdown-item").click();
2525 expect(".o_field_widget[name='product_id'] input").toHaveValue("xphone");
2526
2527 await contains(".o_form_button_cancel").click();
2528 expect(".o_field_widget[name='product_id'] input").toHaveValue("");
2529 });
2530
2531 test("domain and context are correctly used when doing a web_name_search in a m2o", async () => {
2532 expect.assertions(4);
2533
2534 Partner._records[0].timmy = [12];
2535 // Need to take into account the company service which populates context at startup
2536 const DEFAULT_USER_CTX = { ...user.context, allowed_company_ids: [1] };
2537 serverState.userContext = { hey: "ho" };
2538 onRpc("product", "web_name_search", ({ kwargs }) => {
2539 expect(kwargs.domain).toEqual(["&", ["foo", "=", "bar"], ["foo", "=", "yop"]]);
2540 expect(kwargs.context).toEqual({
2541 ...DEFAULT_USER_CTX,
2542 hey: "ho",
2543 hello: "world",
2544 test: "yop",
2545 });
2546 return [];
2547 });
2548 onRpc("partner", "web_name_search", ({ kwargs }) => {
2549 expect(kwargs.domain).toEqual([["id", "in", [12]]]);
2550 expect(kwargs.context).toEqual({
2551 ...DEFAULT_USER_CTX,
2552 hey: "ho",
2553 timmy: [12],
2554 });
2555 return [];
2556 });
2557
2558 await mountView({
2559 type: "form",
2560 resModel: "partner",
2561 resId: 1,
2562 arch: `
2563 <form>
2564 <field name="product_id" domain="[('foo', '=', 'bar'), ('foo', '=', foo)]" context="{'hello': 'world', 'test': foo}" />
2565 <field name="foo" />
2566 <field name="trululu" context="{'timmy': timmy}" domain="[('id', 'in', timmy)]" />
2567 <field name="timmy" widget="many2many_tags" invisible="1" />
2568 </form>`,
2569 });
2570
2571 await contains(".o_field_widget[name='product_id'] input").click();
2572 await contains(".o_field_widget[name='trululu'] input").click();
2573 });
2574
2575 test("quick create on a many2one", async () => {
2576 expect.assertions(1);
2577 onRpc("name_create", ({ args }) => {
2578 expect(args[0]).toBe("new partner");
2579 });
2580
2581 await mountView({
2582 type: "form",
2583 resModel: "partner",
2584 arch: `
2585 <form>
2586 <sheet>
2587 <field name="product_id" />
2588 </sheet>
2589 </form>`,
2590 });
2591
2592 await contains(".o_field_many2one input").edit("new partner", { confirm: false });
2593 await runAllTimers();
2594 await press("tab");
2595 });
2596
2597 test("failing quick create on a many2one because ValidationError", async () => {
2598 expect.assertions(5);
2599
2600 Product._views = {
2601 form: '<form><field name="name" /></form>',
2602 };
2603
2604 onRpc("name_create", () => {
2605 throw makeServerError({ type: "ValidationError" });
2606 });
2607 onRpc("web_save", ({ args }) => {
2608 expect(args[1]).toEqual({ name: "xyz" });
2609 });
2610
2611 await mountView({
2612 type: "form",
2613 resModel: "partner",
2614 arch: '<form><field name="product_id" /></form>',
2615 });
2616
2617 await contains(".o_field_widget[name='product_id'] input").edit("abcd", { confirm: false });
2618 await runAllTimers();
2619 await contains(".o_field_widget[name='product_id'] .o_m2o_dropdown_option_create").click();
2620 await animationFrame(); // wait for the error service to ensure that there's no error dialog
2621 expect(".o_error_dialog").toHaveCount(0);
2622 expect(".modal .o_form_view").toHaveCount(1);
2623 expect(".modal .o_field_widget[name='name'] input").toHaveValue("abcd");
2624
2625 await contains(".modal .o_field_widget[name='name'] input").edit("xyz");
2626 await contains(".modal .o_form_button_save").click();
2627 expect(".o_field_widget[name='product_id'] input").toHaveValue("xyz");
2628 });
2629
2630 test("failing quick create on a many2one", async () => {
2631 expect.assertions(3);
2632 Product._views = {
2633 form: '<form><field name="name" /></form>',
2634 };
2635 onRpc("name_create", () => {
2636 throw makeServerError();
2637 });
2638
2639 await mountView({
2640 type: "form",
2641 resModel: "partner",
2642 arch: '<form><field name="product_id" /></form>',
2643 });
2644
2645 await contains(".o_field_widget[name='product_id'] input").edit("abcd", { confirm: false });
2646 await runAllTimers();
2647 expect.errors(1);
2648 await contains(".o_field_widget[name='product_id'] .o_m2o_dropdown_option_create").click();
2649 await animationFrame(); // wait for the error service
2650 expect.verifyErrors(["RPC_ERROR"]);
2651 expect(".o_error_dialog").toHaveCount(1);
2652 expect(".modal .o_form_view").toHaveCount(0);
2653 });
2654
2655 test("failing quick create on a many2one inside a one2many because ValidationError", async () => {
2656 expect.assertions(4);
2657
2658 Partner._views = {
2659 list: `
2660 <list editable="bottom">
2661 <field name="product_id" />
2662 </list>`,
2663 };
2664 Product._views = {
2665 form: '<form><field name="name" /></form>',
2666 };
2667
2668 onRpc("name_create", () => {
2669 throw makeServerError({ type: "ValidationError" });
2670 });
2671 onRpc("web_save", ({ args }) => {
2672 expect(args[1]).toEqual({ name: "xyz" });
2673 });
2674
2675 await mountView({
2676 type: "form",
2677 resModel: "partner",
2678 arch: '<form><field name="p" /></form>',
2679 });
2680
2681 await contains(".o_field_x2many_list_row_add a").click();
2682 await contains(".o_field_widget[name='product_id'] input").edit("abcd", { confirm: false });
2683 await runAllTimers();
2684 await contains(".o_field_widget[name='product_id'] .o_m2o_dropdown_option_create").click();
2685
2686 expect(".modal .o_form_view").toHaveCount(1);
2687 expect(".modal .o_field_widget[name='name'] input").toHaveValue("abcd");
2688
2689 await contains(".modal .o_field_widget[name='name'] input").edit("xyz");
2690 await contains(".modal .o_form_button_save").click();
2691 expect(".o_field_widget[name='product_id'] input").toHaveValue("xyz");
2692 });
2693
2694 test("slow create on a many2one", async () => {
2695 Product._views = {
2696 form: '<form><field name="name" /></form>',
2697 };
2698
2699 await mountView({
2700 type: "form",
2701 resModel: "partner",
2702 arch: `
2703 <form>
2704 <sheet>
2705 <field name="product_id" options="{'no_quick_create': 1}" />
2706 </sheet>
2707 </form>`,
2708 });
2709 await contains(".o_field_many2one input").edit("new product", { confirm: false });
2710 await runAllTimers();
2711 await press("tab");
2712 await animationFrame();
2713
2714 expect(".modal").toHaveCount(1);
2715 // cancel the many2one creation with Discard button
2716 await contains(".modal .modal-footer .btn:not(.btn-primary)").click();
2717 expect(".modal").toHaveCount(0);
2718 expect(".o_field_many2one input").toHaveValue("");
2719
2720 // cancel the many2one creation with Close button
2721 await contains(".o_field_many2one input").edit("new product", { confirm: false });
2722 await runAllTimers();
2723 await press("tab");
2724 await animationFrame();
2725
2726 expect(".modal").toHaveCount(1);
2727 await contains(".modal .modal-header button").click();
2728 expect(".o_field_many2one input").toHaveValue("");
2729 expect(".modal").toHaveCount(0);
2730
2731 // select a new value then cancel the creation of the new one --> restore the previous
2732 await contains(".o_field_widget[name=product_id] input").click();
2733 await contains(".ui-menu-item").click();
2734 expect(".o_field_many2one input").toHaveValue("xphone");
2735
2736 await contains(".o_field_many2one input").edit("new product", { confirm: false });
2737 await runAllTimers();
2738 await press("tab");
2739 await animationFrame();
2740 expect(".modal").toHaveCount(1);
2741
2742 await contains(".modal .modal-footer .btn:not(.btn-primary)").click();
2743 expect(".o_field_many2one input").toHaveValue("");
2744
2745 // confirm the many2one creation
2746 await contains(".o_field_many2one input").edit("new product", { confirm: false });
2747 await runAllTimers();
2748 await press("tab");
2749 await animationFrame();
2750
2751 expect(".modal .o_form_view").toHaveCount(1);
2752
2753 await contains(".modal .o_form_button_cancel").click();
2754 });
2755
2756 test("select a many2one value by pressing tab", async () => {
2757 await mountView({
2758 type: "form",
2759 resModel: "partner",
2760 arch: '<form><field name="product_id" /></form>',
2761 });
2762
2763 await contains(".o_field_many2one input").edit("xph", { confirm: false });
2764 await runAllTimers();
2765 await press("tab");
2766 await animationFrame();
2767
2768 expect(".modal").toHaveCount(0);
2769 expect(".o_field_many2one input").toHaveValue("xphone");
2770 expect(".o_external_button").toHaveCount(1);
2771 });
2772
2773 test("no_create option on a many2one", async () => {
2774 await mountView({
2775 type: "form",
2776 resModel: "partner",
2777 arch: `
2778 <form>
2779 <sheet>
2780 <field name="product_id" options="{'no_create': 1}" />
2781 </sheet>
2782 </form>`,
2783 });
2784
2785 await contains(".o_field_many2one input").edit("new partner", { confirm: false });
2786 await runAllTimers();
2787 expect(".o_m2o_dropdown_option_create").toHaveCount(0);
2788 expect(".o_m2o_dropdown_option_create_edit").toHaveCount(0);
2789 await press("escape");
2790 });
2791
2792 test("no_create option on a many2one when can_create is absent", async () => {
2793 Partner._fields.product_id.readonly = true;
2794 await mountView({
2795 type: "form",
2796 resModel: "partner",
2797 arch: `
2798 <form>
2799 <sheet>
2800 <field name="product_id" options="{'no_create': 1}" readonly="0" />
2801 </sheet>
2802 </form>`,
2803 });
2804 await contains(".o_field_many2one input").edit("new partner", { confirm: false });
2805 await runAllTimers();
2806 expect(".o_m2o_dropdown_option_create").toHaveCount(0);
2807 expect(".o_m2o_dropdown_option_create_edit").toHaveCount(0);
2808 await press("escape");
2809 });
2810
2811 test("no_quick_create option on a many2one when can_create is absent", async () => {
2812 Partner._fields.product_id.readonly = true;
2813 await mountView({
2814 type: "form",
2815 resModel: "partner",
2816 arch: `
2817 <form>
2818 <sheet>
2819 <field name="product_id" options="{'no_quick_create': 1}" readonly="0" />
2820 </sheet>
2821 </form>`,
2822 });
2823 await contains(".o_field_many2one input").edit("new partner", { confirm: false });
2824 await runAllTimers();
2825 expect(queryAllTexts(".ui-autocomplete .o_m2o_dropdown_option")).toEqual([
2826 "Create and edit...",
2827 ]);
2828 });
2829
2830 test("can_create and can_write option on a many2one", async () => {
2831 Product.options = {
2832 can_create: "false",
2833 can_write: "false",
2834 };
2835 Product._views = {
2836 form: `
2837 <form>
2838 <field name="name" />
2839 </form>`,
2840 };
2841
2842 onRpc("product", "get_formview_id", () => false);
2843
2844 await mountViewInDialog({
2845 type: "form",
2846 resModel: "partner",
2847 arch: `
2848 <form>
2849 <sheet>
2850 <field name="product_id" can_create="false" can_write="false"/>
2851 </sheet>
2852 </form>`,
2853 });
2854
2855 expect(".modal").toHaveCount(1);
2856
2857 await contains(".o_field_many2one input").click();
2858 expect(".o_m2o_dropdown_option.o_m2o_dropdown_option_create").toHaveCount(0);
2859
2860 await contains(".ui-menu-item:eq(1)").click();
2861 expect(".o_field_many2one input").toHaveValue("xpad");
2862 expect(".o_field_many2one .o_external_button").toHaveCount(1);
2863
2864 await contains(".o_field_many2one .o_external_button", { visible: false }).click();
2865 expect(".modal").toHaveCount(2);
2866 expect(".modal .o_form_view .o_form_readonly").toHaveCount(1);
2867
2868 await contains(".o_dialog:not(.o_inactive_modal) .modal-footer .btn-primary").click();
2869
2870 await contains(".o_field_many2one input").edit("new product");
2871 expect(".modal").toHaveCount(1);
2872 });
2873
2874 test("create_name_field option on a many2one", async () => {
2875 // when the 'create_name_field' option is set, the value entered in the
2876 // many2one input should be used to populate this specified field,
2877 // instead of the generic 'name' field.
2878 Partner._views = {
2879 form: `
2880 <form>
2881 <field name="foo" />
2882 </form>`,
2883 };
2884
2885 await mountView({
2886 type: "form",
2887 resModel: "partner",
2888 arch: `
2889 <form>
2890 <sheet>
2891 <field name="trululu" options="{'create_name_field': 'foo'}" />
2892 </sheet>
2893 </form>`,
2894 });
2895
2896 await contains(".o_field_widget[name=trululu] input").edit("yz", { confirm: false });
2897 await runAllTimers();
2898 await contains(".o_field_widget[name=trululu] input").click();
2899 await selectFieldDropdownItem("trululu", "Create and edit...");
2900
2901 expect(".o_field_widget[name=foo] input").toHaveValue("yz");
2902
2903 await contains(".o_form_button_cancel").click();
2904 });
2905
2906 test("propagate can_create onto the search popup", async () => {
2907 Product._records = [
2908 { id: 1, name: "Tromblon1" },
2909 { id: 2, name: "Tromblon2" },
2910 { id: 3, name: "Tromblon3" },
2911 { id: 4, name: "Tromblon4" },
2912 { id: 5, name: "Tromblon5" },
2913 { id: 6, name: "Tromblon6" },
2914 { id: 7, name: "Tromblon7" },
2915 { id: 8, name: "Tromblon8" },
2916 ...Product._records,
2917 ];
2918 Product._views = {
2919 list: `
2920 <list>
2921 <field name="name"/>
2922 </list>`,
2923 search: `
2924 <search>
2925 <field name="name"/>
2926 </search>`,
2927 };
2928 onRpc("get_formview_id", () => false);
2929
2930 await mountView({
2931 type: "form",
2932 resModel: "partner",
2933 resId: 1,
2934 arch: `
2935 <form>
2936 <field name="name"/>
2937 <field name="product_id" can_create="false"/>
2938 </form>`,
2939 });
2940
2941 await contains(".o_field_widget[name=product_id] input").click();
2942
2943 expect(".o-autocomplete a:contains(Start typing...)").toHaveCount(0);
2944
2945 await contains(".o_field_widget[name=product_id] input").edit("a", { confirm: false });
2946 await runAllTimers();
2947
2948 expect(".ui-autocomplete a:contains(Create and Edit)").toHaveCount(0);
2949
2950 await contains(".o_field_many2one[name=product_id] input").edit("", { confirm: false });
2951 await runAllTimers();
2952 await clickFieldDropdownItem("product_id", "Search more...");
2953
2954 expect(".modal-dialog.modal-lg").toHaveCount(1);
2955
2956 expect(queryAllTexts(".modal-footer button")).toEqual(["Close"]);
2957 });
2958
2959 test("many2one with can_create=false shows no result item when searched something that doesn't exist", async () => {
2960 await mountView({
2961 type: "form",
2962 resModel: "partner",
2963 arch: `
2964 <form>
2965 <sheet>
2966 <field name="product_id" can_create="false" can_write="false" />
2967 </sheet>
2968 </form>`,
2969 });
2970
2971 await contains(".o_field_many2one input").click();
2972 await contains(".o_field_many2one[name=product_id] input").edit("abc", { confirm: false });
2973 await runAllTimers();
2974 expect(".o_field_many2one[name=product_id] .o_m2o_dropdown_option_create").toHaveCount(0);
2975 expect(".o_field_many2one[name=product_id] .o_m2o_no_result").toHaveCount(1);
2976 await contains(getFixture()).click();
2977 expect(".o_field_many2one[name=product_id] .o_m2o_no_result").toHaveCount(0);
2978 });
2979
2980 test("pressing enter in a m2o in an editable list", async () => {
2981 await mountView({
2982 type: "list",
2983 resModel: "partner",
2984 arch: `
2985 <list editable="bottom">
2986 <field name="product_id" />
2987 </list>`,
2988 });
2989
2990 await contains("td.o_data_cell").click();
2991 expect(".o_selected_row").toHaveCount(1);
2992
2993 // we now write 'a' and press enter to check that the selection is
2994 // working, and prevent the navigation
2995 await contains("[name=product_id] input").edit("a", { confirm: false });
2996 await runAllTimers();
2997 expect("[name=product_id] .o-autocomplete--dropdown-menu").toHaveCount(1);
2998
2999 // we now trigger ENTER to select first choice
3000 await press("Enter");
3001 await animationFrame();
3002
3003 expect("[name=product_id] input").toBeFocused();
3004 expect("[name=product_id] .o-autocomplete--dropdown-menu").toHaveCount(0);
3005
3006 // we now trigger again ENTER to make sure we can move to next line
3007 await press("Enter");
3008 await animationFrame();
3009
3010 expect("tr.o_data_row:nth-child(1) [name=product_id] input").toHaveCount(0);
3011 expect("tr.o_data_row:nth-child(2)").toHaveClass("o_selected_row");
3012
3013 // we now write again 'a' in the cell to select xpad. We will now
3014 // test with the tab key
3015 await contains("[name=product_id] input").edit("a", { confirm: false });
3016 await runAllTimers();
3017 expect(
3018 "tr.o_data_row:nth-child(2) [name=product_id] .o-autocomplete--dropdown-menu"
3019 ).toHaveCount(1);
3020
3021 await press("Tab");
3022 await animationFrame();
3023
3024 expect("tr.o_data_row:nth-child(2) [name=product_id] input").toHaveCount(0);
3025
3026 expect("tr.o_data_row:nth-child(3)").toHaveClass("o_selected_row");
3027 });
3028
3029 test("pressing ENTER on a 'no_quick_create' many2one should open a M2ODialog", async () => {
3030 Partner._views = {
3031 form: `
3032 <form>
3033 <field name="name" />
3034 </form>`,
3035 };
3036
3037 await mountView({
3038 type: "form",
3039 resModel: "partner",
3040 arch: `
3041 <form>
3042 <field name="trululu" options="{'no_quick_create': 1}" />
3043 <field name="foo" />
3044 </form>`,
3045 });
3046
3047 await contains(".o_field_many2one input").edit("Something that does not exist", {
3048 confirm: false,
3049 });
3050 await runAllTimers();
3051 await press("Enter");
3052 await animationFrame();
3053 expect(".modal").toHaveCount(1);
3054 // Check that discarding clears $input
3055 await contains(".modal .o_form_button_cancel").click();
3056 expect(".o_field_many2one input").toHaveValue("");
3057 });
3058
3059 test("select a value by pressing TAB on a many2one with onchange", async () => {
3060 Partner._onChanges = {
3061 trululu: () => {},
3062 };
3063
3064 const def = new Deferred();
3065
3066 onRpc("onchange", () => def);
3067
3068 await mountView({
3069 type: "form",
3070 resModel: "partner",
3071 resId: 1,
3072 arch: `
3073 <form>
3074 <field name="trululu" />
3075 <field name="name" />
3076 </form>`,
3077 });
3078
3079 await contains(".o_field_many2one input").edit("first", { confirm: "tab" });
3080
3081 // simulate a focusout (e.g. because the user clicks outside)
3082 // before the onchange returns
3083 await click(".o_field_char");
3084
3085 expect(".modal").toHaveCount(0);
3086
3087 // unlock the onchange
3088 def.resolve();
3089 await runAllTimers();
3090
3091 expect(".o_field_many2one input").toHaveValue("first record");
3092 expect(".modal").toHaveCount(0);
3093 });
3094
3095 test("leaving a many2one by pressing tab", async () => {
3096 await mountView({
3097 type: "form",
3098 resModel: "partner",
3099 arch: `
3100 <form>
3101 <field name="trululu"/>
3102 <field name="name"/>
3103 </form>`,
3104 });
3105
3106 await contains(".o_field_many2one input").click();
3107 await runAllTimers();
3108 await press("tab");
3109 await animationFrame();
3110
3111 expect(".o_field_many2one input").toHaveValue("");
3112
3113 // open autocomplete dropdown and manually select item by UP/DOWN key and press TAB
3114 await contains(".o_field_many2one input").click();
3115 await runAllTimers();
3116 await press("arrowdown");
3117 await press("tab");
3118 await animationFrame();
3119
3120 expect(".o_field_many2one input").toHaveValue("second record");
3121
3122 // clear many2one and then open autocomplete, write something and press TAB
3123 await contains(".o_field_many2one input").edit("", { confirm: false });
3124 await runAllTimers();
3125 await contains(".o_field_many2one input").click();
3126 await contains(".o_field_many2one input").edit("se", { confirm: "tab" });
3127 await runAllTimers();
3128
3129 expect(".o_field_many2one input").toHaveValue("second record");
3130 });
3131
3132 test("leaving an empty many2one by pressing tab (after backspace or delete)", async () => {
3133 expect.assertions(4);
3134 await mountView({
3135 type: "form",
3136 resModel: "partner",
3137 resId: 1,
3138 arch: `
3139 <form>
3140 <field name="trululu"/>
3141 <field name="name"/>
3142 </form>`,
3143 });
3144
3145 expect(".o_field_many2one input").toHaveValue();
3146
3147 // simulate backspace to remove values and press TAB
3148 await contains(".o_field_many2one input").edit("", { confirm: false });
3149 await runAllTimers();
3150 await press("backspace");
3151 await press("tab");
3152 await animationFrame();
3153 expect(".o_field_many2one input").toHaveValue("");
3154
3155 // reset a value
3156 await selectFieldDropdownItem("trululu", "first record");
3157 expect(".o_field_many2one input").toHaveValue("first record");
3158
3159 // simulate delete to remove values and press TAB
3160 await contains(".o_field_many2one input").edit("", { confirm: false });
3161 await runAllTimers();
3162 await press("delete");
3163 await press("tab");
3164 // TODO: fix owl
3165 await animationFrame();
3166 expect(".o_field_many2one input").toHaveValue("");
3167 });
3168
3169 test("many2one in editable list + onchange, with enter", async () => {
3170 Partner._onChanges = {
3171 product_id: (obj) => {
3172 obj.int_field = obj.product_id || 0;
3173 },
3174 };
3175
3176 const def = new Deferred();
3177
3178 onRpc("onchange", () => def);
3179 onRpc(({ method }) => {
3180 expect.step(method);
3181 });
3182
3183 await mountView({
3184 type: "list",
3185 resModel: "partner",
3186 arch: `
3187 <list editable="bottom">
3188 <field name="product_id" />
3189 <field name="int_field" />
3190 </list>`,
3191 });
3192
3193 await contains("td.o_data_cell").click();
3194 await contains("td.o_data_cell input").edit("a", { confirm: false });
3195 await runAllTimers();
3196 await press("enter");
3197 def.resolve();
3198 await animationFrame();
3199 await press("enter");
3200 await animationFrame();
3201 expect(".modal").toHaveCount(0);
3202 expect.verifySteps([
3203 "get_views",
3204 "web_search_read", // to display results in the dialog
3205 "has_group",
3206 "web_name_search",
3207 "onchange",
3208 "web_save",
3209 ]);
3210 });
3211
3212 test("many2one in editable list + onchange, with enter, part 2", async () => {
3213 // this is the same test as the previous one, but the onchange is just
3214 // resolved slightly later
3215 Partner._onChanges = {
3216 product_id: (obj) => {
3217 obj.int_field = obj.product_id || 0;
3218 },
3219 };
3220
3221 const def = new Deferred();
3222 onRpc("onchange", () => def);
3223 onRpc(({ method }) => {
3224 expect.step(method);
3225 });
3226
3227 await mountView({
3228 type: "list",
3229 resModel: "partner",
3230 arch: `
3231 <list editable="bottom">
3232 <field name="product_id" />
3233 <field name="int_field" />
3234 </list>`,
3235 });
3236
3237 await contains("td.o_data_cell").click();
3238 await contains("td.o_data_cell input").edit("a", { confirm: false });
3239 await runAllTimers();
3240 await press("enter");
3241 await press("enter");
3242 def.resolve();
3243 await animationFrame();
3244 expect(".modal").toHaveCount(0);
3245 expect.verifySteps([
3246 "get_views",
3247 "web_search_read", // to display results in the dialog
3248 "has_group",
3249 "web_name_search",
3250 "onchange",
3251 "web_save",
3252 ]);
3253 });
3254
3255 test("many2one: dynamic domain set in the field's definition", async () => {
3256 expect.assertions(2);
3257 Partner._fields.trululu = fields.Many2one({
3258 string: "Trululu",
3259 relation: "partner",
3260 domain: "[('foo' ,'=', foo)]",
3261 });
3262 onRpc("web_name_search", ({ kwargs }) => {
3263 expect(kwargs.domain).toEqual([["foo", "=", "yop"]]);
3264 });
3265
3266 await mountView({
3267 type: "list",
3268 resModel: "partner",
3269 arch: `
3270 <list editable="top">
3271 <field name="foo" invisible="1" />
3272 <field name="trululu" />
3273 </list>`,
3274 });
3275
3276 await contains(".o_data_cell:eq(0)").click();
3277 await contains(".o_field_many2one input").click();
3278
3279 expect(".o_field_many2one .o-autocomplete--dropdown-item").toHaveCount(2);
3280 });
3281
3282 test("many2one: domain set in view and on field", async () => {
3283 expect.assertions(2);
3284 Partner._fields.trululu = fields.Many2one({
3285 string: "Trululu",
3286 relation: "partner",
3287 domain: "[('foo' ,'=', 'boum')]",
3288 });
3289 onRpc("web_name_search", ({ kwargs }) => {
3290 // should only use the domain set in the view
3291 expect(kwargs.domain).toEqual([["foo", "=", "blip"]]);
3292 });
3293
3294 await mountView({
3295 type: "list",
3296 resModel: "partner",
3297 arch: `
3298 <list editable="top">
3299 <field name="foo" invisible="1"/>
3300 <field name="trululu" domain="[['foo', '=', 'blip']]"/>
3301 </list>`,
3302 });
3303
3304 await contains(".o_data_cell:eq(0)").click();
3305 await contains(".o_field_many2one input").click();
3306
3307 expect(".o_field_many2one .o-autocomplete--dropdown-item").toHaveCount(2);
3308 });
3309
3310 test("many2one: domain updated by an onchange", async () => {
3311 expect.assertions(2);
3312 Partner._onChanges = {
3313 int_field: () => {},
3314 };
3315
3316 let domain = [];
3317 onRpc("onchange", () => {
3318 domain = [["id", "in", [10]]];
3319 return {
3320 domain: {
3321 trululu: domain,
3322 unexisting_field: domain,
3323 },
3324 };
3325 });
3326 onRpc("web_name_search", ({ kwargs }) => {
3327 expect(kwargs.domain).toEqual(domain);
3328 });
3329 await mountView({
3330 type: "form",
3331 resModel: "partner",
3332 resId: 1,
3333 arch: `
3334 <form>
3335 <field name="int_field" />
3336 <field name="trululu" />
3337 </form>`,
3338 });
3339
3340 // trigger a web_name_search (domain should be [])
3341 await contains(".o_field_widget[name=trululu] input").click();
3342 // close the dropdown
3343 await contains(".o_field_widget[name=trululu] input").click();
3344 // trigger an onchange that will update the domain
3345
3346 // trigger a web_name_search (domain should be [['id', 'in', [10]]])
3347 await contains(".o_field_widget[name='trululu'] input").click();
3348 });
3349
3350 test("search more in many2one: no text in input", async () => {
3351 // when the user clicks on 'Search more...' in a many2one dropdown, and there is no text
3352 // in the input (i.e. no value to search on), we bypass the web_name_search that is meant to
3353 // return a list of preselected ids to filter on in the list view (opened in a dialog)
3354 expect.assertions(2);
3355
3356 for (let i = 0; i < 8; i++) {
3357 Partner._records.push({ id: 100 + i, name: `test_${i}` });
3358 }
3359 Partner._views = {
3360 list: `
3361 <list>
3362 <field name="name" />
3363 </list>`,
3364 };
3365
3366 onRpc(({ method }) => {
3367 expect.step(method);
3368 });
3369 onRpc("web_search_read", ({ kwargs }) => {
3370 expect(kwargs.domain).toEqual([]);
3371 });
3372
3373 await mountView({
3374 type: "form",
3375 resModel: "partner",
3376 arch: '<form><field name="trululu" /></form>',
3377 });
3378
3379 await contains(`.o_field_widget[name="trululu"] input`).clear();
3380
3381 await contains(`.o_field_widget[name="trululu"] input`).click();
3382 await contains(`.o_field_widget[name="trululu"] .o_m2o_dropdown_option_search_more`).click();
3383
3384 expect.verifySteps([
3385 "get_views", // main form view
3386 "onchange",
3387 "web_name_search", // to display results in the dropdown
3388 "get_views", // list view in dialog
3389 "has_group",
3390 "web_search_read", // to display results in the dialog
3391 ]);
3392 });
3393
3394 test("search more in many2one: text in input", async () => {
3395 // when the user clicks on 'Search more...' in a many2one dropdown, and there is some
3396 // text in the input, we perform a web_name_search to get a (limited) list of preselected
3397 // ids and we add a dynamic filter (with those ids) to the search view in the dialog, so
3398 // that the user can remove this filter to bypass the limit
3399 expect.assertions(5);
3400
3401 for (let i = 0; i < 8; i++) {
3402 Partner._records.push({ id: 100 + i, name: `test_${i}` });
3403 }
3404 Partner._views = {
3405 list: `
3406 <list>
3407 <field name="name" />
3408 </list>`,
3409 };
3410
3411 let expectedDomain;
3412 onRpc(({ method }) => {
3413 expect.step(method);
3414 });
3415 onRpc("web_search_read", ({ kwargs }) => {
3416 expect(kwargs.domain).toEqual(expectedDomain);
3417 });
3418 await mountView({
3419 type: "form",
3420 resModel: "partner",
3421 arch: '<form><field name="trululu" /></form>',
3422 });
3423
3424 expectedDomain = [["id", "in", [100, 101, 102, 103, 104, 105, 106, 107]]];
3425 await contains(`.o_field_widget[name="trululu"] input`).click();
3426
3427 await contains(".o_field_widget[name='trululu'] input").edit("test", { confirm: false });
3428 await runAllTimers();
3429 await contains(`.o_field_widget[name="trululu"] .o_m2o_dropdown_option_search_more`).click();
3430
3431 expect(".modal .o_list_view").toHaveCount(1);
3432 expect(".modal .o_cp_searchview .o_facet_values").toHaveCount(1);
3433
3434 // remove the filter on ids
3435 expectedDomain = [];
3436 await contains(".modal .o_cp_searchview .o_facet_remove").click();
3437
3438 expect.verifySteps([
3439 "get_views", // main form view
3440 "onchange",
3441 "web_name_search", // empty search, triggered when the user clicks in the input
3442 "web_name_search", // to display results in the dropdown
3443 "name_search", // to get preselected ids matching the search
3444 "get_views", // list view in dialog
3445 "has_group",
3446 "web_search_read", // to display results in the dialog
3447 "web_search_read", // after removal of dynamic filter
3448 ]);
3449 });
3450
3451 test("search more in many2one: dropdown click", async () => {
3452 for (let i = 0; i < 8; i++) {
3453 Partner._records.push({ id: 100 + i, name: `test_${i}` });
3454 }
3455 Partner._views = {
3456 list: `
3457 <list>
3458 <field name="name" />
3459 </list>`,
3460 };
3461
3462 await mountView({
3463 type: "form",
3464 resModel: "partner",
3465 arch: '<form><field name="trululu" /></form>',
3466 });
3467
3468 await contains(`.o_field_widget[name="trululu"] input`).click();
3469
3470 await contains(".o_field_widget[name='trululu'] input").edit("test", { confirm: false });
3471 await runAllTimers();
3472 await contains(`.o_field_widget[name="trululu"] .o_m2o_dropdown_option_search_more`).click();
3473
3474 // dropdown selector
3475 const searchDropdown = ".o_control_panel_actions .o-dropdown";
3476 await contains(searchDropdown).click();
3477 expect(searchDropdown).toHaveClass("show");
3478 expect(getDropdownMenu(searchDropdown)).toBeVisible();
3479 });
3480
3481 test("updating a many2one from a many2many", async () => {
3482 expect.assertions(5);
3483
3484 Turtle._records[1].turtle_trululu = 1;
3485 Partner._views = {
3486 form: `
3487 <form>
3488 <field name="name" />
3489 </form>`,
3490 };
3491
3492 onRpc("get_formview_id", ({ args }) => {
3493 expect(args[0]).toEqual([1]);
3494 return false;
3495 });
3496
3497 await mountViewInDialog({
3498 type: "form",
3499 resModel: "partner",
3500 resId: 1,
3501 viewId: 1,
3502 arch: `
3503 <form>
3504 <field name="turtles">
3505 <list editable="bottom">
3506 <field name="name" />
3507 <field name="turtle_trululu"/>
3508 </list>
3509 </field>
3510 </form>`,
3511 });
3512 expect(".modal").toHaveCount(1);
3513
3514 // Opening the modal
3515 await contains(".o_data_row td:eq(1)").click();
3516 await contains(".o_external_button", { visible: false }).click();
3517 expect(".modal").toHaveCount(2);
3518
3519 // Changing the 'trululu' value
3520 await contains(".o_dialog:not(.o_inactive_modal) div[name=name] input").edit("test");
3521 await contains(".modal:eq(1) .o_form_button_save").click();
3522
3523 expect(".modal").toHaveCount(1);
3524
3525 // Test whether the value has changed
3526 expect(".o_dialog:not(.o_inactive_modal) div[name=turtle_trululu] input").toHaveValue("test");
3527 });
3528
3529 test("search more in many2one: cannot resequence inside dialog", async () => {
3530 // when the user clicks on 'Search more...' in a many2one dropdown, resequencing inside
3531 // the dialog works
3532 Partner._fields.sequence = fields.Integer();
3533 for (let i = 0; i < 8; i++) {
3534 Partner._records.push({ id: 100 + i, name: `test_${i}` });
3535 }
3536 Partner._views = {
3537 list: `
3538 <list>
3539 <field name="sequence" widget="handle" />
3540 <field name="name" />
3541 </list>`,
3542 };
3543
3544 onRpc("web_search_read", ({ kwargs }) => {
3545 expect(kwargs.domain).toEqual([]);
3546 });
3547 onRpc(({ method }) => {
3548 expect.step(method);
3549 });
3550
3551 await mountView({
3552 type: "form",
3553 resModel: "partner",
3554 arch: '<form><field name="trululu" /></form>',
3555 });
3556
3557 await contains(".o_field_widget[name='trululu'] input").click();
3558 await runAllTimers();
3559 await contains(`.o_field_widget[name="trululu"] .o_m2o_dropdown_option_search_more`).click();
3560
3561 expect(".modal").toHaveCount(1);
3562 expect(".modal .o_row_handle.o_disabled").toHaveCount(11);
3563
3564 expect.verifySteps([
3565 "get_views",
3566 "onchange",
3567 "web_name_search", // to display results in the dropdown
3568 "get_views", // list view in dialog
3569 "web_search_read", // to display results in the dialog
3570 "has_group",
3571 ]);
3572 });
3573
3574 test("many2one dropdown disappears on scroll", async () => {
3575 await mountView({
3576 type: "form",
3577 resModel: "partner",
3578 resId: 1,
3579 arch: `
3580 <form>
3581 <div style="height: 2000px;">
3582 <field name="trululu" />
3583 </div>
3584 </form>`,
3585 });
3586
3587 await contains(".o_field_many2one input").click();
3588 expect(".o_field_many2one .dropdown-menu").toHaveCount(1);
3589
3590 const dropdown = queryOne(".o_field_many2one .dropdown-menu");
3591 dropdown.style = "max-height: 40px;";
3592
3593 await scroll(dropdown, { top: 50 });
3594
3595 expect(dropdown).toHaveProperty("scrollTop", 50);
3596 expect(".o_field_many2one .dropdown-menu").toHaveCount(1);
3597
3598 await scroll(".o_content", { top: 50 });
3599 await animationFrame();
3600
3601 expect(".o_field_many2one .dropdown-menu").toHaveCount(0);
3602 });
3603
3604 test("search more in many2one: group and use the pager", async () => {
3605 Partner._records.push(
3606 {
3607 id: 5,
3608 name: "Partner 4",
3609 },
3610 {
3611 id: 6,
3612 name: "Partner 5",
3613 },
3614 {
3615 id: 7,
3616 name: "Partner 6",
3617 },
3618 {
3619 id: 8,
3620 name: "Partner 7",
3621 },
3622 {
3623 id: 9,
3624 name: "Partner 8",
3625 },
3626 {
3627 id: 10,
3628 name: "Partner 9",
3629 }
3630 );
3631
3632 Partner._views = {
3633 list: `
3634 <list limit="7">
3635 <field name="name" />
3636 </list>`,
3637 search: `
3638 <search><group>
3639 <filter name="bar" string="Bar" context="{'group_by': 'bar'}" />
3640 </group></search>`,
3641 };
3642
3643 await mountView({
3644 type: "form",
3645 resModel: "partner",
3646 resId: 1,
3647 arch: '<form><field name="trululu" /></form>',
3648 });
3649
3650 await selectFieldDropdownItem("trululu", "Search more...");
3651 await toggleSearchBarMenu(".modal");
3652 await toggleMenuItem("Bar");
3653
3654 await contains(".modal .o_group_header:eq(1)").click();
3655
3656 expect(".modal .o_data_row").toHaveCount(7);
3657 await contains(".modal .o_group_header .o_pager_next").click();
3658 expect(".modal .o_data_row").toHaveCount(1);
3659 });
3660
3661 test("focus when closing many2one modal in many2one modal", async () => {
3662 Partner._views = {
3663 form: '<form><field name="trululu"/></form>',
3664 };
3665
3666 onRpc("get_formview_id", () => false);
3667
3668 await mountViewInDialog({
3669 type: "form",
3670 resModel: "partner",
3671 resId: 2,
3672 });
3673
3674 expect(".o_dialog").toHaveCount(1);
3675 expect(document.body).toHaveClass("modal-open");
3676
3677 // Open many2one modal
3678 await contains(".o_external_button", { visible: false }).click();
3679
3680 const originalModal = queryOne(".o_dialog:eq(1)");
3681
3682 expect(".o_dialog").toHaveCount(2);
3683 expect(originalModal).not.toHaveClass("o_inactive_modal");
3684 expect(document.body).toHaveClass("modal-open");
3685
3686 // Open many2one modal of field in many2one modal
3687 await contains(".o_dialog:eq(1) .o_external_button", { visible: false }).click();
3688
3689 expect(".o_dialog").toHaveCount(3);
3690 expect(".o_dialog:eq(2)").not.toHaveClass("o_inactive_modal");
3691 expect(document.body).toHaveClass("modal-open");
3692
3693 // Close second modal
3694 await contains(".o_dialog:eq(2) button[class='btn-close']").click();
3695
3696 expect(".o_dialog").toHaveCount(2);
3697 expect(originalModal).toBe(queryOne(".o_dialog:eq(1)"), {
3698 message: "First modal is still opened",
3699 });
3700 expect(".o_dialog:eq(1)").not.toHaveClass("o_inactive_modal");
3701 expect(document.body).toHaveClass("modal-open");
3702
3703 // Close first modal
3704 await contains(".o_dialog:eq(1) button[class='btn-close']").click();
3705 expect(".o_dialog").toHaveCount(1);
3706 });
3707
3708 test("search more pager is reset when doing a new search", async () => {
3709 Partner._fields.datetime = fields.Datetime({ string: "Datetime Field", searchable: true });
3710 Partner._records.push(
3711 ...new Array(170).fill().map((_, i) => ({ id: i + 10, name: "Partner " + i }))
3712 );
3713 Partner._views = {
3714 list: `
3715 <list>
3716 <field name="name"/>
3717 </list>`,
3718 search: `
3719 <search>
3720 <field name="datetime"/>
3721 <field name="name"/>
3722 </search>`,
3723 };
3724
3725 await mountView({
3726 type: "form",
3727 resModel: "partner",
3728 resId: 1,
3729 arch: `
3730 <form>
3731 <sheet>
3732 <group>
3733 <field name="trululu"/>
3734 </group>
3735 </sheet>
3736 </form>`,
3737 });
3738
3739 await selectFieldDropdownItem("trululu", "Search more...");
3740
3741 await contains(".modal .o_pager_next").click();
3742
3743 expect(".modal .o_pager_limit").toHaveText("173");
3744 expect(".modal .o_pager_value").toHaveText("81-160");
3745 expect(".modal tr.o_data_row").toHaveCount(80);
3746
3747 await editSearch("first");
3748 await validateSearch();
3749
3750 expect(".modal .o_pager_limit").toHaveText("1");
3751 expect(".modal .o_pager_value").toHaveText("1-1");
3752 expect(".modal tr.o_data_row").toHaveCount(1);
3753 });
3754
3755 test("click on many2one link in list view", async () => {
3756 Turtle._records[1].product_id = 37;
3757 Partner._views = {
3758 form: '<form> <field name="turtles"/> </form>',
3759 };
3760 Turtle._views = {
3761 list: `
3762 <list readonly="1">
3763 <field name="product_id" widget="many2one" context="{'field': 'Yes'}"/>
3764 </list>`,
3765 };
3766 onRpc("get_formview_action", (args) => {
3767 expect.step("get_formview_action");
3768 expect(args.kwargs.context.field).toBe("Yes");
3769 expect(args.kwargs.context).not.toInclude("global");
3770 return {
3771 type: "ir.actions.act_window",
3772 res_model: "product",
3773 view_type: "form",
3774 view_mode: "form",
3775 views: [[false, "form"]],
3776 target: "current",
3777 res_id: args[0],
3778 };
3779 });
3780 await mountWithCleanup(WebClient);
3781 await getService("action").doAction({
3782 name: "Partner",
3783 res_model: "partner",
3784 res_id: 1,
3785 type: "ir.actions.act_window",
3786 views: [[false, "form"]],
3787 context: { global: "No" },
3788 });
3789 expect("a.o_form_uri").toHaveCount(1);
3790 expect(".o_breadcrumb").toHaveCount(1);
3791
3792 await contains("a.o_form_uri").click();
3793 expect.verifySteps(["get_formview_action"]);
3794 expect(".breadcrumb-item").toHaveCount(1);
3795 expect(".o_breadcrumb").toHaveCount(1);
3796 });
3797
3798 test("Many2oneField with placeholder", async () => {
3799 await mountView({
3800 type: "form",
3801 resModel: "partner",
3802 arch: '<form><field name="trululu" placeholder="Placeholder"/></form>',
3803 });
3804
3805 expect(".o_field_widget[name='trululu'] input").toHaveAttribute("placeholder", "Placeholder");
3806 });
3807
3808 test("placeholder_field shows as placeholder", async () => {
3809 await mountView({
3810 type: "form",
3811 resModel: "partner",
3812 arch: `<form>
3813 <field name="name"/>
3814 <field name="trululu" options="{'placeholder_field': 'name'}"/>
3815 </form>`,
3816 resId: 1,
3817 });
3818
3819 expect(".o_field_widget[name='trululu'] input").toHaveAttribute("placeholder", "first record");
3820 });
3821
3822 test("external_button performs a doAction by default", async () => {
3823 Partner._views = {
3824 form: '<form><field name="trululu"/></form>',
3825 };
3826 onRpc("get_formview_action", () => {
3827 expect.step("get_formview_action");
3828 return {
3829 type: "ir.actions.act_window",
3830 res_model: "partner",
3831 view_type: "form",
3832 view_mode: "form",
3833 views: [[false, "form"]],
3834 target: "current",
3835 res_id: false,
3836 };
3837 });
3838 await mountWithCleanup(WebClient);
3839 await getService("action").doAction({
3840 name: "Partner",
3841 res_model: "partner",
3842 res_id: 1,
3843 type: "ir.actions.act_window",
3844 views: [[false, "form"]],
3845 });
3846
3847 await selectFieldDropdownItem("trululu", "first record");
3848 expect(".o_field_widget .o_external_button .oi-arrow-right").toHaveCount(1);
3849 await contains(".o_field_widget .o_external_button", { visible: false }).click();
3850
3851 expect.verifySteps(["get_formview_action"]);
3852 expect(".breadcrumb").toHaveText("first record");
3853 });
3854
3855 test("external_button opens a FormViewDialog in dialogs", async () => {
3856 onRpc("get_formview_id", () => {
3857 expect.step("get_formview_id");
3858 return false;
3859 });
3860 await mountViewInDialog({
3861 type: "form",
3862 resModel: "partner",
3863 arch: '<form><field name="trululu"/></form>',
3864 });
3865 expect(".modal").toHaveCount(1);
3866
3867 await selectFieldDropdownItem("trululu", "first record");
3868 expect(".o_field_widget .o_external_button .oi-launch").toHaveCount(1);
3869 await contains(".o_field_widget .o_external_button", { visible: false }).click();
3870
3871 expect.verifySteps(["get_formview_id"]);
3872 expect(".modal").toHaveCount(2);
3873 });
3874
3875 test("external_button opens a new tab when middle clicked or ctrl+click", async () => {
3876 mockService("action", {
3877 doAction(params, options) {
3878 if (options?.newWindow) {
3879 expect.step("opened in a new window");
3880 return;
3881 }
3882 super.doAction(params);
3883 },
3884 loadState() {},
3885 });
3886 Partner._views = {
3887 form: '<form><field name="trululu"/></form>',
3888 };
3889 onRpc("get_formview_action", () => ({
3890 type: "ir.actions.act_window",
3891 res_model: "partner",
3892 view_type: "form",
3893 view_mode: "form",
3894 views: [[false, "form"]],
3895 target: "current",
3896 res_id: false,
3897 }));
3898 await mountWithCleanup(WebClient);
3899 await getService("action").doAction({
3900 name: "Partner",
3901 res_model: "partner",
3902 res_id: 1,
3903 type: "ir.actions.act_window",
3904 views: [[false, "form"]],
3905 });
3906
3907 await selectFieldDropdownItem("trululu", "first record");
3908 await contains(".o_external_button", { visible: false }).click({ ctrlKey: true });
3909 expect.verifySteps(["opened in a new window"]);
3910 await middleClick(".o_external_button");
3911 await animationFrame();
3912 expect.verifySteps(["opened in a new window"]);
3913 });
3914
3915 test("keep changes when editing related record in a dialog", async () => {
3916 Partner._views = {
3917 [["form", 98]]: '<form><field name="int_field"/></form>',
3918 };
3919 onRpc("get_formview_id", () => 98);
3920 onRpc("web_save", () => {
3921 expect.step("web_save");
3922 });
3923 await mountViewInDialog({
3924 type: "form",
3925 resModel: "partner",
3926 arch: '<form><field name="foo"/><field name="trululu"/></form>',
3927 });
3928 expect(".modal").toHaveCount(1);
3929
3930 await contains(".o_field_widget[name=foo] input").edit("some value", { confirm: false });
3931 await runAllTimers();
3932 await selectFieldDropdownItem("trululu", "first record");
3933 expect(".o_field_widget .o_external_button .oi-launch").toHaveCount(1);
3934 await contains(".o_field_widget .o_external_button", { visible: false }).click();
3935 expect(".modal").toHaveCount(2);
3936
3937 await contains(".o_dialog:not(.o_inactive_modal) .o_field_widget[name=int_field] input").edit(
3938 "5464"
3939 );
3940 await contains(
3941 ".o_dialog:not(.o_inactive_modal) .modal-footer .btn-primary:not(.d-none)"
3942 ).click();
3943
3944 expect(".modal").toHaveCount(1);
3945 expect(".o_field_widget[name=foo] input").toHaveValue("some value");
3946 expect.verifySteps(["web_save"]);
3947 });
3948
3949 test("create and edit, save and then discard", async () => {
3950 Partner.views = {
3951 [[98, "form"]]: '<form><field name="name"/></form>',
3952 };
3953 onRpc("get_formview_id", () => 98);
3954 await mountView({
3955 type: "form",
3956 resModel: "partner",
3957 arch: '<form><field name="trululu"/></form>',
3958 resId: 1,
3959 });
3960
3961 expect(".o_field_widget[name=trululu] input").toHaveValue("aaa");
3962
3963 await contains(".o_field_widget[name=trululu] input").edit("new m2o", { confirm: false });
3964 await runAllTimers();
3965 await contains(".o_field_widget[name=trululu] input").click();
3966 await selectFieldDropdownItem("trululu", "Create and edit...");
3967 expect(".modal").toHaveCount(1);
3968
3969 await contains(".modal-footer .btn-primary:not(.d-none)").click();
3970 expect(".modal").toHaveCount(0);
3971 expect(".o_field_widget[name=trululu] input").toHaveValue("new m2o");
3972
3973 await contains(".o_form_button_cancel").click();
3974 expect(".o_field_widget[name=trululu] input").toHaveValue("aaa");
3975 });
3976
3977 test("external button must be displayed after the update caused by an onchange", async () => {
3978 Partner._onChanges = {
3979 name: (obj) => {
3980 if (obj.name) {
3981 obj.trululu = 1;
3982 }
3983 },
3984 };
3985 await mountView({
3986 type: "form",
3987 resModel: "partner",
3988 arch: `
3989 <form>
3990 <field name="name"/>
3991 <field name="trululu"/>
3992 </form>`,
3993 });
3994
3995 expect(".o_external_button").toHaveCount(0);
3996
3997 await contains("[name=name] input").edit("new value");
3998 expect(".o_external_button").toHaveCount(1);
3999 });
4000
4001 test("many2one field with false as name", async () => {
4002 Partner._records[0].name = false;
4003 await mountView({
4004 type: "form",
4005 resModel: "partner",
4006 resId: 2,
4007 arch: `
4008 <form>
4009 <field name="trululu"/>
4010 </form>`,
4011 });
4012
4013 expect(".o_field_widget[name='trululu'] input").toHaveValue("Unnamed");
4014 });
4015
4016 test("many2one search with false as name", async () => {
4017 onRpc("web_name_search", () => [
4018 { id: 1, display_name: false, __formatted_display_name: false },
4019 ]);
4020 await mountView({
4021 type: "form",
4022 resModel: "partner",
4023 arch: `
4024 <form>
4025 <field name="trululu" />
4026 </form>`,
4027 });
4028
4029 await contains(".o_field_many2one input").click();
4030 expect(".o_field_many2one[name='trululu'] .dropdown-menu a.dropdown-item:eq(0)").toHaveText(
4031 "Unnamed"
4032 );
4033 });
4034
4035 test("many2one search with formatted name", async () => {
4036 onRpc("web_name_search", () => [
4037 {
4038 id: 1,
4039 display_name: "Paul Eric",
4040 __formatted_display_name: "Research & Development Test: **Paul** --Eric-- `good guy`\n\tMore text",
4041 },
4042 ]);
4043 await mountView({
4044 type: "form",
4045 resModel: "partner",
4046 arch: `
4047 <form>
4048 <field name="trululu" />
4049 </form>`,
4050 });
4051
4052 await contains(".o_field_many2one input").click();
4053 expect(
4054 ".o_field_many2one[name='trululu'] .dropdown-menu a.dropdown-item:eq(0)"
4055 ).toHaveInnerHTML(
4056 `Research & Development Test: <b>Paul</b> <span class="text-muted">Eric</span> <span class="o_tag position-relative d-inline-flex align-items-center mw-100 o_badge badge rounded-pill lh-1 o_tag_color_0">good guy</span><br/><span style="margin-left: 2em"></span>More text`
4057 );
4058 await contains(
4059 ".o_field_many2one[name='trululu'] .dropdown-menu a.dropdown-item:eq(0)"
4060 ).click();
4061 expect(".o_field_many2one input").toHaveValue("Paul Eric");
4062 });
4063
4064 test.tags("desktop");
4065 test("search typeahead", async () => {
4066 onRpc("web_name_search", () => expect.step("web_name_search"));
4067 await mountView({
4068 type: "form",
4069 resModel: "partner",
4070 arch: `<form><field name="trululu" options="{ 'search_threshold': 3 }"/></form>`,
4071 });
4072
4073 await contains(".o_field_widget[name=trululu] input").click();
4074 await runAllTimers();
4075 expect.verifySteps([]);
4076 expect(queryAllTexts(`.o-autocomplete.dropdown li`)).toEqual([
4077 "Start typing 3 characters",
4078 "Search more...",
4079 ]);
4080
4081 await contains(".o_field_widget[name=trululu] input").edit("r", { confirm: false });
4082 await runAllTimers();
4083 expect.verifySteps([]);
4084 expect(queryAllTexts(`.o-autocomplete.dropdown li`)).toEqual([
4085 "Start typing 3 characters",
4086 'Create "r"',
4087 "Create and edit...",
4088 "Search more...",
4089 ]);
4090
4091 await contains(".o_field_widget[name=trululu] input").edit("re", { confirm: false });
4092 await runAllTimers();
4093 expect.verifySteps([]);
4094 expect(queryAllTexts(`.o-autocomplete.dropdown li`)).toEqual([
4095 "Start typing 3 characters",
4096 'Create "re"',
4097 "Create and edit...",
4098 "Search more...",
4099 ]);
4100
4101 await contains(".o_field_widget[name=trululu] input").edit("rec", { confirm: false });
4102 await runAllTimers();
4103 expect.verifySteps(["web_name_search"]);
4104 expect(queryAllTexts(`.o-autocomplete.dropdown li`)).toEqual([
4105 "first record",
4106 "second record",
4107 'Create "rec"',
4108 "Create and edit...",
4109 "Search more...",
4110 ]);
4111 });
4112
4113 test("highlight search in many2one", async () => {
4114 await mountView({
4115 type: "form",
4116 resModel: "partner",
4117 arch: `<form><field name="trululu"/></form>`,
4118 });
4119 await contains(".o_field_widget[name=trululu] input").edit("rec", { confirm: false });
4120 await runAllTimers();
4121 expect(`.o-autocomplete.dropdown li:not(.o_m2o_dropdown_option) a`).toHaveCount(2);
4122 expect(`.o-autocomplete.dropdown li:eq(0) a`).toHaveInnerHTML(`
4123 first
4124 <span class="text-primary fw-bold">
4125 rec
4126 </span>
4127 ord
4128 `);
4129 expect(`.o-autocomplete.dropdown li:eq(1) a`).toHaveInnerHTML(`
4130 second
4131 <span class="text-primary fw-bold">
4132 rec
4133 </span>
4134 ord
4135 `);
4136 });