| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { queryAll, queryAllTexts, queryFirst } from "@odoo/hoot-dom"; |
| 3 |
import { Deferred, animationFrame } from "@odoo/hoot-mock"; |
| 4 |
|
| 5 |
import { |
| 6 |
clickSave, |
| 7 |
contains, |
| 8 |
defineModels, |
| 9 |
fields, |
| 10 |
models, |
| 11 |
mountView, |
| 12 |
onRpc, |
| 13 |
serverState, |
| 14 |
} from "@web/../tests/web_test_helpers"; |
| 15 |
|
| 16 |
class Partner extends models.Model { |
| 17 |
name = fields.Char(); |
| 18 |
int_field = fields.Integer(); |
| 19 |
float_field = fields.Float({ |
| 20 |
digits: [16, 1], |
| 21 |
}); |
| 22 |
p = fields.One2many({ relation: "partner" }); |
| 23 |
currency_id = fields.Many2one({ relation: "res.currency" }); |
| 24 |
monetary_field = fields.Monetary({ currency_field: "currency_id" }); |
| 25 |
|
| 26 |
_records = [ |
| 27 |
{ id: 1, int_field: 10, float_field: 0.44444 }, |
| 28 |
{ id: 2, int_field: 0, float_field: 0, currency_id: 2 }, |
| 29 |
{ id: 3, int_field: 80, float_field: -3.89859 }, |
| 30 |
{ id: 4, int_field: 0, float_field: 0 }, |
| 31 |
{ id: 5, int_field: -4, float_field: 9.1, monetary_field: 9.1, currency_id: 1 }, |
| 32 |
{ id: 6, float_field: 3.9, monetary_field: 4.2, currency_id: 1 }, |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
class Currency extends models.Model { |
| 37 |
_name = "res.currency"; |
| 38 |
|
| 39 |
name = fields.Char(); |
| 40 |
symbol = fields.Char({ string: "Currency Sumbol" }); |
| 41 |
position = fields.Selection({ |
| 42 |
selection: [ |
| 43 |
["after", "A"], |
| 44 |
["before", "B"], |
| 45 |
], |
| 46 |
}); |
| 47 |
inverse_rate = fields.Float(); |
| 48 |
|
| 49 |
_records = [ |
| 50 |
{ id: 1, name: "USD", symbol: "$", position: "before", inverse_rate: 1 }, |
| 51 |
{ id: 2, name: "EUR", symbol: "€", position: "after", inverse_rate: 0.5 }, |
| 52 |
{ |
| 53 |
id: 3, |
| 54 |
name: "VEF", |
| 55 |
symbol: "Bs.F", |
| 56 |
position: "after", |
| 57 |
inverse_rate: 0.3, |
| 58 |
}, |
| 59 |
]; |
| 60 |
} |
| 61 |
|
| 62 |
defineModels([Partner, Currency]); |
| 63 |
|
| 64 |
onRpc("has_group", () => true); |
| 65 |
|
| 66 |
test("basic flow in form view - float field", async () => { |
| 67 |
await mountView({ |
| 68 |
type: "form", |
| 69 |
resModel: "partner", |
| 70 |
resId: 5, |
| 71 |
arch: ` |
| 72 |
<form> |
| 73 |
<field name="float_field" widget="monetary"/> |
| 74 |
<field name="currency_id" invisible="1"/> |
| 75 |
</form>`, |
| 76 |
}); |
| 77 |
|
| 78 |
expect(".o_field_monetary > div.text-nowrap").toHaveCount(1); |
| 79 |
expect(".o_field_widget input").toHaveValue("9.10", { |
| 80 |
message: "The input should be rendered without the currency symbol.", |
| 81 |
}); |
| 82 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 83 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 84 |
}); |
| 85 |
|
| 86 |
await contains(".o_field_monetary input").edit("108.2458938598598"); |
| 87 |
expect(".o_field_widget input").toHaveValue("108.25", { |
| 88 |
message: "The new value should be rounded properly after the blur", |
| 89 |
}); |
| 90 |
|
| 91 |
await clickSave(); |
| 92 |
expect(".o_field_widget input").toHaveValue("108.25", { |
| 93 |
message: "The new value should be rounded properly.", |
| 94 |
}); |
| 95 |
}); |
| 96 |
|
| 97 |
test("basic flow in form view - monetary field", async () => { |
| 98 |
await mountView({ |
| 99 |
type: "form", |
| 100 |
resModel: "partner", |
| 101 |
resId: 5, |
| 102 |
arch: ` |
| 103 |
<form> |
| 104 |
<field name="monetary_field"/> |
| 105 |
<field name="currency_id" invisible="1"/> |
| 106 |
</form>`, |
| 107 |
}); |
| 108 |
|
| 109 |
expect(".o_field_widget input").toHaveValue("9.10", { |
| 110 |
message: "The input should be rendered without the currency symbol.", |
| 111 |
}); |
| 112 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 113 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 114 |
}); |
| 115 |
|
| 116 |
await contains(".o_field_monetary input").edit("108.2458938598598"); |
| 117 |
expect(".o_field_widget input").toHaveValue("108.25", { |
| 118 |
message: "The new value should be rounded properly after the blur", |
| 119 |
}); |
| 120 |
|
| 121 |
await clickSave(); |
| 122 |
expect(".o_field_widget input").toHaveValue("108.25", { |
| 123 |
message: "The new value should be rounded properly.", |
| 124 |
}); |
| 125 |
}); |
| 126 |
|
| 127 |
test("rounding using formula in form view - float field", async () => { |
| 128 |
await mountView({ |
| 129 |
type: "form", |
| 130 |
resModel: "partner", |
| 131 |
resId: 5, |
| 132 |
arch: ` |
| 133 |
<form> |
| 134 |
<field name="float_field" widget="monetary"/> |
| 135 |
<field name="currency_id" invisible="1"/> |
| 136 |
</form>`, |
| 137 |
}); |
| 138 |
|
| 139 |
// Test computation and rounding |
| 140 |
await contains(".o_field_monetary input").edit("=100/3"); |
| 141 |
await clickSave(); |
| 142 |
expect(".o_field_widget input").toHaveValue("33.33", { |
| 143 |
message: "The new value should be calculated and rounded properly.", |
| 144 |
}); |
| 145 |
}); |
| 146 |
|
| 147 |
test("rounding using formula in form view - monetary field", async () => { |
| 148 |
await mountView({ |
| 149 |
type: "form", |
| 150 |
resModel: "partner", |
| 151 |
resId: 5, |
| 152 |
arch: ` |
| 153 |
<form> |
| 154 |
<field name="monetary_field"/> |
| 155 |
<field name="currency_id" invisible="1"/> |
| 156 |
</form>`, |
| 157 |
}); |
| 158 |
|
| 159 |
// Test computation and rounding |
| 160 |
await contains(".o_field_monetary input").edit("=100/3"); |
| 161 |
await clickSave(); |
| 162 |
expect(".o_field_widget input").toHaveValue("33.33", { |
| 163 |
message: "The new value should be calculated and rounded properly.", |
| 164 |
}); |
| 165 |
}); |
| 166 |
|
| 167 |
test("with currency digits != 2 - float field", async () => { |
| 168 |
serverState.currencies = [ |
| 169 |
{ id: 1, name: "USD", symbol: "$", position: "before" }, |
| 170 |
{ id: 2, name: "EUR", symbol: "€", position: "after" }, |
| 171 |
{ |
| 172 |
id: 3, |
| 173 |
name: "VEF", |
| 174 |
symbol: "Bs.F", |
| 175 |
position: "after", |
| 176 |
digits: [0, 4], |
| 177 |
}, |
| 178 |
]; |
| 179 |
|
| 180 |
Partner._records = [ |
| 181 |
{ |
| 182 |
id: 1, |
| 183 |
float_field: 99.1234, |
| 184 |
currency_id: 3, |
| 185 |
}, |
| 186 |
]; |
| 187 |
|
| 188 |
await mountView({ |
| 189 |
type: "form", |
| 190 |
resModel: "partner", |
| 191 |
resId: 1, |
| 192 |
arch: ` |
| 193 |
<form> |
| 194 |
<field name="float_field" widget="monetary"/> |
| 195 |
<field name="currency_id" invisible="1"/> |
| 196 |
</form>`, |
| 197 |
}); |
| 198 |
|
| 199 |
expect(".o_field_widget input").toHaveValue("99.1234", { |
| 200 |
message: "The input should be rendered without the currency symbol.", |
| 201 |
}); |
| 202 |
expect(".o_field_widget .o_input span:eq(1)").toHaveText("Bs.F", { |
| 203 |
message: "The input should be superposed with a span containing the currency symbol.", |
| 204 |
}); |
| 205 |
|
| 206 |
await contains(".o_field_widget input").edit("99.111111111"); |
| 207 |
expect(".o_field_widget input").toHaveValue("99.1111", { |
| 208 |
message: "The value should should be formatted on blur.", |
| 209 |
}); |
| 210 |
|
| 211 |
await clickSave(); |
| 212 |
expect(".o_field_widget input").toHaveValue("99.1111", { |
| 213 |
message: "The new value should be rounded properly.", |
| 214 |
}); |
| 215 |
}); |
| 216 |
|
| 217 |
test("with currency digits != 2 - monetary field", async () => { |
| 218 |
serverState.currencies = [ |
| 219 |
{ id: 1, name: "USD", symbol: "$", position: "before" }, |
| 220 |
{ id: 2, name: "EUR", symbol: "€", position: "after" }, |
| 221 |
{ |
| 222 |
id: 3, |
| 223 |
name: "VEF", |
| 224 |
symbol: "Bs.F", |
| 225 |
position: "after", |
| 226 |
digits: [0, 4], |
| 227 |
}, |
| 228 |
]; |
| 229 |
|
| 230 |
Partner._records = [ |
| 231 |
{ |
| 232 |
id: 1, |
| 233 |
float_field: 99.1234, |
| 234 |
monetary_field: 99.1234, |
| 235 |
currency_id: 3, |
| 236 |
}, |
| 237 |
]; |
| 238 |
|
| 239 |
await mountView({ |
| 240 |
type: "form", |
| 241 |
resModel: "partner", |
| 242 |
resId: 1, |
| 243 |
arch: ` |
| 244 |
<form> |
| 245 |
<field name="monetary_field"/> |
| 246 |
<field name="currency_id" invisible="1"/> |
| 247 |
</form>`, |
| 248 |
}); |
| 249 |
|
| 250 |
expect(".o_field_widget input").toHaveValue("99.1234", { |
| 251 |
message: "The input should be rendered without the currency symbol.", |
| 252 |
}); |
| 253 |
expect(".o_field_widget .o_input span:eq(1)").toHaveText("Bs.F", { |
| 254 |
message: "The input should be superposed with a span containing the currency symbol.", |
| 255 |
}); |
| 256 |
|
| 257 |
await contains(".o_field_widget input").edit("99.111111111"); |
| 258 |
expect(".o_field_widget input").toHaveValue("99.1111", { |
| 259 |
message: "The value should should be formatted on blur.", |
| 260 |
}); |
| 261 |
|
| 262 |
await clickSave(); |
| 263 |
expect(".o_field_widget input").toHaveValue("99.1111", { |
| 264 |
message: "The new value should be rounded properly.", |
| 265 |
}); |
| 266 |
}); |
| 267 |
|
| 268 |
test("basic flow in editable list view - float field", async () => { |
| 269 |
Partner._records = [ |
| 270 |
{ |
| 271 |
id: 1, |
| 272 |
float_field: 9.1, |
| 273 |
monetary_field: 9.1, |
| 274 |
currency_id: 1, |
| 275 |
}, |
| 276 |
{ |
| 277 |
id: 2, |
| 278 |
float_field: 15.3, |
| 279 |
monetary_field: 15.3, |
| 280 |
currency_id: 2, |
| 281 |
}, |
| 282 |
{ |
| 283 |
id: 3, |
| 284 |
float_field: 0, |
| 285 |
monetary_field: 0, |
| 286 |
currency_id: 1, |
| 287 |
}, |
| 288 |
{ |
| 289 |
id: 4, |
| 290 |
float_field: 5.0, |
| 291 |
monetary_field: 5.0, |
| 292 |
}, |
| 293 |
]; |
| 294 |
|
| 295 |
await mountView({ |
| 296 |
type: "list", |
| 297 |
resModel: "partner", |
| 298 |
arch: ` |
| 299 |
<list editable="bottom"> |
| 300 |
<field name="float_field" widget="monetary"/> |
| 301 |
<field name="currency_id" column_invisible="1"/> |
| 302 |
</list>`, |
| 303 |
}); |
| 304 |
|
| 305 |
const dollarValues = queryAll("td:contains($)"); |
| 306 |
expect(dollarValues).toHaveLength(2, { message: "Only 2 line has dollar as a currency." }); |
| 307 |
|
| 308 |
const euroValues = queryAll("td:contains(€)"); |
| 309 |
expect(euroValues).toHaveLength(1, { message: "Only 1 line has euro as a currency." }); |
| 310 |
|
| 311 |
const noCurrencyValues = queryAll("td.o_data_cell").filter( |
| 312 |
(x) => !(x.textContent.includes("€") || x.textContent.includes("$")) |
| 313 |
); |
| 314 |
expect(noCurrencyValues).toHaveLength(1, { message: "Only 1 line has no currency." }); |
| 315 |
|
| 316 |
// switch to edit mode |
| 317 |
const dollarCell = queryFirst("td.o_field_cell"); |
| 318 |
await contains(dollarCell).click(); |
| 319 |
|
| 320 |
expect(dollarCell.children).toHaveLength(1, { |
| 321 |
message: "The cell td should only contain the special div of monetary widget.", |
| 322 |
}); |
| 323 |
|
| 324 |
expect(".o_field_widget input").toHaveCount(1, { |
| 325 |
message: "The view should have 1 input for editable monetary float.", |
| 326 |
}); |
| 327 |
expect(".o_field_widget input").toHaveValue("9.10", { |
| 328 |
message: "The input should be rendered without the currency symbol.", |
| 329 |
}); |
| 330 |
|
| 331 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 332 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 333 |
}); |
| 334 |
|
| 335 |
await contains(".o_field_widget input").edit("108.2458938598598", { confirm: "blur" }); |
| 336 |
expect(dollarCell).toHaveText("$ 108.25", { message: "The new value should be correct" }); |
| 337 |
}); |
| 338 |
|
| 339 |
test("basic flow in editable list view - monetary field", async () => { |
| 340 |
Partner._records = [ |
| 341 |
{ |
| 342 |
id: 1, |
| 343 |
float_field: 9.1, |
| 344 |
monetary_field: 9.1, |
| 345 |
currency_id: 1, |
| 346 |
}, |
| 347 |
{ |
| 348 |
id: 2, |
| 349 |
float_field: 15.3, |
| 350 |
monetary_field: 15.3, |
| 351 |
currency_id: 2, |
| 352 |
}, |
| 353 |
{ |
| 354 |
id: 3, |
| 355 |
float_field: 0, |
| 356 |
monetary_field: 0, |
| 357 |
currency_id: 1, |
| 358 |
}, |
| 359 |
{ |
| 360 |
id: 4, |
| 361 |
float_field: 5.0, |
| 362 |
monetary_field: 5.0, |
| 363 |
}, |
| 364 |
]; |
| 365 |
|
| 366 |
await mountView({ |
| 367 |
type: "list", |
| 368 |
resModel: "partner", |
| 369 |
arch: ` |
| 370 |
<list editable="bottom"> |
| 371 |
<field name="monetary_field"/> |
| 372 |
<field name="currency_id" column_invisible="1"/> |
| 373 |
</list>`, |
| 374 |
}); |
| 375 |
|
| 376 |
const dollarValues = queryAll("td:contains($)"); |
| 377 |
expect(dollarValues).toHaveLength(2, { message: "Only 2 line has dollar as a currency." }); |
| 378 |
|
| 379 |
const euroValues = queryAll("td:contains(€)"); |
| 380 |
expect(euroValues).toHaveLength(1, { message: "Only 1 line has euro as a currency." }); |
| 381 |
|
| 382 |
const noCurrencyValues = queryAll("td.o_data_cell").filter( |
| 383 |
(x) => !(x.textContent.includes("€") || x.textContent.includes("$")) |
| 384 |
); |
| 385 |
expect(noCurrencyValues).toHaveLength(1, { message: "Only 1 line has no currency." }); |
| 386 |
|
| 387 |
// switch to edit mode |
| 388 |
const dollarCell = queryFirst("td.o_field_cell"); |
| 389 |
await contains(dollarCell).click(); |
| 390 |
|
| 391 |
expect(dollarCell.children).toHaveLength(1, { |
| 392 |
message: "The cell td should only contain the special div of monetary widget.", |
| 393 |
}); |
| 394 |
|
| 395 |
expect(".o_field_widget input").toHaveCount(1, { |
| 396 |
message: "The view should have 1 input for editable monetary float.", |
| 397 |
}); |
| 398 |
expect(".o_field_widget input").toHaveValue("9.10", { |
| 399 |
message: "The input should be rendered without the currency symbol.", |
| 400 |
}); |
| 401 |
|
| 402 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 403 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 404 |
}); |
| 405 |
|
| 406 |
await contains(".o_field_widget input").edit("108.2458938598598", { confirm: "blur" }); |
| 407 |
expect(dollarCell).toHaveText("$ 108.25", { message: "The new value should be correct" }); |
| 408 |
}); |
| 409 |
|
| 410 |
test.tags("desktop"); |
| 411 |
test("changing currency updates the field - float field", async () => { |
| 412 |
Partner._records = [ |
| 413 |
{ |
| 414 |
id: 1, |
| 415 |
float_field: 4.2, |
| 416 |
monetary_field: 4.2, |
| 417 |
currency_id: 1, |
| 418 |
}, |
| 419 |
]; |
| 420 |
|
| 421 |
await mountView({ |
| 422 |
type: "form", |
| 423 |
resModel: "partner", |
| 424 |
resId: 1, |
| 425 |
arch: ` |
| 426 |
<form> |
| 427 |
<field name="float_field" widget="monetary"/> |
| 428 |
<field name="currency_id"/> |
| 429 |
</form>`, |
| 430 |
}); |
| 431 |
|
| 432 |
await contains(".o_field_many2one_selection input").click(); |
| 433 |
await contains(".o-autocomplete--dropdown-item:contains(EUR)").click(); |
| 434 |
expect(".o_field_widget .o_input span:eq(1)").toHaveText("€", { |
| 435 |
message: |
| 436 |
"The input should be preceded by a span containing the currency symbol added on blur.", |
| 437 |
}); |
| 438 |
expect(".o_field_monetary input").toHaveValue("4.20"); |
| 439 |
|
| 440 |
await clickSave(); |
| 441 |
expect(".o_field_monetary input").toHaveValue("4.20", { |
| 442 |
message: "The new value should still be correct.", |
| 443 |
}); |
| 444 |
}); |
| 445 |
|
| 446 |
test.tags("desktop"); |
| 447 |
test("changing currency updates the field - monetary field", async () => { |
| 448 |
Partner._records = [ |
| 449 |
{ |
| 450 |
id: 1, |
| 451 |
float_field: 4.2, |
| 452 |
monetary_field: 4.2, |
| 453 |
currency_id: 1, |
| 454 |
}, |
| 455 |
]; |
| 456 |
|
| 457 |
await mountView({ |
| 458 |
type: "form", |
| 459 |
resModel: "partner", |
| 460 |
resId: 1, |
| 461 |
arch: ` |
| 462 |
<form> |
| 463 |
<field name="monetary_field"/> |
| 464 |
<field name="currency_id"/> |
| 465 |
</form>`, |
| 466 |
}); |
| 467 |
|
| 468 |
await contains(".o_field_many2one_selection input").click(); |
| 469 |
await contains(".o-autocomplete--dropdown-item:contains(EUR)").click(); |
| 470 |
|
| 471 |
expect(".o_field_widget .o_input span:eq(1)").toHaveText("€", { |
| 472 |
message: |
| 473 |
"The input should be preceded by a span containing the currency symbol added on blur.", |
| 474 |
}); |
| 475 |
expect(".o_field_monetary input").toHaveValue("4.20"); |
| 476 |
|
| 477 |
await clickSave(); |
| 478 |
expect(".o_field_monetary input").toHaveValue("4.20", { |
| 479 |
message: "The new value should still be correct.", |
| 480 |
}); |
| 481 |
}); |
| 482 |
|
| 483 |
test("MonetaryField with monetary field given in options", async () => { |
| 484 |
Partner._fields.company_currency_id = fields.Many2one({ |
| 485 |
string: "Company Currency", |
| 486 |
relation: "res.currency", |
| 487 |
}); |
| 488 |
Partner._records[4].company_currency_id = 2; |
| 489 |
await mountView({ |
| 490 |
type: "form", |
| 491 |
resModel: "partner", |
| 492 |
arch: ` |
| 493 |
<form edit="0"> |
| 494 |
<sheet> |
| 495 |
<field name="monetary_field" options="{'currency_field': 'company_currency_id'}"/> |
| 496 |
<field name="company_currency_id"/> |
| 497 |
</sheet> |
| 498 |
</form>`, |
| 499 |
resId: 5, |
| 500 |
}); |
| 501 |
|
| 502 |
expect(".o_field_monetary").toHaveText("9.10 €", { |
| 503 |
message: "field monetary should be formatted with correct currency", |
| 504 |
}); |
| 505 |
}); |
| 506 |
|
| 507 |
test("should keep the focus when being edited in x2many lists", async () => { |
| 508 |
Partner._fields.currency_id.default = 1; |
| 509 |
Partner._fields.m2m = fields.Many2many({ |
| 510 |
relation: "partner", |
| 511 |
default: [[4, 2]], |
| 512 |
}); |
| 513 |
Partner._views = { |
| 514 |
list: ` |
| 515 |
<list editable="bottom"> |
| 516 |
<field name="float_field" widget="monetary"/> |
| 517 |
<field name="currency_id" invisible="1"/> |
| 518 |
</list>`, |
| 519 |
}; |
| 520 |
|
| 521 |
await mountView({ |
| 522 |
type: "form", |
| 523 |
resModel: "partner", |
| 524 |
arch: ` |
| 525 |
<form> |
| 526 |
<sheet> |
| 527 |
<field name="p"/> |
| 528 |
<field name="m2m"/> |
| 529 |
</sheet> |
| 530 |
</form>`, |
| 531 |
}); |
| 532 |
|
| 533 |
// test the monetary field inside the one2many |
| 534 |
await contains(".o_field_x2many_list_row_add a").click(); |
| 535 |
await contains(".o_field_widget[name=float_field] input").edit("22", { confirm: "blur" }); |
| 536 |
|
| 537 |
expect(".o_field_widget[name=p] .o_field_widget[name=float_field] span").toHaveInnerHTML( |
| 538 |
"$ 22.00", |
| 539 |
{ type: "html" } |
| 540 |
); |
| 541 |
|
| 542 |
// test the monetary field inside the many2many |
| 543 |
await contains(".o_field_widget[name=m2m] .o_data_cell").click(); |
| 544 |
await contains(".o_field_widget[name=float_field] input").edit("22", { confirm: "blur" }); |
| 545 |
expect(".o_field_widget[name=m2m] .o_field_widget[name=float_field] span").toHaveInnerHTML( |
| 546 |
"22.00 €", |
| 547 |
{ type: "html" } |
| 548 |
); |
| 549 |
}); |
| 550 |
|
| 551 |
test("MonetaryField with currency set by an onchange", async () => { |
| 552 |
// this test ensures that the monetary field can be re-rendered with and |
| 553 |
// without currency (which can happen as the currency can be set by an |
| 554 |
// onchange) |
| 555 |
Partner._onChanges = { |
| 556 |
int_field: function (obj) { |
| 557 |
obj.currency_id = obj.int_field ? 2 : null; |
| 558 |
}, |
| 559 |
}; |
| 560 |
|
| 561 |
await mountView({ |
| 562 |
type: "list", |
| 563 |
resModel: "partner", |
| 564 |
arch: ` |
| 565 |
<list editable="top"> |
| 566 |
<field name="int_field"/> |
| 567 |
<field name="float_field" widget="monetary"/> |
| 568 |
<field name="currency_id" invisible="1"/> |
| 569 |
</list>`, |
| 570 |
}); |
| 571 |
|
| 572 |
await contains(".o_control_panel_main_buttons .o_list_button_add").click(); |
| 573 |
expect(".o_selected_row .o_field_widget[name=float_field] input").toHaveCount(1, { |
| 574 |
message: "monetary field should have been rendered correctly (without currency)", |
| 575 |
}); |
| 576 |
expect(".o_selected_row .o_field_widget[name=float_field] span").toHaveCount(2, { |
| 577 |
message: "monetary field should have been rendered correctly (without currency)", |
| 578 |
}); |
| 579 |
|
| 580 |
// set a value for int_field -> should set the currency and re-render float_field |
| 581 |
await contains(".o_field_widget[name=int_field] input").edit("7", { confirm: "blur" }); |
| 582 |
await contains(".o_field_cell[name=int_field]").click(); |
| 583 |
expect(".o_selected_row .o_field_widget[name=float_field] input").toHaveCount(1, { |
| 584 |
message: "monetary field should have been re-rendered correctly (with currency)", |
| 585 |
}); |
| 586 |
expect( |
| 587 |
queryAllTexts(".o_selected_row .o_field_widget[name=float_field] .o_input span") |
| 588 |
).toEqual(["0.00", "€"], { |
| 589 |
message: "monetary field should have been re-rendered correctly (with currency)", |
| 590 |
}); |
| 591 |
await contains(".o_field_widget[name=float_field] input").click(); |
| 592 |
expect(".o_field_widget[name=float_field] input").toBeFocused({ |
| 593 |
message: "focus should be on the float_field field's input", |
| 594 |
}); |
| 595 |
|
| 596 |
// unset the value of int_field -> should unset the currency and re-render float_field |
| 597 |
await contains(".o_field_widget[name=int_field]").click(); |
| 598 |
await contains(".o_field_widget[name=int_field] input").edit("0", { confirm: "blur" }); |
| 599 |
await contains(".o_field_cell[name=int_field]").click(); |
| 600 |
expect(".o_selected_row .o_field_widget[name=float_field] input").toHaveCount(1, { |
| 601 |
message: "monetary field should have been re-rendered correctly (without currency)", |
| 602 |
}); |
| 603 |
expect(".o_selected_row .o_field_widget[name=float_field] span").toHaveCount(2, { |
| 604 |
message: "monetary field should have been re-rendered correctly (without currency)", |
| 605 |
}); |
| 606 |
await contains(".o_field_widget[name=float_field] input").click(); |
| 607 |
expect(".o_field_widget[name=float_field] input").toBeFocused({ |
| 608 |
message: "focus should be on the float_field field's input", |
| 609 |
}); |
| 610 |
}); |
| 611 |
|
| 612 |
test("float widget on monetary field", async () => { |
| 613 |
Partner._fields.monetary = fields.Monetary({ currency_field: "currency_id" }); |
| 614 |
Partner._records[0].monetary = 9.99; |
| 615 |
Partner._records[0].currency_id = 1; |
| 616 |
|
| 617 |
await mountView({ |
| 618 |
type: "form", |
| 619 |
resModel: "partner", |
| 620 |
arch: ` |
| 621 |
<form edit="0"> |
| 622 |
<sheet> |
| 623 |
<field name="monetary" widget="float"/> |
| 624 |
<field name="currency_id" invisible="1"/> |
| 625 |
</sheet> |
| 626 |
</form>`, |
| 627 |
resId: 1, |
| 628 |
}); |
| 629 |
|
| 630 |
expect(".o_field_widget[name=monetary]").toHaveText("9.99", { |
| 631 |
message: "value should be correctly formatted (with the float formatter)", |
| 632 |
}); |
| 633 |
}); |
| 634 |
|
| 635 |
test("float field with monetary widget and decimal precision", async () => { |
| 636 |
Partner._records = [ |
| 637 |
{ |
| 638 |
id: 1, |
| 639 |
float_field: -8.89859, |
| 640 |
currency_id: 1, |
| 641 |
}, |
| 642 |
]; |
| 643 |
serverState.currencies = [ |
| 644 |
{ id: 1, name: "USD", symbol: "$", position: "before", digits: [0, 4] }, |
| 645 |
{ id: 2, name: "EUR", symbol: "€", position: "after" }, |
| 646 |
]; |
| 647 |
|
| 648 |
await mountView({ |
| 649 |
type: "form", |
| 650 |
resModel: "partner", |
| 651 |
arch: ` |
| 652 |
<form> |
| 653 |
<sheet> |
| 654 |
<field name="float_field" widget="monetary" options="{'field_digits': True}"/> |
| 655 |
<field name="currency_id" invisible="1"/> |
| 656 |
</sheet> |
| 657 |
</form>`, |
| 658 |
resId: 1, |
| 659 |
}); |
| 660 |
|
| 661 |
expect(".o_field_widget[name=float_field] input").toHaveValue("-8.9", { |
| 662 |
message: "The input should be rendered without the currency symbol.", |
| 663 |
}); |
| 664 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 665 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 666 |
}); |
| 667 |
|
| 668 |
await contains(".o_field_monetary input").edit("109.2458938598598"); |
| 669 |
expect(".o_field_widget[name=float_field] input").toHaveValue("109.2", { |
| 670 |
message: "The value should should be formatted on blur.", |
| 671 |
}); |
| 672 |
|
| 673 |
await clickSave(); |
| 674 |
expect(".o_field_widget input").toHaveValue("109.2", { |
| 675 |
message: "The new value should be rounded properly.", |
| 676 |
}); |
| 677 |
}); |
| 678 |
|
| 679 |
test("MonetaryField without currency symbol", async () => { |
| 680 |
await mountView({ |
| 681 |
type: "form", |
| 682 |
resModel: "partner", |
| 683 |
resId: 5, |
| 684 |
arch: ` |
| 685 |
<form> |
| 686 |
<sheet> |
| 687 |
<field name="float_field" widget="monetary" options="{'no_symbol': True}" /> |
| 688 |
<field name="currency_id" invisible="1" /> |
| 689 |
</sheet> |
| 690 |
</form>`, |
| 691 |
}); |
| 692 |
|
| 693 |
// Non-breaking space between the currency and the amount |
| 694 |
expect(".o_field_widget[name=float_field] input").toHaveValue("9.10", { |
| 695 |
message: "The currency symbol is not displayed", |
| 696 |
}); |
| 697 |
}); |
| 698 |
|
| 699 |
test("required monetary field with zero value", async () => { |
| 700 |
await mountView({ |
| 701 |
type: "form", |
| 702 |
resModel: "partner", |
| 703 |
arch: ` |
| 704 |
<form> |
| 705 |
<field name="monetary_field" required="1"/> |
| 706 |
</form>`, |
| 707 |
}); |
| 708 |
|
| 709 |
expect(".o_form_editable").toHaveCount(1); |
| 710 |
expect("[name=monetary_field] input").toHaveValue("0.00"); |
| 711 |
}); |
| 712 |
|
| 713 |
test("uses 'currency_id' as currency field by default", async () => { |
| 714 |
await mountView({ |
| 715 |
type: "form", |
| 716 |
resModel: "partner", |
| 717 |
arch: ` |
| 718 |
<form> |
| 719 |
<field name="monetary_field"/> |
| 720 |
<field name="currency_id" invisible="1"/> |
| 721 |
</form>`, |
| 722 |
resId: 6, |
| 723 |
}); |
| 724 |
|
| 725 |
expect(".o_form_editable").toHaveCount(1); |
| 726 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 727 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 728 |
}); |
| 729 |
}); |
| 730 |
|
| 731 |
test("automatically uses currency_field if defined", async () => { |
| 732 |
Partner._fields.custom_currency_id = fields.Many2one({ |
| 733 |
string: "Currency", |
| 734 |
relation: "res.currency", |
| 735 |
}); |
| 736 |
Partner._fields.monetary_field.currency_field = "custom_currency_id"; |
| 737 |
Partner._records[5].custom_currency_id = 1; |
| 738 |
|
| 739 |
await mountView({ |
| 740 |
type: "form", |
| 741 |
resModel: "partner", |
| 742 |
arch: ` |
| 743 |
<form> |
| 744 |
<field name="monetary_field"/> |
| 745 |
<field name="custom_currency_id" invisible="1"/> |
| 746 |
</form>`, |
| 747 |
resId: 6, |
| 748 |
}); |
| 749 |
|
| 750 |
expect(".o_form_editable").toHaveCount(1); |
| 751 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$", { |
| 752 |
message: "The input should be preceded by a span containing the currency symbol.", |
| 753 |
}); |
| 754 |
}); |
| 755 |
|
| 756 |
test("monetary field with pending onchange", async () => { |
| 757 |
const def = new Deferred(); |
| 758 |
Partner._onChanges = { |
| 759 |
async name(record) { |
| 760 |
record.float_field = 132; |
| 761 |
}, |
| 762 |
}; |
| 763 |
onRpc("onchange", async () => { |
| 764 |
await def; |
| 765 |
}); |
| 766 |
await mountView({ |
| 767 |
type: "form", |
| 768 |
resModel: "partner", |
| 769 |
arch: ` |
| 770 |
<form> |
| 771 |
<field name="float_field" widget="monetary"/> |
| 772 |
<field name="name"/> |
| 773 |
<field name="currency_id" invisible="1"/> |
| 774 |
</form>`, |
| 775 |
resId: 1, |
| 776 |
}); |
| 777 |
|
| 778 |
await contains(".o_field_widget[name='name'] input").edit("test", { confirm: "blur" }); |
| 779 |
await contains(".o_field_widget[name='float_field'] input").edit("1", { confirm: false }); |
| 780 |
def.resolve(); |
| 781 |
await animationFrame(); |
| 782 |
expect(".o_field_monetary .o_monetary_ghost_value").toHaveText("1"); |
| 783 |
}); |
| 784 |
|
| 785 |
test("with 'hide_trailing_zeros' option", async () => { |
| 786 |
await mountView({ |
| 787 |
type: "form", |
| 788 |
resModel: "partner", |
| 789 |
resId: 5, |
| 790 |
arch: ` |
| 791 |
<form> |
| 792 |
<field name="float_field" widget="monetary" options="{'hide_trailing_zeros': true}"/> |
| 793 |
<field name="currency_id" invisible="1"/> |
| 794 |
</form>`, |
| 795 |
}); |
| 796 |
expect(".o_field_widget input").toHaveValue("9.1"); |
| 797 |
expect(".o_field_widget .o_input span:eq(0)").toHaveText("$"); |
| 798 |
}); |