Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import {
3 clickSave,
4 contains,
5 defineModels,
6 fields,
7 MockServer,
8 models,
9 mountView,
10 onRpc,
11 } from "@web/../tests/web_test_helpers";
12
13 class Partner extends models.Model {
14 _name = "res.partner";
15
16 product_id = fields.Many2one({ relation: "product" });
17 color = fields.Selection({
18 selection: [
19 ["red", "Red"],
20 ["black", "Black"],
21 ],
22 default: "red",
23 });
24 product_color_id = fields.Integer({
25 relation: "product",
26 related: "product_id.color",
27 default: 20,
28 });
29
30 _records = [{ id: 1 }, { id: 2, product_id: 37, product_color_id: 6 }];
31 }
32
33 class Product extends models.Model {
34 _rec_name = "display_name";
35
36 name = fields.Char("name");
37 color = fields.Integer("color");
38
39 _records = [
40 { id: 37, display_name: "xphone", name: "xphone", color: 6 },
41 { id: 41, display_name: "xpad", name: "xpad", color: 7 },
42 ];
43 }
44
45 defineModels([Partner, Product]);
46
47 onRpc("has_group", () => true);
48
49 test("BadgeSelectionField widget on a many2one in a new record", async () => {
50 onRpc("web_save", ({ args }) => {
51 expect.step(`saved product_id: ${args[1]["product_id"]}`);
52 });
53
54 await mountView({
55 resModel: "res.partner",
56 type: "form",
57 arch: `<form><field name="product_id" widget="selection_badge"/></form>`,
58 });
59
60 expect(`div.o_field_selection_badge`).toHaveCount(1, {
61 message: "should have rendered outer div",
62 });
63 expect(`span.o_selection_badge`).toHaveCount(2, { message: "should have 2 possible choices" });
64 expect(`span.o_selection_badge:contains(xphone)`).toHaveCount(1, {
65 message: "one of them should be xphone",
66 });
67 expect(`span.active`).toHaveCount(0, { message: "none of the input should be checked" });
68
69 await contains(`span.o_selection_badge`).click();
70 expect(`span.active`).toHaveCount(1, { message: "one of the input should be checked" });
71
72 await clickSave();
73 expect.verifySteps(["saved product_id: 37"]);
74 });
75
76 test("BadgeSelectionField widget on a selection in a new record", async () => {
77 onRpc("web_save", ({ args }) => {
78 expect.step(`saved color: ${args[1]["color"]}`);
79 });
80 await mountView({
81 resModel: "res.partner",
82 type: "form",
83 arch: `<form><field name="color" widget="selection_badge"/></form>`,
84 });
85
86 expect(`div.o_field_selection_badge`).toHaveCount(1, {
87 message: "should have rendered outer div",
88 });
89 expect("span.o_selection_badge").toHaveCount(2, { message: "should have 2 possible choices" });
90 expect(`span.o_selection_badge:contains(Red)`).toHaveCount(1, {
91 message: "one of them should be Red",
92 });
93
94 await contains(`span.o_selection_badge:last`).click();
95 await clickSave();
96 expect.verifySteps(["saved color: black"]);
97 });
98
99 test("BadgeSelectionField widget on a selection in a readonly mode", async () => {
100 await mountView({
101 resModel: "res.partner",
102 type: "form",
103 arch: `<form><field name="color" widget="selection_badge" readonly="1"/></form>`,
104 });
105 expect(`div.o_readonly_modifier span`).toHaveCount(1, {
106 message: "should have 1 possible value in readonly mode",
107 });
108 });
109
110 test("BadgeSelectionField widget on a selection unchecking selected value", async () => {
111 onRpc("res.partner", "web_save", ({ args }) => {
112 expect.step("web_save");
113 expect(args[1]).toEqual({ color: false });
114 });
115 await mountView({
116 type: "form",
117 resModel: "res.partner",
118 arch: '<form><field name="color" widget="selection_badge"/></form>',
119 });
120
121 expect("div.o_field_selection_badge").toHaveCount(1, {
122 message: "should have rendered outer div",
123 });
124 expect("span.o_selection_badge").toHaveCount(2, { message: "should have 2 possible choices" });
125 expect("span.o_selection_badge.active").toHaveCount(1, { message: "one is active" });
126 expect("span.o_selection_badge.active").toHaveText("Red", {
127 message: "the active one should be Red",
128 });
129
130 // click again on red option and save to update the server data
131 await contains("span.o_selection_badge.active").click();
132 expect.verifySteps([]);
133 await contains(".o_form_button_save").click();
134 expect.verifySteps(["web_save"]);
135
136 expect(MockServer.env["res.partner"].at(-1).color).toBe(false, {
137 message: "the new value should be false as we have selected same value as default",
138 });
139 });
140
141 test("BadgeSelectionField widget on a selection unchecking selected value (required field)", async () => {
142 Partner._fields.color.required = true;
143 onRpc("res.partner", "web_save", ({ args }) => {
144 expect.step("web_save");
145 expect(args[1]).toEqual({ color: "red" });
146 });
147 await mountView({
148 type: "form",
149 resModel: "res.partner",
150 arch: '<form><field name="color" widget="selection_badge"/></form>',
151 });
152
153 expect("div.o_field_selection_badge").toHaveCount(1, {
154 message: "should have rendered outer div",
155 });
156 expect("span.o_selection_badge").toHaveCount(2, { message: "should have 2 possible choices" });
157 expect("span.o_selection_badge.active").toHaveCount(1, { message: "one is active" });
158 expect("span.o_selection_badge.active").toHaveText("Red", {
159 message: "the active one should be Red",
160 });
161
162 // click again on red option and save to update the server data
163 await contains("span.o_selection_badge.active").click();
164 expect.verifySteps([]);
165 await contains(".o_form_button_save").click();
166 expect.verifySteps(["web_save"]);
167
168 expect(MockServer.env["res.partner"].at(-1).color).toBe("red", {
169 message: "the new value should be red",
170 });
171 });
172
173 test("BadgeSelectionField widget in list with the color_field option", async () => {
174 await mountView({
175 resModel: "res.partner",
176 type: "list",
177 arch: `
178 <list editable="top">
179 <field name="product_color_id" invisible="1"/>
180 <field name="product_id" widget="selection_badge" options="{'color_field': 'product_color_id'}"/>
181 </list>
182 `,
183 });
184
185
186 // Ensure that the correct o_badge_color is used.
187 expect(`.o_field_selection_badge[name="product_id"] .o_badge_color_6`).toHaveCount(1);
188 expect(`.o_field_selection_badge[name="product_id"] .o_badge_color_7`).toHaveCount(0);
189 expect(`div.o_field_selection_badge span:contains(xphone)`).toHaveCount(1);
190 expect(`div.o_field_selection_badge span:contains(xpad)`).toHaveCount(0);
191
192 // Open the M2O selection.
193 await contains(`.o_field_selection_badge[name="product_id"] .o_badge_color_6`).click();
194
195 // Ensure that the 'badge' display is used.
196 expect("span.btn-secondary.badge").toHaveCount(2);
197 expect(`span.btn-secondary.active:contains(xphone)`).toHaveCount(1);
198 expect(`span.btn-secondary.active:contains(xpad)`).toHaveCount(0);
199
200 // Select the second product.
201 await contains(`span.btn-secondary:contains(xpad)`).click();
202
203 expect(`span.btn-secondary.active:contains(xphone)`).toHaveCount(0);
204 expect(`span.btn-secondary.active:contains(xpad)`).toHaveCount(1);
205
206 // Save changes.
207 await contains(".o_list_button_save").click();
208
209 expect(`.o_field_selection_badge[name="product_id"] .o_badge_color_6`).toHaveCount(0);
210 expect(`.o_field_selection_badge[name="product_id"] .o_badge_color_7`).toHaveCount(1);
211 expect(`div.o_field_selection_badge span:contains(xphone)`).toHaveCount(0);
212 expect(`div.o_field_selection_badge span:contains(xpad)`).toHaveCount(1);
213 });
214
215 test("BadgeSelectionField widget in list without the color_field option", async () => {
216 await mountView({
217 resModel: "res.partner",
218 type: "list",
219 arch: `
220 <list editable="top">
221 <field name="id"/>
222 <field name="product_id" widget="selection_badge"/>
223 </list>
224 `,
225 });
226
227 // Ensure that the 'btn btn-secondary' display is used instead of the 'o_badge_color' one.
228 expect(`div.o_field_selection_badge span.btn-secondary`).toHaveCount(1);
229 expect(`div.o_field_selection_badge span.btn-secondary:contains(xphone)`).toHaveCount(1);
230 expect(`div.o_field_selection_badge span.btn-secondary:contains(xpad)`).toHaveCount(0);
231
232 // Open the M2O selection.
233 await contains(`div.o_field_selection_badge span:contains(xphone)`).click();
234
235 // Ensure that the 'badge' display is used.
236 expect("span.btn-secondary.badge").toHaveCount(2);
237 expect(`span.btn-secondary.active:contains(xphone)`).toHaveCount(1);
238 expect(`span.btn-secondary.active:contains(xpad)`).toHaveCount(0);
239
240 // Select the second product.
241 await contains(`span.btn-secondary:contains(xpad)`).click();
242
243 expect(`span.btn-secondary.active:contains(xphone)`).toHaveCount(0);
244 expect(`span.btn-secondary.active:contains(xpad)`).toHaveCount(1);
245
246 // Save changes.
247 await contains(".o_list_button_save").click();
248
249 expect(`div.o_field_selection_badge span.btn-secondary`).toHaveCount(1);
250 expect(`div.o_field_selection_badge span.btn-secondary:contains(xphone)`).toHaveCount(0);
251 expect(`div.o_field_selection_badge span.btn-secondary:contains(xpad)`).toHaveCount(1);
252 });