Debian Codegraph @

addons/web/static/tests/views/fields/datetime_field.test.js

raw
1 import { after, expect, test } from "@odoo/hoot";
2 import {
3 click,
4 edit,
5 queryAll,
6 queryAllProperties,
7 queryAllTexts,
8 queryRect,
9 resize,
10 } from "@odoo/hoot-dom";
11 import { animationFrame, mockTimeZone } from "@odoo/hoot-mock";
12 import {
13 editTime,
14 getPickerCell,
15 zoomOut,
16 } from "@web/../tests/core/datetime/datetime_test_helpers";
17 import {
18 clickSave,
19 defineModels,
20 defineParams,
21 fields,
22 models,
23 mountView,
24 onRpc,
25 contains,
26 } from "@web/../tests/web_test_helpers";
27 import { resetDateFieldWidths } from "@web/views/list/column_width_hook";
28
29 class Partner extends models.Model {
30 date = fields.Date({ string: "A date", searchable: true });
31 datetime = fields.Datetime({ string: "A datetime", searchable: true });
32 p = fields.One2many({
33 string: "one2many field",
34 relation: "partner",
35 searchable: true,
36 });
37 _records = [
38 {
39 id: 1,
40 date: "2017-02-03",
41 datetime: "2017-02-08 10:00:00",
42 p: [],
43 },
44 {
45 id: 2,
46 date: false,
47 datetime: false,
48 },
49 ];
50 }
51 class User extends models.Model {
52 _name = "res.users";
53 has_group() {
54 return true;
55 }
56 }
57 defineModels([Partner, User]);
58
59 test("DatetimeField in form view", async () => {
60 mockTimeZone(+2); // UTC+2
61
62 await mountView({
63 type: "form",
64 resModel: "partner",
65 resId: 1,
66 arch: `<form>
67 <field name="datetime"/>
68 <field name="datetime" readonly="1"/>
69 </form>`,
70 });
71
72 const expectedDateString = "02/08/2017 12:00:00"; // 10:00:00 without timezone
73 expect(".o_field_datetime button").toHaveValue(expectedDateString, {
74 message: "the datetime should be correctly displayed",
75 });
76 expect(".o_field_datetime button").toHaveAttribute("data-tooltip", expectedDateString);
77 expect(".o_field_datetime span").toHaveAttribute("data-tooltip", expectedDateString);
78
79 // datepicker should not open on focus
80 expect(".o_datetime_picker").toHaveCount(0);
81
82 await click(".o_field_datetime button");
83 await animationFrame();
84 expect(".o_datetime_picker").toHaveCount(1);
85
86 // select 22 April 2018 at 8:25
87 await zoomOut();
88 await zoomOut();
89 await click(getPickerCell("2018"));
90 await animationFrame();
91 await click(getPickerCell("Apr"));
92 await animationFrame();
93 await click(getPickerCell("22"));
94 await animationFrame();
95 await editTime("8:25");
96 // Close the datepicker
97 await click(".o_form_view_container");
98 await animationFrame();
99
100 expect(".o_datetime_picker").toHaveCount(0, { message: "datepicker should be closed" });
101
102 const newExpectedDateString = "04/22/2018 08:25:00";
103 expect(".o_field_datetime button").toHaveValue(newExpectedDateString, {
104 message: "the selected date should be displayed in the input",
105 });
106
107 // save
108 await clickSave();
109 expect(".o_field_datetime button").toHaveValue(newExpectedDateString, {
110 message: "the selected date should be displayed after saving",
111 });
112 });
113
114 test("DatetimeField only triggers fieldChange when a day is picked and when an hour/minute is selected", async () => {
115 mockTimeZone(+2);
116
117 Partner._onChanges.datetime = () => {};
118
119 onRpc("onchange", () => expect.step("onchange"));
120
121 await mountView({
122 type: "form",
123 resModel: "partner",
124 resId: 1,
125 arch: /* xml */ '<form><field name="datetime"/></form>',
126 });
127
128 await click(".o_field_datetime button");
129 await animationFrame();
130
131 expect(".o_datetime_picker").toHaveCount(1);
132 // select 22 April 2018 at 8:25
133 await zoomOut();
134 await zoomOut();
135 await click(getPickerCell("2018"));
136 await animationFrame();
137 await click(getPickerCell("Apr"));
138 await animationFrame();
139 await click(getPickerCell("22"));
140 await animationFrame();
141
142 expect.verifySteps([]);
143
144 await editTime("8:25");
145
146 expect.verifySteps([]);
147
148 // Close the datepicker
149 await click(document.body);
150 await animationFrame();
151
152 expect(".o_datetime_picker").toHaveCount(0);
153
154 expect(".o_field_datetime button").toHaveValue("04/22/2018 08:25:00");
155 expect.verifySteps(["onchange"]);
156 });
157
158 test("DatetimeField edit hour/minute and click away", async () => {
159 mockTimeZone(0);
160
161 onRpc("web_save", ({ args }) => {
162 expect(args[1].datetime).toBe("2017-02-08 08:30:00", {
163 message: "the correct value should be saved",
164 });
165 });
166
167 await mountView({
168 type: "form",
169 resModel: "partner",
170 resId: 1,
171 arch: /* xml */ '<form><field name="datetime"/></form>',
172 });
173
174 // Open the datepicker
175 await click(".o_field_datetime button");
176 await animationFrame();
177 expect(".o_datetime_picker").toHaveCount(1);
178
179 // Manually change the time without { confirm: "enter" }
180 await click(`.o_time_picker_input:eq(0)`);
181 await animationFrame();
182 await edit("8:30");
183 await animationFrame();
184 expect(".o_field_datetime input").toHaveValue("02/08/2017 10:00:00", {
185 message: "Input value shouldn't be updated yet",
186 });
187
188 // Close the datepicker
189 await click(document.body);
190 await animationFrame();
191 expect(".o_datetime_picker").toHaveCount(0);
192
193 expect(".o_field_datetime button").toHaveValue("02/08/2017 08:30:00");
194 await clickSave();
195 });
196
197 test("DatetimeField with datetime formatted without second", async () => {
198 mockTimeZone(0);
199
200 Partner._fields.datetime = fields.Datetime({
201 string: "A datetime",
202 searchable: true,
203 default: "2017-08-02 12:00:05",
204 required: true,
205 });
206
207 defineParams({
208 lang_parameters: {
209 date_format: "%m/%d/%Y",
210 time_format: "%H:%M",
211 },
212 });
213
214 await mountView({
215 type: "form",
216 resModel: "partner",
217 arch: '<form><field name="datetime"/></form>',
218 });
219
220 const expectedDateString = "08/02/2017 12:00";
221 expect(".o_field_datetime button").toHaveValue(expectedDateString, {
222 message: "the datetime should be correctly displayed",
223 });
224
225 await click(".o_form_button_cancel");
226 expect(".modal").toHaveCount(0, { message: "there should not be a Warning dialog" });
227 });
228
229 test("DatetimeField in editable list view", async () => {
230 mockTimeZone(+2);
231 onRpc("has_group", () => true);
232
233 await mountView({
234 type: "list",
235 resModel: "partner",
236 arch: /* xml */ `<list editable="bottom"><field name="datetime"/></list>`,
237 });
238
239 expect("tr.o_data_row td:not(.o_list_record_selector):first").toHaveText(
240 "Feb 8, 2017, 12:00 PM",
241 {
242 message: "the datetime should be correctly displayed",
243 }
244 );
245
246 // switch to edit mode
247 await click(".o_data_row .o_data_cell");
248 await animationFrame();
249 expect(".o_field_datetime button").toHaveCount(1, {
250 message: "the view should have a date input for editable mode",
251 });
252
253 expect(".o_field_datetime button").toBeFocused({
254 message: "date input should have the focus",
255 });
256
257 expect(".o_field_datetime button").toHaveValue("02/08/2017 12:00:00", {
258 message: "the date should be correct in edit mode",
259 });
260
261 expect(".o_datetime_picker").toHaveCount(0);
262
263 await click(".o_field_datetime button");
264 await animationFrame();
265
266 expect(".o_datetime_picker").toHaveCount(1);
267
268 // select 22 April 2018 at 8:25
269 await zoomOut();
270 await zoomOut();
271 await click(getPickerCell("2018"));
272 await animationFrame();
273 await click(getPickerCell("Apr"));
274 await animationFrame();
275 await click(getPickerCell("22"));
276 await animationFrame();
277 await editTime("8:25");
278 await animationFrame();
279
280 expect(".o_field_datetime input").toHaveValue("04/22/2018 08:25:00", {
281 message: "the date should be correct in edit mode",
282 });
283 // save
284
285 await click(".o_list_button_save");
286 await animationFrame();
287 expect("tr.o_data_row td:not(.o_list_record_selector):first").toHaveText(
288 "Apr 22, 2018, 8:25 AM",
289 { message: "the selected datetime should be displayed after saving" }
290 );
291 });
292
293 test("DatetimeField input in editable list view keeps its parent's width when empty", async () => {
294 await mountView({
295 type: "list",
296 resModel: "partner",
297 arch: /* xml */ `<list editable="bottom"><field name="datetime"/></list>`,
298 });
299 await contains(".o_data_row:eq(1) .o_data_cell").click();
300 expect(".o_data_row:eq(1) .o_data_cell input").toHaveRect(
301 queryRect(".o_data_row:eq(1) .o_data_cell .o_field_datetime"),
302 { message: "input should have the same size as its parent when empty" }
303 );
304 });
305
306 test.tags("desktop");
307 test("multi edition of DatetimeField in list view: edit date in input", async () => {
308 mockTimeZone(0);
309 onRpc("has_group", () => true);
310
311 await mountView({
312 type: "list",
313 resModel: "partner",
314 arch: '<list multi_edit="1"><field name="datetime"/></list>',
315 });
316
317 // select two records and edit them
318 await click(".o_data_row:eq(0) .o_list_record_selector input");
319 await animationFrame();
320 await click(".o_data_row:eq(1) .o_list_record_selector input");
321 await animationFrame();
322
323 await click(".o_data_row:eq(0) .o_data_cell");
324 await animationFrame();
325
326 expect(".o_field_datetime button").toHaveCount(1);
327
328 await click(".o_field_datetime button");
329 await animationFrame();
330 await click(".o_field_datetime input");
331 await animationFrame();
332 await edit("10/02/2019 09:00:00", { confirm: "Enter" });
333 await animationFrame();
334
335 expect(".modal").toHaveCount(1);
336
337 await click(".modal .modal-footer .btn-primary");
338 await animationFrame();
339
340 expect(".o_data_row:first-child .o_data_cell:first").toHaveText("Oct 2, 9:00 AM");
341 expect(".o_data_row:nth-child(2) .o_data_cell:first").toHaveText("Oct 2, 9:00 AM");
342 });
343
344 test.tags("desktop");
345 test("multi edition of DatetimeField in list view: clear date in input", async () => {
346 Partner._records[1].datetime = "2017-02-08 10:00:00";
347 onRpc("has_group", () => true);
348
349 await mountView({
350 type: "list",
351 resModel: "partner",
352 arch: '<list multi_edit="1"><field name="datetime"/></list>',
353 });
354
355 // select two records and edit them
356 await click(".o_data_row:eq(0) .o_list_record_selector input");
357 await animationFrame();
358 await click(".o_data_row:eq(1) .o_list_record_selector input");
359 await animationFrame();
360 await click(".o_data_row:eq(0) .o_data_cell");
361 await animationFrame();
362
363 expect(".o_field_datetime button").toHaveCount(1);
364
365 await click(".o_field_datetime button");
366 await animationFrame();
367 await click(".o_field_datetime input");
368 await animationFrame();
369 await edit("", { confirm: "Enter" });
370 await animationFrame();
371
372 expect(".modal").toHaveCount(1);
373
374 await click(".modal .modal-footer .btn-primary");
375 await animationFrame();
376
377 expect(".o_data_row:first-child .o_data_cell:first").toHaveText("");
378 expect(".o_data_row:nth-child(2) .o_data_cell:first").toHaveText("");
379 });
380
381 test("DatetimeField remove value", async () => {
382 expect.assertions(4);
383
384 mockTimeZone(+2);
385
386 onRpc("web_save", ({ args }) => {
387 expect(args[1].datetime).toBe(false, { message: "the correct value should be saved" });
388 });
389 await mountView({
390 type: "form",
391 resModel: "partner",
392 resId: 1,
393 arch: /* xml */ '<form><field name="datetime"/></form>',
394 });
395
396 expect(".o_field_datetime button").toHaveValue("02/08/2017 12:00:00", {
397 message: "the date should be correct in edit mode",
398 });
399
400 await click(".o_field_datetime button");
401 await animationFrame();
402 await click(".o_field_datetime input");
403 await animationFrame();
404 await edit("");
405 await animationFrame();
406 await click(document.body);
407 await animationFrame();
408
409 expect(".o_field_datetime input:first").toHaveValue("", {
410 message: "should have an empty input",
411 });
412
413 // save
414 await clickSave();
415 expect(".o_field_datetime:first").toHaveText("", {
416 message: "the selected date should be displayed after saving",
417 });
418 });
419
420 test("datetime field: hit enter should update value", async () => {
421 // This test verifies that the field datetime is correctly computed when:
422 // - we press enter to validate our entry
423 // - we click outside the field to validate our entry
424 // - we save
425 mockTimeZone(+2);
426
427 await mountView({
428 type: "form",
429 resModel: "partner",
430 arch: '<form><field name="datetime"/></form>',
431 resId: 1,
432 });
433
434 // Enter a beginning of date and press enter to validate
435 await click(".o_field_datetime button");
436 await animationFrame();
437 await click(".o_field_datetime input");
438 await animationFrame();
439 await edit("01/08/22 14:30", { confirm: "Enter" });
440
441 const datetimeValue = `01/08/2022 14:30:00`;
442
443 expect(".o_field_datetime input:first").toHaveValue(datetimeValue);
444
445 // Click outside the field to check that the field is not changed
446 await click(document.body);
447 await animationFrame();
448 expect(".o_field_datetime button").toHaveValue(datetimeValue);
449
450 // Save and check that it's still ok
451 await clickSave();
452
453 expect(".o_field_datetime button").toHaveValue(datetimeValue);
454 });
455
456 test("DateTimeField with label opens datepicker on click", async () => {
457 await mountView({
458 type: "form",
459 resModel: "partner",
460 resId: 1,
461 arch: /* xml */ `
462 <form>
463 <label for="datetime" string="When is it" />
464 <field name="datetime" />
465 </form>`,
466 });
467
468 await click("label.o_form_label");
469 await animationFrame();
470 expect(".o_datetime_picker").toHaveCount(1, { message: "datepicker should be opened" });
471 });
472
473 test("datetime field: use picker with arabic numbering system", async () => {
474 defineParams({ lang: "ar_001" }); // Select Arab language
475
476 await mountView({
477 type: "form",
478 resModel: "partner",
479 resId: 1,
480 arch: /* xml */ `<form string="Partners"><field name="datetime" /></form>`,
481 });
482
483 expect("[name=datetime] button").toHaveValue("٠٢/٠٨/٢٠١٧ ١١:٠٠:٠٠");
484
485 await click("[name=datetime] button");
486 await animationFrame();
487 await editTime("11:45");
488 expect("[name=datetime] input").toHaveValue("٠٢/٠٨/٢٠١٧ ١١:٤٥:٠٠");
489 });
490
491 test("datetime field in list view with show_seconds option", async () => {
492 mockTimeZone(+2);
493 onRpc("has_group", () => true);
494
495 await mountView({
496 type: "list",
497 resModel: "partner",
498 arch: /* xml */ `
499 <list>
500 <field name="datetime" widget="datetime" string="show_seconds as false"/>
501 <field name="datetime" widget="datetime" options="{'show_seconds': true}" string="show_seconds as true"/>
502 </list>`,
503 });
504
505 expect(queryAllTexts(".o_data_row:first .o_field_datetime")).toEqual([
506 "Feb 8, 2017, 12:00 PM",
507 "Feb 8, 2017, 12:00:00 PM",
508 ]);
509 });
510
511 test("edit a datetime field in form view with show_seconds option", async () => {
512 mockTimeZone(+2);
513
514 await mountView({
515 type: "form",
516 resModel: "partner",
517 arch: /* xml */ `
518 <form>
519 <field name="datetime" widget="datetime" string="show_seconds as false"/>
520 <field name="datetime" widget="datetime" options="{'show_seconds': true}" string="show_seconds as true"/>
521 </form>`,
522 });
523
524 await contains(".o_input:eq(0)").click();
525 await animationFrame();
526 expect(".o_time_picker_input").toHaveValue("11:00");
527 await edit("02/08/2017 11:00:00", { confirm: "Enter" });
528 await animationFrame();
529
530 expect(".o_input:eq(0)").toHaveValue("02/08/2017 11:00:00", {
531 message: "seconds should be hidden for showSeconds false",
532 });
533
534 await contains(".o_input:eq(1)").click();
535 await animationFrame();
536 expect(".o_time_picker_input").toHaveValue("11:00:00");
537 await edit("02/08/2017 11:00:30", { confirm: "Enter" });
538 await animationFrame();
539
540 expect(".o_input:eq(1)").toHaveValue("02/08/2017 11:00:30", {
541 message: "seconds should be visible for showSeconds true",
542 });
543 });
544
545 test("datetime field (with widget) in kanban with show_time option", async () => {
546 mockTimeZone(+2);
547
548 await mountView({
549 type: "kanban",
550 resModel: "partner",
551 arch: `
552 <kanban>
553 <templates>
554 <t t-name="card">
555 <field name="datetime" widget="datetime" options="{'show_time': false}"/>
556 </t>
557 </templates>
558 </kanban>`,
559 resId: 1,
560 });
561
562 expect(".o_kanban_record:first").toHaveText("Feb 8, 2017");
563 });
564
565 test("datetime field in list with show_time option", async () => {
566 mockTimeZone(+2);
567 onRpc("has_group", () => true);
568
569 await mountView({
570 type: "list",
571 resModel: "partner",
572 arch: `
573 <list editable="bottom">
574 <field name="datetime" options="{'show_time': false}"/>
575 <field name="datetime" />
576 </list>
577 `,
578 });
579
580 const dates = queryAll(".o_field_cell");
581
582 expect(dates[0]).toHaveText("Feb 8, 2017", {
583 message: "for date field only date should be visible with date widget",
584 });
585 expect(dates[1]).toHaveText("Feb 8, 2017, 12:00 PM", {
586 message: "for datetime field only date should be visible with date widget",
587 });
588 await contains(dates[0]).click();
589 await animationFrame();
590 expect(".o_field_datetime input:first").toHaveValue("02/08/2017 12:00:00", {
591 message: "for datetime field both date and time should be visible with datetime widget",
592 });
593 });
594
595 test("placeholder_field shows as placeholder (char)", async () => {
596 Partner._fields.char = fields.Char({
597 default: "My Placeholder",
598 });
599
600 await mountView({
601 type: "form",
602 resModel: "partner",
603 resId: 1,
604 arch: `
605 <form>
606 <field name="char"/>
607 <field name="datetime" options="{'placeholder_field': 'char'}"/>
608 </form>`,
609 });
610 await contains("div[name='datetime'] .o_input").click();
611 expect("div[name='datetime'] .o_input").toHaveAttribute("placeholder", "My Placeholder", {
612 message: "placeholder_field should be the placeholder",
613 });
614 });
615
616 test("placeholder_field shows as placeholder (datetime)", async () => {
617 mockTimeZone(0);
618
619 defineParams({
620 lang_parameters: {
621 date_format: "%d/%m/%Y",
622 time_format: "%H:%M",
623 },
624 });
625
626 Partner._fields.datetime_example = fields.Datetime({
627 string: "A datetime",
628 searchable: true,
629 default: "2025-04-01 09:11:11",
630 required: true,
631 });
632
633 await mountView({
634 type: "form",
635 resModel: "partner",
636 resId: 1,
637 arch: `
638 <form>
639 <field name="datetime_example"/>
640 <field name="datetime" options="{'placeholder_field': 'datetime_example'}"/>
641 </form>`,
642 });
643 await contains("div[name='datetime'] button").click();
644 expect("div[name='datetime'] input").toHaveAttribute("placeholder", /Apr 1, 2025, 9:11\sAM/, {
645 message: "placeholder_field should be the placeholder",
646 });
647 });
648
649 test("list datetime: column widths (show_time=false)", async () => {
650 await resize({ width: 800 });
651 document.body.style.fontFamily = "sans-serif";
652 resetDateFieldWidths();
653 after(resetDateFieldWidths);
654
655 await mountView({
656 type: "list",
657 resModel: "partner",
658 arch: /* xml */ `
659 <list>
660 <field name="datetime" widget="datetime" options="{'show_time': false }" />
661 <field name="display_name" />
662 </list>`,
663 });
664
665 expect(queryAllTexts(".o_data_row:eq(0) .o_data_cell")).toEqual(["Feb 8, 2017", "partner,1"]);
666 expect(queryAllProperties(".o_list_table thead th", "offsetWidth")).toEqual([40, 99, 661]);
667 });
668
669 test("list datetime: column widths (numeric format)", async () => {
670 await resize({ width: 800 });
671 document.body.style.fontFamily = "sans-serif";
672 resetDateFieldWidths();
673 after(resetDateFieldWidths);
674
675 await mountView({
676 type: "list",
677 resModel: "partner",
678 arch: /* xml */ `
679 <list>
680 <field name="datetime" widget="datetime" options="{'numeric': true }" />
681 <field name="display_name" />
682 </list>`,
683 });
684
685 expect(queryAllTexts(".o_data_row:eq(0) .o_data_cell")).toEqual([
686 "02/08/2017 11:00:00",
687 "partner,1",
688 ]);
689 expect(queryAllProperties(".o_list_table thead th", "offsetWidth")).toEqual([40, 144, 616]);
690 });