Debian Codegraph @

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

raw
1 import { describe, expect, test } from "@odoo/hoot";
2 import { click, edit, press, queryAllValues, queryFirst, select } from "@odoo/hoot-dom";
3 import { animationFrame, Deferred, runAllTimers } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 defineModels,
7 fields,
8 mockService,
9 models,
10 mountView,
11 mountViewInDialog,
12 onRpc,
13 } from "@web/../tests/web_test_helpers";
14
15 class Partner extends models.Model {
16 name = fields.Char();
17 foo = fields.Char({ default: "My little Foo Value" });
18 bar = fields.Boolean({ default: true });
19 int_field = fields.Integer();
20 p = fields.One2many({
21 relation: "partner",
22 relation_field: "trululu",
23 });
24 turtles = fields.One2many({
25 relation: "turtle",
26 relation_field: "turtle_trululu",
27 });
28 trululu = fields.Many2one({ relation: "partner" });
29 color = fields.Selection({
30 selection: [
31 ["red", "Red"],
32 ["black", "Black"],
33 ],
34 default: "red",
35 });
36 reference = fields.Reference({
37 selection: [
38 ["product", "Product"],
39 ["partner.type", "Partner Type"],
40 ["partner", "Partner"],
41 ],
42 });
43 reference_char = fields.Char();
44 model_id = fields.Many2one({ relation: "ir.model" });
45
46 _records = [
47 {
48 id: 1,
49 name: "first record",
50 bar: true,
51 foo: "yop",
52 int_field: 10,
53 p: [],
54 turtles: [2],
55 trululu: 4,
56 reference: "product,37",
57 },
58 {
59 id: 2,
60 name: "second record",
61 bar: true,
62 foo: "blip",
63 int_field: 9,
64 p: [],
65 trululu: 1,
66 },
67 {
68 id: 4,
69 name: "aaa",
70 bar: false,
71 },
72 ];
73 }
74
75 class Product extends models.Model {
76 name = fields.Char();
77
78 _records = [
79 { id: 37, name: "xphone" },
80 { id: 41, name: "xpad" },
81 ];
82 }
83
84 class PartnerType extends models.Model {
85 name = fields.Char();
86
87 _records = [
88 { id: 12, name: "gold" },
89 { id: 14, name: "silver" },
90 ];
91 }
92
93 class Turtle extends models.Model {
94 name = fields.Char();
95 turtle_trululu = fields.Many2one({ relation: "partner" });
96 turtle_ref = fields.Reference({
97 selection: [
98 ["product", "Product"],
99 ["partner", "Partner"],
100 ],
101 });
102 partner_ids = fields.Many2many({ relation: "partner" });
103
104 _records = [
105 { id: 1, name: "leonardo", partner_ids: [] },
106 { id: 2, name: "donatello", partner_ids: [2, 4] },
107 { id: 3, name: "raphael", partner_ids: [], turtle_ref: "product,37" },
108 ];
109 }
110
111 class IrModel extends models.Model {
112 _name = "ir.model";
113
114 name = fields.Char();
115 model = fields.Char();
116
117 _records = [
118 { id: 17, name: "Partner", model: "partner" },
119 { id: 20, name: "Product", model: "product" },
120 { id: 21, name: "Partner Type", model: "partner.type" },
121 ];
122 }
123
124 defineModels([Partner, Product, PartnerType, Turtle, IrModel]);
125
126 describe.current.tags("desktop");
127
128 test("ReferenceField can quick create models", async () => {
129 onRpc(({ method }) => expect.step(method));
130
131 await mountView({
132 type: "form",
133 resModel: "partner",
134 arch: /* xml */ `<form><field name="reference" /></form>`,
135 });
136
137 await click("select");
138 await select("partner");
139 await animationFrame();
140 await click(".o_field_widget[name='reference'] input");
141 await edit("new partner");
142 await runAllTimers();
143 await click(".o_field_widget[name='reference'] .o_m2o_dropdown_option_create");
144 await animationFrame();
145
146 await clickSave();
147
148 // The name_create method should have been called
149 expect.verifySteps([
150 "get_views",
151 "onchange",
152 "web_name_search", // for the select
153 "web_name_search", // for the spawned many2one
154 "name_create",
155 "web_save",
156 ]);
157 });
158
159 test("ReferenceField respects no_quick_create", async () => {
160 await mountView({
161 type: "form",
162 resModel: "partner",
163 arch: /* xml */ `<form><field name="reference" options="{'no_quick_create': 1}" /></form>`,
164 });
165
166 await click("select");
167 await select("partner");
168 await animationFrame();
169 await click(".o_field_widget[name='reference'] input");
170 await edit("new partner");
171 await runAllTimers();
172 expect(".ui-autocomplete .o_m2o_dropdown_option").toHaveCount(1, {
173 message: "Dropdown should be opened and have one item",
174 });
175 expect(".ui-autocomplete .o_m2o_dropdown_option:eq(0)").toHaveClass(
176 "o_m2o_dropdown_option_create_edit"
177 );
178 });
179
180 test("ReferenceField in modal readonly mode", async () => {
181 Partner._records[0].p = [2];
182 Partner._records[1].trululu = 1;
183 Partner._records[1].reference = "product,41";
184
185 Partner._views[["form", false]] = /* xml */ `
186 <form>
187 <field name="display_name" />
188 <field name="reference" />
189 </form>
190 `;
191 Partner._views[["list", false]] = /* xml */ `
192 <list>
193 <field name="display_name"/>
194 <field name="reference" />
195 </list>
196 `;
197
198 await mountView({
199 type: "form",
200 resModel: "partner",
201 resId: 1,
202 arch: /* xml */ `
203 <form edit="0">
204 <field name="reference" />
205 <field name="p" />
206 </form>
207 `,
208 });
209
210 // Current Form
211 expect(".o_field_widget[name=reference] .o_form_uri").toHaveText("xphone", {
212 message: "the field reference of the form should have the right value",
213 });
214 expect(queryFirst(".o_data_cell")).toHaveText("second record", {
215 message: "the list should have one record",
216 });
217 await click(".o_data_cell");
218 await animationFrame();
219
220 // In modal
221 expect(".modal-lg").toHaveCount(1);
222 expect(".modal-lg .o_field_widget[name=reference] .o_form_uri").toHaveText("xpad", {
223 message: "The field reference in the modal should have the right value",
224 });
225 });
226
227 test("ReferenceField in modal write mode", async () => {
228 Partner._records[0].p = [2];
229 Partner._records[1].trululu = 1;
230 Partner._records[1].reference = "product,41";
231
232 Partner._views[["form", false]] = /* xml */ `
233 <form>
234 <field name="display_name" />
235 <field name="reference" />
236 </form>
237 `;
238 Partner._views[["list", false]] = /* xml */ `
239 <list>
240 <field name="display_name"/>
241 <field name="reference" />
242 </list>
243 `;
244
245 await mountView({
246 type: "form",
247 resModel: "partner",
248 resId: 1,
249 arch: /* xml */ `
250 <form>
251 <field name="reference" />
252 <field name="p" />
253 </form>
254 `,
255 });
256
257 // Current Form
258 expect(".o_field_widget[name=reference] option:checked").toHaveText("Product", {
259 message: "The reference field's model should be Product",
260 });
261 expect(".o_field_widget[name=reference] .o-autocomplete--input").toHaveValue("xphone", {
262 message: "The reference field's record should be xphone",
263 });
264
265 await click(".o_data_cell");
266 await animationFrame();
267
268 // In modal
269 expect(".modal-lg").toHaveCount(1, { message: "there should be one modal opened" });
270 expect(".modal-lg .o_field_widget[name=reference] option:checked").toHaveText("Product", {
271 message: "The reference field's model should be Product",
272 });
273 expect(".modal-lg .o_field_widget[name=reference] .o-autocomplete--input").toHaveValue("xpad", {
274 message: "The reference field's record should be xpad",
275 });
276 });
277
278 test("reference in form view", async () => {
279 expect.assertions(11);
280
281 Product._views[["form", false]] = /* xml */ `
282 <form>
283 <field name="display_name" />
284 </form>
285 `;
286
287 onRpc(({ args, method, model }) => {
288 if (method === "get_formview_action") {
289 expect(args[0]).toEqual([37], {
290 message: "should call get_formview_action with correct id",
291 });
292 return {
293 res_id: 17,
294 type: "ir.actions.act_window",
295 target: "current",
296 res_model: "res.partner",
297 };
298 }
299 if (method === "get_formview_id") {
300 expect(args[0]).toEqual([37], {
301 message: "should call get_formview_id with correct id",
302 });
303 return false;
304 }
305 if (method === "web_name_search") {
306 expect(model).toBe("partner.type", {
307 message: "the web_name_search should be done on the newly set model",
308 });
309 }
310 if (method === "web_save") {
311 expect(model).toBe("partner", { message: "should write on the current model" });
312 expect(args).toEqual([[1], { reference: "partner.type,12" }], {
313 message: "should write the correct value",
314 });
315 }
316 });
317
318 mockService("action", {
319 doAction(action) {
320 expect(action.res_id).toBe(17, {
321 message: "should do a do_action with correct parameters",
322 });
323 },
324 });
325
326 await mountViewInDialog({
327 type: "form",
328 resModel: "partner",
329 resId: 1,
330 arch: /* xml */ `
331 <form>
332 <sheet>
333 <group>
334 <field name="reference" string="custom label"/>
335 </group>
336 </sheet>
337 </form>
338 `,
339 });
340
341 expect(".o_field_many2one_selection").toHaveCount(1, {
342 message: "should contain one many2one",
343 });
344 expect(".o_field_widget select").toHaveValue("product", {
345 message: "widget should contain one select with the model",
346 });
347 expect(".o_field_widget input").toHaveValue("xphone", {
348 message: "widget should contain one input with the record",
349 });
350
351 expect(queryAllValues(".o_field_widget select > option")).toEqual(
352 ["", "product", "partner.type", "partner"],
353 {
354 message: "the options should be correctly set",
355 }
356 );
357
358 await click(".o_external_button");
359 await animationFrame();
360
361 expect(".o_dialog:not(.o_inactive_modal) .modal-title").toHaveText("Open: custom label", {
362 message: "dialog title should display the custom string label",
363 });
364
365 await click(".o_dialog:not(.o_inactive_modal) .o_form_button_cancel");
366 await animationFrame();
367
368 await select("partner.type", { target: ".o_field_widget select" });
369 await animationFrame();
370
371 expect(".o_field_widget input").toHaveValue("", {
372 message: "many2one value should be reset after model change",
373 });
374
375 await click(".o_field_widget[name=reference] input");
376 await animationFrame();
377 await click(".o_field_widget[name=reference] .ui-menu-item");
378
379 await clickSave();
380 expect(".o_field_widget[name=reference] input").toHaveValue("gold", {
381 message: "should contain a link with the new value",
382 });
383 });
384
385 test("Many2One 'Search more...' updates on resModel change", async () => {
386 onRpc("has_group", () => true);
387
388 Product._views[["list", false]] = /* xml */ `<list><field name="display_name"/></list>`;
389 Product._views[["search", false]] = /* xml */ `<search/>`;
390
391 await mountView({
392 type: "form",
393 resModel: "partner",
394 arch: /* xml */ `<form><field name="reference"/></form>`,
395 });
396
397 // Selecting a relation
398 await click("div.o_field_reference select.o_input");
399 await select("partner.type");
400
401 // Selecting another relation
402 await click("div.o_field_reference select.o_input");
403 await select("product");
404 await animationFrame();
405
406 // Opening the Search more... option
407 await click("div.o_field_reference input.o_input");
408 await animationFrame();
409 await click("div.o_field_reference .o_m2o_dropdown_option_search_more");
410 await animationFrame();
411
412 expect(queryFirst("div.modal td.o_data_cell")).toHaveText("xphone", {
413 message: "The search more should lead to the values of product.",
414 });
415 });
416
417 test("computed reference field changed by onchange to 'False,0' value", async () => {
418 expect.assertions(1);
419
420 Partner._onChanges.bar = (obj) => {
421 if (!obj.bar) {
422 obj.reference_char = "False,0";
423 }
424 };
425 onRpc("web_save", ({ args }) => {
426 expect(args[1]).toEqual({
427 bar: false,
428 reference_char: "False,0",
429 });
430 });
431 await mountView({
432 type: "form",
433 resModel: "partner",
434 arch: /* xml */ `
435 <form>
436 <field name="bar"/>
437 <field name="reference_char" widget="reference"/>
438 </form>
439 `,
440 });
441
442 // trigger the onchange to set a value for the reference field
443 await click(".o_field_boolean input");
444 await animationFrame();
445
446 await clickSave();
447 });
448
449 test("interact with reference field changed by onchange", async () => {
450 expect.assertions(2);
451
452 Partner._onChanges.bar = (obj) => {
453 if (!obj.bar) {
454 obj.reference = "partner,1";
455 }
456 };
457 onRpc("web_save", ({ args }) => {
458 expect(args[1]).toEqual({
459 bar: false,
460 reference: "partner,4",
461 });
462 });
463 await mountView({
464 type: "form",
465 resModel: "partner",
466 arch: /* xml */ `
467 <form>
468 <field name="bar"/>
469 <field name="reference"/>
470 </form>
471 `,
472 });
473
474 // trigger the onchange to set a value for the reference field
475 await click(".o_field_boolean input");
476 await animationFrame();
477
478 expect(".o_field_widget[name=reference] select").toHaveValue("partner");
479
480 // manually update reference field
481 queryFirst(".o_field_widget[name=reference] input").tabIndex = 0;
482 await click(".o_field_widget[name=reference] input");
483 await edit("aaa");
484 await runAllTimers();
485 await click(".ui-autocomplete .ui-menu-item");
486
487 // save
488 await clickSave();
489 });
490
491 test("default_get and onchange with a reference field", async () => {
492 Partner._fields.reference = fields.Reference({
493 selection: [
494 ["product", "Product"],
495 ["partner.type", "Partner Type"],
496 ["partner", "Partner"],
497 ],
498 default: "product,37",
499 });
500 Partner._onChanges.int_field = (obj) => {
501 if (obj.int_field) {
502 obj.reference = "partner.type," + obj.int_field;
503 }
504 };
505
506 await mountView({
507 type: "form",
508 resModel: "partner",
509 arch: /* xml */ `
510 <form>
511 <sheet>
512 <group>
513 <field name="int_field" />
514 <field name="reference" />
515 </group>
516 </sheet>
517 </form>
518 `,
519 });
520
521 expect(".o_field_widget[name='reference'] select").toHaveValue("product", {
522 message: "reference field model should be correctly set",
523 });
524 expect(".o_field_widget[name='reference'] input").toHaveValue("xphone", {
525 message: "reference field value should be correctly set",
526 });
527
528 // trigger onchange
529 await click(".o_field_widget[name=int_field] input");
530 await edit(12, { confirm: "enter" });
531 await animationFrame();
532
533 expect(".o_field_widget[name='reference'] select").toHaveValue("partner.type", {
534 message: "reference field model should be correctly set",
535 });
536 expect(".o_field_widget[name='reference'] input").toHaveValue("gold", {
537 message: "reference field value should be correctly set",
538 });
539 });
540
541 test("default_get a reference field in a x2m", async () => {
542 Partner._fields.turtles = fields.One2many({
543 relation: "turtle",
544 relation_field: "turtle_trululu",
545 default: [[0, 0, { turtle_ref: "product,37" }]],
546 });
547 Turtle._views[["form", false]] = /* xml */ `
548 <form>
549 <field name="display_name" />
550 <field name="turtle_ref" />
551 </form>
552 `;
553
554 await mountView({
555 type: "form",
556 resModel: "partner",
557 arch: /* xml */ `
558 <form>
559 <sheet>
560 <field name="turtles">
561 <list>
562 <field name="turtle_ref" />
563 </list>
564 </field>
565 </sheet>
566 </form>
567 `,
568 });
569
570 expect('.o_field_widget[name="turtles"] .o_data_row').toHaveText("xphone", {
571 message: "the default value should be correctly handled",
572 });
573 });
574
575 test("ReferenceField on char field, reset by onchange", async () => {
576 Partner._records[0].foo = "product,37";
577 Partner._onChanges.int_field = (obj) => (obj.foo = "product," + obj.int_field);
578 let nbNameGet = 0;
579 onRpc("product", "read", ({ args }) => {
580 if (args[1].length === 1 && args[1][0] === "display_name") {
581 nbNameGet++;
582 }
583 });
584 await mountView({
585 type: "form",
586 resModel: "partner",
587 resId: 1,
588 arch: /* xml */ `
589 <form>
590 <sheet>
591 <group>
592 <field name="int_field" />
593 <field name="foo" widget="reference" readonly="1" />
594 </group>
595 </sheet>
596 </form>
597 `,
598 });
599
600 expect(nbNameGet).toBe(1, { message: "the first name_get should have been done" });
601 expect(".o_field_widget[name=foo]").toHaveText("xphone", {
602 message: "foo field should be correctly set",
603 });
604 // trigger onchange
605 await click(".o_field_widget[name=int_field] input");
606 await edit(41, { confirm: "enter" });
607 await runAllTimers();
608 await animationFrame();
609 expect(nbNameGet).toBe(2, { message: "the second name_get should have been done" });
610 expect(".o_field_widget[name=foo]").toHaveText("xpad", {
611 message: "foo field should have been updated",
612 });
613 });
614
615 test("reference and list navigation", async () => {
616 onRpc("has_group", () => true);
617
618 await mountView({
619 type: "list",
620 resModel: "partner",
621 arch: /* xml */ `
622 <list editable="bottom">
623 <field name="reference" />
624 </list>
625 `,
626 });
627
628 // edit first row
629 await click(".o_data_row .o_data_cell");
630 await animationFrame();
631 expect(".o_data_row [name='reference'] input").toBeFocused();
632
633 await press("Tab");
634 await animationFrame();
635 expect(".o_data_row:nth-child(2) [name='reference'] select").toBeFocused();
636 });
637
638 test("ReferenceField with model_field option", async () => {
639 Partner._records[0].reference = false;
640 Partner._records[0].model_id = 20;
641 Partner._records[1].name = "John Smith";
642 Product._records[0].name = "Product 1";
643
644 await mountView({
645 type: "form",
646 resModel: "partner",
647 resId: 1,
648 arch: /* xml */ `
649 <form>
650 <field name="model_id" />
651 <field name="reference" options="{'model_field': 'model_id'}" />
652 </form>
653 `,
654 });
655 expect("select").toHaveCount(0, {
656 message: "the selection list of the reference field should not exist.",
657 });
658 expect(".o_field_widget[name='reference'] input").toHaveValue("", {
659 message: "no record should be selected in the reference field",
660 });
661
662 await click(".o_field_widget[name='reference'] input");
663 await edit("Product 1");
664 await runAllTimers();
665 await click(".ui-autocomplete .ui-menu-item:first-child");
666 await animationFrame();
667 expect(".o_field_widget[name='reference'] input").toHaveValue("Product 1", {
668 message: "the Product 1 record should be selected in the reference field",
669 });
670
671 await click(".o_field_widget[name='model_id'] input");
672 await edit("Partner");
673 await runAllTimers();
674 await click(".ui-autocomplete .ui-menu-item:first-child");
675 await runAllTimers();
676 await animationFrame();
677 expect(".o_field_widget[name='reference'] input").toHaveValue("", {
678 message: "no record should be selected in the reference field",
679 });
680
681 await click(".o_field_widget[name='reference'] input");
682 await edit("John");
683 await runAllTimers();
684 await click(".ui-autocomplete .ui-menu-item:first-child");
685 await animationFrame();
686 expect(".o_field_widget[name='reference'] input").toHaveValue("John Smith", {
687 message: "the John Smith record should be selected in the reference field",
688 });
689 });
690
691 test("ReferenceField with model_field option (model_field not synchronized with reference)", async () => {
692 // Checks that the data is not modified even though it is not synchronized.
693 // Not synchronized = model_id contains a different model than the one used in reference.
694 Partner._records[0].reference = "partner,1";
695 Partner._records[0].model_id = 20;
696 Partner._records[0].name = "John Smith";
697
698 await mountView({
699 type: "form",
700 resModel: "partner",
701 resId: 1,
702 arch: /* xml */ `
703 <form>
704 <field name="model_id" />
705 <field name="reference" options="{'model_field': 'model_id'}" />
706 </form>
707 `,
708 });
709
710 expect("select").toHaveCount(0, {
711 message: "the selection list of the reference field should not exist.",
712 });
713 expect(".o_field_widget[name='model_id'] input").toHaveValue("Product", {
714 message: "the Product model should be selected in the model_id field",
715 });
716 expect(".o_field_widget[name='reference'] input").toHaveValue("John Smith", {
717 message: "the John Smith record should be selected in the reference field",
718 });
719 });
720
721 test("Reference field with default value in list view", async () => {
722 expect.assertions(1);
723
724 onRpc("has_group", () => true);
725 onRpc(({ method, args }) => {
726 if (method === "onchange") {
727 return {
728 value: {
729 reference: {
730 id: { id: 2, model: "partner" },
731 name: "second record",
732 },
733 },
734 };
735 } else if (method === "web_save") {
736 expect(args[1].reference).toBe("partner,2");
737 }
738 });
739 await mountView({
740 type: "list",
741 resModel: "partner",
742 arch: /* xml */ `
743 <list string="Test" editable="top">
744 <field name="reference"/>
745 <field name="name"/>
746 </list>
747 `,
748 });
749 await click(".o_control_panel_main_buttons .o_list_button_add");
750 await animationFrame();
751 await click('.o_list_char[name="name"] input');
752 await edit("Blabla");
753 await runAllTimers();
754 await click(".o_control_panel_main_buttons .o_list_button_save");
755 await animationFrame();
756 });
757
758 test("ReferenceField with model_field option (tree list in form view)", async () => {
759 Turtle._records[0].partner_ids = [1];
760 Partner._records[0].reference = "product,41";
761 Partner._records[0].model_id = 20;
762
763 await mountView({
764 type: "form",
765 resModel: "turtle",
766 resId: 1,
767 arch: /* xml */ `
768 <form>
769 <field name="partner_ids">
770 <list editable="bottom">
771 <field name="name" />
772 <field name="model_id" />
773 <field name="reference" options="{'model_field': 'model_id'}" class="reference_field" />
774 </list>
775 </field>
776 </form>
777 `,
778 });
779
780 expect(".reference_field").toHaveText("xpad");
781
782 // Select the second product without changing the model
783 await click(".o_list_table .reference_field");
784 await animationFrame();
785
786 await click(".o_list_table .reference_field input");
787 await animationFrame();
788
789 // Enter to select it
790 await press("Enter");
791 await animationFrame();
792
793 expect(".reference_field input").toHaveValue("xphone", {
794 message: "should have selected the first product",
795 });
796 });
797
798 test("edit a record containing a ReferenceField with model_field option (list in form view)", async () => {
799 Turtle._records[0].partner_ids = [1];
800 Partner._records[0].reference = "product,41";
801 Partner._records[0].model_id = 20;
802
803 await mountView({
804 type: "form",
805 resModel: "turtle",
806 resId: 1,
807 arch: /* xml */ `
808 <form>
809 <field name="partner_ids">
810 <list editable="bottom">
811 <field name="name" />
812 <field name="model_id" />
813 <field name="reference" options='{"model_field": "model_id"}'/>
814 </list>
815 </field>
816 </form>
817 `,
818 });
819
820 expect(".o_list_table [name='name']").toHaveText("first record");
821 expect(".o_list_table [name='reference']").toHaveText("xpad");
822
823 await click(".o_list_table .o_data_cell");
824 await animationFrame();
825
826 await click(".o_list_table [name='name'] input");
827 await edit("plop");
828 await animationFrame();
829 await click(".o_form_view");
830 await animationFrame();
831
832 expect(".o_list_table [name='name']").toHaveText("plop");
833 expect(".o_list_table [name='reference']").toHaveText("xpad");
834 });
835
836 test("Change model field of a ReferenceField then select an invalid value (tree list in form view)", async () => {
837 Turtle._records[0].partner_ids = [1];
838 Partner._records[0].reference = "product,41";
839 Partner._records[0].model_id = 20;
840
841 await mountView({
842 type: "form",
843 resModel: "turtle",
844 resId: 1,
845 arch: /* xml */ `
846 <form>
847 <field name="partner_ids">
848 <list editable="bottom">
849 <field name="name" />
850 <field name="model_id"/>
851 <field name="reference" required="true" options="{'model_field': 'model_id'}" class="reference_field" />
852 </list>
853 </field>
854 </form>
855 `,
856 });
857
858 expect(".reference_field").toHaveText("xpad");
859 expect(".o_list_many2one").toHaveText("Product");
860
861 await click(".o_list_table td.o_list_many2one");
862 await animationFrame();
863 await click(".o_list_table .o_list_many2one input");
864 await animationFrame();
865 //Select the "Partner" option, different from original "Product"
866 await click(
867 ".o_list_table .o_list_many2one .o_input_dropdown .dropdown-item:contains(Partner)"
868 );
869 await runAllTimers();
870 await animationFrame();
871 expect(".reference_field input").toHaveValue("");
872 expect(".o_list_many2one input").toHaveValue("Partner");
873 //Void the associated, required, "reference" field and make sure the form marks the field as required
874 await click(".o_list_table .reference_field input");
875 const textInput = queryFirst(".o_list_table .reference_field input");
876 textInput.setSelectionRange(0, textInput.value.length);
877 await click(".o_list_table .reference_field input");
878 await press("Backspace");
879 await click(".o_form_view_container");
880 await animationFrame();
881
882 expect(".o_list_table .reference_field.o_field_invalid").toHaveCount(1);
883 });
884
885 test("model selector is displayed only when it should be", async () => {
886 //The model selector should be only displayed if
887 //there is no hide_model=True options AND no model_field specified
888 await mountView({
889 type: "form",
890 resModel: "partner",
891 resId: 1,
892 arch: /* xml */ `
893 <form>
894 <group>
895 <field name="reference" options="{'model_field': 'model_id'}" />
896 </group>
897 <group>
898 <field name="reference" options="{'model_field': 'model_id', 'hide_model': True}" />
899 </group>
900 <group>
901 <field name="reference" options="{'hide_model': True}" />
902 </group>
903 <group>
904 <field name="reference" />
905 </group>
906 </form>
907 `,
908 });
909
910 expect(".o_inner_group:eq(0) select").toHaveCount(0, {
911 message:
912 "the selection list of the reference field should not exist when model_field is specified.",
913 });
914 expect(".o_inner_group:eq(1) select").toHaveCount(0, {
915 message:
916 "the selection list of the reference field should not exist when model_field is specified and hide_model=True.",
917 });
918 expect(".o_inner_group:eq(2) select").toHaveCount(0, {
919 message: "the selection list of the reference field should not exist when hide_model=True.",
920 });
921 expect(".o_inner_group:eq(3) select").toHaveCount(1, {
922 message:
923 "the selection list of the reference field should exist when hide_model=False and no model_field specified.",
924 });
925 });
926
927 test("reference field should await fetch model before render", async () => {
928 Partner._records[0].model_id = 20;
929
930 const def = new Deferred();
931 onRpc("ir.model", "read", async () => {
932 await def;
933 });
934 mountView({
935 type: "form",
936 resModel: "partner",
937 resId: 1,
938 arch: /* xml */ `
939 <form>
940 <field name="model_id" invisible="1"/>
941 <field name="reference" options="{'model_field': 'model_id'}" />
942 </form>
943 `,
944 });
945
946 await animationFrame();
947 expect(".o_form_view").toHaveCount(0);
948 def.resolve();
949
950 await animationFrame();
951 expect(".o_form_view").toHaveCount(1);
952 });
953
954 test("do not ask for display_name if field is invisible", async () => {
955 expect.assertions(1);
956
957 onRpc("web_read", ({ kwargs }) => {
958 expect(kwargs.specification).toEqual({
959 display_name: {},
960 reference: {
961 fields: {},
962 },
963 });
964 });
965 await mountView({
966 type: "form",
967 resModel: "partner",
968 resId: 1,
969 arch: `<form><field name="reference" invisible="1"/></form>`,
970 });
971 });
972
973 test("reference char with list view pager navigation", async () => {
974 Partner._records[0].reference_char = "product,37";
975 Partner._records[1].reference_char = "product,41";
976 await mountView({
977 type: "form",
978 resModel: "partner",
979 resId: 1,
980 resIds: [1, 2],
981 arch: `<form edit="0"><field name="reference_char" widget="reference" string="Record"/></form>`,
982 });
983 expect(".o_field_reference").toHaveText("xphone");
984 await click(".o_pager_next");
985 await animationFrame();
986 expect(".o_field_reference").toHaveText("xpad");
987 });