Debian Codegraph @

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

raw
1 import { expect, getFixture, test } from "@odoo/hoot";
2 import { queryAllTexts } from "@odoo/hoot-dom";
3 import {
4 clickSave,
5 contains,
6 defineModels,
7 fieldInput,
8 fields,
9 models,
10 mountView,
11 onRpc,
12 } from "@web/../tests/web_test_helpers";
13
14 class Product extends models.Model {
15 price = fields.Integer();
16 }
17
18 defineModels([Product]);
19
20 test("human readable format 1", async () => {
21 Product._records = [{ id: 1, price: 3.756754e6 }];
22 await mountView({
23 type: "form",
24 resModel: "product",
25 resId: 1,
26 arch: `<form><field name="price" options="{'human_readable': 'true'}"/></form>`,
27 });
28 expect(".o_field_widget input").toHaveValue("4M", {
29 message: "The value should be rendered in human readable format (k, M, G, T)",
30 });
31 });
32
33 test("human readable format 2", async () => {
34 Product._records = [{ id: 1, price: 2.034e3 }];
35 await mountView({
36 type: "form",
37 resModel: "product",
38 resId: 1,
39 arch: `<form><field name="price" options="{'human_readable': 'true', 'decimals': 1}"/></form>`,
40 });
41 expect(".o_field_widget input").toHaveValue("2.0k", {
42 message: "The value should be rendered in human readable format (k, M, G, T)",
43 });
44 });
45
46 test("human readable format 3", async () => {
47 Product._records = [{ id: 1, price: 6.67543577586e12 }];
48 await mountView({
49 type: "form",
50 resModel: "product",
51 resId: 1,
52 arch: `<form><field name="price" options="{'human_readable': 'true', 'decimals': 4}"/></form>`,
53 });
54 expect(".o_field_widget input").toHaveValue("6.6754T", {
55 message: "The value should be rendered in human readable format (k, M, G, T)",
56 });
57 });
58
59 test("still human readable when readonly", async () => {
60 Product._records = [{ id: 1, price: 6.67543577586e12 }];
61 await mountView({
62 type: "form",
63 resModel: "product",
64 resId: 1,
65 arch: `<form><field name="price" readonly="true" options="{'human_readable': 'true', 'decimals': 4}"/></form>`,
66 });
67 expect(".o_field_widget span").toHaveText("6.6754T");
68 });
69
70 test("should be 0 when unset", async () => {
71 Product._records = [{ id: 1 }];
72 await mountView({
73 type: "form",
74 resModel: "product",
75 resId: 1,
76 arch: '<form><field name="price"/></form>',
77 });
78 expect(".o_field_widget input").not.toHaveClass("o_field_empty");
79 expect(".o_field_widget input").toHaveValue("0");
80 });
81
82 test("basic form view flow", async () => {
83 Product._records = [{ id: 1, price: 10 }];
84 await mountView({
85 type: "form",
86 resModel: "product",
87 resId: 1,
88 arch: '<form><field name="price"/></form>',
89 });
90
91 expect(".o_field_widget input").toHaveValue("10");
92 await fieldInput("price").edit("30");
93 expect(".o_field_widget input").toHaveValue("30");
94 await clickSave();
95 expect(".o_field_widget input").toHaveValue("30");
96 });
97
98 test("no need to focus out of the input to save the record after correcting an invalid input", async () => {
99 Product._records = [{ id: 1, price: 10 }];
100 await mountView({
101 type: "form",
102 resModel: "product",
103 resId: 1,
104 arch: '<form><field name="price"/></form>',
105 });
106
107 expect(".o_field_widget input").toHaveValue("10");
108 await fieldInput("price").edit("a");
109 expect(".o_field_widget input").toHaveValue("a");
110 expect(".o_form_status_indicator span i.fa-warning").toHaveCount(1);
111 expect(".o_form_button_save[disabled]").toHaveCount(1);
112 await fieldInput("price").edit("1");
113 expect(".o_field_widget input").toHaveValue("1");
114 expect(".o_form_status_indicator span i.fa-warning").toHaveCount(0);
115 expect(".o_form_button_save[disabled]").toHaveCount(0);
116 await clickSave(); // makes sure there is an enabled save button
117 });
118
119 test("rounded when using formula in form view", async () => {
120 Product._records = [{ id: 1, price: 10 }];
121 await mountView({
122 type: "form",
123 resModel: "product",
124 resId: 1,
125 arch: '<form><field name="price"/></form>',
126 });
127 await fieldInput("price").edit("=100/3");
128 expect(".o_field_widget input").toHaveValue("33");
129 });
130
131 test("with input type 'number' option", async () => {
132 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
133 Product._records = [{ id: 1, price: 10 }];
134 await mountView({
135 type: "form",
136 resModel: "product",
137 resId: 1,
138 arch: `<form><field name="price" options="{'type': 'number'}"/></form>`,
139 });
140 expect(".o_field_widget input").toHaveAttribute("type", "number");
141 await fieldInput("price").edit("1234567890");
142 expect(".o_field_widget input").toHaveValue(1234567890, {
143 message: "Integer value must be not formatted if input type is number",
144 });
145 });
146
147 test("with 'step' option", async () => {
148 Product._records = [{ id: 1, price: 10 }];
149 await mountView({
150 type: "form",
151 resModel: "product",
152 resId: 1,
153 arch: `<form><field name="price" options="{'type': 'number', 'step': 3}"/></form>`,
154 });
155 expect(".o_field_widget input").toHaveAttribute("step", "3");
156 });
157
158 test("with 'min'/'max' option", async () => {
159 Product._records = [{ id: 1, price: 10 }];
160 await mountView({
161 type: "form",
162 resModel: "product",
163 resId: 1,
164 arch: `<form><field name="price" options="{'type': 'number', 'min': 3, 'max': 10}"/></form>`,
165 });
166 expect(".o_field_widget input").toHaveAttribute("min", "3");
167 expect(".o_field_widget input").toHaveAttribute("max", "10");
168 });
169
170 test("without input type option", async () => {
171 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
172 Product._records = [{ id: 1, price: 10 }];
173 await mountView({
174 type: "form",
175 resModel: "product",
176 resId: 1,
177 arch: '<form><field name="price"/></form>',
178 });
179
180 expect(".o_field_widget input").toHaveAttribute("type", "text");
181 await fieldInput("price").edit("1234567890");
182 expect(".o_field_widget input").toHaveValue("1,234,567,890");
183 });
184
185 test("is formatted by default", async () => {
186 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
187 Product._records = [{ id: 1, price: 8069 }];
188 await mountView({
189 type: "form",
190 resModel: "product",
191 resId: 1,
192 arch: `<form><field name="price" options="{'enable_formatting': 'false'}"/></form>`,
193 });
194 expect(".o_field_widget input").toHaveValue("8,069");
195 });
196
197 test("basic flow in editable list view", async () => {
198 Product._records = [{ id: 1 }, { id: 2, price: 10 }];
199 onRpc("has_group", () => true);
200 await mountView({
201 type: "list",
202 resModel: "product",
203 arch: '<list editable="bottom"><field name="price"/></list>',
204 });
205 const zeroValues = queryAllTexts("td").filter((text) => text === "0");
206 expect(zeroValues).toHaveLength(1, {
207 message: "Unset integer values should not be rendered as zeros",
208 });
209 await contains("td.o_data_cell").click();
210 expect('.o_field_widget[name="price"] input').toHaveCount(1);
211 await contains('.o_field_widget[name="price"] input').edit("-28");
212 expect("td.o_data_cell:first").toHaveText("-28");
213 expect('.o_field_widget[name="price"] input').toHaveValue("10");
214 await contains(getFixture()).click();
215 expect(queryAllTexts("td.o_data_cell")).toEqual(["-28", "10"]);
216 });
217
218 test("with enable_formatting option as false", async () => {
219 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
220 Product._records = [{ id: 1, price: 8069 }];
221 await mountView({
222 type: "form",
223 resModel: "product",
224 resId: 1,
225 arch: `<form><field name="price" options="{'enable_formatting': false}"/></form>`,
226 });
227 expect(".o_field_widget input").toHaveValue("8069");
228 await fieldInput("price").edit("1234567890");
229 expect(".o_field_widget input").toHaveValue("1234567890");
230 });
231
232 test("value is formatted on Enter", async () => {
233 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
234 await mountView({
235 type: "form",
236 resModel: "product",
237 arch: '<form><field name="price"/></form>',
238 });
239
240 expect(".o_field_widget input").toHaveValue("0");
241
242 await fieldInput("price").edit("1000", { confirm: "Enter" });
243 expect(".o_field_widget input").toHaveValue("1,000");
244 });
245
246 test("value is formatted on Enter (even if same value)", async () => {
247 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
248 Product._records = [{ id: 1, price: 8069 }];
249
250 await mountView({
251 type: "form",
252 resModel: "product",
253 resId: 1,
254 arch: '<form><field name="price"/></form>',
255 });
256
257 expect(".o_field_widget input").toHaveValue("8,069");
258
259 await fieldInput("price").edit("8069", { confirm: "Enter" });
260 expect(".o_field_widget input").toHaveValue("8,069");
261 });
262
263 test("value is formatted on click out (even if same value)", async () => {
264 // `localization > grouping` required for this test is [3, 0], which is the default in mock server
265 Product._records = [{ id: 1, price: 8069 }];
266
267 await mountView({
268 type: "form",
269 resModel: "product",
270 resId: 1,
271 arch: '<form><field name="price"/></form>',
272 });
273
274 expect(".o_field_widget input").toHaveValue("8,069");
275
276 await fieldInput("price").edit("8069", { confirm: false });
277 expect(".o_field_widget input").toHaveValue("8069");
278
279 await contains(".o_control_panel").click();
280 expect(".o_field_widget input").toHaveValue("8,069");
281 });
282
283 test("Value should not be a boolean when enable_formatting is false", async () => {
284 onRpc("has_group", () => true);
285 await mountView({
286 type: "list",
287 resModel: "product",
288 arch: `
289 <list editable="bottom">
290 <field name="id" options="{'enable_formatting': false}"/>
291 <field name="price"/>
292 </list>
293 `,
294 });
295 await contains(`.o_list_button_add`).click();
296 expect(".o_selected_row .o_field_integer").toHaveText("");
297 });