| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { |
| 3 |
clickSave, |
| 4 |
contains, |
| 5 |
defineModels, |
| 6 |
defineParams, |
| 7 |
fields, |
| 8 |
models, |
| 9 |
mountView, |
| 10 |
onRpc, |
| 11 |
} from "@web/../tests/web_test_helpers"; |
| 12 |
|
| 13 |
import { Component, xml } from "@odoo/owl"; |
| 14 |
import { registry } from "@web/core/registry"; |
| 15 |
|
| 16 |
class Partner extends models.Model { |
| 17 |
float_field = fields.Float({ string: "Float field" }); |
| 18 |
|
| 19 |
_records = [ |
| 20 |
{ id: 1, float_field: 0.36 }, |
| 21 |
{ id: 2, float_field: 0 }, |
| 22 |
{ id: 3, float_field: -3.89859 }, |
| 23 |
{ id: 4, float_field: 0 }, |
| 24 |
{ id: 5, float_field: 9.1 }, |
| 25 |
{ id: 100, float_field: 2.034567e3 }, |
| 26 |
{ id: 101, float_field: 3.75675456e6 }, |
| 27 |
{ id: 102, float_field: 6.67543577586e12 }, |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
defineModels([Partner]); |
| 32 |
|
| 33 |
onRpc("has_group", () => true); |
| 34 |
|
| 35 |
test("human readable format 1", async () => { |
| 36 |
await mountView({ |
| 37 |
type: "form", |
| 38 |
resModel: "partner", |
| 39 |
resId: 101, |
| 40 |
arch: `<form><field name="float_field" options="{'human_readable': 'true'}"/></form>`, |
| 41 |
}); |
| 42 |
expect(".o_field_widget input").toHaveValue("4M", { |
| 43 |
message: "The value should be rendered in human readable format (k, M, G, T).", |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
test("human readable format 2", async () => { |
| 48 |
await mountView({ |
| 49 |
type: "form", |
| 50 |
resModel: "partner", |
| 51 |
resId: 100, |
| 52 |
arch: `<form><field name="float_field" options="{'human_readable': 'true', 'decimals': 1}"/></form>`, |
| 53 |
}); |
| 54 |
expect(".o_field_widget input").toHaveValue("2.0k", { |
| 55 |
message: "The value should be rendered in human readable format (k, M, G, T).", |
| 56 |
}); |
| 57 |
}); |
| 58 |
|
| 59 |
test("human readable format 3", async () => { |
| 60 |
await mountView({ |
| 61 |
type: "form", |
| 62 |
resModel: "partner", |
| 63 |
resId: 102, |
| 64 |
arch: `<form><field name="float_field" options="{'human_readable': 'true', 'decimals': 4}"/></form>`, |
| 65 |
}); |
| 66 |
expect(".o_field_widget input").toHaveValue("6.6754T", { |
| 67 |
message: "The value should be rendered in human readable format (k, M, G, T).", |
| 68 |
}); |
| 69 |
}); |
| 70 |
|
| 71 |
test("still human readable when readonly", async () => { |
| 72 |
await mountView({ |
| 73 |
type: "form", |
| 74 |
resModel: "partner", |
| 75 |
resId: 102, |
| 76 |
arch: `<form><field readonly="true" name="float_field" options="{'human_readable': 'true', 'decimals': 4}"/></form>`, |
| 77 |
}); |
| 78 |
expect(".o_field_widget span").toHaveText("6.6754T", { |
| 79 |
message: "The value should be rendered in human readable format when input is readonly.", |
| 80 |
}); |
| 81 |
}); |
| 82 |
|
| 83 |
test("unset field should be set to 0", async () => { |
| 84 |
await mountView({ |
| 85 |
type: "form", |
| 86 |
resModel: "partner", |
| 87 |
resId: 4, |
| 88 |
arch: '<form><field name="float_field"/></form>', |
| 89 |
}); |
| 90 |
|
| 91 |
expect(".o_field_widget").not.toHaveClass("o_field_empty", { |
| 92 |
message: "Non-set float field should be considered as 0.00", |
| 93 |
}); |
| 94 |
|
| 95 |
expect(".o_field_widget input").toHaveValue("0.00", { |
| 96 |
message: "Non-set float field should be considered as 0.", |
| 97 |
}); |
| 98 |
}); |
| 99 |
|
| 100 |
test("use correct digit precision from field definition", async () => { |
| 101 |
Partner._fields.float_field = fields.Float({ string: "Float field", digits: [0, 1] }); |
| 102 |
|
| 103 |
await mountView({ |
| 104 |
type: "form", |
| 105 |
resModel: "partner", |
| 106 |
resId: 1, |
| 107 |
arch: '<form><field name="float_field"/></form>', |
| 108 |
}); |
| 109 |
|
| 110 |
expect(".o_field_float input").toHaveValue("0.4", { |
| 111 |
message: "should contain a number rounded to 1 decimal", |
| 112 |
}); |
| 113 |
}); |
| 114 |
|
| 115 |
test("use correct digit precision from options", async () => { |
| 116 |
await mountView({ |
| 117 |
type: "form", |
| 118 |
resModel: "partner", |
| 119 |
resId: 1, |
| 120 |
arch: `<form><field name="float_field" options="{ 'digits': [0, 1] }" /></form>`, |
| 121 |
}); |
| 122 |
|
| 123 |
expect(".o_field_float input").toHaveValue("0.4", { |
| 124 |
message: "should contain a number rounded to 1 decimal", |
| 125 |
}); |
| 126 |
}); |
| 127 |
|
| 128 |
test("use correct digit precision from field attrs", async () => { |
| 129 |
await mountView({ |
| 130 |
type: "form", |
| 131 |
resModel: "partner", |
| 132 |
resId: 1, |
| 133 |
arch: '<form><field name="float_field" digits="[0, 1]" /></form>', |
| 134 |
}); |
| 135 |
|
| 136 |
expect(".o_field_float input").toHaveValue("0.4", { |
| 137 |
message: "should contain a number rounded to 1 decimal", |
| 138 |
}); |
| 139 |
}); |
| 140 |
|
| 141 |
test("with 'step' option", async () => { |
| 142 |
await mountView({ |
| 143 |
type: "form", |
| 144 |
resModel: "partner", |
| 145 |
resId: 1, |
| 146 |
arch: `<form><field name="float_field" options="{'type': 'number', 'step': 0.3}"/></form>`, |
| 147 |
}); |
| 148 |
|
| 149 |
expect(".o_field_widget input").toHaveAttribute("step", "0.3", { |
| 150 |
message: 'Integer field with option type must have a step attribute equals to "3".', |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 154 |
test("with 'hide_trailing_zeros' option", async () => { |
| 155 |
await mountView({ |
| 156 |
type: "form", |
| 157 |
resModel: "partner", |
| 158 |
resId: 5, |
| 159 |
arch: `<form><field name="float_field" options="{'hide_trailing_zeros': true}"/></form>`, |
| 160 |
}); |
| 161 |
|
| 162 |
expect(".o_field_widget input").toHaveValue("9.1", { |
| 163 |
message: "Input would show 9.10 without the option", |
| 164 |
}); |
| 165 |
}); |
| 166 |
|
| 167 |
test("basic flow in form view", async () => { |
| 168 |
await mountView({ |
| 169 |
type: "form", |
| 170 |
resModel: "partner", |
| 171 |
resId: 2, |
| 172 |
arch: `<form><field name="float_field" options="{ 'digits': [0, 3] }" /></form>`, |
| 173 |
}); |
| 174 |
|
| 175 |
expect(".o_field_widget").not.toHaveClass("o_field_empty", { |
| 176 |
message: "Float field should be considered set for value 0.", |
| 177 |
}); |
| 178 |
expect(".o_field_widget input").toHaveValue("0.000", { |
| 179 |
message: "The value should be displayed properly.", |
| 180 |
}); |
| 181 |
|
| 182 |
await contains('div[name="float_field"] input').edit("108.2451938598598"); |
| 183 |
expect(".o_field_widget[name=float_field] input").toHaveValue("108.245", { |
| 184 |
message: "The value should have been formatted on blur.", |
| 185 |
}); |
| 186 |
|
| 187 |
await contains(".o_field_widget[name=float_field] input").edit("18.8958938598598"); |
| 188 |
await clickSave(); |
| 189 |
|
| 190 |
expect(".o_field_widget input").toHaveValue("18.896", { |
| 191 |
message: "The new value should be rounded properly.", |
| 192 |
}); |
| 193 |
}); |
| 194 |
|
| 195 |
test("use a formula", async () => { |
| 196 |
await mountView({ |
| 197 |
type: "form", |
| 198 |
resModel: "partner", |
| 199 |
resId: 2, |
| 200 |
arch: `<form><field name="float_field" options="{ 'digits': [0, 3] }" /></form>`, |
| 201 |
}); |
| 202 |
|
| 203 |
await contains(".o_field_widget[name=float_field] input").edit("=20+3*2"); |
| 204 |
await clickSave(); |
| 205 |
|
| 206 |
expect(".o_field_widget input").toHaveValue("26.000", { |
| 207 |
message: "The new value should be calculated properly.", |
| 208 |
}); |
| 209 |
|
| 210 |
await contains(".o_field_widget[name=float_field] input").edit("=2**3"); |
| 211 |
await clickSave(); |
| 212 |
|
| 213 |
expect(".o_field_widget input").toHaveValue("8.000", { |
| 214 |
message: "The new value should be calculated properly.", |
| 215 |
}); |
| 216 |
|
| 217 |
await contains(".o_field_widget[name=float_field] input").edit("=2^3"); |
| 218 |
await clickSave(); |
| 219 |
expect(".o_field_widget input").toHaveValue("8.000", { |
| 220 |
message: "The new value should be calculated properly.", |
| 221 |
}); |
| 222 |
|
| 223 |
await contains(".o_field_widget[name=float_field] input").edit("=100/3"); |
| 224 |
await clickSave(); |
| 225 |
expect(".o_field_widget input").toHaveValue("33.333", { |
| 226 |
message: "The new value should be calculated properly.", |
| 227 |
}); |
| 228 |
}); |
| 229 |
|
| 230 |
test("use incorrect formula", async () => { |
| 231 |
await mountView({ |
| 232 |
type: "form", |
| 233 |
resModel: "partner", |
| 234 |
resId: 2, |
| 235 |
arch: `<form><field name="float_field" options="{ 'digits': [0, 3] }" /></form>`, |
| 236 |
}); |
| 237 |
|
| 238 |
await contains(".o_field_widget[name=float_field] input").edit("=abc", { confirm: false }); |
| 239 |
await clickSave(); |
| 240 |
|
| 241 |
expect(".o_field_widget[name=float_field]").toHaveClass("o_field_invalid", { |
| 242 |
message: "fload field should be displayed as invalid", |
| 243 |
}); |
| 244 |
expect(".o_form_editable").toHaveCount(1, { message: "form view should still be editable" }); |
| 245 |
|
| 246 |
await contains(".o_field_widget[name=float_field] input").edit("=3:2?+4", { confirm: false }); |
| 247 |
await clickSave(); |
| 248 |
|
| 249 |
expect(".o_form_editable").toHaveCount(1, { message: "form view should still be editable" }); |
| 250 |
expect(".o_field_widget[name=float_field]").toHaveClass("o_field_invalid", { |
| 251 |
message: "float field should be displayed as invalid", |
| 252 |
}); |
| 253 |
}); |
| 254 |
|
| 255 |
test.tags("desktop"); |
| 256 |
test("float field in editable list view", async () => { |
| 257 |
await mountView({ |
| 258 |
type: "list", |
| 259 |
resModel: "partner", |
| 260 |
arch: ` |
| 261 |
<list editable="bottom"> |
| 262 |
<field name="float_field" widget="float" digits="[5,3]" /> |
| 263 |
</list>`, |
| 264 |
}); |
| 265 |
|
| 266 |
// switch to edit mode |
| 267 |
await contains("tr.o_data_row td:not(.o_list_record_selector)").click(); |
| 268 |
|
| 269 |
expect('div[name="float_field"] input').toHaveCount(1, { |
| 270 |
message: "The view should have 1 input for editable float.", |
| 271 |
}); |
| 272 |
|
| 273 |
await contains('div[name="float_field"] input').edit("108.2458938598598", { confirm: "blur" }); |
| 274 |
expect(".o_field_widget:eq(0)").toHaveText("108.246", { |
| 275 |
message: "The value should have been formatted on blur.", |
| 276 |
}); |
| 277 |
|
| 278 |
await contains("tr.o_data_row td:not(.o_list_record_selector)").click(); |
| 279 |
await contains('div[name="float_field"] input').edit("18.8958938598598", { confirm: false }); |
| 280 |
await contains(".o_control_panel_main_buttons .o_list_button_save").click(); |
| 281 |
expect(".o_field_widget:eq(0)").toHaveText("18.896", { |
| 282 |
message: "The new value should be rounded properly.", |
| 283 |
}); |
| 284 |
}); |
| 285 |
|
| 286 |
test("float field with type number option", async () => { |
| 287 |
defineParams({ |
| 288 |
lang_parameters: { |
| 289 |
grouping: [3, 0], |
| 290 |
}, |
| 291 |
}); |
| 292 |
await mountView({ |
| 293 |
type: "form", |
| 294 |
resModel: "partner", |
| 295 |
arch: ` |
| 296 |
<form> |
| 297 |
<field name="float_field" options="{'type': 'number'}"/> |
| 298 |
</form>`, |
| 299 |
resId: 4, |
| 300 |
}); |
| 301 |
expect(".o_field_widget input").toHaveAttribute("type", "number", { |
| 302 |
message: 'Float field with option type must have a type attribute equals to "number".', |
| 303 |
}); |
| 304 |
await contains(".o_field_widget input").fill("123456.7890", { instantly: true }); |
| 305 |
await clickSave(); |
| 306 |
expect(".o_field_widget input").toHaveValue(123456.789, { |
| 307 |
message: |
| 308 |
"Float value must be not formatted if input type is number. (but the trailing 0 is gone)", |
| 309 |
}); |
| 310 |
}); |
| 311 |
|
| 312 |
test("float field with type number option and comma decimal separator", async () => { |
| 313 |
defineParams({ |
| 314 |
lang_parameters: { |
| 315 |
thousands_sep: ".", |
| 316 |
decimal_point: ",", |
| 317 |
grouping: [3, 0], |
| 318 |
}, |
| 319 |
}); |
| 320 |
await mountView({ |
| 321 |
type: "form", |
| 322 |
resModel: "partner", |
| 323 |
arch: ` |
| 324 |
<form> |
| 325 |
<field name="float_field" options="{'type': 'number'}"/> |
| 326 |
</form>`, |
| 327 |
resId: 4, |
| 328 |
}); |
| 329 |
|
| 330 |
expect(".o_field_widget input").toHaveAttribute("type", "number", { |
| 331 |
message: 'Float field with option type must have a type attribute equals to "number".', |
| 332 |
}); |
| 333 |
await contains(".o_field_widget[name=float_field] input").fill("123456.789", { |
| 334 |
instantly: true, |
| 335 |
}); |
| 336 |
await clickSave(); |
| 337 |
expect(".o_field_widget input").toHaveValue(123456.789, { |
| 338 |
message: "Float value must be not formatted if input type is number.", |
| 339 |
}); |
| 340 |
}); |
| 341 |
|
| 342 |
test("float field without type number option", async () => { |
| 343 |
defineParams({ |
| 344 |
lang_parameters: { |
| 345 |
grouping: [3, 0], |
| 346 |
}, |
| 347 |
}); |
| 348 |
await mountView({ |
| 349 |
type: "form", |
| 350 |
resModel: "partner", |
| 351 |
arch: '<form><field name="float_field"/></form>', |
| 352 |
resId: 4, |
| 353 |
}); |
| 354 |
expect(".o_field_widget input").toHaveAttribute("type", "text", { |
| 355 |
message: "Float field with option type must have a text type (default type).", |
| 356 |
}); |
| 357 |
|
| 358 |
await contains(".o_field_widget[name=float_field] input").edit("123456.7890"); |
| 359 |
await clickSave(); |
| 360 |
expect(".o_field_widget input").toHaveValue("123,456.79", { |
| 361 |
message: "Float value must be formatted if input type isn't number.", |
| 362 |
}); |
| 363 |
}); |
| 364 |
|
| 365 |
test("field with enable_formatting option as false", async () => { |
| 366 |
defineParams({ |
| 367 |
lang_parameters: { |
| 368 |
grouping: [3, 0], |
| 369 |
}, |
| 370 |
}); |
| 371 |
|
| 372 |
await mountView({ |
| 373 |
type: "form", |
| 374 |
resModel: "partner", |
| 375 |
resId: 1, |
| 376 |
arch: `<form><field name="float_field" options="{'enable_formatting': false}"/></form>`, |
| 377 |
}); |
| 378 |
|
| 379 |
expect(".o_field_widget input").toHaveValue("0.36", { |
| 380 |
message: "Integer value must not be formatted", |
| 381 |
}); |
| 382 |
|
| 383 |
await contains(".o_field_widget[name=float_field] input").edit("123456.789"); |
| 384 |
await clickSave(); |
| 385 |
expect(".o_field_widget input").toHaveValue("123456.789", { |
| 386 |
message: "Integer value must be not formatted if input type is number.", |
| 387 |
}); |
| 388 |
}); |
| 389 |
|
| 390 |
test.tags("desktop"); |
| 391 |
test("field with enable_formatting option as false in editable list view", async () => { |
| 392 |
await mountView({ |
| 393 |
type: "list", |
| 394 |
resModel: "partner", |
| 395 |
arch: ` |
| 396 |
<list editable="bottom"> |
| 397 |
<field name="float_field" widget="float" digits="[5,3]" options="{'enable_formatting': false}" /> |
| 398 |
</list>`, |
| 399 |
}); |
| 400 |
|
| 401 |
// switch to edit mode |
| 402 |
await contains("tr.o_data_row td:not(.o_list_record_selector)").click(); |
| 403 |
|
| 404 |
expect('div[name="float_field"] input').toHaveCount(1, { |
| 405 |
message: "The view should have 1 input for editable float.", |
| 406 |
}); |
| 407 |
|
| 408 |
await contains('div[name="float_field"] input').edit("108.2458938598598", { |
| 409 |
confirm: "blur", |
| 410 |
}); |
| 411 |
expect(".o_field_widget:eq(0)").toHaveText("108.2458938598598", { |
| 412 |
message: "The value should not be formatted on blur.", |
| 413 |
}); |
| 414 |
|
| 415 |
await contains("tr.o_data_row td:not(.o_list_record_selector)").click(); |
| 416 |
await contains('div[name="float_field"] input').edit("18.8958938598598", { |
| 417 |
confirm: false, |
| 418 |
}); |
| 419 |
await contains(".o_control_panel_main_buttons .o_list_button_save").click(); |
| 420 |
expect(".o_field_widget:eq(0)").toHaveText("18.8958938598598", { |
| 421 |
message: "The new value should not be rounded as well.", |
| 422 |
}); |
| 423 |
}); |
| 424 |
|
| 425 |
test("float field can be updated by another field/widget", async () => { |
| 426 |
class MyWidget extends Component { |
| 427 |
static template = xml`<button t-on-click="onClick">do it</button>`; |
| 428 |
static props = ["*"]; |
| 429 |
onClick() { |
| 430 |
const val = this.props.record.data.float_field; |
| 431 |
this.props.record.update({ float_field: val + 1 }); |
| 432 |
} |
| 433 |
} |
| 434 |
const myWidget = { |
| 435 |
component: MyWidget, |
| 436 |
}; |
| 437 |
registry.category("view_widgets").add("wi", myWidget); |
| 438 |
await mountView({ |
| 439 |
type: "form", |
| 440 |
resModel: "partner", |
| 441 |
arch: ` |
| 442 |
<form> |
| 443 |
<field name="float_field"/> |
| 444 |
<field name="float_field"/> |
| 445 |
<widget name="wi"/> |
| 446 |
</form>`, |
| 447 |
}); |
| 448 |
|
| 449 |
await contains(".o_field_widget[name=float_field] input").edit("40"); |
| 450 |
|
| 451 |
expect(".o_field_widget[name=float_field] input:eq(0)").toHaveValue("40.00"); |
| 452 |
expect(".o_field_widget[name=float_field] input:eq(1)").toHaveValue("40.00"); |
| 453 |
|
| 454 |
await contains(".o_widget button").click(); |
| 455 |
|
| 456 |
expect(".o_field_widget[name=float_field] input:eq(0)").toHaveValue("41.00"); |
| 457 |
expect(".o_field_widget[name=float_field] input:eq(1)").toHaveValue("41.00"); |
| 458 |
}); |