| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { queryAll, queryFirst } from "@odoo/hoot-dom"; |
| 3 |
import { Deferred, animationFrame } from "@odoo/hoot-mock"; |
| 4 |
import { |
| 5 |
clickSave, |
| 6 |
contains, |
| 7 |
defineModels, |
| 8 |
fieldInput, |
| 9 |
fields, |
| 10 |
models, |
| 11 |
mountView, |
| 12 |
onRpc, |
| 13 |
serverState, |
| 14 |
} from "@web/../tests/web_test_helpers"; |
| 15 |
|
| 16 |
class Currency extends models.Model { |
| 17 |
digits = fields.Integer(); |
| 18 |
symbol = fields.Char({ string: "Currency Symbol" }); |
| 19 |
position = fields.Char({ string: "Currency Position" }); |
| 20 |
|
| 21 |
_records = [ |
| 22 |
{ |
| 23 |
id: 1, |
| 24 |
display_name: "$", |
| 25 |
symbol: "$", |
| 26 |
position: "before", |
| 27 |
}, |
| 28 |
{ |
| 29 |
id: 2, |
| 30 |
display_name: "€", |
| 31 |
symbol: "€", |
| 32 |
position: "after", |
| 33 |
}, |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
class Partner extends models.Model { |
| 38 |
_name = "res.partner"; |
| 39 |
_inherit = []; |
| 40 |
|
| 41 |
name = fields.Char({ |
| 42 |
string: "Name", |
| 43 |
default: "My little Name Value", |
| 44 |
trim: true, |
| 45 |
}); |
| 46 |
int_field = fields.Integer(); |
| 47 |
partner_ids = fields.One2many({ |
| 48 |
string: "one2many field", |
| 49 |
relation: "res.partner", |
| 50 |
}); |
| 51 |
product_id = fields.Many2one({ relation: "product" }); |
| 52 |
|
| 53 |
placeholder_name = fields.Char(); |
| 54 |
|
| 55 |
_records = [ |
| 56 |
{ |
| 57 |
id: 1, |
| 58 |
display_name: "first record", |
| 59 |
name: "yop", |
| 60 |
int_field: 10, |
| 61 |
partner_ids: [], |
| 62 |
placeholder_name: "Placeholder Name", |
| 63 |
}, |
| 64 |
{ |
| 65 |
id: 2, |
| 66 |
display_name: "second record", |
| 67 |
name: "blip", |
| 68 |
int_field: 0, |
| 69 |
partner_ids: [], |
| 70 |
}, |
| 71 |
{ id: 3, name: "gnap", int_field: 80 }, |
| 72 |
{ |
| 73 |
id: 4, |
| 74 |
display_name: "aaa", |
| 75 |
name: "abc", |
| 76 |
}, |
| 77 |
{ id: 5, name: "blop", int_field: -4 }, |
| 78 |
]; |
| 79 |
|
| 80 |
_views = { |
| 81 |
form: /* xml */ ` |
| 82 |
<form> |
| 83 |
<sheet> |
| 84 |
<group> |
| 85 |
<field name="name"/> |
| 86 |
</group> |
| 87 |
</sheet> |
| 88 |
</form> |
| 89 |
`, |
| 90 |
}; |
| 91 |
} |
| 92 |
|
| 93 |
class PartnerType extends models.Model { |
| 94 |
color = fields.Integer({ string: "Color index" }); |
| 95 |
name = fields.Char({ string: "Partner Type" }); |
| 96 |
|
| 97 |
_records = [ |
| 98 |
{ id: 12, display_name: "gold", color: 2 }, |
| 99 |
{ id: 14, display_name: "silver", color: 5 }, |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
class Product extends models.Model { |
| 104 |
name = fields.Char({ string: "Product Name" }); |
| 105 |
|
| 106 |
_records = [ |
| 107 |
{ |
| 108 |
id: 37, |
| 109 |
name: "xphone", |
| 110 |
}, |
| 111 |
{ |
| 112 |
id: 41, |
| 113 |
name: "xpad", |
| 114 |
}, |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
class Users extends models.Model { |
| 119 |
_name = "res.users"; |
| 120 |
|
| 121 |
name = fields.Char(); |
| 122 |
|
| 123 |
has_group() { |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
_records = [ |
| 128 |
{ |
| 129 |
id: 1, |
| 130 |
name: "Aline", |
| 131 |
}, |
| 132 |
{ |
| 133 |
id: 2, |
| 134 |
name: "Christine", |
| 135 |
}, |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
defineModels([Currency, Partner, PartnerType, Product, Users]); |
| 140 |
|
| 141 |
test("char field in form view", async () => { |
| 142 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 143 |
|
| 144 |
expect(".o_field_widget input[type='text']").toHaveCount(1, { |
| 145 |
message: "should have an input for the char field", |
| 146 |
}); |
| 147 |
expect(".o_field_widget input[type='text']").toHaveValue("yop", { |
| 148 |
message: "input should contain field value in edit mode", |
| 149 |
}); |
| 150 |
await fieldInput("name").edit("limbo"); |
| 151 |
await clickSave(); |
| 152 |
expect(".o_field_widget input[type='text']").toHaveValue("limbo", { |
| 153 |
message: "the new value should be displayed", |
| 154 |
}); |
| 155 |
}); |
| 156 |
|
| 157 |
test("basic rendering text field", async () => { |
| 158 |
Product._fields.description = fields.Text(); |
| 159 |
Product._records = [{ id: 1, description: "Description as text" }]; |
| 160 |
await mountView({ |
| 161 |
type: "form", |
| 162 |
resModel: "product", |
| 163 |
resId: 1, |
| 164 |
arch: '<form><field name="description" widget="char"/></form>', |
| 165 |
}); |
| 166 |
|
| 167 |
expect(".o_field_widget input[type='text']").toHaveCount(1); |
| 168 |
expect(".o_field_widget input[type='text']").toHaveValue("Description as text"); |
| 169 |
}); |
| 170 |
|
| 171 |
test("setting a char field to empty string is saved as a false value", async () => { |
| 172 |
expect.assertions(1); |
| 173 |
|
| 174 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 175 |
|
| 176 |
onRpc("web_save", ({ args }) => { |
| 177 |
expect(args[1].name).toBe(false); |
| 178 |
}); |
| 179 |
await fieldInput("name").clear(); |
| 180 |
await clickSave(); |
| 181 |
}); |
| 182 |
|
| 183 |
test("char field with size attribute", async () => { |
| 184 |
Partner._fields.name.size = 5; |
| 185 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 186 |
|
| 187 |
expect("input").toHaveAttribute("maxlength", "5", { |
| 188 |
message: "maxlength attribute should have been set correctly on the input", |
| 189 |
}); |
| 190 |
}); |
| 191 |
|
| 192 |
test.tags("desktop"); |
| 193 |
test("char field in editable list view", async () => { |
| 194 |
await mountView({ |
| 195 |
type: "list", |
| 196 |
resModel: "res.partner", |
| 197 |
arch: ` |
| 198 |
<list editable="bottom"> |
| 199 |
<field name="name" /> |
| 200 |
</list>`, |
| 201 |
}); |
| 202 |
|
| 203 |
expect("tbody td:not(.o_list_record_selector)").toHaveCount(5, { |
| 204 |
message: "should have 5 cells", |
| 205 |
}); |
| 206 |
expect("tbody td:not(.o_list_record_selector):first").toHaveText("yop", { |
| 207 |
message: "value should be displayed properly as text", |
| 208 |
}); |
| 209 |
|
| 210 |
const cellSelector = "tbody td:not(.o_list_record_selector)"; |
| 211 |
await contains(cellSelector).click(); |
| 212 |
expect(queryFirst(cellSelector).parentElement).toHaveClass("o_selected_row", { |
| 213 |
message: "should be set as edit mode", |
| 214 |
}); |
| 215 |
expect(`${cellSelector} input`).toHaveValue("yop", { |
| 216 |
message: "should have the corect value in internal input", |
| 217 |
}); |
| 218 |
await fieldInput("name").edit("brolo", { confirm: false }); |
| 219 |
|
| 220 |
await contains(".o_list_button_save").click(); |
| 221 |
expect(cellSelector).not.toHaveClass("o_selected_row", { |
| 222 |
message: "should not be in edit mode anymore", |
| 223 |
}); |
| 224 |
}); |
| 225 |
|
| 226 |
test("char field translatable", async () => { |
| 227 |
Partner._fields.name.translate = true; |
| 228 |
|
| 229 |
serverState.lang = "en_US"; |
| 230 |
serverState.multiLang = true; |
| 231 |
|
| 232 |
await mountView({ |
| 233 |
type: "form", |
| 234 |
resModel: "res.partner", |
| 235 |
resId: 1, |
| 236 |
}); |
| 237 |
|
| 238 |
let callGetFieldTranslations = 0; |
| 239 |
onRpc("res.lang", "get_installed", () => [ |
| 240 |
["en_US", "English"], |
| 241 |
["fr_BE", "French (Belgium)"], |
| 242 |
["es_ES", "Spanish"], |
| 243 |
]); |
| 244 |
onRpc("res.partner", "get_field_translations", () => { |
| 245 |
if (callGetFieldTranslations++ === 0) { |
| 246 |
return [ |
| 247 |
[ |
| 248 |
{ lang: "en_US", source: "yop", value: "yop" }, |
| 249 |
{ lang: "fr_BE", source: "yop", value: "yop français" }, |
| 250 |
{ lang: "es_ES", source: "yop", value: "yop español" }, |
| 251 |
], |
| 252 |
{ translation_type: "char", translation_show_source: false }, |
| 253 |
]; |
| 254 |
} else { |
| 255 |
return [ |
| 256 |
[ |
| 257 |
{ lang: "en_US", source: "bar", value: "bar" }, |
| 258 |
{ lang: "fr_BE", source: "bar", value: "yop français" }, |
| 259 |
{ lang: "es_ES", source: "bar", value: "bar" }, |
| 260 |
], |
| 261 |
{ translation_type: "char", translation_show_source: false }, |
| 262 |
]; |
| 263 |
} |
| 264 |
}); |
| 265 |
onRpc("res.partner", "update_field_translations", function ({ args, kwargs }) { |
| 266 |
expect(args[2]).toEqual( |
| 267 |
{ en_US: "bar", es_ES: false }, |
| 268 |
{ |
| 269 |
message: |
| 270 |
"the new translation value should be written and the value false voids the translation", |
| 271 |
} |
| 272 |
); |
| 273 |
for (const record of this.env["res.partner"].browse(args[0])) { |
| 274 |
record[args[1]] = args[2][kwargs.context.lang]; |
| 275 |
} |
| 276 |
return true; |
| 277 |
}); |
| 278 |
expect("[name=name] input").toHaveClass("o_field_translate"); |
| 279 |
await contains("[name=name] input").click(); |
| 280 |
expect(".o_field_char .btn.o_field_translate").toHaveCount(1, { |
| 281 |
message: "should have a translate button", |
| 282 |
}); |
| 283 |
expect(".o_field_char .btn.o_field_translate").toHaveText("EN", { |
| 284 |
message: "the button should have as test the current language", |
| 285 |
}); |
| 286 |
await contains(".o_field_char .btn.o_field_translate").click(); |
| 287 |
expect(".modal").toHaveCount(1, { |
| 288 |
message: "a translate modal should be visible", |
| 289 |
}); |
| 290 |
expect(".modal .o_translation_dialog .translation").toHaveCount(3, { |
| 291 |
message: "three rows should be visible", |
| 292 |
}); |
| 293 |
let translations = queryAll(".modal .o_translation_dialog .translation input"); |
| 294 |
expect(translations[0]).toHaveValue("yop", { |
| 295 |
message: "English translation should be filled", |
| 296 |
}); |
| 297 |
expect(translations[1]).toHaveValue("yop français", { |
| 298 |
message: "French translation should be filled", |
| 299 |
}); |
| 300 |
expect(translations[2]).toHaveValue("yop español", { |
| 301 |
message: "Spanish translation should be filled", |
| 302 |
}); |
| 303 |
await contains(translations[0]).edit("bar"); |
| 304 |
await contains(translations[2]).clear(); |
| 305 |
await contains("footer .btn.btn-primary").click(); |
| 306 |
expect(".o_field_widget.o_field_char input").toHaveValue("bar", { |
| 307 |
message: "the new translation should be transfered to modified record", |
| 308 |
}); |
| 309 |
await fieldInput("name").edit("baz"); |
| 310 |
await contains(".o_field_char .btn.o_field_translate").click(); |
| 311 |
|
| 312 |
translations = queryAll(".modal .o_translation_dialog .translation input"); |
| 313 |
expect(translations[0]).toHaveValue("baz", { |
| 314 |
message: "Modified value should be used instead of translation", |
| 315 |
}); |
| 316 |
expect(translations[1]).toHaveValue("yop français", { |
| 317 |
message: "French translation shouldn't be changed", |
| 318 |
}); |
| 319 |
expect(translations[2]).toHaveValue("bar", { |
| 320 |
message: "Spanish translation should fallback to the English translation", |
| 321 |
}); |
| 322 |
}); |
| 323 |
|
| 324 |
test("translation dialog should close if field is not there anymore", async () => { |
| 325 |
expect.assertions(4); |
| 326 |
// In this test, we simulate the case where the field is removed from the view |
| 327 |
// this can happen for example if the user click the back button of the browser. |
| 328 |
Partner._fields.name.translate = true; |
| 329 |
|
| 330 |
serverState.lang = "en_US"; |
| 331 |
serverState.multiLang = true; |
| 332 |
|
| 333 |
await mountView({ |
| 334 |
type: "form", |
| 335 |
resModel: "res.partner", |
| 336 |
resId: 1, |
| 337 |
arch: ` |
| 338 |
<form> |
| 339 |
<sheet> |
| 340 |
<group> |
| 341 |
<field name="int_field" /> |
| 342 |
<field name="name" invisible="int_field == 9"/> |
| 343 |
</group> |
| 344 |
</sheet> |
| 345 |
</form>`, |
| 346 |
}); |
| 347 |
onRpc("res.lang", "get_installed", () => [ |
| 348 |
["en_US", "English"], |
| 349 |
["fr_BE", "French (Belgium)"], |
| 350 |
["es_ES", "Spanish"], |
| 351 |
]); |
| 352 |
onRpc("res.partner", "get_field_translations", () => [ |
| 353 |
[ |
| 354 |
{ lang: "en_US", source: "yop", value: "yop" }, |
| 355 |
{ lang: "fr_BE", source: "yop", value: "valeur français" }, |
| 356 |
{ lang: "es_ES", source: "yop", value: "yop español" }, |
| 357 |
], |
| 358 |
{ translation_type: "char", translation_show_source: false }, |
| 359 |
]); |
| 360 |
expect("[name=name] input").toHaveClass("o_field_translate"); |
| 361 |
await contains("[name=name] input").click(); |
| 362 |
await contains(".o_field_char .btn.o_field_translate").click(); |
| 363 |
expect(".modal").toHaveCount(1, { |
| 364 |
message: "a translate modal should be visible", |
| 365 |
}); |
| 366 |
await fieldInput("int_field").edit("9"); |
| 367 |
await animationFrame(); |
| 368 |
expect("[name=name] input").toHaveCount(0, { |
| 369 |
message: "the field name should be invisible", |
| 370 |
}); |
| 371 |
expect(".modal").toHaveCount(0, { |
| 372 |
message: "a translate modal should not be visible", |
| 373 |
}); |
| 374 |
}); |
| 375 |
|
| 376 |
test("html field translatable", async () => { |
| 377 |
expect.assertions(5); |
| 378 |
Partner._fields.name.translate = true; |
| 379 |
|
| 380 |
serverState.lang = "en_US"; |
| 381 |
serverState.multiLang = true; |
| 382 |
|
| 383 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 384 |
|
| 385 |
onRpc("res.lang", "get_installed", () => [ |
| 386 |
["en_US", "English"], |
| 387 |
["fr_BE", "French (Belgium)"], |
| 388 |
]); |
| 389 |
onRpc("res.partner", "get_field_translations", () => [ |
| 390 |
[ |
| 391 |
{ |
| 392 |
lang: "en_US", |
| 393 |
source: "first paragraph", |
| 394 |
value: "first paragraph", |
| 395 |
}, |
| 396 |
{ |
| 397 |
lang: "en_US", |
| 398 |
source: "second paragraph", |
| 399 |
value: "second paragraph", |
| 400 |
}, |
| 401 |
{ |
| 402 |
lang: "fr_BE", |
| 403 |
source: "first paragraph", |
| 404 |
value: "premier paragraphe", |
| 405 |
}, |
| 406 |
{ |
| 407 |
lang: "fr_BE", |
| 408 |
source: "second paragraph", |
| 409 |
value: "deuxième paragraphe", |
| 410 |
}, |
| 411 |
], |
| 412 |
{ |
| 413 |
translation_type: "char", |
| 414 |
translation_show_source: true, |
| 415 |
}, |
| 416 |
]); |
| 417 |
onRpc("res.partner", "update_field_translations", ({ args }) => { |
| 418 |
expect(args[2]).toEqual( |
| 419 |
{ en_US: { "first paragraph": "first paragraph modified" } }, |
| 420 |
{ message: "the new translation value should be written" } |
| 421 |
); |
| 422 |
return true; |
| 423 |
}); |
| 424 |
|
| 425 |
// this will not affect the translate_fields effect until the record is |
| 426 |
// saved but is set for consistency of the test |
| 427 |
await fieldInput("name").edit("<p>first paragraph</p><p>second paragraph</p>"); |
| 428 |
await contains(".o_field_char .btn.o_field_translate").click(); |
| 429 |
expect(".modal").toHaveCount(1, { |
| 430 |
message: "a translate modal should be visible", |
| 431 |
}); |
| 432 |
expect(".modal .o_translation_dialog .translation").toHaveCount(4, { |
| 433 |
message: "four rows should be visible", |
| 434 |
}); |
| 435 |
const enField = queryFirst(".modal .o_translation_dialog .translation input"); |
| 436 |
expect(enField).toHaveValue("first paragraph", { |
| 437 |
message: "first part of english translation should be filled", |
| 438 |
}); |
| 439 |
await contains(enField).edit("first paragraph modified"); |
| 440 |
await contains(".modal button.btn-primary").click(); |
| 441 |
expect(".o_field_char input[type='text']").toHaveValue( |
| 442 |
"<p>first paragraph</p><p>second paragraph</p>", |
| 443 |
{ |
| 444 |
message: "the new partial translation should not be transfered", |
| 445 |
} |
| 446 |
); |
| 447 |
}); |
| 448 |
|
| 449 |
test("char field translatable in create mode", async () => { |
| 450 |
Partner._fields.name.translate = true; |
| 451 |
|
| 452 |
serverState.multiLang = true; |
| 453 |
|
| 454 |
await mountView({ type: "form", resModel: "res.partner" }); |
| 455 |
|
| 456 |
expect(".o_field_char .btn.o_field_translate").toHaveCount(1, { |
| 457 |
message: "should have a translate button in create mode", |
| 458 |
}); |
| 459 |
}); |
| 460 |
|
| 461 |
test("char field does not allow html injections", async () => { |
| 462 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 463 |
await fieldInput("name").edit("<script>throw Error();</script>"); |
| 464 |
await clickSave(); |
| 465 |
expect(".o_field_widget input").toHaveValue("<script>throw Error();</script>", { |
| 466 |
message: "the value should have been properly escaped", |
| 467 |
}); |
| 468 |
}); |
| 469 |
|
| 470 |
test("char field trim (or not) characters", async () => { |
| 471 |
Partner._fields.foo2 = fields.Char({ trim: false }); |
| 472 |
|
| 473 |
await mountView({ |
| 474 |
type: "form", |
| 475 |
resModel: "res.partner", |
| 476 |
resId: 1, |
| 477 |
arch: ` |
| 478 |
<form> |
| 479 |
<sheet> |
| 480 |
<group> |
| 481 |
<field name="name" /> |
| 482 |
<field name="foo2" /> |
| 483 |
</group> |
| 484 |
</sheet> |
| 485 |
</form>`, |
| 486 |
}); |
| 487 |
|
| 488 |
await fieldInput("name").edit(" abc "); |
| 489 |
await fieldInput("foo2").edit(" def "); |
| 490 |
await clickSave(); |
| 491 |
expect(".o_field_widget[name='name'] input").toHaveValue("abc", { |
| 492 |
message: "Name value should have been trimmed", |
| 493 |
}); |
| 494 |
expect(".o_field_widget[name='foo2'] input:only").toHaveValue(" def "); |
| 495 |
}); |
| 496 |
|
| 497 |
test.tags("desktop"); |
| 498 |
test("input field: change value before pending onchange returns", async () => { |
| 499 |
await mountView({ |
| 500 |
type: "form", |
| 501 |
resModel: "res.partner", |
| 502 |
resId: 1, |
| 503 |
arch: ` |
| 504 |
<form> |
| 505 |
<sheet> |
| 506 |
<field name="partner_ids"> |
| 507 |
<list editable="bottom"> |
| 508 |
<field name="product_id" /> |
| 509 |
<field name="name" /> |
| 510 |
</list> |
| 511 |
</field> |
| 512 |
</sheet> |
| 513 |
</form>`, |
| 514 |
}); |
| 515 |
|
| 516 |
let def; |
| 517 |
onRpc("onchange", () => def); |
| 518 |
|
| 519 |
await contains(".o_field_x2many_list_row_add a").click(); |
| 520 |
expect(".o_field_widget[name='name'] input").toHaveValue("My little Name Value", { |
| 521 |
message: "should contain the default value", |
| 522 |
}); |
| 523 |
|
| 524 |
def = new Deferred(); |
| 525 |
await contains(".o-autocomplete--input").click(); |
| 526 |
await contains(".o-autocomplete--dropdown-item").click(); |
| 527 |
await fieldInput("name").edit("tralala", { confirm: false }); |
| 528 |
expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 529 |
message: "should contain tralala", |
| 530 |
}); |
| 531 |
def.resolve(); |
| 532 |
await animationFrame(); |
| 533 |
expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 534 |
message: "should contain the same value as before onchange", |
| 535 |
}); |
| 536 |
}); |
| 537 |
|
| 538 |
test("input field: change value before pending onchange returns (2)", async () => { |
| 539 |
Partner._onChanges.int_field = (obj) => { |
| 540 |
if (obj.int_field === 7) { |
| 541 |
obj.name = "blabla"; |
| 542 |
} else { |
| 543 |
obj.name = "tralala"; |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
const def = new Deferred(); |
| 548 |
await mountView({ |
| 549 |
type: "form", |
| 550 |
resModel: "res.partner", |
| 551 |
resId: 1, |
| 552 |
arch: ` |
| 553 |
<form> |
| 554 |
<sheet> |
| 555 |
<field name="int_field" /> |
| 556 |
<field name="name" /> |
| 557 |
</sheet> |
| 558 |
</form>`, |
| 559 |
}); |
| 560 |
|
| 561 |
onRpc("onchange", () => def); |
| 562 |
|
| 563 |
expect(".o_field_widget[name='name'] input").toHaveValue("yop", { |
| 564 |
message: "should contain the correct value", |
| 565 |
}); |
| 566 |
|
| 567 |
// trigger a deferred onchange |
| 568 |
await fieldInput("int_field").edit("7"); |
| 569 |
await fieldInput("name").edit("test", { confirm: false }); |
| 570 |
|
| 571 |
def.resolve(); |
| 572 |
await animationFrame(); |
| 573 |
expect(".o_field_widget[name='name'] input").toHaveValue("test", { |
| 574 |
message: "The onchage value should not be applied because the input is in edition", |
| 575 |
}); |
| 576 |
await fieldInput("name").press("Enter"); |
| 577 |
await expect(".o_field_widget[name='name'] input").toHaveValue("test"); |
| 578 |
await fieldInput("int_field").edit("10"); |
| 579 |
await expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 580 |
message: "The onchange value should be applied because the input is not in edition", |
| 581 |
}); |
| 582 |
}); |
| 583 |
|
| 584 |
test.tags("desktop"); |
| 585 |
test("input field: change value before pending onchange returns (with fieldDebounce)", async () => { |
| 586 |
// this test is exactly the same as the previous one, except that in |
| 587 |
// this scenario the onchange return *before* we validate the change |
| 588 |
// on the input field (before the "change" event is triggered). |
| 589 |
Partner._onChanges.product_id = (obj) => { |
| 590 |
obj.int_field = obj.product_id ? 7 : false; |
| 591 |
}; |
| 592 |
let def; |
| 593 |
|
| 594 |
await mountView({ |
| 595 |
type: "form", |
| 596 |
resModel: "res.partner", |
| 597 |
arch: ` |
| 598 |
<form> |
| 599 |
<field name="partner_ids"> |
| 600 |
<list editable="bottom"> |
| 601 |
<field name="product_id"/> |
| 602 |
<field name="name"/> |
| 603 |
<field name="int_field"/> |
| 604 |
</list> |
| 605 |
</field> |
| 606 |
</form>`, |
| 607 |
}); |
| 608 |
|
| 609 |
onRpc("onchange", () => def); |
| 610 |
|
| 611 |
await contains(".o_field_x2many_list_row_add a").click(); |
| 612 |
expect(".o_field_widget[name='name'] input").toHaveValue("My little Name Value", { |
| 613 |
message: "should contain the default value", |
| 614 |
}); |
| 615 |
|
| 616 |
def = new Deferred(); |
| 617 |
await contains(".o-autocomplete--input").click(); |
| 618 |
await contains(".o-autocomplete--dropdown-item").click(); |
| 619 |
await fieldInput("name").edit("tralala", { confirm: false }); |
| 620 |
expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 621 |
message: "should contain tralala", |
| 622 |
}); |
| 623 |
expect(".o_field_widget[name='int_field'] input").toHaveValue(""); |
| 624 |
def.resolve(); |
| 625 |
await animationFrame(); |
| 626 |
expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 627 |
message: "should contain the same value as before onchange", |
| 628 |
}); |
| 629 |
expect(".o_field_widget[name='int_field'] input").toHaveValue("7", { |
| 630 |
message: "should contain the value returned by the onchange", |
| 631 |
}); |
| 632 |
}); |
| 633 |
|
| 634 |
test("onchange return value before editing input", async () => { |
| 635 |
Partner._onChanges.name = (obj) => { |
| 636 |
obj.name = "yop"; |
| 637 |
}; |
| 638 |
await mountView({ type: "form", resModel: "res.partner", resId: 1 }); |
| 639 |
expect(".o_field_widget[name='name'] input").toHaveValue("yop"); |
| 640 |
await fieldInput("name").edit("tralala"); |
| 641 |
await expect("[name='name'] input").toHaveValue("yop"); |
| 642 |
}); |
| 643 |
|
| 644 |
test.tags("desktop"); |
| 645 |
test("input field: change value before pending onchange renaming", async () => { |
| 646 |
Partner._onChanges.product_id = (obj) => { |
| 647 |
obj.name = "on change value"; |
| 648 |
}; |
| 649 |
|
| 650 |
await mountView({ |
| 651 |
type: "form", |
| 652 |
resModel: "res.partner", |
| 653 |
resId: 1, |
| 654 |
arch: ` |
| 655 |
<form> |
| 656 |
<sheet> |
| 657 |
<field name="product_id" /> |
| 658 |
<field name="name" /> |
| 659 |
</sheet> |
| 660 |
</form>`, |
| 661 |
}); |
| 662 |
|
| 663 |
onRpc("onchange", () => def); |
| 664 |
|
| 665 |
const def = new Deferred(); |
| 666 |
|
| 667 |
expect(".o_field_widget[name='name'] input").toHaveValue("yop", { |
| 668 |
message: "should contain the correct value", |
| 669 |
}); |
| 670 |
await contains(".o-autocomplete--input").click(); |
| 671 |
await contains(".o-autocomplete--dropdown-item").click(); |
| 672 |
// set name before onchange |
| 673 |
await fieldInput("name").edit("tralala"); |
| 674 |
await expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 675 |
message: "should contain tralala", |
| 676 |
}); |
| 677 |
|
| 678 |
// complete the onchange |
| 679 |
def.resolve(); |
| 680 |
await animationFrame(); |
| 681 |
expect(".o_field_widget[name='name'] input").toHaveValue("tralala", { |
| 682 |
message: "input should contain the same value as before onchange", |
| 683 |
}); |
| 684 |
}); |
| 685 |
|
| 686 |
test("support autocomplete attribute", async () => { |
| 687 |
await mountView({ |
| 688 |
type: "form", |
| 689 |
resModel: "res.partner", |
| 690 |
resId: 1, |
| 691 |
arch: ` |
| 692 |
<form> |
| 693 |
<field name="name" autocomplete="coucou"/> |
| 694 |
</form>`, |
| 695 |
}); |
| 696 |
expect(".o_field_widget[name='name'] input").toHaveAttribute("autocomplete", "coucou", { |
| 697 |
message: "attribute autocomplete should be set", |
| 698 |
}); |
| 699 |
}); |
| 700 |
|
| 701 |
test("input autocomplete attribute set to none by default", async () => { |
| 702 |
await mountView({ |
| 703 |
type: "form", |
| 704 |
resModel: "res.partner", |
| 705 |
resId: 1, |
| 706 |
arch: ` |
| 707 |
<form> |
| 708 |
<field name="name"/> |
| 709 |
</form>`, |
| 710 |
}); |
| 711 |
expect(".o_field_widget[name='name'] input").toHaveAttribute("autocomplete", "off", { |
| 712 |
message: "attribute autocomplete should be set to none by default", |
| 713 |
}); |
| 714 |
}); |
| 715 |
|
| 716 |
test("support password attribute", async () => { |
| 717 |
await mountView({ |
| 718 |
type: "form", |
| 719 |
resModel: "res.partner", |
| 720 |
resId: 1, |
| 721 |
arch: ` |
| 722 |
<form> |
| 723 |
<field name="name" password="True"/> |
| 724 |
</form>`, |
| 725 |
}); |
| 726 |
expect(".o_field_widget[name='name'] input").toHaveValue("yop", { |
| 727 |
message: "input value should be the password", |
| 728 |
}); |
| 729 |
expect(".o_field_widget[name='name'] input").toHaveAttribute("type", "password", { |
| 730 |
message: "input should be of type password", |
| 731 |
}); |
| 732 |
}); |
| 733 |
|
| 734 |
test("input field: readonly password", async () => { |
| 735 |
await mountView({ |
| 736 |
type: "form", |
| 737 |
resModel: "res.partner", |
| 738 |
resId: 1, |
| 739 |
arch: ` |
| 740 |
<form> |
| 741 |
<field name="name" password="True" readonly="1"/> |
| 742 |
</form>`, |
| 743 |
}); |
| 744 |
|
| 745 |
expect(".o_field_char").not.toHaveText("yop", { |
| 746 |
message: "password field value should be visible in read mode", |
| 747 |
}); |
| 748 |
|
| 749 |
expect(".o_field_char").toHaveText("***", { |
| 750 |
message: "password field value should be hidden with '*' in read mode", |
| 751 |
}); |
| 752 |
}); |
| 753 |
|
| 754 |
test("input field: change password value", async () => { |
| 755 |
await mountView({ |
| 756 |
type: "form", |
| 757 |
resModel: "res.partner", |
| 758 |
resId: 1, |
| 759 |
arch: ` |
| 760 |
<form> |
| 761 |
<field name="name" password="True"/> |
| 762 |
</form>`, |
| 763 |
}); |
| 764 |
|
| 765 |
expect(".o_field_char input").toHaveAttribute("type", "password", { |
| 766 |
message: "password field input value should with type 'password' in edit mode", |
| 767 |
}); |
| 768 |
expect(".o_field_char input").toHaveValue("yop", { |
| 769 |
message: "password field input value should be the (hidden) password value", |
| 770 |
}); |
| 771 |
}); |
| 772 |
|
| 773 |
test("input field: empty password", async () => { |
| 774 |
Partner._records[0].name = false; |
| 775 |
await mountView({ |
| 776 |
type: "form", |
| 777 |
resModel: "res.partner", |
| 778 |
resId: 1, |
| 779 |
arch: ` |
| 780 |
<form> |
| 781 |
<field name="name" password="True"/> |
| 782 |
</form>`, |
| 783 |
}); |
| 784 |
expect(".o_field_char input").toHaveAttribute("type", "password", { |
| 785 |
message: "password field input value should with type 'password' in edit mode", |
| 786 |
}); |
| 787 |
expect(".o_field_char input").toHaveValue("", { |
| 788 |
message: "password field input value should be the (non-hidden, empty) password value", |
| 789 |
}); |
| 790 |
}); |
| 791 |
|
| 792 |
test.tags("desktop"); |
| 793 |
test("input field: set and remove value, then wait for onchange", async () => { |
| 794 |
Partner._onChanges.product_id = (obj) => { |
| 795 |
obj.name = obj.product_id ? "onchange value" : false; |
| 796 |
}; |
| 797 |
|
| 798 |
await mountView({ |
| 799 |
type: "form", |
| 800 |
resModel: "res.partner", |
| 801 |
arch: ` |
| 802 |
<form> |
| 803 |
<field name="partner_ids"> |
| 804 |
<list editable="bottom"> |
| 805 |
<field name="product_id"/> |
| 806 |
<field name="name"/> |
| 807 |
</list> |
| 808 |
</field> |
| 809 |
</form>`, |
| 810 |
}); |
| 811 |
await contains(".o_field_x2many_list_row_add a").click(); |
| 812 |
expect(".o_field_widget[name=name] input").toHaveValue(""); |
| 813 |
await fieldInput("name").edit("test", { confirm: false }); |
| 814 |
await fieldInput("name").clear({ confirm: false }); |
| 815 |
|
| 816 |
// trigger the onchange by setting a product |
| 817 |
await contains(".o-autocomplete--input").click(); |
| 818 |
await contains(".o-autocomplete--dropdown-item").click(); |
| 819 |
expect(".o_field_widget[name=name] input").toHaveValue("onchange value", { |
| 820 |
message: "input should contain correct value after onchange", |
| 821 |
}); |
| 822 |
}); |
| 823 |
|
| 824 |
test("char field with placeholder", async () => { |
| 825 |
Partner._fields.name.default = false; |
| 826 |
await mountView({ |
| 827 |
type: "form", |
| 828 |
resModel: "res.partner", |
| 829 |
arch: ` |
| 830 |
<form> |
| 831 |
<sheet> |
| 832 |
<group> |
| 833 |
<field name="name" placeholder="Placeholder" /> |
| 834 |
</group> |
| 835 |
</sheet> |
| 836 |
</form>`, |
| 837 |
}); |
| 838 |
expect(".o_field_widget[name='name'] input").toHaveAttribute("placeholder", "Placeholder", { |
| 839 |
message: "placeholder attribute should be set", |
| 840 |
}); |
| 841 |
}); |
| 842 |
|
| 843 |
test("Form: placeholder_field shows as placeholder", async () => { |
| 844 |
Partner._records[0].name = false; |
| 845 |
await mountView({ |
| 846 |
type: "form", |
| 847 |
resModel: "res.partner", |
| 848 |
resId: 1, |
| 849 |
arch: ` |
| 850 |
<form> |
| 851 |
<sheet> |
| 852 |
<group> |
| 853 |
<field name="placeholder_name" invisible="1" /> |
| 854 |
<field name="name" options="{'placeholder_field': 'placeholder_name'}" /> |
| 855 |
</group> |
| 856 |
</sheet> |
| 857 |
</form>`, |
| 858 |
}); |
| 859 |
expect("input").toHaveValue("", { |
| 860 |
message: "should have no value in input", |
| 861 |
}); |
| 862 |
expect("input").toHaveAttribute("placeholder", "Placeholder Name", { |
| 863 |
message: "placeholder_field should be the placeholder", |
| 864 |
}); |
| 865 |
}); |
| 866 |
|
| 867 |
test("char field: correct value is used to evaluate the modifiers", async () => { |
| 868 |
Partner._records[0].name = false; |
| 869 |
Partner._records[0].display_name = false; |
| 870 |
Partner._onChanges.name = (obj) => { |
| 871 |
if (obj.name === "a") { |
| 872 |
obj.display_name = false; |
| 873 |
} else if (obj.name === "b") { |
| 874 |
obj.display_name = ""; |
| 875 |
} |
| 876 |
}; |
| 877 |
await mountView({ |
| 878 |
type: "form", |
| 879 |
resModel: "res.partner", |
| 880 |
resId: 1, |
| 881 |
arch: ` |
| 882 |
<form> |
| 883 |
<field name="name" /> |
| 884 |
<field name="display_name" invisible="'' == display_name"/> |
| 885 |
</form>`, |
| 886 |
}); |
| 887 |
expect("[name='display_name']").toHaveCount(1); |
| 888 |
|
| 889 |
await fieldInput("name").edit("a"); |
| 890 |
await animationFrame(); |
| 891 |
expect("[name='display_name']").toHaveCount(1); |
| 892 |
|
| 893 |
await fieldInput("name").edit("b"); |
| 894 |
await animationFrame(); |
| 895 |
expect("[name='display_name']").toHaveCount(0); |
| 896 |
}); |
| 897 |
|
| 898 |
test("edit a char field should display the status indicator buttons without flickering", async () => { |
| 899 |
Partner._records[0].partner_ids = [2]; |
| 900 |
Partner._onChanges.name = (obj) => { |
| 901 |
obj.display_name = "cc"; |
| 902 |
}; |
| 903 |
|
| 904 |
const def = new Deferred(); |
| 905 |
await mountView({ |
| 906 |
type: "form", |
| 907 |
resModel: "res.partner", |
| 908 |
resId: 1, |
| 909 |
arch: ` |
| 910 |
<form> |
| 911 |
<field name="partner_ids"> |
| 912 |
<list editable="bottom"> |
| 913 |
<field name="name"/> |
| 914 |
</list> |
| 915 |
</field> |
| 916 |
</form>`, |
| 917 |
}); |
| 918 |
onRpc("onchange", () => { |
| 919 |
expect.step("onchange"); |
| 920 |
return def; |
| 921 |
}); |
| 922 |
expect(".o_form_status_indicator_buttons").not.toBeVisible({ |
| 923 |
message: "form view is not dirty", |
| 924 |
}); |
| 925 |
await contains(".o_data_cell").click(); |
| 926 |
await fieldInput("name").edit("a"); |
| 927 |
expect(".o_form_status_indicator_buttons").toBeVisible({ |
| 928 |
message: "form view is dirty", |
| 929 |
}); |
| 930 |
def.resolve(); |
| 931 |
expect.verifySteps(["onchange"]); |
| 932 |
await animationFrame(); |
| 933 |
expect(".o_form_status_indicator_buttons").toBeVisible({ |
| 934 |
message: "form view is dirty", |
| 935 |
}); |
| 936 |
expect.verifySteps(["onchange"]); |
| 937 |
}); |