| 1 |
import { beforeEach, expect, test } from "@odoo/hoot"; |
| 2 |
import { click, edit, queryAll, queryAllTexts, queryOne } from "@odoo/hoot-dom"; |
| 3 |
import { animationFrame, mockDate } from "@odoo/hoot-mock"; |
| 4 |
import { getPickerCell } from "@web/../tests/core/datetime/datetime_test_helpers"; |
| 5 |
import { |
| 6 |
defineModels, |
| 7 |
fields, |
| 8 |
models, |
| 9 |
mountView, |
| 10 |
onRpc, |
| 11 |
contains, |
| 12 |
} from "@web/../tests/web_test_helpers"; |
| 13 |
|
| 14 |
class Partner extends models.Model { |
| 15 |
date = fields.Date({ string: "A date", searchable: true }); |
| 16 |
datetime = fields.Datetime({ string: "A datetime", searchable: true }); |
| 17 |
} |
| 18 |
beforeEach(() => { |
| 19 |
onRpc("has_group", () => true); |
| 20 |
}); |
| 21 |
defineModels([Partner]); |
| 22 |
|
| 23 |
test("RemainingDaysField on a date field in list view", async () => { |
| 24 |
mockDate("2017-10-08 15:35:11"); |
| 25 |
|
| 26 |
Partner._records = [ |
| 27 |
{ id: 1, date: "2017-10-08" }, // today |
| 28 |
{ id: 2, date: "2017-10-09" }, // tomorrow |
| 29 |
{ id: 3, date: "2017-10-07" }, // yesterday |
| 30 |
{ id: 4, date: "2017-10-10" }, // + 2 days |
| 31 |
{ id: 5, date: "2017-10-05" }, // - 3 days |
| 32 |
{ id: 6, date: "2018-02-08" }, // + 4 months (diff >= 100 days) |
| 33 |
{ id: 7, date: "2017-06-08" }, // - 4 months (diff >= 100 days) |
| 34 |
{ id: 8, date: false }, |
| 35 |
]; |
| 36 |
|
| 37 |
await mountView({ |
| 38 |
type: "list", |
| 39 |
resModel: "partner", |
| 40 |
arch: /* xml */ `<list><field name="date" widget="remaining_days" /></list>`, |
| 41 |
}); |
| 42 |
|
| 43 |
const cells = queryAll(".o_data_cell"); |
| 44 |
expect(cells[0]).toHaveText("Today"); |
| 45 |
expect(cells[1]).toHaveText("Tomorrow"); |
| 46 |
expect(cells[2]).toHaveText("Yesterday"); |
| 47 |
expect(cells[3]).toHaveText("In 2 days"); |
| 48 |
expect(cells[4]).toHaveText("3 days ago"); |
| 49 |
expect(cells[5]).toHaveText("Feb 8, 2018"); |
| 50 |
expect(cells[6]).toHaveText("Jun 8"); |
| 51 |
expect(cells[7]).toHaveText(""); |
| 52 |
|
| 53 |
expect(queryOne(".o_field_widget > div", { root: cells[0] })).toHaveAttribute( |
| 54 |
"title", |
| 55 |
"10/08/2017" |
| 56 |
); |
| 57 |
expect(queryOne(".o_field_widget > div", { root: cells[0] })).toHaveClass([ |
| 58 |
"fw-bold", |
| 59 |
"text-warning", |
| 60 |
]); |
| 61 |
expect(queryOne(".o_field_widget > div", { root: cells[1] })).not.toHaveClass([ |
| 62 |
"fw-bold", |
| 63 |
"text-warning", |
| 64 |
"text-danger", |
| 65 |
]); |
| 66 |
expect(queryOne(".o_field_widget > div", { root: cells[2] })).toHaveClass([ |
| 67 |
"fw-bold", |
| 68 |
"text-danger", |
| 69 |
]); |
| 70 |
expect(queryOne(".o_field_widget > div", { root: cells[3] })).not.toHaveClass([ |
| 71 |
"fw-bold", |
| 72 |
"text-warning", |
| 73 |
"text-danger", |
| 74 |
]); |
| 75 |
expect(queryOne(".o_field_widget > div", { root: cells[4] })).toHaveClass([ |
| 76 |
"fw-bold", |
| 77 |
"text-danger", |
| 78 |
]); |
| 79 |
expect(queryOne(".o_field_widget > div", { root: cells[5] })).not.toHaveClass([ |
| 80 |
"fw-bold", |
| 81 |
"text-warning", |
| 82 |
"text-danger", |
| 83 |
]); |
| 84 |
expect(queryOne(".o_field_widget > div", { root: cells[6] })).toHaveClass([ |
| 85 |
"fw-bold", |
| 86 |
"text-danger", |
| 87 |
]); |
| 88 |
}); |
| 89 |
|
| 90 |
test.tags("desktop"); |
| 91 |
test("RemainingDaysField on a date field in multi edit list view", async () => { |
| 92 |
mockDate("2017-10-08 15:35:11"); // October 8 2017, 15:35:11 |
| 93 |
|
| 94 |
Partner._records = [ |
| 95 |
{ id: 1, date: "2017-10-08" }, // today |
| 96 |
{ id: 2, date: "2017-10-09" }, // tomorrow |
| 97 |
{ id: 8, date: false }, |
| 98 |
]; |
| 99 |
|
| 100 |
await mountView({ |
| 101 |
type: "list", |
| 102 |
resModel: "partner", |
| 103 |
arch: /* xml */ `<list multi_edit="1"><field name="date" widget="remaining_days" /></list>`, |
| 104 |
}); |
| 105 |
|
| 106 |
expect(queryAllTexts(".o_data_cell").slice(0, 2)).toEqual(["Today", "Tomorrow"]); |
| 107 |
|
| 108 |
// select two records and edit them |
| 109 |
await click(".o_data_row:eq(0) .o_list_record_selector input:first"); |
| 110 |
await animationFrame(); |
| 111 |
await click(".o_data_row:eq(1) .o_list_record_selector input:first"); |
| 112 |
await animationFrame(); |
| 113 |
|
| 114 |
await click(".o_data_row:eq(0) .o_data_cell:first"); |
| 115 |
await animationFrame(); |
| 116 |
|
| 117 |
expect(".o_field_remaining_days button").toHaveCount(1); |
| 118 |
|
| 119 |
await contains(".o_field_remaining_days button").click(); |
| 120 |
await edit("10/10/2017", { confirm: "enter" }); |
| 121 |
await animationFrame(); |
| 122 |
expect(".modal").toHaveCount(1); |
| 123 |
expect(".modal .o_field_widget").toHaveText("In 2 days", { |
| 124 |
message: "should have 'In 2 days' value to change", |
| 125 |
}); |
| 126 |
await click(".modal .modal-footer .btn-primary"); |
| 127 |
await animationFrame(); |
| 128 |
|
| 129 |
expect(".o_data_row:eq(0) .o_data_cell:first").toHaveText("In 2 days", { |
| 130 |
message: "should have 'In 2 days' as date field value", |
| 131 |
}); |
| 132 |
expect(".o_data_row:eq(1) .o_data_cell:first").toHaveText("In 2 days", { |
| 133 |
message: "should have 'In 2 days' as date field value", |
| 134 |
}); |
| 135 |
}); |
| 136 |
|
| 137 |
test.tags("desktop"); |
| 138 |
test("RemainingDaysField, enter wrong value manually in multi edit list view", async () => { |
| 139 |
mockDate("2017-10-08 15:35:11"); // October 8 2017, 15:35:11 |
| 140 |
Partner._records = [ |
| 141 |
{ id: 1, date: "2017-10-08" }, // today |
| 142 |
{ id: 2, date: "2017-10-09" }, // tomorrow |
| 143 |
{ id: 8, date: false }, |
| 144 |
]; |
| 145 |
|
| 146 |
await mountView({ |
| 147 |
type: "list", |
| 148 |
resModel: "partner", |
| 149 |
arch: /* xml */ `<list multi_edit="1"><field name="date" widget="remaining_days" /></list>`, |
| 150 |
}); |
| 151 |
|
| 152 |
const cells = queryAll(".o_data_cell"); |
| 153 |
const rows = queryAll(".o_data_row"); |
| 154 |
|
| 155 |
expect(cells[0]).toHaveText("Today"); |
| 156 |
expect(cells[1]).toHaveText("Tomorrow"); |
| 157 |
|
| 158 |
// select two records and edit them |
| 159 |
await click(".o_list_record_selector input", { root: rows[0] }); |
| 160 |
await animationFrame(); |
| 161 |
await click(".o_list_record_selector input", { root: rows[1] }); |
| 162 |
await animationFrame(); |
| 163 |
|
| 164 |
await click(".o_data_cell", { root: rows[0] }); |
| 165 |
await animationFrame(); |
| 166 |
|
| 167 |
expect(".o_field_remaining_days button").toHaveCount(1); |
| 168 |
|
| 169 |
await contains(".o_field_remaining_days button").click(); |
| 170 |
await edit("blabla", { confirm: "enter" }); |
| 171 |
await animationFrame(); |
| 172 |
expect(".modal").toHaveCount(0); |
| 173 |
expect(cells[0]).toHaveText("Today"); |
| 174 |
expect(cells[1]).toHaveText("Tomorrow"); |
| 175 |
}); |
| 176 |
|
| 177 |
test("RemainingDaysField on a date field in form view", async () => { |
| 178 |
mockDate("2017-10-08 15:35:11"); // October 8 2017, 15:35:11 |
| 179 |
Partner._records = [ |
| 180 |
{ id: 1, date: "2017-10-08" }, // today |
| 181 |
]; |
| 182 |
|
| 183 |
await mountView({ |
| 184 |
type: "form", |
| 185 |
resModel: "partner", |
| 186 |
resId: 1, |
| 187 |
arch: /* xml */ `<form><field name="date" widget="remaining_days" /></form>`, |
| 188 |
}); |
| 189 |
|
| 190 |
expect(".o_field_widget button").toHaveValue("10/08/2017"); |
| 191 |
|
| 192 |
expect(".o_form_editable").toHaveCount(1); |
| 193 |
expect("div.o_field_widget[name='date'] button").toHaveCount(1); |
| 194 |
|
| 195 |
await contains(".o_field_remaining_days button").click(); |
| 196 |
await animationFrame(); |
| 197 |
expect(".o_datetime_picker").toHaveCount(1, { message: "datepicker should be opened" }); |
| 198 |
|
| 199 |
await click(getPickerCell("9")); |
| 200 |
await animationFrame(); |
| 201 |
await click(".o_form_button_save"); |
| 202 |
await animationFrame(); |
| 203 |
expect(".o_field_widget button").toHaveValue("10/09/2017"); |
| 204 |
}); |
| 205 |
|
| 206 |
test("RemainingDaysField on a date field on a new record in form", async () => { |
| 207 |
await mountView({ |
| 208 |
type: "form", |
| 209 |
resModel: "partner", |
| 210 |
arch: ` |
| 211 |
<form> |
| 212 |
<field name="date" widget="remaining_days" /> |
| 213 |
</form>`, |
| 214 |
}); |
| 215 |
|
| 216 |
expect(".o_form_editable .o_field_widget[name='date'] input").toHaveCount(1); |
| 217 |
await click(".o_field_widget[name='date'] input"); |
| 218 |
await animationFrame(); |
| 219 |
expect(".o_datetime_picker").toHaveCount(1); |
| 220 |
}); |
| 221 |
|
| 222 |
test("RemainingDaysField in form view (readonly)", async () => { |
| 223 |
mockDate("2017-10-08 15:35:11"); // October 8 2017, 15:35:11 |
| 224 |
Partner._records = [ |
| 225 |
{ id: 1, date: "2017-10-08", datetime: "2017-10-08 10:00:00" }, // today |
| 226 |
]; |
| 227 |
|
| 228 |
await mountView({ |
| 229 |
type: "form", |
| 230 |
resModel: "partner", |
| 231 |
resId: 1, |
| 232 |
arch: /* xml */ ` |
| 233 |
<form> |
| 234 |
<field name="date" widget="remaining_days" readonly="1" /> |
| 235 |
<field name="datetime" widget="remaining_days" readonly="1" /> |
| 236 |
</form>`, |
| 237 |
}); |
| 238 |
|
| 239 |
expect(".o_field_widget[name='date']").toHaveText("Today"); |
| 240 |
expect(".o_field_widget[name='date'] > div ").toHaveClass(["fw-bold", "text-warning"]); |
| 241 |
expect(".o_field_widget[name='datetime']").toHaveText("Today"); |
| 242 |
expect(".o_field_widget[name='datetime'] > div ").toHaveClass(["fw-bold", "text-warning"]); |
| 243 |
}); |
| 244 |
|
| 245 |
test("RemainingDaysField on a datetime field in form view", async () => { |
| 246 |
mockDate("2017-10-08 15:35:11"); // October 8 2017, 15:35:11 |
| 247 |
Partner._records = [ |
| 248 |
{ id: 1, datetime: "2017-10-08 10:00:00" }, // today |
| 249 |
]; |
| 250 |
|
| 251 |
await mountView({ |
| 252 |
type: "form", |
| 253 |
resModel: "partner", |
| 254 |
resId: 1, |
| 255 |
arch: /* xml */ `<form><field name="datetime" widget="remaining_days" /></form>`, |
| 256 |
}); |
| 257 |
expect(".o_field_widget button").toHaveValue("10/08/2017 11:00:00"); |
| 258 |
expect("div.o_field_widget[name='datetime'] button").toHaveCount(1); |
| 259 |
|
| 260 |
await contains(".o_field_widget button").click(); |
| 261 |
await animationFrame(); |
| 262 |
expect(".o_datetime_picker").toHaveCount(1, { message: "datepicker should be opened" }); |
| 263 |
|
| 264 |
await click(getPickerCell("9")); |
| 265 |
await animationFrame(); |
| 266 |
await click(".o_form_button_save"); |
| 267 |
await animationFrame(); |
| 268 |
expect(".o_field_widget button").toHaveValue("10/09/2017 11:00:00"); |
| 269 |
}); |
| 270 |
|
| 271 |
test("RemainingDaysField on a datetime field in list view in UTC", async () => { |
| 272 |
mockDate("2017-10-08 15:35:11", 0); // October 8 2017, 15:35:11 |
| 273 |
Partner._records = [ |
| 274 |
{ id: 1, datetime: "2017-10-08 20:00:00" }, // today |
| 275 |
{ id: 2, datetime: "2017-10-09 08:00:00" }, // tomorrow |
| 276 |
{ id: 3, datetime: "2017-10-07 18:00:00" }, // yesterday |
| 277 |
{ id: 4, datetime: "2017-10-10 22:00:00" }, // + 2 days |
| 278 |
{ id: 5, datetime: "2017-10-05 04:00:00" }, // - 3 days |
| 279 |
{ id: 6, datetime: "2018-02-08 04:00:00" }, // + 4 months (diff >= 100 days) |
| 280 |
{ id: 7, datetime: "2017-06-08 04:00:00" }, // - 4 months (diff >= 100 days) |
| 281 |
{ id: 8, datetime: false }, |
| 282 |
]; |
| 283 |
|
| 284 |
await mountView({ |
| 285 |
type: "list", |
| 286 |
resModel: "partner", |
| 287 |
arch: /* xml */ `<list><field name="datetime" widget="remaining_days" /></list>`, |
| 288 |
}); |
| 289 |
|
| 290 |
expect(queryAllTexts(".o_data_cell")).toEqual([ |
| 291 |
"Today", |
| 292 |
"Tomorrow", |
| 293 |
"Yesterday", |
| 294 |
"In 2 days", |
| 295 |
"3 days ago", |
| 296 |
"Feb 8, 2018", |
| 297 |
"Jun 8", |
| 298 |
"", |
| 299 |
]); |
| 300 |
|
| 301 |
expect(".o_data_cell .o_field_widget div:first").toHaveAttribute("title", "10/08/2017"); |
| 302 |
|
| 303 |
const cells = queryAll(".o_data_cell div div"); |
| 304 |
expect(cells[0]).toHaveClass(["fw-bold", "text-warning"]); |
| 305 |
expect(cells[1]).not.toHaveClass(["fw-bold", "text-warning", "text-danger"]); |
| 306 |
expect(cells[2]).toHaveClass(["fw-bold", "text-danger"]); |
| 307 |
expect(cells[3]).not.toHaveClass(["fw-bold", "text-warning", "text-danger"]); |
| 308 |
expect(cells[4]).toHaveClass(["fw-bold", "text-danger"]); |
| 309 |
expect(cells[5]).not.toHaveClass(["fw-bold", "text-warning", "text-danger"]); |
| 310 |
expect(cells[6]).toHaveClass(["fw-bold", "text-danger"]); |
| 311 |
}); |
| 312 |
|
| 313 |
test("RemainingDaysField on a datetime field in list view in UTC+6", async () => { |
| 314 |
mockDate("2017-10-08 15:35:11", +6); // October 8 2017, 15:35:11, UTC+6 |
| 315 |
|
| 316 |
Partner._records = [ |
| 317 |
{ id: 1, datetime: "2017-10-08 20:00:00" }, // tomorrow |
| 318 |
{ id: 2, datetime: "2017-10-09 08:00:00" }, // tomorrow |
| 319 |
{ id: 3, datetime: "2017-10-07 18:30:00" }, // today |
| 320 |
{ id: 4, datetime: "2017-10-07 12:00:00" }, // yesterday |
| 321 |
{ id: 5, datetime: "2017-10-09 20:00:00" }, // + 2 days |
| 322 |
]; |
| 323 |
|
| 324 |
await mountView({ |
| 325 |
type: "list", |
| 326 |
resModel: "partner", |
| 327 |
arch: /* xml */ `<list><field name="datetime" widget="remaining_days" /></list>`, |
| 328 |
}); |
| 329 |
|
| 330 |
expect(queryAllTexts(".o_data_cell")).toEqual([ |
| 331 |
"Tomorrow", |
| 332 |
"Tomorrow", |
| 333 |
"Today", |
| 334 |
"Yesterday", |
| 335 |
"In 2 days", |
| 336 |
]); |
| 337 |
expect(".o_data_cell .o_field_widget div:first").toHaveAttribute("title", "10/09/2017"); |
| 338 |
}); |
| 339 |
|
| 340 |
test("RemainingDaysField on a date field in list view in UTC-6", async () => { |
| 341 |
mockDate("2017-10-08 15:35:11", -6); // October 8 2017, 15:35:11, UTC-6 |
| 342 |
|
| 343 |
Partner._records = [ |
| 344 |
{ id: 1, date: "2017-10-08" }, // today |
| 345 |
{ id: 2, date: "2017-10-09" }, // tomorrow |
| 346 |
{ id: 3, date: "2017-10-07" }, // yesterday |
| 347 |
{ id: 4, date: "2017-10-10" }, // + 2 days |
| 348 |
{ id: 5, date: "2017-10-05" }, // - 3 days |
| 349 |
]; |
| 350 |
|
| 351 |
await mountView({ |
| 352 |
type: "list", |
| 353 |
resModel: "partner", |
| 354 |
arch: /* xml */ `<list><field name="date" widget="remaining_days" /></list>`, |
| 355 |
}); |
| 356 |
expect(queryAllTexts(".o_data_cell")).toEqual([ |
| 357 |
"Today", |
| 358 |
"Tomorrow", |
| 359 |
"Yesterday", |
| 360 |
"In 2 days", |
| 361 |
"3 days ago", |
| 362 |
]); |
| 363 |
expect(".o_data_cell .o_field_widget div:first").toHaveAttribute("title", "10/08/2017"); |
| 364 |
}); |
| 365 |
|
| 366 |
test("RemainingDaysField on a datetime field in list view in UTC-8", async () => { |
| 367 |
mockDate("2017-10-08 15:35:11", -8); // October 8 2017, 15:35:11, UTC-8 |
| 368 |
|
| 369 |
Partner._records = [ |
| 370 |
{ id: 1, datetime: "2017-10-08 20:00:00" }, // today |
| 371 |
{ id: 2, datetime: "2017-10-09 07:00:00" }, // today |
| 372 |
{ id: 3, datetime: "2017-10-09 10:00:00" }, // tomorrow |
| 373 |
{ id: 4, datetime: "2017-10-08 06:00:00" }, // yesterday |
| 374 |
{ id: 5, datetime: "2017-10-07 02:00:00" }, // - 2 days |
| 375 |
]; |
| 376 |
|
| 377 |
await mountView({ |
| 378 |
type: "list", |
| 379 |
resModel: "partner", |
| 380 |
arch: /* xml */ `<list><field name="datetime" widget="remaining_days" /></list>`, |
| 381 |
}); |
| 382 |
|
| 383 |
expect(queryAllTexts(".o_data_cell")).toEqual([ |
| 384 |
"Today", |
| 385 |
"Today", |
| 386 |
"Tomorrow", |
| 387 |
"Yesterday", |
| 388 |
"2 days ago", |
| 389 |
]); |
| 390 |
}); |
| 391 |
|
| 392 |
test("RemainingDaysField with custom decoration classes", async () => { |
| 393 |
mockDate("2017-10-08 15:35:11"); |
| 394 |
|
| 395 |
Partner._records = [ |
| 396 |
{ id: 1, date: "2017-10-08" }, // today |
| 397 |
{ id: 2, date: "2017-10-09" }, // tomorrow |
| 398 |
{ id: 3, date: "2017-10-07" }, // yesterday |
| 399 |
{ id: 4, date: "2017-10-10" }, // + 2 days |
| 400 |
{ id: 5, date: "2017-10-05" }, // - 3 days |
| 401 |
{ id: 6, date: "2018-02-08" }, // + 4 months (diff >= 100 days) |
| 402 |
{ id: 7, date: "2017-06-08" }, // - 4 months (diff >= 100 days) |
| 403 |
{ id: 8, date: false }, |
| 404 |
]; |
| 405 |
|
| 406 |
await mountView({ |
| 407 |
type: "list", |
| 408 |
resModel: "partner", |
| 409 |
arch: /* xml */ ` |
| 410 |
<list> |
| 411 |
<field |
| 412 |
name="date" |
| 413 |
widget="remaining_days" |
| 414 |
options="{ |
| 415 |
'classes': { |
| 416 |
'muted': 'days < -30', |
| 417 |
'danger': 'days < 0', |
| 418 |
'success': 'days == 0', |
| 419 |
'warning': 'days > 30', |
| 420 |
'info': 'days >= 2' |
| 421 |
} |
| 422 |
}" |
| 423 |
/> |
| 424 |
</list>`, |
| 425 |
}); |
| 426 |
|
| 427 |
const cells = queryAll(".o_data_cell div div"); |
| 428 |
expect(cells[0]).toHaveClass("text-success"); |
| 429 |
expect(cells[1]).not.toHaveAttribute("class"); |
| 430 |
expect(cells[2]).toHaveClass("text-danger"); |
| 431 |
expect(cells[3]).toHaveClass("text-info"); |
| 432 |
expect(cells[4]).toHaveClass("text-danger"); |
| 433 |
expect(cells[5]).toHaveClass("text-warning"); |
| 434 |
expect(cells[6]).toHaveClass("text-muted"); |
| 435 |
expect(cells[7]).not.toHaveAttribute("class"); |
| 436 |
}); |