| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { queryAllTexts } from "@odoo/hoot-dom"; |
| 3 |
import { runAllTimers } from "@odoo/hoot-mock"; |
| 4 |
import { |
| 5 |
clickSave, |
| 6 |
contains, |
| 7 |
defineModels, |
| 8 |
fields, |
| 9 |
models, |
| 10 |
mountView, |
| 11 |
onRpc, |
| 12 |
} from "@web/../tests/web_test_helpers"; |
| 13 |
|
| 14 |
class Partner extends models.Model { |
| 15 |
int_field = fields.Integer({ sortable: true }); |
| 16 |
timmy = fields.Many2many({ string: "pokemon", relation: "partner.type" }); |
| 17 |
p = fields.One2many({ |
| 18 |
string: "one2many field", |
| 19 |
relation: "partner", |
| 20 |
relation_field: "trululu", |
| 21 |
}); |
| 22 |
trululu = fields.Many2one({ relation: "partner" }); |
| 23 |
_records = [{ id: 1, int_field: 10, p: [1] }]; |
| 24 |
} |
| 25 |
|
| 26 |
class PartnerType extends models.Model { |
| 27 |
name = fields.Char(); |
| 28 |
_records = [ |
| 29 |
{ id: 12, name: "gold" }, |
| 30 |
{ id: 14, name: "silver" }, |
| 31 |
]; |
| 32 |
} |
| 33 |
|
| 34 |
defineModels([Partner, PartnerType]); |
| 35 |
|
| 36 |
test("Many2ManyCheckBoxesField", async () => { |
| 37 |
Partner._records[0].timmy = [12]; |
| 38 |
const commands = [[[4, 14]], [[3, 12]]]; |
| 39 |
onRpc("web_save", (args) => { |
| 40 |
expect.step("web_save"); |
| 41 |
expect(args.args[1].timmy).toEqual(commands.shift()); |
| 42 |
}); |
| 43 |
await mountView({ |
| 44 |
type: "form", |
| 45 |
resModel: "partner", |
| 46 |
resId: 1, |
| 47 |
arch: ` |
| 48 |
<form> |
| 49 |
<group> |
| 50 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 51 |
</group> |
| 52 |
</form>`, |
| 53 |
}); |
| 54 |
|
| 55 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 56 |
|
| 57 |
expect("div.o_field_widget div.form-check input:eq(0)").toBeChecked(); |
| 58 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 59 |
|
| 60 |
expect("div.o_field_widget div.form-check input:disabled").toHaveCount(0); |
| 61 |
|
| 62 |
// add a m2m value by clicking on input |
| 63 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 64 |
await runAllTimers(); |
| 65 |
await clickSave(); |
| 66 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(2); |
| 67 |
|
| 68 |
// remove a m2m value by clinking on label |
| 69 |
await contains("div.o_field_widget div.form-check > label").click(); |
| 70 |
await runAllTimers(); |
| 71 |
await clickSave(); |
| 72 |
expect("div.o_field_widget div.form-check input:eq(0)").not.toBeChecked(); |
| 73 |
expect("div.o_field_widget div.form-check input:eq(1)").toBeChecked(); |
| 74 |
|
| 75 |
expect.verifySteps(["web_save", "web_save"]); |
| 76 |
}); |
| 77 |
|
| 78 |
test("Many2ManyCheckBoxesField (readonly)", async () => { |
| 79 |
Partner._records[0].timmy = [12]; |
| 80 |
await mountView({ |
| 81 |
type: "form", |
| 82 |
resModel: "partner", |
| 83 |
resId: 1, |
| 84 |
arch: ` |
| 85 |
<form> |
| 86 |
<group> |
| 87 |
<field name="timmy" widget="many2many_checkboxes" readonly="True" /> |
| 88 |
</group> |
| 89 |
</form>`, |
| 90 |
}); |
| 91 |
|
| 92 |
expect("div.o_field_widget div.form-check").toHaveCount(2, { |
| 93 |
message: "should have fetched and displayed the 2 values of the many2many", |
| 94 |
}); |
| 95 |
expect("div.o_field_widget div.form-check input:disabled").toHaveCount(2, { |
| 96 |
message: "the checkboxes should be disabled", |
| 97 |
}); |
| 98 |
|
| 99 |
await contains("div.o_field_widget div.form-check > label:eq(1)").click(); |
| 100 |
|
| 101 |
expect("div.o_field_widget div.form-check input:eq(0)").toBeChecked(); |
| 102 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 103 |
}); |
| 104 |
|
| 105 |
test("Many2ManyCheckBoxesField does not read added record", async () => { |
| 106 |
Partner._records[0].timmy = []; |
| 107 |
onRpc((args) => { |
| 108 |
expect.step(args.method); |
| 109 |
}); |
| 110 |
await mountView({ |
| 111 |
type: "form", |
| 112 |
resModel: "partner", |
| 113 |
resId: 1, |
| 114 |
arch: ` |
| 115 |
<form> |
| 116 |
<group> |
| 117 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 118 |
</group> |
| 119 |
</form>`, |
| 120 |
}); |
| 121 |
|
| 122 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 123 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 124 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(0); |
| 125 |
|
| 126 |
await contains("div.o_field_widget div.form-check input").click(); |
| 127 |
await runAllTimers(); |
| 128 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 129 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 130 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(1); |
| 131 |
|
| 132 |
await clickSave(); |
| 133 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 134 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 135 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(1); |
| 136 |
|
| 137 |
expect.verifySteps(["get_views", "web_read", "name_search", "web_save"]); |
| 138 |
}); |
| 139 |
|
| 140 |
test("Many2ManyCheckBoxesField: start non empty, then remove twice", async () => { |
| 141 |
Partner._records[0].timmy = [12, 14]; |
| 142 |
await mountView({ |
| 143 |
type: "form", |
| 144 |
resModel: "partner", |
| 145 |
resId: 1, |
| 146 |
arch: ` |
| 147 |
<form> |
| 148 |
<group> |
| 149 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 150 |
</group> |
| 151 |
</form>`, |
| 152 |
}); |
| 153 |
|
| 154 |
await contains("div.o_field_widget div.form-check input:eq(0)").click(); |
| 155 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 156 |
await runAllTimers(); |
| 157 |
await clickSave(); |
| 158 |
expect("div.o_field_widget div.form-check input:eq(0)").not.toBeChecked(); |
| 159 |
expect("div.o_field_widget div.form-check input:eq(1)").not.toBeChecked(); |
| 160 |
}); |
| 161 |
|
| 162 |
test("Many2ManyCheckBoxesField: many2many read, field context is properly sent", async () => { |
| 163 |
onRpc((args) => { |
| 164 |
expect.step(args.method); |
| 165 |
if (args.method === "web_read") { |
| 166 |
expect(args.kwargs.specification.timmy.context).toEqual({ hello: "world" }); |
| 167 |
} else if (args.method === "name_search") { |
| 168 |
expect(args.kwargs.context.hello).toEqual("world"); |
| 169 |
} |
| 170 |
}); |
| 171 |
await mountView({ |
| 172 |
type: "form", |
| 173 |
resModel: "partner", |
| 174 |
resId: 1, |
| 175 |
arch: ` |
| 176 |
<form> |
| 177 |
<field name="timmy" widget="many2many_checkboxes" context="{ 'hello': 'world' }" /> |
| 178 |
</form>`, |
| 179 |
}); |
| 180 |
expect.verifySteps(["get_views", "web_read", "name_search"]); |
| 181 |
}); |
| 182 |
|
| 183 |
test("Many2ManyCheckBoxesField: values are updated when domain changes", async () => { |
| 184 |
await mountView({ |
| 185 |
type: "form", |
| 186 |
resModel: "partner", |
| 187 |
resId: 1, |
| 188 |
arch: ` |
| 189 |
<form> |
| 190 |
<field name="int_field" /> |
| 191 |
<field name="timmy" widget="many2many_checkboxes" domain="[['id', '>', int_field]]" /> |
| 192 |
</form>`, |
| 193 |
}); |
| 194 |
|
| 195 |
expect(".o_field_widget[name='int_field'] input").toHaveValue("10"); |
| 196 |
expect(".o_field_widget[name='timmy'] .form-check").toHaveCount(2); |
| 197 |
expect(".o_field_widget[name='timmy']").toHaveText("gold\nsilver"); |
| 198 |
|
| 199 |
await contains(".o_field_widget[name='int_field'] input").edit(13); |
| 200 |
expect(".o_field_widget[name='timmy'] .form-check").toHaveCount(1); |
| 201 |
expect(".o_field_widget[name='timmy']").toHaveText("silver"); |
| 202 |
}); |
| 203 |
|
| 204 |
test("Many2ManyCheckBoxesField with 40+ values", async () => { |
| 205 |
// 40 is the default limit for x2many fields. However, the many2many_checkboxes is a |
| 206 |
// special field that fetches its data through the fetchSpecialData mechanism, and it |
| 207 |
// uses the name_search server-side limit of 100. This test comes with a fix for a bug |
| 208 |
// that occurred when the user (un)selected a checkbox that wasn't in the 40 first checkboxes, |
| 209 |
// because the piece of data corresponding to that checkbox hadn't been processed by the |
| 210 |
// BasicModel, whereas the code handling the change assumed it had. |
| 211 |
expect.assertions(3); |
| 212 |
|
| 213 |
const records = []; |
| 214 |
for (let id = 1; id <= 90; id++) { |
| 215 |
records.push({ |
| 216 |
id, |
| 217 |
name: `type ${id}`, |
| 218 |
}); |
| 219 |
} |
| 220 |
PartnerType._records = records; |
| 221 |
Partner._records[0].timmy = records.map((r) => r.id); |
| 222 |
|
| 223 |
onRpc("web_save", ({ args }) => { |
| 224 |
expect(args[1].timmy).toEqual([[3, records[records.length - 1].id]]); |
| 225 |
}); |
| 226 |
await mountView({ |
| 227 |
type: "form", |
| 228 |
resModel: "partner", |
| 229 |
resId: 1, |
| 230 |
arch: ` |
| 231 |
<form> |
| 232 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 233 |
</form>`, |
| 234 |
}); |
| 235 |
|
| 236 |
expect(".o_field_widget[name='timmy'] input[type='checkbox']:checked").toHaveCount(90); |
| 237 |
|
| 238 |
// toggle the last value |
| 239 |
await contains(".o_field_widget[name='timmy'] input[type='checkbox']:last").click(); |
| 240 |
await runAllTimers(); |
| 241 |
|
| 242 |
await clickSave(); |
| 243 |
expect(".o_field_widget[name='timmy'] input[type='checkbox']:last").not.toBeChecked(); |
| 244 |
}); |
| 245 |
|
| 246 |
test("Many2ManyCheckBoxesField with 100+ values", async () => { |
| 247 |
// The many2many_checkboxes widget limits the displayed values to 100 (this is the |
| 248 |
// server-side name_search limit). This test encodes a scenario where there are more than |
| 249 |
// 100 records in the co-model, and all values in the many2many relationship aren't |
| 250 |
// displayed in the widget (due to the limit). If the user (un)selects a checkbox, we don't |
| 251 |
// want to remove all values that aren't displayed from the relation. |
| 252 |
expect.assertions(5); |
| 253 |
|
| 254 |
const records = []; |
| 255 |
for (let id = 1; id < 150; id++) { |
| 256 |
records.push({ |
| 257 |
id, |
| 258 |
name: `type ${id}`, |
| 259 |
}); |
| 260 |
} |
| 261 |
PartnerType._records = records; |
| 262 |
Partner._records[0].timmy = records.map((r) => r.id); |
| 263 |
onRpc("web_save", ({ args }) => { |
| 264 |
expect(args[1].timmy).toEqual([[3, records[0].id]]); |
| 265 |
expect.step("web_save"); |
| 266 |
}); |
| 267 |
onRpc("name_search", () => { |
| 268 |
expect.step("name_search"); |
| 269 |
}); |
| 270 |
|
| 271 |
await mountView({ |
| 272 |
type: "form", |
| 273 |
resModel: "partner", |
| 274 |
resId: 1, |
| 275 |
arch: ` |
| 276 |
<form> |
| 277 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 278 |
</form>`, |
| 279 |
}); |
| 280 |
|
| 281 |
expect(".o_field_widget[name='timmy'] input[type='checkbox']").toHaveCount(100); |
| 282 |
expect(".o_field_widget[name='timmy'] input[type='checkbox']").toBeChecked(); |
| 283 |
|
| 284 |
// toggle the first value |
| 285 |
await contains(".o_field_widget[name='timmy'] input[type='checkbox']").click(); |
| 286 |
await runAllTimers(); |
| 287 |
|
| 288 |
await clickSave(); |
| 289 |
expect(".o_field_widget[name='timmy'] input[type='checkbox']:first").not.toBeChecked(); |
| 290 |
expect.verifySteps(["name_search", "web_save"]); |
| 291 |
}); |
| 292 |
|
| 293 |
test("Many2ManyCheckBoxesField in a one2many", async () => { |
| 294 |
expect.assertions(3); |
| 295 |
|
| 296 |
PartnerType._records.push({ id: 15, name: "bronze" }); |
| 297 |
Partner._records[0].timmy = [14, 15]; |
| 298 |
|
| 299 |
onRpc("web_save", ({ args }) => { |
| 300 |
expect(args[1]).toEqual({ |
| 301 |
p: [ |
| 302 |
[ |
| 303 |
1, |
| 304 |
1, |
| 305 |
{ |
| 306 |
timmy: [ |
| 307 |
[4, 12], |
| 308 |
[3, 14], |
| 309 |
], |
| 310 |
}, |
| 311 |
], |
| 312 |
], |
| 313 |
}); |
| 314 |
}); |
| 315 |
|
| 316 |
await mountView({ |
| 317 |
type: "form", |
| 318 |
resModel: "partner", |
| 319 |
arch: ` |
| 320 |
<form> |
| 321 |
<field name="p"> |
| 322 |
<list><field name="id"/></list> |
| 323 |
<form> |
| 324 |
<field name="timmy" widget="many2many_checkboxes"/> |
| 325 |
</form> |
| 326 |
</field> |
| 327 |
</form>`, |
| 328 |
resId: 1, |
| 329 |
}); |
| 330 |
|
| 331 |
await contains(".o_data_cell").click(); |
| 332 |
|
| 333 |
// edit the timmy field by (un)checking boxes on the widget |
| 334 |
await contains(".modal .form-check-input:eq(0)").click(); |
| 335 |
expect(".modal .form-check-input:eq(0)").toBeChecked(); |
| 336 |
await contains(".modal .form-check-input:eq(1)").click(); |
| 337 |
expect(".modal .form-check-input:eq(1)").not.toBeChecked(); |
| 338 |
|
| 339 |
await contains(".modal .o_form_button_save").click(); |
| 340 |
await clickSave(); |
| 341 |
}); |
| 342 |
|
| 343 |
test("Many2ManyCheckBoxesField with default values", async () => { |
| 344 |
expect.assertions(7); |
| 345 |
|
| 346 |
Partner._fields.timmy = fields.Many2many({ |
| 347 |
string: "pokemon", |
| 348 |
relation: "partner.type", |
| 349 |
default: [[4, 3]], |
| 350 |
}); |
| 351 |
PartnerType._records.push({ id: 3, name: "bronze" }); |
| 352 |
|
| 353 |
onRpc("web_save", ({ args }) => { |
| 354 |
expect(args[1].timmy).toEqual([[4, 12]], { |
| 355 |
message: "correct values should have been sent to create", |
| 356 |
}); |
| 357 |
}); |
| 358 |
await mountView({ |
| 359 |
type: "form", |
| 360 |
resModel: "partner", |
| 361 |
arch: ` |
| 362 |
<form> |
| 363 |
<field name="timmy" widget="many2many_checkboxes"/> |
| 364 |
</form>`, |
| 365 |
}); |
| 366 |
|
| 367 |
expect(".o_form_view .form-check input:eq(0)").not.toBeChecked(); |
| 368 |
expect(".o_form_view .form-check input:eq(1)").not.toBeChecked(); |
| 369 |
expect(".o_form_view .form-check input:eq(2)").toBeChecked(); |
| 370 |
|
| 371 |
await contains(".o_form_view .form-check input:checked").click(); |
| 372 |
await contains(".o_form_view .form-check input:eq(0)").click(); |
| 373 |
await contains(".o_form_view .form-check input:eq(0)").click(); |
| 374 |
await contains(".o_form_view .form-check input:eq(0)").click(); |
| 375 |
await runAllTimers(); |
| 376 |
|
| 377 |
expect(".o_form_view .form-check input:eq(0)").toBeChecked(); |
| 378 |
expect(".o_form_view .form-check input:eq(1)").not.toBeChecked(); |
| 379 |
expect(".o_form_view .form-check input:eq(2)").not.toBeChecked(); |
| 380 |
|
| 381 |
await clickSave(); |
| 382 |
}); |
| 383 |
|
| 384 |
test("Many2ManyCheckBoxesField batches successive changes", async () => { |
| 385 |
Partner._fields.timmy = fields.Many2many({ |
| 386 |
string: "pokemon", |
| 387 |
relation: "partner.type", |
| 388 |
onChange: () => {}, |
| 389 |
}); |
| 390 |
Partner._records[0].timmy = []; |
| 391 |
onRpc(({ method }) => { |
| 392 |
expect.step(method); |
| 393 |
}); |
| 394 |
await mountView({ |
| 395 |
type: "form", |
| 396 |
resModel: "partner", |
| 397 |
resId: 1, |
| 398 |
arch: ` |
| 399 |
<form> |
| 400 |
<group> |
| 401 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 402 |
</group> |
| 403 |
</form>`, |
| 404 |
}); |
| 405 |
|
| 406 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 407 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 408 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(0); |
| 409 |
|
| 410 |
await contains("div.o_field_widget div.form-check input:eq(0)").click(); |
| 411 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 412 |
// checkboxes are updated directly |
| 413 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(2); |
| 414 |
// but no onchanges has been fired yet |
| 415 |
expect.verifySteps(["get_views", "web_read", "name_search"]); |
| 416 |
await runAllTimers(); |
| 417 |
expect.verifySteps(["onchange"]); |
| 418 |
}); |
| 419 |
|
| 420 |
test("Many2ManyCheckBoxesField sends batched changes on save", async () => { |
| 421 |
Partner._fields.timmy = fields.Many2many({ |
| 422 |
string: "pokemon", |
| 423 |
relation: "partner.type", |
| 424 |
onChange: () => {}, |
| 425 |
}); |
| 426 |
Partner._records[0].timmy = []; |
| 427 |
onRpc(({ method }) => { |
| 428 |
expect.step(method); |
| 429 |
}); |
| 430 |
await mountView({ |
| 431 |
type: "form", |
| 432 |
resModel: "partner", |
| 433 |
resId: 1, |
| 434 |
arch: ` |
| 435 |
<form> |
| 436 |
<group> |
| 437 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 438 |
</group> |
| 439 |
</form>`, |
| 440 |
}); |
| 441 |
|
| 442 |
expect("div.o_field_widget div.form-check").toHaveCount(2); |
| 443 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 444 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(0); |
| 445 |
|
| 446 |
await contains("div.o_field_widget div.form-check input:eq(0)").click(); |
| 447 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 448 |
// checkboxes are updated directly |
| 449 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(2); |
| 450 |
// but no onchanges has been fired yet |
| 451 |
expect.verifySteps(["get_views", "web_read", "name_search"]); |
| 452 |
await runAllTimers(); |
| 453 |
// save |
| 454 |
await clickSave(); |
| 455 |
expect.verifySteps(["onchange", "web_save"]); |
| 456 |
}); |
| 457 |
|
| 458 |
test("Many2ManyCheckBoxesField in a notebook tab", async () => { |
| 459 |
Partner._records[0].timmy = []; |
| 460 |
onRpc(({ method }) => { |
| 461 |
expect.step(method); |
| 462 |
}); |
| 463 |
await mountView({ |
| 464 |
type: "form", |
| 465 |
resModel: "partner", |
| 466 |
resId: 1, |
| 467 |
arch: ` |
| 468 |
<form> |
| 469 |
<notebook> |
| 470 |
<page string="Page 1"> |
| 471 |
<field name="timmy" widget="many2many_checkboxes" /> |
| 472 |
</page> |
| 473 |
<page string="Page 2"> |
| 474 |
<field name="int_field" /> |
| 475 |
</page> |
| 476 |
</notebook> |
| 477 |
</form>`, |
| 478 |
}); |
| 479 |
|
| 480 |
expect("div.o_field_widget[name=timmy]").toHaveCount(1); |
| 481 |
expect("div.o_field_widget[name=timmy] div.form-check").toHaveCount(2); |
| 482 |
expect(queryAllTexts(".o_field_widget .form-check-label")).toEqual(["gold", "silver"]); |
| 483 |
expect("div.o_field_widget[name=timmy] div.form-check input:checked").toHaveCount(0); |
| 484 |
|
| 485 |
await contains("div.o_field_widget div.form-check input:eq(0)").click(); |
| 486 |
await contains("div.o_field_widget div.form-check input:eq(1)").click(); |
| 487 |
// checkboxes are updated directly |
| 488 |
expect("div.o_field_widget div.form-check input:checked").toHaveCount(2); |
| 489 |
// go to the other tab |
| 490 |
await contains(".o_notebook .nav-link:eq(1)").click(); |
| 491 |
expect("div.o_field_widget[name=timmy]").toHaveCount(0); |
| 492 |
expect("div.o_field_widget[name=int_field]").toHaveCount(1); |
| 493 |
// save |
| 494 |
await clickSave(); |
| 495 |
expect.verifySteps(["get_views", "web_read", "name_search", "web_save"]); |
| 496 |
}); |