Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, press, queryAll, queryAllTexts, queryFirst } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import { defineModels, fields, models, mountView, onRpc } from "@web/../tests/web_test_helpers";
5
6 class Partner extends models.Model {
7 foo = fields.Char({ string: "Foo" });
8 sequence = fields.Integer({ string: "Sequence", searchable: true });
9 selection = fields.Selection({
10 string: "Selection",
11 selection: [
12 ["normal", "Normal"],
13 ["blocked", "Blocked"],
14 ["done", "Done"],
15 ],
16 });
17
18 _records = [
19 { id: 1, foo: "yop", selection: "blocked" },
20 { id: 2, foo: "blip", selection: "normal" },
21 { id: 4, foo: "abc", selection: "done" },
22 { id: 3, foo: "gnap" },
23 { id: 5, foo: "blop" },
24 ];
25 }
26
27 class User extends models.Model {
28 _name = "res.users";
29 has_group() {
30 return true;
31 }
32 }
33
34 defineModels([Partner, User]);
35
36 test("StateSelectionField in form view", async () => {
37 await mountView({
38 type: "form",
39 resModel: "partner",
40 arch: /* xml */ `
41 <form>
42 <sheet>
43 <group>
44 <field name="selection" widget="state_selection"/>
45 </group>
46 </sheet>
47 </form>
48 `,
49 resId: 1,
50 });
51
52 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_red").toHaveCount(1, {
53 message: "should have one red status since selection is the second, blocked state",
54 });
55 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_green").toHaveCount(0, {
56 message: "should not have one green status since selection is the second, blocked state",
57 });
58 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
59
60 // Click on the status button to make the dropdown appear
61 await click(".o_field_widget.o_field_state_selection .o_status");
62 await animationFrame();
63 expect(".o-dropdown--menu").toHaveCount(1, { message: "there should be a dropdown" });
64 expect(".o-dropdown--menu .dropdown-item").toHaveCount(3, {
65 message: "there should be three options in the dropdown",
66 });
67 expect(".o-dropdown--menu .dropdown-item:nth-child(2)").toHaveClass("active", {
68 message: "current value has a checkmark",
69 });
70
71 // Click on the first option, "Normal"
72 await click(".o-dropdown--menu .dropdown-item");
73 await animationFrame();
74 expect(".o-dropdown--menu").toHaveCount(0, {
75 message: "there should not be a dropdown anymore",
76 });
77 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_red").toHaveCount(0, {
78 message: "should not have one red status since selection is the first, normal state",
79 });
80 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_green").toHaveCount(0, {
81 message: "should not have one green status since selection is the first, normal state",
82 });
83 expect(".o_field_widget.o_field_state_selection span.o_status").toHaveCount(1, {
84 message: "should have one grey status since selection is the first, normal state",
85 });
86
87 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should still not be a dropdown" });
88 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_red").toHaveCount(0, {
89 message: "should still not have one red status since selection is the first, normal state",
90 });
91 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_green").toHaveCount(0, {
92 message:
93 "should still not have one green status since selection is the first, normal state",
94 });
95 expect(".o_field_widget.o_field_state_selection span.o_status").toHaveCount(1, {
96 message: "should still have one grey status since selection is the first, normal state",
97 });
98
99 // Click on the status button to make the dropdown appear
100 await click(".o_field_widget.o_field_state_selection .o_status");
101 await animationFrame();
102 expect(".o-dropdown--menu .dropdown-item").toHaveCount(3, {
103 message: "there should be three options in the dropdown",
104 });
105
106 // Click on the last option, "Done"
107 await click(".o-dropdown--menu .dropdown-item:last-child");
108 await animationFrame();
109 expect(".o-dropdown--menu").toHaveCount(0, {
110 message: "there should not be a dropdown anymore",
111 });
112 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_red").toHaveCount(0, {
113 message: "should not have one red status since selection is the third, done state",
114 });
115 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_green").toHaveCount(1, {
116 message: "should have one green status since selection is the third, done state",
117 });
118
119 // save
120 await click(".o_form_button_save");
121 await animationFrame();
122 expect(".o-dropdown--menu").toHaveCount(0, {
123 message: "there should still not be a dropdown anymore",
124 });
125 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_red").toHaveCount(0, {
126 message: "should still not have one red status since selection is the third, done state",
127 });
128 expect(".o_field_widget.o_field_state_selection span.o_status.o_status_green").toHaveCount(1, {
129 message: "should still have one green status since selection is the third, done state",
130 });
131 });
132
133 test("Check role attribute for dropdown items", async () => {
134 await mountView({
135 type: "form",
136 resModel: "partner",
137 arch: /* xml */ `
138 <form>
139 <sheet>
140 <group>
141 <field name="selection" widget="state_selection"/>
142 </group>
143 </sheet>
144 </form>
145 `,
146 resId: 1,
147 });
148
149 // Open the dropdown
150 click(".o_field_widget.o_field_state_selection .o_status");
151 await animationFrame();
152
153 // Assert that the dropdown is open
154 expect(".o-dropdown--menu").toHaveCount(1, { message: "there should be a dropdown" });
155
156 // Assert that each dropdown item has role="checkbox"
157 expect(queryFirst(".o-dropdown--menu .dropdown-item")).toHaveAttribute(
158 "role",
159 "menuitemcheckbox"
160 );
161 });
162
163 test("StateSelectionField with readonly modifier", async () => {
164 await mountView({
165 type: "form",
166 resModel: "partner",
167 arch: /* xml */ `<form><field name="selection" widget="state_selection" readonly="1"/></form>`,
168 resId: 1,
169 });
170
171 expect(".o_field_state_selection").toHaveClass("o_readonly_modifier");
172 expect(".o_field_state_selection button").toHaveClass("o_disabled");
173 expect(".dropdown-menu:visible").not.toHaveCount();
174 await click(".o_field_state_selection span.o_status");
175 await animationFrame();
176 expect(".dropdown-menu:visible").not.toHaveCount();
177 });
178
179 test("StateSelectionField for form view with hide_label option", async () => {
180 await mountView({
181 type: "form",
182 resModel: "partner",
183 arch: /* xml */ `
184 <form>
185 <field name="selection" widget="state_selection" options="{'hide_label': False}"/>
186 </form>
187 `,
188 resId: 1,
189 });
190 expect(".o_status_label").toHaveCount(1);
191 });
192
193 test("StateSelectionField for list view with hide_label option", async () => {
194 onRpc("has_group", () => true);
195 Partner._fields.graph_type = fields.Selection({
196 type: "selection",
197 selection: [
198 ["line", "Line"],
199 ["bar", "Bar"],
200 ],
201 });
202 Partner._records[0].graph_type = "bar";
203 Partner._records[1].graph_type = "line";
204
205 await mountView({
206 type: "list",
207 resModel: "partner",
208 arch: /* xml */ `
209 <list>
210 <field name="graph_type" widget="state_selection" options="{'hide_label': True}"/>
211 <field name="selection" widget="state_selection" options="{'hide_label': False}"/>
212 </list>
213 `,
214 });
215
216 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(10, {
217 message: "should have ten status selection widgets",
218 });
219 const selector =
220 ".o_state_selection_cell .o_field_state_selection[name=selection] span.o_status_label";
221 expect(selector).toHaveCount(5, { message: "should have five label on selection widgets" });
222 expect(`${selector}:contains("Done")`).toHaveCount(1, {
223 message: "should have one Done status label",
224 });
225 expect(`${selector}:contains("Normal")`).toHaveCount(3, {
226 message: "should have three Normal status label",
227 });
228
229 expect(
230 ".o_state_selection_cell .o_field_state_selection[name=graph_type] span.o_status"
231 ).toHaveCount(5, { message: "should have five status selection widgets" });
232 expect(
233 ".o_state_selection_cell .o_field_state_selection[name=graph_type] span.o_status_label"
234 ).toHaveCount(0, { message: "should not have status label in selection widgets" });
235 });
236
237 test("StateSelectionField in editable list view", async () => {
238 onRpc("has_group", () => true);
239 await mountView({
240 type: "list",
241 resModel: "partner",
242 arch: /* xml */ `
243 <list editable="bottom">
244 <field name="foo"/>
245 <field name="selection" widget="state_selection"/>
246 </list>
247 `,
248 });
249
250 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(5, {
251 message: "should have five status selection widgets",
252 });
253 expect(
254 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_red"
255 ).toHaveCount(1, { message: "should have one red status" });
256 expect(
257 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_green"
258 ).toHaveCount(1, { message: "should have one green status" });
259 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
260
261 // Click on the status button to make the dropdown appear
262 let cell = queryFirst("tbody td.o_state_selection_cell");
263 await click(".o_state_selection_cell .o_field_state_selection span.o_status");
264 await animationFrame();
265 expect(cell.parentElement).not.toHaveClass("o_selected_row", {
266 message: "should not be in edit mode since we clicked on the state selection widget",
267 });
268 expect(".o-dropdown--menu").toHaveCount(1, { message: "there should be a dropdown" });
269 expect(".o-dropdown--menu .dropdown-item").toHaveCount(3, {
270 message: "there should be three options in the dropdown",
271 });
272
273 // Click on the first option, "Normal"
274 await click(".o-dropdown--menu .dropdown-item");
275 await animationFrame();
276 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(5, {
277 message: "should still have five status selection widgets",
278 });
279 expect(
280 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_red"
281 ).toHaveCount(0, { message: "should now have no red status" });
282 expect(
283 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_green"
284 ).toHaveCount(1, { message: "should still have one green status" });
285 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
286 expect("tr.o_selected_row").toHaveCount(0, { message: "should not be in edit mode" });
287
288 // switch to edit mode and check the result
289 cell = queryFirst("tbody td.o_state_selection_cell");
290 await click(cell);
291 await animationFrame();
292 expect(cell.parentElement).toHaveClass("o_selected_row", {
293 message: "should now be in edit mode",
294 });
295 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(5, {
296 message: "should still have five status selection widgets",
297 });
298 expect(
299 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_red"
300 ).toHaveCount(0, { message: "should now have no red status" });
301 expect(
302 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_green"
303 ).toHaveCount(1, { message: "should still have one green status" });
304 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
305
306 // Click on the third status button to make the dropdown appear
307 await click(".o_state_selection_cell .o_field_state_selection span.o_status:eq(2)");
308 await animationFrame();
309 expect(".o-dropdown--menu").toHaveCount(1, "there should be a dropdown".msg);
310 expect(".o-dropdown--menu .dropdown-item").toHaveCount(3, {
311 message: "there should be three options in the dropdown",
312 });
313
314 // Click on the last option, "Done"
315 await click(".o-dropdown--menu .dropdown-item:last-child");
316 await animationFrame();
317 expect(".o-dropdown--menu").toHaveCount(0, {
318 message: "there should not be a dropdown anymore",
319 });
320 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(5, {
321 message: "should still have five status selection widgets",
322 });
323 expect(
324 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_red"
325 ).toHaveCount(0, { message: "should still have no red status" });
326 expect(
327 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_green"
328 ).toHaveCount(2, { message: "should now have two green status" });
329 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
330
331 // save
332 await click(".o_control_panel_main_buttons .o_list_button_save");
333 await animationFrame();
334 expect(".o_state_selection_cell .o_field_state_selection span.o_status").toHaveCount(5, {
335 message: "should have five status selection widgets",
336 });
337 expect(
338 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_red"
339 ).toHaveCount(0, { message: "should have no red status" });
340 expect(
341 ".o_state_selection_cell .o_field_state_selection span.o_status.o_status_green"
342 ).toHaveCount(2, { message: "should have two green status" });
343 expect(".o-dropdown--menu").toHaveCount(0, { message: "there should not be a dropdown" });
344 });
345
346 test.tags("desktop");
347 test("StateSelectionField line stay in edit mode when StateSelectionField is opened", async () => {
348 await mountView({
349 type: "list",
350 resModel: "partner",
351 arch: /* xml */ `
352 <list editable="bottom">
353 <field name="foo"/>
354 <field name="selection" widget="state_selection"/>
355 </list>
356 `,
357 });
358
359 // Click on the status button to make the dropdown appear
360 await click(".o_state_selection_cell .o_field_state_selection span.o_status");
361 await animationFrame();
362 expect(".o-dropdown--menu").toHaveCount(1, { message: "there should be a dropdown" });
363 expect(".o-dropdown--menu .dropdown-item").toHaveCount(3, {
364 message: "there should be three options in the dropdown",
365 });
366
367 // Click on another row
368 const lastCell = queryAll("tbody td.o_state_selection_cell")[4];
369 await click(lastCell);
370 await animationFrame();
371 expect(".o-dropdown--menu").toHaveCount(0, {
372 message: "there should not be a dropdown anymore",
373 });
374 const firstCell = queryFirst("tbody td.o_state_selection_cell");
375 expect(firstCell.parentElement).not.toHaveClass("o_selected_row", {
376 message: "first row should not be in edit mode anymore",
377 });
378 expect(lastCell.parentElement).toHaveClass("o_selected_row", {
379 message: "last row should be in edit mode",
380 });
381 });
382
383 test('StateSelectionField edited by the smart actions "Set kanban state as <state name>"', async () => {
384 await mountView({
385 type: "form",
386 resModel: "partner",
387 arch: /* xml */ `
388 <form>
389 <field name="selection" widget="state_selection"/>
390 </form>
391 `,
392 resId: 1,
393 });
394
395 expect(".o_status_red").toHaveCount(1);
396 await press(["control", "k"]);
397 await animationFrame();
398 expect(`.o_command:contains("Set kanban state as Normal ALT + D")`).toHaveCount(1);
399 const doneItem = `.o_command:contains("Set kanban state as Done ALT + G")`;
400 expect(doneItem).toHaveCount(1);
401
402 await click(doneItem);
403 await animationFrame();
404 expect(".o_status_green").toHaveCount(1);
405
406 await press(["control", "k"]);
407 await animationFrame();
408 expect(`.o_command:contains("Set kanban state as Normal ALT + D")`).toHaveCount(1);
409 expect(`.o_command:contains("Set kanban state as Blocked ALT + F")`).toHaveCount(1);
410 expect(`.o_command:contains("Set kanban state as Done ALT + G")`).toHaveCount(0);
411 });
412
413 test("StateSelectionField uses legend_* fields", async () => {
414 Partner._fields.legend_normal = fields.Char();
415 Partner._fields.legend_blocked = fields.Char();
416 Partner._fields.legend_done = fields.Char();
417 Partner._records[0].legend_normal = "Custom normal";
418 Partner._records[0].legend_blocked = "Custom blocked";
419 Partner._records[0].legend_done = "Custom done";
420
421 await mountView({
422 type: "form",
423 resModel: "partner",
424 arch: /* xml */ `
425 <form>
426 <sheet>
427 <group>
428 <field name="legend_normal" invisible="1" />
429 <field name="legend_blocked" invisible="1" />
430 <field name="legend_done" invisible="1" />
431 <field name="selection" widget="state_selection"/>
432 </group>
433 </sheet>
434 </form>
435 `,
436 resId: 1,
437 });
438
439 await click(".o_status");
440 await animationFrame();
441 expect(queryAllTexts(".o-dropdown--menu .dropdown-item")).toEqual([
442 "Custom normal",
443 "Custom blocked",
444 "Custom done",
445 ]);
446
447 await click(".dropdown-item .o_status");
448 await animationFrame();
449 await click(".o_status");
450 await animationFrame();
451 expect(queryAllTexts(".o-dropdown--menu .dropdown-item")).toEqual([
452 "Custom normal",
453 "Custom blocked",
454 "Custom done",
455 ]);
456 });
457
458 test("works when required in a readonly view", async () => {
459 Partner._records[0].selection = "normal";
460 Partner._records = [Partner._records[0]];
461 onRpc("web_save", ({ method }) => expect.step(method));
462 await mountView({
463 type: "kanban",
464 resModel: "partner",
465 arch: /* xml */ `
466 <kanban>
467 <templates>
468 <t t-name="card">
469 <field name="selection" widget="state_selection" required="1"/>
470 </t>
471 </templates>
472 </kanban>
473 `,
474 });
475 expect(".o_status_label").toHaveCount(0);
476 await click(".o_field_state_selection button");
477 await animationFrame();
478 await click(".dropdown-item:eq(2)");
479 await animationFrame();
480 expect.verifySteps(["web_save"]);
481 expect(".o_field_state_selection span").toHaveClass("o_status_green");
482 });
483
484 test("StateSelectionField - auto save record when field toggled", async () => {
485 onRpc("web_save", ({ method }) => expect.step(method));
486 await mountView({
487 type: "form",
488 resModel: "partner",
489 arch: /* xml */ `
490 <form>
491 <sheet>
492 <group>
493 <field name="selection" widget="state_selection"/>
494 </group>
495 </sheet>
496 </form>
497 `,
498 resId: 1,
499 });
500
501 await click(".o_field_widget.o_field_state_selection .o_status");
502 await animationFrame();
503 await click(".dropdown-menu .dropdown-item:last-child");
504 await animationFrame();
505 expect.verifySteps(["web_save"]);
506 });
507
508 test("StateSelectionField - prevent auto save with autosave option", async () => {
509 onRpc("write", ({ method }) => expect.step(method));
510 await mountView({
511 type: "form",
512 resModel: "partner",
513 arch: /* xml */ `
514 <form>
515 <sheet>
516 <group>
517 <field name="selection" widget="state_selection" options="{'autosave': False}"/>
518 </group>
519 </sheet>
520 </form>
521 `,
522 resId: 1,
523 });
524
525 await click(".o_field_widget.o_field_state_selection .o_status");
526 await animationFrame();
527 await click(".dropdown-menu .dropdown-item:last-child");
528 await animationFrame();
529 expect.verifySteps([]);
530 });
531
532 test("StateSelectionField - hotkey handling when there are more than 3 options available", async () => {
533 Partner._fields.selection = fields.Selection({
534 string: "Selection",
535 selection: [
536 ["normal", "Normal"],
537 ["blocked", "Blocked"],
538 ["done", "Done"],
539 ["martin", "Martin"],
540 ["martine", "Martine"],
541 ],
542 });
543 Partner._records[0].selection = null;
544
545 await mountView({
546 type: "form",
547 resModel: "partner",
548 arch: /* xml */ `
549 <form>
550 <sheet>
551 <group>
552 <field name="selection" widget="state_selection" options="{'autosave': False}"/>
553 </group>
554 </sheet>
555 </form>
556 `,
557 resId: 1,
558 });
559
560 await click(".o_field_widget.o_field_state_selection .o_status");
561 await animationFrame();
562 expect(".dropdown-menu .dropdown-item").toHaveCount(5, {
563 message: "Five choices are displayed",
564 });
565 await press(["control", "k"]);
566 await animationFrame();
567
568 expect(".o_command#o_command_2").toHaveText("Set kanban state as Done ALT + G", {
569 inline: true,
570 message: "hotkey and command are present",
571 });
572 expect(".o_command#o_command_4").toHaveText("Set kanban state as Martine", {
573 message: "no hotkey is present, but the command exists",
574 });
575
576 await click(".o_command#o_command_2");
577 await animationFrame();
578 expect(".o_field_state_selection .o_status").toHaveClass("o_status_green", {
579 message: "green color and Done state have been set",
580 });
581 });