Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { animationFrame, queryAllTexts } from "@odoo/hoot-dom";
3 import { followRelation } from "@web/../tests/core/tree_editor/condition_tree_editor_test_helpers";
4 import { contains, defineModels, fields, models, mountView } from "../../web_test_helpers";
5
6 class Contact extends models.Model {
7 email = fields.Char();
8 child_ids = fields.One2many({ relation: "contact" });
9 }
10
11 class Lead extends models.Model {
12 contact_id = fields.Many2one({ relation: "contact" });
13 salesperson_id = fields.Many2one({ relation: "contact" });
14 note = fields.Text();
15 }
16
17 class UpdateRecordAction extends models.Model {
18 model = fields.Char();
19 update_path = fields.Char();
20 non_searchable = fields.Char({ searchable: false });
21 }
22
23 defineModels([Contact, Lead, UpdateRecordAction]);
24
25 test("readonly", async () => {
26 UpdateRecordAction._records = [
27 {
28 id: 1,
29 update_path: "non_searchable",
30 },
31 ];
32 await mountView({
33 type: "form",
34 resModel: "update.record.action",
35 resId: 1,
36 arch: /* xml */ `
37 <form>
38 <field name="update_path" widget="field_selector" readonly="1"/>
39 </form>
40 `,
41 });
42 expect(".o_field_widget[name='update_path']").toHaveText("non_searchable");
43 expect(".o_field_widget[name='update_path']").toHaveClass(
44 "o_field_widget o_readonly_modifier o_field_field_selector"
45 );
46 expect(".o_field_widget[name='update_path'] .o_input").toHaveCount(0);
47 });
48
49 test("no specified options", async () => {
50 await mountView({
51 type: "form",
52 resModel: "update.record.action",
53 arch: /* xml */ `
54 <form>
55 <field name="update_path" widget="field_selector"/>
56 </form>
57 `,
58 });
59 await contains(".o_field_widget[name='update_path'] .o_input").click();
60 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
61 [
62 "Created on",
63 "Display name",
64 "Id",
65 "Last Modified on",
66 "Model",
67 "Non searchable",
68 "Update path",
69 ],
70 { message: "should display fields from same model by default" }
71 );
72 });
73
74 test("only_searchable option", async () => {
75 await mountView({
76 type: "form",
77 resModel: "update.record.action",
78 arch: /* xml */ `
79 <form>
80 <field name="update_path" widget="field_selector" options="{'only_searchable': true}"/>
81 </form>
82 `,
83 });
84 await contains(".o_field_widget[name='update_path'] .o_input").click();
85 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
86 ["Created on", "Display name", "Id", "Last Modified on", "Model", "Update path"],
87 { message: "should not display non searchable fields" }
88 );
89 });
90
91 test("model option", async () => {
92 await mountView({
93 type: "form",
94 resModel: "update.record.action",
95 arch: /* xml */ `
96 <form>
97 <field name="model"/>
98 <field name="update_path" widget="field_selector" options="{'model': 'model'}"/>
99 </form>
100 `,
101 });
102 await contains(".o_field_widget[name='update_path'] .o_input").click();
103 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
104 [
105 "Created on",
106 "Display name",
107 "Id",
108 "Last Modified on",
109 "Model",
110 "Non searchable",
111 "Update path",
112 ],
113 { message: "should display fields from same model by default" }
114 );
115 await contains(".o_field_widget[name='model'] .o_input").edit("lead");
116 await contains(".o_field_widget[name='update_path'] .o_input").click();
117 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
118 ["Contact", "Created on", "Display name", "Id", "Last Modified on", "Note", "Salesperson"],
119 { message: "should display fields of the specified model" }
120 );
121 expect(".o_model_field_selector_popover_item_relation").toHaveCount(2, {
122 message: "following relations is supported by default",
123 });
124 await followRelation();
125 await animationFrame();
126 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
127 ["Childs", "Created on", "Display name", "Email", "Id", "Last Modified on"],
128 { message: "should display fields of the selected relation" }
129 );
130 });
131
132 test("follow_relations option", async () => {
133 await mountView({
134 type: "form",
135 resModel: "update.record.action",
136 arch: /* xml */ `
137 <form>
138 <field name="model"/>
139 <field name="update_path" widget="field_selector" options="{
140 'model': 'model',
141 'follow_relations': false,
142 }"/>
143 </form>
144 `,
145 });
146 await contains(".o_field_widget[name='model'] .o_input").edit("lead");
147 await contains(".o_field_widget[name='update_path'] .o_input").click();
148 expect(queryAllTexts(".o_model_field_selector_popover_item")).toEqual(
149 ["Contact", "Created on", "Display name", "Id", "Last Modified on", "Note", "Salesperson"],
150 { message: "should display fields of the specified model" }
151 );
152 expect(".o_model_field_selector_popover_item_relation").toHaveCount(0, {
153 message: "should not allow to follow relations",
154 });
155 });