Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, queryAllTexts, queryFirst, queryOne } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 editSelectMenu,
9 fields,
10 models,
11 mountView,
12 onRpc,
13 } from "@web/../tests/web_test_helpers";
14
15 class Partner extends models.Model {
16 display_name = fields.Char({ string: "Displayed name" });
17 int_field = fields.Integer({ string: "int_field" });
18 trululu = fields.Many2one({ string: "Trululu", relation: "partner" });
19 product_id = fields.Many2one({ string: "Product", relation: "product" });
20 color = fields.Selection({
21 selection: [
22 ["red", "Red"],
23 ["black", "Black"],
24 ],
25 default: "red",
26 string: "Color",
27 });
28 _records = [
29 {
30 id: 1,
31 display_name: "first record",
32 int_field: 10,
33 trululu: 4,
34 },
35 {
36 id: 2,
37 display_name: "second record",
38 int_field: 9,
39 trululu: 1,
40 product_id: 37,
41 },
42 {
43 id: 4,
44 display_name: "aaa",
45 },
46 ];
47 }
48
49 class Product extends models.Model {
50 name = fields.Char({ string: "Product Name" });
51 _records = [
52 {
53 id: 37,
54 name: "xphone",
55 },
56 {
57 id: 41,
58 name: "xpad",
59 },
60 ];
61 }
62 class User extends models.Model {
63 _name = "res.users";
64 has_group() {
65 return true;
66 }
67 }
68 defineModels([Partner, Product, User]);
69
70 test("SelectionField in a list view", async () => {
71 Partner._records.forEach((r) => (r.color = "red"));
72 onRpc("has_group", () => true);
73 await mountView({
74 type: "list",
75 resModel: "partner",
76 arch: '<list string="Colors" editable="top"><field name="color"/></list>',
77 });
78
79 expect(".o_data_row").toHaveCount(3);
80 await click(".o_data_cell");
81 await animationFrame();
82 const td = queryFirst("tbody tr.o_selected_row td:not(.o_list_record_selector)");
83 expect(queryOne(".o_select_menu input", { root: td })).toHaveCount(1, {
84 message: "td should have a child 'select'",
85 });
86 expect(td.children).toHaveCount(1, { message: "select tag should be only child of td" });
87 });
88
89 test.tags("desktop");
90 test("SelectionField in a list view with multi_edit", async () => {
91 Partner._records.forEach((r) => (r.color = "red"));
92 onRpc("has_group", () => true);
93 await mountView({
94 type: "list",
95 resModel: "partner",
96 arch: '<list string="Colors" multi_edit="1"><field name="color"/></list>',
97 });
98 // select two records and edit them
99 await click(".o_data_row:eq(0) .o_list_record_selector input:first");
100 await animationFrame();
101 await click(".o_data_row:eq(1) .o_list_record_selector input:first");
102 await animationFrame();
103
104 await contains(".o_field_cell[name='color']").click();
105 await editSelectMenu(".o_field_widget[name='color'] input", { value: "" });
106 await contains(".o_dialog footer button").click();
107 expect(queryAllTexts(".o_field_cell")).toEqual(["", "", "Red"]);
108
109 await contains(".o_field_cell[name='color']").click();
110 await editSelectMenu(".o_field_widget[name='color'] input", { value: "Black" });
111 await contains(".o_dialog footer button").click();
112 expect(queryAllTexts(".o_field_cell")).toEqual(["Black", "Black", "Red"]);
113 });
114
115 test("SelectionField, edition and on many2one field", async () => {
116 Partner._onChanges.product_id = () => {};
117 Partner._records[0].product_id = 37;
118 Partner._records[0].trululu = false;
119 onRpc(({ method }) => expect.step(method));
120 await mountView({
121 type: "form",
122 resModel: "partner",
123 resId: 1,
124 arch: /* xml */ `
125 <form>
126 <field name="product_id" widget="selection" />
127 <field name="trululu" widget="selection" />
128 <field name="color" widget="selection" />
129 </form>`,
130 });
131 expect(".o_select_menu").toHaveCount(3);
132 await contains(".o_field_widget[name='product_id'] input").click();
133 expect(queryAllTexts(".o_select_menu_item")).toEqual(["xphone", "xpad"]);
134 expect(".o_field_widget[name='product_id'] input").toHaveValue("xphone");
135 expect(".o_field_widget[name='trululu'] input").toHaveValue("");
136 await editSelectMenu(".o_field_widget[name='product_id'] input", { value: "xpad" });
137 expect(".o_field_widget[name='color'] input").toHaveValue("Red");
138 expect.verifySteps(["get_views", "web_read", "name_search", "name_search", "onchange"]);
139 });
140
141 test("unset selection field with 0 as key", async () => {
142 // The server doesn't make a distinction between false value (the field
143 // is unset), and selection 0, as in that case the value it returns is
144 // false. So the client must convert false to value 0 if it exists.
145 Partner._fields.selection = fields.Selection({
146 selection: [
147 [0, "Value O"],
148 [1, "Value 1"],
149 ],
150 });
151
152 await mountView({
153 type: "form",
154 resModel: "partner",
155 resId: 1,
156 arch: /* xml */ '<form edit="0"><field name="selection" /></form>',
157 });
158
159 expect(".o_field_widget").toHaveText("Value O", {
160 message: "the displayed value should be 'Value O'",
161 });
162 expect(".o_field_widget").not.toHaveClass("o_field_empty", {
163 message: "should not have class o_field_empty",
164 });
165 });
166
167 test("unset selection field with string keys", async () => {
168 // The server doesn't make a distinction between false value (the field
169 // is unset), and selection 0, as in that case the value it returns is
170 // false. So the client must convert false to value 0 if it exists. In
171 // this test, it doesn't exist as keys are strings.
172 Partner._fields.selection = fields.Selection({
173 selection: [
174 ["0", "Value O"],
175 ["1", "Value 1"],
176 ],
177 });
178 await mountView({
179 type: "form",
180 resModel: "partner",
181 resId: 1,
182 arch: /* xml */ '<form edit="0"><field name="selection" /></form>',
183 });
184
185 expect(".o_field_widget").toHaveText("", { message: "there should be no displayed value" });
186 expect(".o_field_widget").toHaveClass("o_field_empty", {
187 message: "should have class o_field_empty",
188 });
189 });
190
191 test("unset selection on a many2one field", async () => {
192 expect.assertions(1);
193 onRpc("web_save", ({ args }) => {
194 expect(args[1].trululu).toBe(false, {
195 message: "should send 'false' as trululu value",
196 });
197 });
198 await mountView({
199 type: "form",
200 resModel: "partner",
201 resId: 1,
202 arch: /* xml */ '<form><field name="trululu" widget="selection" /></form>',
203 });
204
205 await editSelectMenu(".o_field_widget[name='trululu'] input", { value: "" });
206 await animationFrame();
207 await clickSave();
208 await animationFrame();
209 });
210
211 test("field selection with many2ones and special characters", async () => {
212 // edit the partner with id=4
213 Partner._records[2].display_name = "<span>hey</span>";
214
215 await mountView({
216 type: "form",
217 resModel: "partner",
218 resId: 1,
219 arch: /* xml */ '<form><field name="trululu" widget="selection" /></form>',
220 });
221
222 await contains(".o_field_widget[name='trululu'] input").click();
223 expect(".o_select_menu_item:contains(<span>hey</span>)").toHaveCount(1);
224 });
225
226 test("required selection widget should not have blank option", async () => {
227 Partner._fields.feedback_value = fields.Selection({
228 required: true,
229 selection: [
230 ["good", "Good"],
231 ["bad", "Bad"],
232 ],
233 default: "good",
234 string: "Good",
235 });
236 await mountView({
237 type: "form",
238 resModel: "partner",
239 resId: 1,
240 arch: /* xml */ `
241 <form>
242 <field name="feedback_value" />
243 <field name="color" required="feedback_value == 'bad'" />
244 </form>`,
245 });
246
247 await contains(".o_field_widget[name='feedback_value'] input").click();
248 expect(queryAllTexts(".o_select_menu_item")).toEqual(["Good", "Bad"]);
249
250 // change value to update widget modifier values
251 await editSelectMenu(".o_field_widget[name='feedback_value'] input", { value: "Bad" });
252 await contains(".o_field_widget[name='color'] input").click();
253 expect(queryAllTexts(".o_select_menu_item")).toEqual(["Red", "Black"]);
254 });
255
256 test("selection field with placeholder", async () => {
257 await mountView({
258 type: "form",
259 resModel: "partner",
260 arch: /* xml */ `<form><field name="trululu" widget="selection" placeholder="Placeholder"/></form>`,
261 });
262
263 expect(`.o_field_widget[name='trululu'] input`).toHaveAttribute("placeholder", "Placeholder");
264 });
265
266 test("placeholder_field shows as placeholder", async () => {
267 Partner._fields.char = fields.Char({
268 default: "My Placeholder",
269 });
270 await mountView({
271 type: "form",
272 resModel: "partner",
273 arch: `<form>
274 <field name="trululu" widget="selection" options="{'placeholder_field' : 'char'}"/>
275 <field name="char"/>
276 </form>`,
277 });
278 expect(`.o_field_widget[name='trululu'] input`).toHaveAttribute(
279 "placeholder",
280 "My Placeholder"
281 );
282 });
283
284 test("SelectionField in kanban view", async () => {
285 await mountView({
286 type: "kanban",
287 resModel: "partner",
288 arch: /* xml */ `
289 <kanban>
290 <templates>
291 <t t-name="card">
292 <field name="color" widget="selection" />
293 </t>
294 </templates>
295 </kanban>`,
296 domain: [["id", "=", 1]],
297 });
298
299 await contains(".o_field_widget[name='color'] input").click();
300 expect(".o_select_menu_item").toHaveCount(2, {
301 message: "Two options are displayed",
302 });
303 expect(queryAllTexts(".o_select_menu_item")).toEqual(["Red", "Black"]);
304 });
305
306 test("SelectionField - auto save record in kanban view", async () => {
307 onRpc("web_save", ({ method }) => expect.step(method));
308 await mountView({
309 type: "kanban",
310 resModel: "partner",
311 arch: /* xml */ `
312 <kanban>
313 <templates>
314 <t t-name="card">
315 <field name="color" widget="selection" />
316 </t>
317 </templates>
318 </kanban>`,
319 domain: [["id", "=", 1]],
320 });
321 await editSelectMenu(".o_field_widget[name='color'] input", { value: "Black" });
322 expect.verifySteps(["web_save"]);
323 });
324
325 test("SelectionField don't open form view on click in kanban view", async function (assert) {
326 await mountView({
327 type: "kanban",
328 resModel: "partner",
329 arch: /* xml */ `
330 <kanban>
331 <templates>
332 <t t-name="card">
333 <field name="color" widget="selection" />
334 </t>
335 </templates>
336 </kanban>`,
337 domain: [["id", "=", 1]],
338 selectRecord: () => {
339 expect.step("selectRecord");
340 },
341 });
342
343 await contains(".o_field_widget[name='color'] input").click();
344 expect.verifySteps([]);
345 });
346
347 test("SelectionField is disabled if field readonly", async () => {
348 Partner._fields.color = fields.Selection({
349 selection: [
350 ["red", "Red"],
351 ["black", "Black"],
352 ],
353 default: "red",
354 string: "Color",
355 readonly: true,
356 });
357
358 await mountView({
359 type: "kanban",
360 resModel: "partner",
361 arch: /* xml */ `
362 <kanban>
363 <templates>
364 <t t-name="card">
365 <field name="color" widget="selection" />
366 </t>
367 </templates>
368 </kanban>
369 `,
370 domain: [["id", "=", 1]],
371 });
372
373 expect(".o_field_widget[name='color'] span").toHaveCount(1, {
374 message: "field should be readonly",
375 });
376 });
377
378 test("SelectionField is disabled with a readonly attribute", async () => {
379 await mountView({
380 type: "kanban",
381 resModel: "partner",
382 arch: /* xml */ `
383 <kanban>
384 <templates>
385 <t t-name="card">
386 <field name="color" widget="selection" readonly="1" />
387 </t>
388 </templates>
389 </kanban>
390 `,
391 domain: [["id", "=", 1]],
392 });
393
394 expect(".o_field_widget[name='color'] span").toHaveCount(1, {
395 message: "field should be readonly",
396 });
397 });
398
399 test.tags("mobile");
400 test("SelectionField search is disabled in BottomSheet", async function (assert) {
401 await mountView({
402 type: "kanban",
403 resModel: "partner",
404 arch: /* xml */ `
405 <kanban>
406 <templates>
407 <t t-name="card">
408 <field name="color" widget="selection" />
409 </t>
410 </templates>
411 </kanban>`,
412 domain: [["id", "=", 1]],
413 });
414
415 await contains(".o_field_widget[name='color'] input").click();
416 expect(".o_bottom_sheet input").toHaveCount(0);
417 });