Debian Codegraph @

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

raw
1 import { expect, test } from "@odoo/hoot";
2 import { click, edit, queryOne, queryText, queryValue } from "@odoo/hoot-dom";
3 import { animationFrame } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 defineModels,
7 defineParams,
8 fields,
9 models,
10 mountView,
11 onRpc,
12 } from "@web/../tests/web_test_helpers";
13
14 class Partner extends models.Model {
15 name = fields.Char({ string: "Display Name" });
16 int_field = fields.Integer({
17 string: "int_field",
18 });
19 int_field2 = fields.Integer({
20 string: "int_field",
21 });
22 int_field3 = fields.Integer({
23 string: "int_field",
24 });
25 float_field = fields.Float({
26 string: "Float_field",
27 digits: [16, 1],
28 });
29 _records = [
30 {
31 int_field: 10,
32 float_field: 0.44444,
33 },
34 ];
35 }
36 class User extends models.Model {
37 _name = "res.users";
38 has_group() {
39 return true;
40 }
41 }
42 defineModels([Partner, User]);
43
44 test("ProgressBarField: max_value should update", async () => {
45 expect.assertions(3);
46 Partner._records[0].float_field = 2;
47 Partner._onChanges.name = (record) => {
48 record.int_field = 999;
49 record.float_field = 5;
50 };
51
52 onRpc("web_save", ({ args }) => {
53 expect(args[1]).toEqual(
54 { int_field: 999, float_field: 5, name: "new name" },
55 { message: "New value of progress bar saved" }
56 );
57 });
58 await mountView({
59 type: "form",
60 resModel: "partner",
61 arch: /* xml */ `
62 <form>
63 <field name="name" />
64 <field name="float_field" invisible="1" />
65 <field name="int_field" widget="progressbar" options="{'current_value': 'int_field', 'max_value': 'float_field'}" />
66 </form>`,
67 resId: 1,
68 });
69
70 expect(".o_progressbar").toHaveText("10\n/\n2");
71 await click(".o_field_widget[name=name] input");
72 await edit("new name", { confirm: "enter" });
73 await clickSave();
74 await animationFrame();
75 expect(".o_progressbar").toHaveText("999\n/\n5");
76 });
77
78 test("ProgressBarField: value should update in edit mode when typing in input", async () => {
79 expect.assertions(4);
80 Partner._records[0].int_field = 99;
81 onRpc("web_save", ({ args }) => {
82 expect(args[1].int_field).toBe(69);
83 });
84 await mountView({
85 type: "form",
86 resModel: "partner",
87 arch: /* xml */ `
88 <form>
89 <field name="int_field" widget="progressbar" options="{'editable': true}"/>
90 </form>`,
91 resId: 1,
92 });
93
94 expect(queryValue(".o_progressbar_value .o_input") + queryText(".o_progressbar")).toBe("99%", {
95 message: "Initial value should be correct",
96 });
97 await click(".o_progressbar_value .o_input");
98 // wait for apply dom change
99 await animationFrame();
100 await edit("69", { confirm: "enter" });
101 expect(".o_progressbar_value .o_input").toHaveValue("69", {
102 message: "New value should be different after focusing out of the field",
103 });
104 // wait for apply dom change
105 await animationFrame();
106 await clickSave();
107 // wait for rpc
108 await animationFrame();
109 expect(".o_progressbar_value .o_input").toHaveValue("69", {
110 message: "New value is still displayed after save",
111 });
112 });
113
114 test("ProgressBarField: value should update in edit mode when typing in input with field max value", async () => {
115 expect.assertions(4);
116 Partner._records[0].int_field = 99;
117
118 onRpc("web_save", ({ args }) => {
119 expect(args[1].int_field).toBe(69);
120 });
121
122 await mountView({
123 type: "form",
124 resModel: "partner",
125 arch: /* xml */ `
126 <form>
127 <field name="float_field" invisible="1" />
128 <field name="int_field" widget="progressbar" options="{'editable': true, 'max_value': 'float_field'}" />
129 </form>`,
130 resId: 1,
131 });
132 expect(".o_form_view .o_form_editable").toHaveCount(1, { message: "Form in edit mode" });
133 expect(queryValue(".o_progressbar_value .o_input") + queryText(".o_progressbar")).toBe(
134 "99/\n0",
135 { message: "Initial value should be correct" }
136 );
137
138 await click(".o_progressbar_value .o_input");
139 await animationFrame();
140 await edit("69", { confirm: "enter" });
141 await animationFrame();
142 await clickSave();
143 await animationFrame();
144 expect(queryValue(".o_progressbar_value .o_input") + queryText(".o_progressbar")).toBe(
145 "69/\n0",
146 { message: "New value should be different than initial after click" }
147 );
148 });
149
150 test("ProgressBarField: max value should update in edit mode when typing in input with field max value", async () => {
151 expect.assertions(5);
152 Partner._records[0].int_field = 99;
153
154 onRpc("web_save", ({ args }) => {
155 expect(args[1].float_field).toBe(69);
156 });
157 await mountView({
158 type: "form",
159 resModel: "partner",
160 arch: /* xml */ `
161 <form>
162 <field name="float_field" invisible="1" />
163 <field name="int_field" widget="progressbar" options="{'editable': true, 'max_value': 'float_field', 'edit_max_value': true}" />
164 </form>`,
165 resId: 1,
166 });
167
168 expect(queryText(".o_progressbar") + queryValue(".o_progressbar_value .o_input")).toBe(
169 "99\n/0",
170 { message: "Initial value should be correct" }
171 );
172 expect(".o_form_view .o_form_editable").toHaveCount(1, { message: "Form in edit mode" });
173 queryOne(".o_progressbar input").focus();
174 await animationFrame();
175
176 expect(queryText(".o_progressbar") + queryValue(".o_progressbar_value .o_input")).toBe(
177 "99\n/0.44",
178 { message: "Initial value is not formatted when focused" }
179 );
180
181 await click(".o_progressbar_value .o_input");
182 await edit("69", { confirm: "enter" });
183 await clickSave();
184
185 expect(queryText(".o_progressbar") + queryValue(".o_progressbar_value .o_input")).toBe(
186 "99\n/69",
187 { message: "New value should be different than initial after click" }
188 );
189 });
190
191 test("ProgressBarField: Standard readonly mode is readonly", async () => {
192 Partner._records[0].int_field = 99;
193
194 onRpc(({ method }) => expect.step(method));
195 await mountView({
196 type: "form",
197 resModel: "partner",
198 arch: /* xml */ `
199 <form edit="0">
200 <field name="float_field" invisible="1"/>
201 <field name="int_field" widget="progressbar" options="{'editable': true, 'max_value': 'float_field', 'edit_max_value': true}"/>
202 </form>`,
203 resId: 1,
204 });
205
206 expect(".o_progressbar").toHaveText("99\n/\n0", {
207 message: "Initial value should be correct",
208 });
209
210 await click(".o_progress");
211 await animationFrame();
212
213 expect(".o_progressbar_value .o_input").toHaveCount(0, {
214 message: "no input in readonly mode",
215 });
216 expect.verifySteps(["get_views", "web_read"]);
217 });
218
219 test("ProgressBarField: field is editable in kanban", async () => {
220 expect.assertions(7);
221 Partner._records[0].int_field = 99;
222
223 onRpc("web_save", ({ args }) => {
224 expect(args[1].int_field).toBe(69);
225 });
226 await mountView({
227 type: "kanban",
228 resModel: "partner",
229 arch: /* xml */ `
230 <kanban>
231 <templates>
232 <t t-name="card">
233 <field name="int_field" title="ProgressBarTitle" widget="progressbar" options="{'editable': true, 'max_value': 'float_field'}" />
234 </t>
235 </templates>
236 </kanban>`,
237 resId: 1,
238 });
239
240 expect(".o_progressbar_value .o_input").toHaveValue("99", {
241 message: "Initial input value should be correct",
242 });
243 expect(".o_progressbar_value span").toHaveText("100", {
244 message: "Initial max value should be correct",
245 });
246 expect(".o_progressbar_title").toHaveText("ProgressBarTitle");
247
248 await click(".o_progressbar_value .o_input");
249 await edit("69", { confirm: "enter" });
250 await animationFrame();
251
252 expect(".o_progressbar_value .o_input").toHaveValue("69");
253 expect(".o_progressbar_value span").toHaveText("100", {
254 message: "Max value is still the same be correct",
255 });
256 expect(".o_progressbar_title").toHaveText("ProgressBarTitle");
257 });
258
259 test("force readonly in kanban", async (assert) => {
260 expect.assertions(2);
261 Partner._records[0].int_field = 99;
262 onRpc("web_save", () => {
263 throw new Error("Not supposed to write");
264 });
265 await mountView({
266 type: "kanban",
267 resModel: "partner",
268 arch: /* xml */ `
269 <kanban>
270 <templates>
271 <t t-name="card">
272 <field name="int_field" widget="progressbar" options="{'editable': true, 'max_value': 'float_field', 'readonly': True}" />
273 </t>
274 </templates>
275 </kanban>`,
276 resId: 1,
277 });
278 expect(".o_progressbar").toHaveText("99\n/\n100");
279 expect(".o_progressbar_value .o_input").toHaveCount(0);
280 });
281
282 test("ProgressBarField: readonly and editable attrs/options in kanban", async () => {
283 expect.assertions(4);
284 Partner._records[0].int_field = 29;
285 Partner._records[0].int_field2 = 59;
286 Partner._records[0].int_field3 = 99;
287
288 await mountView({
289 type: "kanban",
290 resModel: "partner",
291 arch: /* xml */ `
292 <kanban>
293 <templates>
294 <t t-name="card">
295 <field name="int_field" readonly="1" widget="progressbar" options="{'max_value': 'float_field'}" />
296 <field name="int_field2" widget="progressbar" options="{'max_value': 'float_field'}" />
297 <field name="int_field3" widget="progressbar" options="{'editable': true, 'max_value': 'float_field'}" />
298 </t>
299 </templates>
300 </kanban>`,
301 resId: 1,
302 });
303
304 expect("[name='int_field'] .o_progressbar_value .o_input").toHaveCount(0, {
305 message: "the field is still in readonly since there is readonly attribute",
306 });
307 expect("[name='int_field2'] .o_progressbar_value .o_input").toHaveCount(0, {
308 message: "the field is still in readonly since there is readonly attribute",
309 });
310 expect("[name='int_field3'] .o_progressbar_value .o_input").toHaveCount(1, {
311 message: "the field is still in readonly since there is readonly attribute",
312 });
313
314 await click(".o_field_progressbar[name='int_field3'] .o_progressbar_value .o_input");
315 await edit("69", { confirm: "enter" });
316 await animationFrame();
317 expect(".o_field_progressbar[name='int_field3'] .o_progressbar_value .o_input").toHaveValue(
318 "69",
319 { message: "New value should be different than initial after click" }
320 );
321 });
322
323 test("ProgressBarField: write float instead of int works, in locale", async () => {
324 expect.assertions(4);
325 Partner._records[0].int_field = 99;
326 defineParams({
327 lang_parameters: {
328 decimal_point: ":",
329 thousands_sep: "#",
330 },
331 });
332 onRpc("web_save", ({ args }) => {
333 expect(args[1].int_field).toBe(1037);
334 });
335 await mountView({
336 type: "form",
337 resModel: "partner",
338 arch: /* xml */ `
339 <form>
340 <field name="int_field" widget="progressbar" options="{'editable': true}"/>
341 </form>`,
342 resId: 1,
343 });
344
345 expect(queryValue(".o_progressbar_value .o_input") + queryText(".o_progressbar")).toBe("99%", {
346 message: "Initial value should be correct",
347 });
348
349 expect(".o_form_view .o_form_editable").toHaveCount(1, { message: "Form in edit mode" });
350
351 await click(".o_field_widget input");
352 await animationFrame();
353 await edit("1#037:9", { confirm: "enter" });
354 await animationFrame();
355 await clickSave();
356 await animationFrame();
357 expect(".o_progressbar_value .o_input").toHaveValue("1k", {
358 message: "New value should be different than initial after click",
359 });
360 });
361
362 test("ProgressBarField: write gibberish instead of int throws warning", async () => {
363 Partner._records[0].int_field = 99;
364
365 await mountView({
366 type: "form",
367 resModel: "partner",
368 arch: /* xml */ `
369 <form>
370 <field name="int_field" widget="progressbar" options="{'editable': true}"/>
371 </form>`,
372 resId: 1,
373 });
374
375 expect(".o_progressbar_value .o_input").toHaveValue("99", {
376 message: "Initial value in input is correct",
377 });
378
379 await click(".o_progressbar_value .o_input");
380 await animationFrame();
381 await edit("trente sept virgule neuf", { confirm: "enter" });
382 await animationFrame();
383 await click(".o_form_button_save");
384 await animationFrame();
385 expect(".o_form_status_indicator span.text-danger").toHaveCount(1, {
386 message: "The form has not been saved",
387 });
388 expect(".o_form_button_save").toHaveProperty("disabled", true, {
389 message: "save button is disabled",
390 });
391 });
392
393 test("ProgressBarField: color is correctly set when value > max value", async () => {
394 Partner._records[0].float_field = 101;
395 await mountView({
396 type: "form",
397 resModel: "partner",
398 arch: /* xml */ `
399 <form>
400 <field name="float_field" widget="progressbar" options="{'overflow_class': 'bg-warning'}"/>
401 </form>`,
402 resId: 1,
403 });
404 expect(".o_progressbar .bg-warning").toHaveCount(1, {
405 message: "As the value has excedded the max value, the color should be set to bg-warning",
406 });
407 });