| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { click, hover, leave, press, queryAll, queryAllTexts } from "@odoo/hoot-dom"; |
| 3 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 4 |
import { defineModels, fields, models, mountView, onRpc } from "@web/../tests/web_test_helpers"; |
| 5 |
|
| 6 |
class Partner extends models.Model { |
| 7 |
foo = fields.Char({ string: "Foo" }); |
| 8 |
id = fields.Integer({ string: "Sequence" }); |
| 9 |
selection = fields.Selection({ |
| 10 |
string: "Selection", |
| 11 |
selection: [ |
| 12 |
["normal", "Normal"], |
| 13 |
["blocked", "Blocked"], |
| 14 |
["done", "Done"], |
| 15 |
], |
| 16 |
}); |
| 17 |
|
| 18 |
_records = [ |
| 19 |
{ |
| 20 |
id: 1, |
| 21 |
foo: "yop", |
| 22 |
selection: "blocked", |
| 23 |
}, |
| 24 |
{ |
| 25 |
id: 2, |
| 26 |
foo: "blip", |
| 27 |
selection: "normal", |
| 28 |
}, |
| 29 |
{ |
| 30 |
id: 4, |
| 31 |
foo: "abc", |
| 32 |
selection: "done", |
| 33 |
}, |
| 34 |
{ id: 3, foo: "gnap" }, |
| 35 |
{ id: 5, foo: "blop" }, |
| 36 |
]; |
| 37 |
} |
| 38 |
class User extends models.Model { |
| 39 |
_name = "res.users"; |
| 40 |
has_group() { |
| 41 |
return true; |
| 42 |
} |
| 43 |
} |
| 44 |
defineModels([Partner, User]); |
| 45 |
|
| 46 |
test("PriorityField when not set", async () => { |
| 47 |
await mountView({ |
| 48 |
type: "form", |
| 49 |
resModel: "partner", |
| 50 |
resId: 2, |
| 51 |
arch: /* xml */ ` |
| 52 |
<form> |
| 53 |
<sheet> |
| 54 |
<group> |
| 55 |
<field name="selection" widget="priority" /> |
| 56 |
</group> |
| 57 |
</sheet> |
| 58 |
</form>`, |
| 59 |
}); |
| 60 |
|
| 61 |
expect(".o_field_widget .o_priority:not(.o_field_empty)").toHaveCount(1, { |
| 62 |
message: "widget should be considered set, even though there is no value for this field", |
| 63 |
}); |
| 64 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2, { |
| 65 |
message: |
| 66 |
"should have two stars for representing each possible value: no star, one star and two stars", |
| 67 |
}); |
| 68 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(0, { |
| 69 |
message: "should have no full star since there is no value", |
| 70 |
}); |
| 71 |
|
| 72 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(2, { |
| 73 |
message: "should have two empty stars since there is no value", |
| 74 |
}); |
| 75 |
}); |
| 76 |
|
| 77 |
test("PriorityField tooltip", async () => { |
| 78 |
await mountView({ |
| 79 |
type: "form", |
| 80 |
resModel: "partner", |
| 81 |
arch: /* xml */ ` |
| 82 |
<form> |
| 83 |
<sheet> |
| 84 |
<group> |
| 85 |
<field name="selection" widget="priority"/> |
| 86 |
</group> |
| 87 |
</sheet> |
| 88 |
</form>`, |
| 89 |
resId: 1, |
| 90 |
}); |
| 91 |
|
| 92 |
// check data-tooltip attribute (used by the tooltip service) |
| 93 |
const stars = queryAll(".o_field_widget .o_priority a.o_priority_star"); |
| 94 |
expect(stars[0]).toHaveAttribute("data-tooltip", "Selection: Blocked"); |
| 95 |
expect(stars[1]).toHaveAttribute("data-tooltip", "Selection: Done"); |
| 96 |
}); |
| 97 |
|
| 98 |
test("PriorityField in form view", async () => { |
| 99 |
expect.assertions(8); |
| 100 |
|
| 101 |
onRpc("web_save", ({ args }) => { |
| 102 |
expect(args).toEqual([[1], { selection: "done" }]); |
| 103 |
}); |
| 104 |
await mountView({ |
| 105 |
type: "form", |
| 106 |
resModel: "partner", |
| 107 |
resId: 1, |
| 108 |
arch: /* xml */ ` |
| 109 |
<form> |
| 110 |
<sheet> |
| 111 |
<group> |
| 112 |
<field name="selection" widget="priority" /> |
| 113 |
</group> |
| 114 |
</sheet> |
| 115 |
</form>`, |
| 116 |
}); |
| 117 |
|
| 118 |
expect(".o_field_widget .o_priority:not(.o_field_empty)").toHaveCount(1); |
| 119 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2); |
| 120 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(1); |
| 121 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(1); |
| 122 |
|
| 123 |
// click on the second star in edit mode |
| 124 |
await click(".o_field_widget .o_priority a.o_priority_star.fa-star-o:last"); |
| 125 |
await animationFrame(); |
| 126 |
|
| 127 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2); |
| 128 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(2); |
| 129 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(0); |
| 130 |
}); |
| 131 |
|
| 132 |
test.tags("desktop"); |
| 133 |
test("PriorityField hover a star in form view", async () => { |
| 134 |
expect.assertions(10); |
| 135 |
await mountView({ |
| 136 |
type: "form", |
| 137 |
resModel: "partner", |
| 138 |
resId: 1, |
| 139 |
arch: /* xml */ ` |
| 140 |
<form> |
| 141 |
<sheet> |
| 142 |
<group> |
| 143 |
<field name="selection" widget="priority" /> |
| 144 |
</group> |
| 145 |
</sheet> |
| 146 |
</form>`, |
| 147 |
}); |
| 148 |
|
| 149 |
expect(".o_field_widget .o_priority:not(.o_field_empty)").toHaveCount(1); |
| 150 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2); |
| 151 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(1); |
| 152 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(1); |
| 153 |
|
| 154 |
// hover last star |
| 155 |
const star = ".o_field_widget .o_priority a.o_priority_star.fa-star-o:last"; |
| 156 |
await hover(star); |
| 157 |
await animationFrame(); |
| 158 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2); |
| 159 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(2, { |
| 160 |
message: "should temporary have two full stars since we are hovering the third value", |
| 161 |
}); |
| 162 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(0, { |
| 163 |
message: "should temporary have no empty star since we are hovering the third value", |
| 164 |
}); |
| 165 |
|
| 166 |
await leave(star); |
| 167 |
await animationFrame(); |
| 168 |
|
| 169 |
expect(".o_field_widget .o_priority a.o_priority_star").toHaveCount(2); |
| 170 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(1); |
| 171 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star-o").toHaveCount(1); |
| 172 |
}); |
| 173 |
|
| 174 |
test("PriorityField can write after adding a record -- kanban", async () => { |
| 175 |
Partner._fields.selection = fields.Selection({ |
| 176 |
string: "Selection", |
| 177 |
selection: [ |
| 178 |
["0", 0], |
| 179 |
["1", 1], |
| 180 |
], |
| 181 |
}); |
| 182 |
Partner._records[0].selection = "0"; |
| 183 |
Partner._views[["form", "myquickview"]] = /* xml */ `<form/>`; |
| 184 |
onRpc("web_save", ({ args }) => expect.step(`web_save ${JSON.stringify(args)}`)); |
| 185 |
await mountView({ |
| 186 |
type: "kanban", |
| 187 |
resModel: "partner", |
| 188 |
domain: [["id", "=", 1]], |
| 189 |
groupBy: ["foo"], |
| 190 |
arch: /* xml */ ` |
| 191 |
<kanban on_create="quick_create" quick_create_view="myquickview"> |
| 192 |
<templates> |
| 193 |
<t t-name="card"> |
| 194 |
<field name="selection" widget="priority"/> |
| 195 |
</t> |
| 196 |
</templates> |
| 197 |
</kanban>`, |
| 198 |
}); |
| 199 |
expect(".o_kanban_record .fa-star").toHaveCount(0); |
| 200 |
|
| 201 |
await click(".o_priority a.o_priority_star.fa-star-o"); |
| 202 |
// wait for web_save |
| 203 |
await animationFrame(); |
| 204 |
expect.verifySteps(['web_save [[1],{"selection":"1"}]']); |
| 205 |
expect(".o_kanban_record .fa-star").toHaveCount(1); |
| 206 |
await click(".o_control_panel_main_buttons .o-kanban-button-new"); |
| 207 |
await animationFrame(); |
| 208 |
await animationFrame(); |
| 209 |
await click(".o_kanban_quick_create .o_kanban_add"); |
| 210 |
await animationFrame(); |
| 211 |
expect.verifySteps(["web_save [[],{}]"]); |
| 212 |
await click(".o_priority a.o_priority_star.fa-star-o"); |
| 213 |
await animationFrame(); |
| 214 |
expect.verifySteps([`web_save [[6],{"selection":"1"}]`]); |
| 215 |
expect(".o_kanban_record .fa-star").toHaveCount(2); |
| 216 |
}); |
| 217 |
|
| 218 |
test("PriorityField in editable list view", async () => { |
| 219 |
onRpc("has_group", () => true); |
| 220 |
await mountView({ |
| 221 |
type: "list", |
| 222 |
resModel: "partner", |
| 223 |
arch: /* xml */ `<list editable="bottom"><field name="selection" widget="priority" /></list>`, |
| 224 |
}); |
| 225 |
|
| 226 |
expect(".o_data_row:first-child .o_priority:not(.o_field_empty)").toHaveCount(1); |
| 227 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 228 |
message: |
| 229 |
"should have two stars for representing each possible value: no star, one star and two stars", |
| 230 |
}); |
| 231 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(1, { |
| 232 |
message: "should have one full star since the value is the second value", |
| 233 |
}); |
| 234 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(1, { |
| 235 |
message: "should have one empty star since the value is the second value", |
| 236 |
}); |
| 237 |
|
| 238 |
// switch to edit mode and check the result |
| 239 |
await click("tbody td:not(.o_list_record_selector)"); |
| 240 |
await animationFrame(); |
| 241 |
|
| 242 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 243 |
message: |
| 244 |
"should have two stars for representing each possible value: no star, one star and two stars", |
| 245 |
}); |
| 246 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(1, { |
| 247 |
message: "should have one full star since the value is the second value", |
| 248 |
}); |
| 249 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(1, { |
| 250 |
message: "should have one empty star since the value is the second value", |
| 251 |
}); |
| 252 |
|
| 253 |
// save |
| 254 |
await click(".o_control_panel_main_buttons .o_list_button_save"); |
| 255 |
await animationFrame(); |
| 256 |
|
| 257 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 258 |
message: |
| 259 |
"should have two stars for representing each possible value: no star, one star and two stars", |
| 260 |
}); |
| 261 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(1, { |
| 262 |
message: "should have one full star since the value is the second value", |
| 263 |
}); |
| 264 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(1, { |
| 265 |
message: "should have one empty star since the value is the second value", |
| 266 |
}); |
| 267 |
|
| 268 |
// click on the first star in readonly mode |
| 269 |
await click(".o_priority a.o_priority_star.fa-star"); |
| 270 |
await animationFrame(); |
| 271 |
|
| 272 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 273 |
message: "should still have two stars", |
| 274 |
}); |
| 275 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(0, { |
| 276 |
message: "should now have no full star since the value is the first value", |
| 277 |
}); |
| 278 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(2, { |
| 279 |
message: "should now have two empty stars since the value is the first value", |
| 280 |
}); |
| 281 |
|
| 282 |
// re-enter edit mode to force re-rendering the widget to check if the value was correctly saved |
| 283 |
await click("tbody td:not(.o_list_record_selector)"); |
| 284 |
await animationFrame(); |
| 285 |
|
| 286 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 287 |
message: "should still have two stars", |
| 288 |
}); |
| 289 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(0, { |
| 290 |
message: "should now have no full star since the value is the first value", |
| 291 |
}); |
| 292 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(2, { |
| 293 |
message: "should now have two empty stars since the value is the first value", |
| 294 |
}); |
| 295 |
|
| 296 |
// Click on second star in edit mode |
| 297 |
await click(".o_priority a.o_priority_star.fa-star-o:last"); |
| 298 |
await animationFrame(); |
| 299 |
|
| 300 |
expect(".o_data_row:last-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 301 |
message: "should still have two stars", |
| 302 |
}); |
| 303 |
expect(".o_data_row:last-child .o_priority a.o_priority_star.fa-star").toHaveCount(2, { |
| 304 |
message: "should now have two full stars since the value is the third value", |
| 305 |
}); |
| 306 |
expect(".o_data_row:last-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(0, { |
| 307 |
message: "should now have no empty star since the value is the third value", |
| 308 |
}); |
| 309 |
|
| 310 |
// save |
| 311 |
await click(".o_control_panel_main_buttons .o_list_button_save"); |
| 312 |
await animationFrame(); |
| 313 |
|
| 314 |
expect(".o_data_row:last-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 315 |
message: "should still have two stars", |
| 316 |
}); |
| 317 |
expect(".o_data_row:last-child .o_priority a.o_priority_star.fa-star").toHaveCount(2, { |
| 318 |
message: "should now have two full stars since the value is the third value", |
| 319 |
}); |
| 320 |
expect(".o_data_row:last-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(0, { |
| 321 |
message: "should now have no empty star since the value is the third value", |
| 322 |
}); |
| 323 |
}); |
| 324 |
|
| 325 |
test.tags("desktop"); |
| 326 |
test("PriorityField hover in editable list view", async () => { |
| 327 |
onRpc("has_group", () => true); |
| 328 |
await mountView({ |
| 329 |
type: "list", |
| 330 |
resModel: "partner", |
| 331 |
arch: /* xml */ `<list editable="bottom"><field name="selection" widget="priority" /></list>`, |
| 332 |
}); |
| 333 |
|
| 334 |
expect(".o_data_row:first-child .o_priority:not(.o_field_empty)").toHaveCount(1); |
| 335 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2, { |
| 336 |
message: |
| 337 |
"should have two stars for representing each possible value: no star, one star and two stars", |
| 338 |
}); |
| 339 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(1, { |
| 340 |
message: "should have one full star since the value is the second value", |
| 341 |
}); |
| 342 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(1, { |
| 343 |
message: "should have one empty star since the value is the second value", |
| 344 |
}); |
| 345 |
|
| 346 |
// hover last star |
| 347 |
const star = ".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o:last"; |
| 348 |
await hover(star); |
| 349 |
await animationFrame(); |
| 350 |
|
| 351 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2); |
| 352 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(2, { |
| 353 |
message: "should temporary have two full stars since we are hovering the third value", |
| 354 |
}); |
| 355 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(0, { |
| 356 |
message: "should temporary have no empty star since we are hovering the third value", |
| 357 |
}); |
| 358 |
|
| 359 |
await leave(star); |
| 360 |
await animationFrame(); |
| 361 |
|
| 362 |
expect(".o_data_row:first-child .o_priority a.o_priority_star").toHaveCount(2); |
| 363 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star").toHaveCount(1); |
| 364 |
expect(".o_data_row:first-child .o_priority a.o_priority_star.fa-star-o").toHaveCount(1); |
| 365 |
}); |
| 366 |
|
| 367 |
test("PriorityField with readonly attribute", async () => { |
| 368 |
onRpc("write", () => { |
| 369 |
expect.step("write"); |
| 370 |
throw new Error("should not save"); |
| 371 |
}); |
| 372 |
|
| 373 |
await mountView({ |
| 374 |
type: "form", |
| 375 |
resModel: "partner", |
| 376 |
resId: 2, |
| 377 |
arch: '<form><field name="selection" widget="priority" readonly="1"/></form>', |
| 378 |
}); |
| 379 |
|
| 380 |
expect("span.o_priority_star.fa.fa-star-o.o_disabled").toHaveCount(2, { |
| 381 |
message: "stars of priority widget should rendered with span tag if readonly", |
| 382 |
}); |
| 383 |
await hover(".o_priority_star.fa-star-o:last"); |
| 384 |
await animationFrame(); |
| 385 |
expect.step("hover"); |
| 386 |
expect(".o_field_widget .o_priority a.o_priority_star.fa-star").toHaveCount(0, { |
| 387 |
message: "should have no full stars on hover since the field is readonly", |
| 388 |
}); |
| 389 |
await click(".o_priority_star.fa-star-o:last"); |
| 390 |
await animationFrame(); |
| 391 |
expect.step("click"); |
| 392 |
expect("span.o_priority_star.fa.fa-star-o.o_disabled").toHaveCount(2, { |
| 393 |
message: "should still have two stars", |
| 394 |
}); |
| 395 |
expect.verifySteps(["hover", "click"]); |
| 396 |
}); |
| 397 |
|
| 398 |
test('PriorityField edited by the smart action "Set priority..."', async () => { |
| 399 |
await mountView({ |
| 400 |
type: "form", |
| 401 |
resModel: "partner", |
| 402 |
arch: /* xml */ `<form><field name="selection" widget="priority"/></form>`, |
| 403 |
resId: 1, |
| 404 |
}); |
| 405 |
|
| 406 |
expect("a.fa-star").toHaveCount(1); |
| 407 |
|
| 408 |
await press(["control", "k"]); |
| 409 |
await animationFrame(); |
| 410 |
const idx = queryAllTexts(".o_command").indexOf("Set priority...\nALT + R"); |
| 411 |
expect(idx).toBeGreaterThan(-1); |
| 412 |
await click(queryAll(".o_command")[idx]); |
| 413 |
await animationFrame(); |
| 414 |
expect(queryAllTexts(".o_command")).toEqual(["Normal", "Blocked", "Done"]); |
| 415 |
await click("#o_command_2"); |
| 416 |
await animationFrame(); |
| 417 |
expect("a.fa-star").toHaveCount(2); |
| 418 |
}); |
| 419 |
|
| 420 |
test("PriorityField - auto save record when field toggled", async () => { |
| 421 |
onRpc("web_save", () => expect.step("web_save")); |
| 422 |
await mountView({ |
| 423 |
type: "form", |
| 424 |
resModel: "partner", |
| 425 |
resId: 1, |
| 426 |
arch: /* xml */ ` |
| 427 |
<form> |
| 428 |
<sheet> |
| 429 |
<group> |
| 430 |
<field name="selection" widget="priority" /> |
| 431 |
</group> |
| 432 |
</sheet> |
| 433 |
</form>`, |
| 434 |
}); |
| 435 |
await click(".o_field_widget .o_priority a.o_priority_star.fa-star-o:last"); |
| 436 |
await animationFrame(); |
| 437 |
expect.verifySteps(["web_save"]); |
| 438 |
}); |
| 439 |
|
| 440 |
test("PriorityField - prevent auto save with autosave option", async () => { |
| 441 |
onRpc("write", () => expect.step("write")); |
| 442 |
await mountView({ |
| 443 |
type: "form", |
| 444 |
resModel: "partner", |
| 445 |
resId: 1, |
| 446 |
arch: /* xml */ ` |
| 447 |
<form> |
| 448 |
<sheet> |
| 449 |
<group> |
| 450 |
<field name="selection" widget="priority" options="{'autosave': False}"/> |
| 451 |
</group> |
| 452 |
</sheet> |
| 453 |
</form>`, |
| 454 |
}); |
| 455 |
|
| 456 |
await click(".o_field_widget .o_priority a.o_priority_star.fa-star-o:last"); |
| 457 |
await animationFrame(); |
| 458 |
expect.verifySteps([]); |
| 459 |
}); |