| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { queryAll, queryAllTexts } from "@odoo/hoot-dom"; |
| 3 |
import { runAllTimers } from "@odoo/hoot-mock"; |
| 4 |
|
| 5 |
import { |
| 6 |
clickFieldDropdown, |
| 7 |
clickFieldDropdownItem, |
| 8 |
clickSave, |
| 9 |
contains, |
| 10 |
defineModels, |
| 11 |
fields, |
| 12 |
models, |
| 13 |
mountView, |
| 14 |
patchWithCleanup, |
| 15 |
stepAllNetworkCalls, |
| 16 |
} from "@web/../tests/web_test_helpers"; |
| 17 |
import { registry } from "@web/core/registry"; |
| 18 |
|
| 19 |
class Partner extends models.Model { |
| 20 |
int_field = fields.Integer(); |
| 21 |
user_id = fields.Many2one({ string: "Users", relation: "res.users" }); |
| 22 |
_records = [ |
| 23 |
{ id: 1, user_id: 1 }, |
| 24 |
{ id: 2, user_id: 2 }, |
| 25 |
{ id: 3, user_id: 1 }, |
| 26 |
{ id: 4, user_id: false }, |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
class Users extends models.Model { |
| 31 |
_name = "res.users"; |
| 32 |
name = fields.Char(); |
| 33 |
partner_ids = fields.One2many({ relation: "partner", relation_field: "user_id" }); |
| 34 |
|
| 35 |
has_group() { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
_records = [ |
| 40 |
{ |
| 41 |
id: 1, |
| 42 |
name: "Aline", |
| 43 |
}, |
| 44 |
{ |
| 45 |
id: 2, |
| 46 |
name: "Christine", |
| 47 |
}, |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
defineModels([Partner, Users]); |
| 52 |
|
| 53 |
test.tags("desktop"); |
| 54 |
test("basic form view flow", async () => { |
| 55 |
await mountView({ |
| 56 |
type: "form", |
| 57 |
resModel: "partner", |
| 58 |
resId: 1, |
| 59 |
arch: ` |
| 60 |
<form> |
| 61 |
<field name="user_id" widget="many2one_avatar"/> |
| 62 |
</form>`, |
| 63 |
}); |
| 64 |
|
| 65 |
expect(".o_field_widget[name=user_id] input").toHaveValue("Aline"); |
| 66 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/1/avatar_128"]').toHaveCount(1); |
| 67 |
expect(".o_field_many2one_avatar > div").toHaveCount(1); |
| 68 |
|
| 69 |
expect(".o_input_dropdown").toHaveCount(1); |
| 70 |
expect(".o_input_dropdown input").toHaveValue("Aline"); |
| 71 |
expect(".o_external_button").toHaveCount(1); |
| 72 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/1/avatar_128"]').toHaveCount(1); |
| 73 |
|
| 74 |
await clickFieldDropdown("user_id"); |
| 75 |
expect(".o_field_many2one_selection .o_avatar_many2x_autocomplete").toHaveCount(2); |
| 76 |
await clickFieldDropdownItem("user_id", "Christine"); |
| 77 |
|
| 78 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/2/avatar_128"]').toHaveCount(1); |
| 79 |
await clickSave(); |
| 80 |
|
| 81 |
expect(".o_field_widget[name=user_id] input").toHaveValue("Christine"); |
| 82 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/2/avatar_128"]').toHaveCount(1); |
| 83 |
|
| 84 |
await contains('.o_field_widget[name="user_id"] input').clear({ confirm: "blur" }); |
| 85 |
|
| 86 |
expect(".o_m2o_avatar > img").toHaveCount(0); |
| 87 |
expect(".o_m2o_avatar > .o_m2o_avatar_empty").toHaveCount(1); |
| 88 |
await clickSave(); |
| 89 |
|
| 90 |
expect(".o_m2o_avatar > img").toHaveCount(0); |
| 91 |
expect(".o_m2o_avatar > .o_m2o_avatar_empty").toHaveCount(1); |
| 92 |
}); |
| 93 |
|
| 94 |
test("onchange in form view flow", async () => { |
| 95 |
Partner._fields.int_field = fields.Integer({ |
| 96 |
onChange: (obj) => { |
| 97 |
if (obj.int_field === 1) { |
| 98 |
obj.user_id = [2, "Christine"]; |
| 99 |
} else if (obj.int_field === 2) { |
| 100 |
obj.user_id = false; |
| 101 |
} else { |
| 102 |
obj.user_id = [1, "Aline"]; // default value |
| 103 |
} |
| 104 |
}, |
| 105 |
}); |
| 106 |
|
| 107 |
await mountView({ |
| 108 |
type: "form", |
| 109 |
resModel: "partner", |
| 110 |
arch: ` |
| 111 |
<form> |
| 112 |
<field name="int_field"/> |
| 113 |
<field name="user_id" widget="many2one_avatar" readonly="1"/> |
| 114 |
</form>`, |
| 115 |
}); |
| 116 |
|
| 117 |
expect(".o_field_widget[name=user_id]").toHaveText("Aline"); |
| 118 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/1/avatar_128"]').toHaveCount(1); |
| 119 |
|
| 120 |
await contains("div[name=int_field] input").edit(1); |
| 121 |
|
| 122 |
expect(".o_field_widget[name=user_id]").toHaveText("Christine"); |
| 123 |
expect('.o_m2o_avatar > img[data-src="/web/image/res.users/2/avatar_128"]').toHaveCount(1); |
| 124 |
|
| 125 |
await contains("div[name=int_field] input").edit(2); |
| 126 |
|
| 127 |
expect(".o_field_widget[name=user_id]").toHaveText(""); |
| 128 |
expect(".o_m2o_avatar > img").toHaveCount(0); |
| 129 |
}); |
| 130 |
|
| 131 |
test("basic list view flow", async () => { |
| 132 |
await mountView({ |
| 133 |
type: "list", |
| 134 |
resModel: "partner", |
| 135 |
arch: '<list><field name="user_id" widget="many2one_avatar"/></list>', |
| 136 |
}); |
| 137 |
|
| 138 |
expect(queryAllTexts(".o_data_cell[name='user_id']")).toEqual([ |
| 139 |
"Aline", |
| 140 |
"Christine", |
| 141 |
"Aline", |
| 142 |
"", |
| 143 |
]); |
| 144 |
const imgs = queryAll(".o_m2o_avatar > img"); |
| 145 |
expect(imgs[0]).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 146 |
expect(imgs[1]).toHaveAttribute("data-src", "/web/image/res.users/2/avatar_128"); |
| 147 |
expect(imgs[2]).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 148 |
}); |
| 149 |
|
| 150 |
test("basic flow in editable list view", async () => { |
| 151 |
await mountView({ |
| 152 |
type: "list", |
| 153 |
resModel: "partner", |
| 154 |
arch: '<list editable="top"><field name="user_id" widget="many2one_avatar"/></list>', |
| 155 |
}); |
| 156 |
|
| 157 |
expect(queryAllTexts(".o_data_cell[name='user_id']")).toEqual([ |
| 158 |
"Aline", |
| 159 |
"Christine", |
| 160 |
"Aline", |
| 161 |
"", |
| 162 |
]); |
| 163 |
|
| 164 |
const imgs = queryAll(".o_m2o_avatar > img"); |
| 165 |
expect(imgs[0]).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 166 |
expect(imgs[1]).toHaveAttribute("data-src", "/web/image/res.users/2/avatar_128"); |
| 167 |
expect(imgs[2]).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 168 |
|
| 169 |
await contains(".o_data_row .o_data_cell:eq(0)").click(); |
| 170 |
|
| 171 |
expect(".o_m2o_avatar > img:eq(0)").toHaveAttribute( |
| 172 |
"data-src", |
| 173 |
"/web/image/res.users/1/avatar_128" |
| 174 |
); |
| 175 |
}); |
| 176 |
|
| 177 |
test("Many2OneAvatar with placeholder", async () => { |
| 178 |
await mountView({ |
| 179 |
type: "form", |
| 180 |
resModel: "partner", |
| 181 |
arch: '<form><field name="user_id" widget="many2one_avatar" placeholder="Placeholder"/></form>', |
| 182 |
}); |
| 183 |
|
| 184 |
expect(".o_field_widget[name='user_id'] input").toHaveAttribute("placeholder", "Placeholder"); |
| 185 |
}); |
| 186 |
|
| 187 |
test.tags("desktop"); |
| 188 |
test("click on many2one_avatar in a list view (multi_edit='1')", async () => { |
| 189 |
const listView = registry.category("views").get("list"); |
| 190 |
patchWithCleanup(listView.Controller.prototype, { |
| 191 |
openRecord() { |
| 192 |
expect.step("openRecord"); |
| 193 |
}, |
| 194 |
}); |
| 195 |
|
| 196 |
await mountView({ |
| 197 |
type: "list", |
| 198 |
resModel: "partner", |
| 199 |
arch: ` |
| 200 |
<list multi_edit="1"> |
| 201 |
<field name="user_id" widget="many2one_avatar"/> |
| 202 |
</list>`, |
| 203 |
}); |
| 204 |
|
| 205 |
await contains(".o_data_row:eq(0) .o_list_record_selector input").click(); |
| 206 |
await contains(".o_data_row .o_data_cell [name='user_id']").click(); |
| 207 |
expect(".o_data_row:eq(0)").toHaveClass("o_selected_row"); |
| 208 |
|
| 209 |
expect.verifySteps([]); |
| 210 |
}); |
| 211 |
|
| 212 |
test("click on many2one_avatar in an editable list view", async () => { |
| 213 |
const listView = registry.category("views").get("list"); |
| 214 |
patchWithCleanup(listView.Controller.prototype, { |
| 215 |
openRecord() { |
| 216 |
expect.step("openRecord"); |
| 217 |
}, |
| 218 |
}); |
| 219 |
|
| 220 |
await mountView({ |
| 221 |
type: "list", |
| 222 |
resModel: "partner", |
| 223 |
arch: ` |
| 224 |
<list> |
| 225 |
<field name="user_id" widget="many2one_avatar"/> |
| 226 |
</list>`, |
| 227 |
}); |
| 228 |
|
| 229 |
await contains(".o_data_row .o_data_cell [name='user_id']").click(); |
| 230 |
expect(".o_selected_row").toHaveCount(0); |
| 231 |
|
| 232 |
expect.verifySteps(["openRecord"]); |
| 233 |
}); |
| 234 |
|
| 235 |
test.tags("desktop"); |
| 236 |
test("click on many2one_avatar in an editable list view (editable top)", async () => { |
| 237 |
const listView = registry.category("views").get("list"); |
| 238 |
patchWithCleanup(listView.Controller.prototype, { |
| 239 |
openRecord() { |
| 240 |
expect.step("openRecord"); |
| 241 |
}, |
| 242 |
}); |
| 243 |
|
| 244 |
await mountView({ |
| 245 |
type: "list", |
| 246 |
resModel: "partner", |
| 247 |
arch: ` |
| 248 |
<list editable="top"> |
| 249 |
<field name="user_id" widget="many2one_avatar"/> |
| 250 |
</list>`, |
| 251 |
}); |
| 252 |
|
| 253 |
await contains(".o_data_row .o_data_cell [name='user_id']").click(); |
| 254 |
expect(".o_data_row:eq(0)").toHaveClass("o_selected_row"); |
| 255 |
|
| 256 |
expect.verifySteps([]); |
| 257 |
}); |
| 258 |
|
| 259 |
test("readonly many2one_avatar in form view should contain a link", async () => { |
| 260 |
await mountView({ |
| 261 |
type: "form", |
| 262 |
resModel: "partner", |
| 263 |
resId: 1, |
| 264 |
arch: `<form><field name="user_id" widget="many2one_avatar" readonly="1"/></form>`, |
| 265 |
}); |
| 266 |
|
| 267 |
expect("[name='user_id'] a").toHaveCount(1); |
| 268 |
}); |
| 269 |
|
| 270 |
test("readonly many2one_avatar in list view should not contain a link", async () => { |
| 271 |
await mountView({ |
| 272 |
type: "list", |
| 273 |
resModel: "partner", |
| 274 |
arch: `<list><field name="user_id" widget="many2one_avatar"/></list>`, |
| 275 |
}); |
| 276 |
|
| 277 |
expect("[name='user_id'] a").toHaveCount(0); |
| 278 |
}); |
| 279 |
|
| 280 |
test("readonly many2one_avatar in form view with no_open set to true", async () => { |
| 281 |
await mountView({ |
| 282 |
type: "form", |
| 283 |
resModel: "partner", |
| 284 |
resId: 1, |
| 285 |
arch: `<form><field name="user_id" widget="many2one_avatar" readonly="1" options="{'no_open': 1}"/></form>`, |
| 286 |
}); |
| 287 |
|
| 288 |
expect("[name='user_id'] a").toHaveCount(0); |
| 289 |
}); |
| 290 |
|
| 291 |
test("readonly many2one_avatar in list view with no_open set to false", async () => { |
| 292 |
await mountView({ |
| 293 |
type: "list", |
| 294 |
resModel: "partner", |
| 295 |
arch: `<list><field name="user_id" widget="many2one_avatar" options="{'no_open': 0}"/></list>`, |
| 296 |
}); |
| 297 |
|
| 298 |
expect("[name='user_id'] a").toHaveCount(3); |
| 299 |
}); |
| 300 |
|
| 301 |
test.tags("desktop"); |
| 302 |
test("cancelling create dialog should clear value in the field", async () => { |
| 303 |
Users._views = { |
| 304 |
form: ` |
| 305 |
<form> |
| 306 |
<field name="name" /> |
| 307 |
</form>`, |
| 308 |
}; |
| 309 |
|
| 310 |
await mountView({ |
| 311 |
type: "list", |
| 312 |
resModel: "partner", |
| 313 |
arch: ` |
| 314 |
<list editable="top"> |
| 315 |
<field name="user_id" widget="many2one_avatar"/> |
| 316 |
</list>`, |
| 317 |
}); |
| 318 |
|
| 319 |
await contains(".o_data_cell:eq(0)").click(); |
| 320 |
await contains(".o_field_widget[name=user_id] input").edit("yy", { confirm: false }); |
| 321 |
await runAllTimers(); |
| 322 |
await clickFieldDropdownItem("user_id", "Create and edit..."); |
| 323 |
|
| 324 |
await contains(".o_form_button_cancel").click(); |
| 325 |
expect(".o_field_widget[name=user_id] input").toHaveValue(""); |
| 326 |
expect(".o_field_widget[name=user_id] span.o_m2o_avatar_empty").toHaveCount(1); |
| 327 |
}); |
| 328 |
|
| 329 |
test.tags("desktop"); |
| 330 |
test("widget many2one_avatar in kanban view (load more dialog)", async () => { |
| 331 |
expect.assertions(1); |
| 332 |
|
| 333 |
for (let id = 3; id <= 12; id++) { |
| 334 |
Users._records.push({ |
| 335 |
id, |
| 336 |
display_name: `record ${id}`, |
| 337 |
}); |
| 338 |
} |
| 339 |
|
| 340 |
Users._views = { |
| 341 |
list: '<list><field name="display_name"/></list>', |
| 342 |
}; |
| 343 |
await mountView({ |
| 344 |
type: "kanban", |
| 345 |
resModel: "partner", |
| 346 |
arch: ` |
| 347 |
<kanban> |
| 348 |
<templates> |
| 349 |
<t t-name="card"> |
| 350 |
<footer> |
| 351 |
<field name="user_id" widget="many2one_avatar"/> |
| 352 |
</footer> |
| 353 |
</t> |
| 354 |
</templates> |
| 355 |
</kanban>`, |
| 356 |
}); |
| 357 |
|
| 358 |
// open popover |
| 359 |
await contains( |
| 360 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > a.o_quick_assign" |
| 361 |
).click(); |
| 362 |
|
| 363 |
// load more |
| 364 |
await contains(".o-overlay-container .o_m2o_dropdown_option_search_more").click(); |
| 365 |
await contains(".o_dialog .o_list_table .o_data_row .o_data_cell").click(); |
| 366 |
expect( |
| 367 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > img" |
| 368 |
).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 369 |
}); |
| 370 |
|
| 371 |
test("widget many2one_avatar in kanban view", async () => { |
| 372 |
await mountView({ |
| 373 |
type: "kanban", |
| 374 |
resModel: "partner", |
| 375 |
arch: ` |
| 376 |
<kanban> |
| 377 |
<templates> |
| 378 |
<t t-name="card"> |
| 379 |
<footer> |
| 380 |
<field name="user_id" widget="many2one_avatar"/> |
| 381 |
</footer> |
| 382 |
</t> |
| 383 |
</templates> |
| 384 |
</kanban>`, |
| 385 |
}); |
| 386 |
stepAllNetworkCalls(); |
| 387 |
|
| 388 |
expect( |
| 389 |
".o_kanban_record:nth-child(1) .o_field_many2one_avatar .o_m2o_avatar > img" |
| 390 |
).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 391 |
expect( |
| 392 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > .o_quick_assign" |
| 393 |
).toHaveCount(1); |
| 394 |
// open popover |
| 395 |
await contains( |
| 396 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > .o_quick_assign" |
| 397 |
).click(); |
| 398 |
expect(".o-overlay-container input").toBeFocused(); |
| 399 |
expect.verifySteps(["web_name_search"]); |
| 400 |
// select first input |
| 401 |
await contains(".o-overlay-container .o-autocomplete--dropdown-item").click(); |
| 402 |
expect.verifySteps(["web_save"]); |
| 403 |
expect( |
| 404 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > img" |
| 405 |
).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 406 |
expect( |
| 407 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > .o_quick_assign" |
| 408 |
).toHaveCount(0); |
| 409 |
}); |
| 410 |
|
| 411 |
test("widget many2one_avatar in kanban view without access rights", async () => { |
| 412 |
expect.assertions(2); |
| 413 |
await mountView({ |
| 414 |
type: "kanban", |
| 415 |
resModel: "partner", |
| 416 |
arch: ` |
| 417 |
<kanban edit="0" create="0"> |
| 418 |
<templates> |
| 419 |
<t t-name="card"> |
| 420 |
<footer> |
| 421 |
<field name="user_id" widget="many2one_avatar"/> |
| 422 |
</footer> |
| 423 |
</t> |
| 424 |
</templates> |
| 425 |
</kanban>`, |
| 426 |
}); |
| 427 |
expect( |
| 428 |
".o_kanban_record:nth-child(1) .o_field_many2one_avatar .o_m2o_avatar > img" |
| 429 |
).toHaveAttribute("data-src", "/web/image/res.users/1/avatar_128"); |
| 430 |
expect( |
| 431 |
".o_kanban_record:nth-child(4) .o_field_many2one_avatar .o_m2o_avatar > .o_quick_assign" |
| 432 |
).toHaveCount(0); |
| 433 |
}); |