Debian Codegraph @

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

raw
1 import { expect, queryFirst, test } from "@odoo/hoot";
2 import { click, edit, press, queryAllTexts, queryOne, scroll } from "@odoo/hoot-dom";
3 import { Deferred, animationFrame, mockDate, mockTimeZone } from "@odoo/hoot-mock";
4 import {
5 assertDateTimePicker,
6 getPickerCell,
7 zoomOut,
8 } from "@web/../tests/core/datetime/datetime_test_helpers";
9 import {
10 clickSave,
11 contains,
12 defineModels,
13 defineParams,
14 fieldInput,
15 fields,
16 models,
17 mountView,
18 onRpc,
19 serverState,
20 } from "@web/../tests/web_test_helpers";
21
22 class Partner extends models.Model {
23 _name = "res.partner";
24
25 date = fields.Date();
26 char_field = fields.Char({ string: "Char" });
27
28 _records = [
29 {
30 id: 1,
31 date: "2017-02-03",
32 char_field: "first char field",
33 },
34 ];
35
36 _views = {
37 form: /* xml */ `
38 <form>
39 <sheet>
40 <group>
41 <field name="date"/>
42 <field name="char_field"/>
43 </group>
44 </sheet>
45 </form>
46 `,
47 };
48 }
49
50 defineModels([Partner]);
51
52 test("toggle datepicker", async () => {
53 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
54
55 expect(".o_datetime_picker").toHaveCount(0);
56 await contains(".o_field_date button").click();
57 await animationFrame();
58 expect(".o_datetime_picker").toHaveCount(1);
59
60 await fieldInput("char_field").click();
61 expect(".o_datetime_picker").toHaveCount(0);
62 });
63
64 test("datepicker is automatically closed after selecting a value", async () => {
65 Partner._onChanges.date = () => {};
66 const def = new Deferred();
67 onRpc("onchange", () => def);
68
69 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
70
71 expect(".o_datetime_picker").toHaveCount(0);
72 await contains(".o_field_date button").click();
73 await animationFrame();
74 expect(".o_datetime_picker").toHaveCount(1);
75
76 await contains(getPickerCell(22)).click();
77 await animationFrame();
78 // The picker shouldn't be reopened, even if the onChange RPC is slow.
79 expect(".o_datetime_picker").toHaveCount(0);
80 def.resolve();
81 });
82
83 test("Ensure only one datepicker is open", async () => {
84 Partner._fields.date_start = fields.Date();
85
86 await mountView({
87 type: "form",
88 resModel: "res.partner",
89 arch: `
90 <form>
91 <field name="date_start"/>
92 <field name="date"/>
93 </form>`,
94 resId: 1,
95 });
96
97 await queryFirst("[data-field='date_start']").click();
98 await queryFirst("[data-field='date']").click();
99 await animationFrame();
100 expect(".o_datetime_picker").toHaveCount(1);
101 });
102
103 test.tags("desktop");
104 test("open datepicker on Control+Enter", async () => {
105 defineParams({
106 lang_parameters: {
107 date_format: "%d/%m/%Y",
108 time_format: "%H:%M:%S",
109 },
110 });
111 await mountView({
112 resModel: "res.partner",
113 type: "form",
114 arch: `
115 <form>
116 <field name="date"/>
117 </form>
118 `,
119 });
120
121 expect(".o_field_date input").toHaveCount(1);
122
123 await press(["ctrl", "enter"]);
124 await animationFrame();
125 expect(".o_datetime_picker").toHaveCount(1);
126
127 //edit the input and open the datepicker again with ctrl+enter
128 await contains(".o_field_date .o_input").click();
129 await edit("09/01/1997");
130 await press(["ctrl", "enter"]);
131 await animationFrame();
132 assertDateTimePicker({
133 title: "January 1997",
134 date: [
135 {
136 cells: [
137 [29, 30, 31, 1, 2, 3, 4],
138 [5, 6, 7, 8, [9], 10, 11],
139 [12, 13, 14, 15, 16, 17, 18],
140 [19, 20, 21, 22, 23, 24, 25],
141 [26, 27, 28, 29, 30, 31, 1],
142 [2, 3, 4, 5, 6, 7, 8],
143 ],
144 daysOfWeek: ["", "S", "M", "T", "W", "T", "F", "S"],
145 weekNumbers: [1, 2, 3, 4, 5, 6],
146 },
147 ],
148 });
149 });
150 test("toggle datepicker far in the future", async () => {
151 Partner._records[0].date = "9999-12-31";
152 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
153
154 expect(".o_datetime_picker").toHaveCount(0);
155 await contains(".o_field_date button").click();
156 expect(".o_datetime_picker").toHaveCount(1);
157
158 // focus another field
159 await fieldInput("char_field").click();
160 expect(".o_datetime_picker").toHaveCount(0);
161 });
162
163 test("date field is empty if no date is set", async () => {
164 Partner._records[0].date = false;
165 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
166
167 expect(".o_field_date input").toHaveCount(1);
168 expect(".o_field_date input").toHaveValue("");
169 });
170
171 test("set an invalid date when the field is already set", async () => {
172 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
173
174 await contains(".o_field_widget[name='date'] button").click();
175 expect(".o_field_widget[name='date'] input").toHaveValue("02/03/2017");
176 await fieldInput("date").edit("invalid date");
177 await contains(".o_field_widget[name='date'] button").click();
178 expect(".o_field_widget[name='date'] input").toHaveValue("02/03/2017", {
179 message: "Should have been reset to the original value",
180 });
181 });
182
183 test("set an invalid date when the field is not set yet", async () => {
184 Partner._records[0].date = false;
185 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
186
187 expect(".o_field_widget[name='date'] input").toHaveValue("");
188 await fieldInput("date").edit("invalid date");
189 expect(".o_field_widget[name='date'] input").toHaveValue("");
190 });
191
192 test("value should not set on first click", async () => {
193 Partner._records[0].date = false;
194 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
195
196 await contains(".o_field_date input").click();
197 expect(".o_field_widget[name='date'] input").toHaveValue("");
198 await contains(getPickerCell(22)).click();
199
200 await contains(".o_field_date button").click();
201 expect(".o_date_item_cell.o_selected").toHaveText("22");
202 });
203
204 test("date field in form view (with positive time zone offset)", async () => {
205 mockTimeZone(2); // should be ignored by date fields
206 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
207
208 onRpc("web_save", ({ args }) => {
209 expect.step(args[1].date);
210 });
211
212 expect(".o_field_date").toHaveText("Feb 3, 2017");
213
214 // open datepicker and select another value
215 await contains(".o_field_date button").click();
216 expect(".o_datetime_picker").toHaveCount(1);
217 expect(".o_date_item_cell.o_selected").toHaveCount(1);
218
219 // select 22 Feb 2017
220 await zoomOut();
221 await zoomOut();
222 await contains(getPickerCell("2017")).click();
223 await contains(getPickerCell("Feb")).click();
224 await contains(getPickerCell("22")).click();
225 expect(".o_datetime_picker").toHaveCount(0);
226 expect(".o_field_date").toHaveText("Feb 22, 2017");
227
228 await clickSave();
229 expect.verifySteps(["2017-02-22"]);
230 expect(".o_field_date").toHaveText("Feb 22, 2017");
231 });
232
233 test("date field in form view (with negative time zone offset)", async () => {
234 mockTimeZone(-2); // should be ignored by date fields
235 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
236
237 expect(".o_field_date button").toHaveText("Feb 3, 2017");
238 });
239
240 test("date field dropdown doesn't dissapear on scroll", async () => {
241 await mountView({
242 type: "form",
243 resModel: "res.partner",
244 resId: 1,
245 arch: `
246 <form>
247 <div class="scrollable overflow-auto" style="height: 50px;">
248 <div style="height: 2000px;">
249 <field name="date" />
250 </div>
251 </div>
252 </form>`,
253 });
254
255 await contains(".o_field_date button").click();
256 expect(".o_datetime_picker").toHaveCount(1);
257 await scroll(".scrollable", { top: 50 });
258 expect(".scrollable").toHaveProperty("scrollTop", 50);
259 expect(".o_datetime_picker").toHaveCount(1);
260 });
261
262 test("date field with label opens datepicker on click", async () => {
263 await mountView({
264 type: "form",
265 resModel: "res.partner",
266 resId: 1,
267 arch: `
268 <form>
269 <label for="date" string="What date is it" />
270 <field name="date" />
271 </form>`,
272 });
273
274 await contains("label.o_form_label").click();
275 expect(".o_datetime_picker").toHaveCount(1);
276 });
277
278 test("date field with warn_future option ", async () => {
279 Partner._records[0] = { id: 1 };
280 await mountView({
281 type: "form",
282 resModel: "res.partner",
283 resId: 1,
284 arch: `
285 <form>
286 <field name="date" options="{'warn_future': true}" />
287 </form>`,
288 });
289
290 await contains(".o_field_date input").click();
291 await zoomOut();
292 await zoomOut();
293 await contains(getPickerCell("2020")).click();
294 await contains(getPickerCell("Dec")).click();
295 await contains(getPickerCell("22")).click();
296 expect(".o_field_date button").toHaveClass("text-danger");
297 await contains(".o_field_date button").click();
298 await fieldInput("date").clear();
299 expect(".o_field_date input").not.toHaveClass("text-danger");
300 });
301
302 test("date field with warn_future option: do not overwrite datepicker option", async () => {
303 Partner._onChanges.date = () => {};
304
305 await mountView({
306 type: "form",
307 resModel: "res.partner",
308 resId: 1,
309 // Do not let the date field get the focus in the first place
310 arch: `
311 <form>
312 <group>
313 <field name="char_field" />
314 <field name="date" options="{'warn_future': true}" />
315 </group>
316 </form>`,
317 });
318
319 expect(".o_field_widget[name='date']").toHaveText("Feb 3, 2017");
320 await contains(".o_form_button_create").click();
321 expect(".o_field_widget[name='date'] input").toHaveValue("");
322 });
323
324 test.tags("desktop");
325 test("date field in editable list view", async () => {
326 onRpc("has_group", () => true);
327 await mountView({
328 type: "list",
329 resModel: "res.partner",
330 arch: `
331 <list editable="bottom">
332 <field name="date"/>
333 </list>`,
334 });
335
336 const cell = queryOne("tr.o_data_row td:not(.o_list_record_selector)");
337 expect(cell).toHaveText("Feb 3, 2017");
338 await contains(cell).click();
339 expect(".o_field_date button").toHaveCount(1);
340 expect(".o_field_date button").toHaveText("Feb 3, 2017");
341
342 // open datepicker and select another value
343 await contains(".o_field_date button").click();
344 expect(".o_datetime_picker").toHaveCount(1);
345 await zoomOut();
346 await zoomOut();
347 await contains(getPickerCell("2017")).click();
348 await contains(getPickerCell("Feb")).click();
349 await contains(getPickerCell("22")).click();
350 expect(".o_datetime_picker").toHaveCount(0);
351 expect(".o_field_date button").toHaveText("Feb 22, 2017");
352
353 await contains(".o_list_button_save").click();
354 expect("tr.o_data_row td:not(.o_list_record_selector)").toHaveText("Feb 22, 2017");
355 });
356
357 test.tags("desktop");
358 test("multi edition of date field in list view: clear date in input", async () => {
359 onRpc("has_group", () => true);
360 Partner._records = [
361 { id: 1, date: "2017-02-03" },
362 { id: 2, date: "2017-02-03" },
363 ];
364
365 await mountView({
366 type: "list",
367 resModel: "res.partner",
368 arch: `
369 <list multi_edit="1">
370 <field name="date"/>
371 </list>`,
372 });
373
374 await contains(".o_data_row:eq(0) .o_list_record_selector input").click();
375 await contains(".o_data_row:eq(1) .o_list_record_selector input").click();
376 await contains(".o_data_row:eq(0) .o_data_cell").click();
377
378 expect(".o_field_date button").toHaveCount(1);
379 await contains(".o_field_date button").click();
380 await fieldInput("date").clear();
381
382 expect(".modal").toHaveCount(1);
383 await contains(".modal .modal-footer .btn-primary").click();
384
385 expect(".o_data_row:first-child .o_data_cell").toHaveText("");
386 expect(".o_data_row:nth-child(2) .o_data_cell").toHaveText("");
387 });
388
389 test("date field remove value", async () => {
390 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
391 onRpc("web_save", ({ args }) => {
392 expect.step(args[1].date);
393 });
394
395 expect(".o_field_date").toHaveText("Feb 3, 2017");
396
397 await contains(".o_field_date button").click();
398 await fieldInput("date").clear();
399 expect(".o_field_date input").toHaveValue("");
400
401 await clickSave();
402 expect(".o_field_date").toHaveText("");
403 expect.verifySteps([false]);
404 });
405
406 test("date field should select its content onclick when there is one", async () => {
407 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
408
409 await contains(".o_field_date button").click();
410 await contains(".o_field_date input").click();
411 expect(".o_datetime_picker").toHaveCount(1);
412 const active = document.activeElement;
413 expect(active.tagName).toBe("INPUT");
414 expect(active.value.slice(active.selectionStart, active.selectionEnd)).toBe("02/03/2017");
415 });
416
417 test("date field supports custom formats", async () => {
418 defineParams({ lang_parameters: { date_format: "%d-%m-%Y" } });
419 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
420
421 expect(".o_field_date").toHaveText("Feb 3, 2017");
422 await contains(".o_field_date button").click();
423 expect(".o_field_date input").toHaveValue("03-02-2017");
424
425 await contains(getPickerCell("22")).click();
426 await clickSave();
427 expect(".o_field_date").toHaveText("Feb 22, 2017");
428 });
429
430 test("date field supports internationalization", async () => {
431 serverState.lang = "nb_NO";
432 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
433
434 expect(".o_field_date").toHaveText("3. feb. 2017");
435 await contains(".o_field_date button").click();
436 expect(".o_field_date input").toHaveValue("02/03/2017");
437 expect(".o_zoom_out strong").toHaveText("februar 2017");
438
439 await contains(getPickerCell("22")).click();
440 await clickSave();
441 expect(".o_field_date").toHaveText("22. feb. 2017");
442 });
443
444 test("hit enter should update value", async () => {
445 mockTimeZone(2);
446 await mountView({ type: "form", resModel: "res.partner", resId: 1 });
447 await contains(".o_field_date button").click();
448 await contains(".o_field_date input").edit("01/08");
449 expect(".o_field_widget[name='date']").toHaveText("Jan 8");
450 await contains(".o_field_date button").click();
451 await contains(".o_field_date input").edit("08/01");
452 expect(".o_field_widget[name='date']").toHaveText("Aug 1");
453 });
454
455 test("allow to use compute dates (+5d for instance)", async () => {
456 mockDate({ year: 2021, month: 2, day: 15 });
457
458 Partner._fields.date.default = "2019-09-15";
459 await mountView({ type: "form", resModel: "res.partner" });
460
461 expect(".o_field_date").toHaveText("Sep 15, 2019");
462 await contains(".o_field_date button").click();
463 await fieldInput("date").edit("+5d");
464 expect(".o_field_date").toHaveText("Feb 20");
465
466 // Discard and do it again
467 await contains(".o_form_button_cancel").click();
468 expect(".o_field_date").toHaveText("Sep 15, 2019");
469 await contains(".o_field_date button").click();
470 await fieldInput("date").edit("+5d");
471 expect(".o_field_date").toHaveText("Feb 20");
472
473 // Save and do it again
474 await clickSave();
475 expect(".o_field_date").toHaveText("Feb 20");
476 await contains(".o_field_date button").click();
477 await fieldInput("date").edit("+5d");
478 expect(".o_field_date").toHaveText("Feb 20");
479 });
480
481 test("date field with min_precision option", async () => {
482 await mountView({
483 type: "form",
484 resModel: "res.partner",
485 resId: 1,
486 // Do not let the date field get the focus in the first place
487 arch: `
488 <form>
489 <group>
490 <field name="date" options="{'min_precision': 'months'}" />
491 </group>
492 </form>`,
493 });
494
495 await click(".o_field_date button");
496 await animationFrame();
497 expect(".o_date_item_cell").toHaveCount(12);
498 expect(queryAllTexts(".o_date_item_cell")).toEqual([
499 "Jan",
500 "Feb",
501 "Mar",
502 "Apr",
503 "May",
504 "Jun",
505 "Jul",
506 "Aug",
507 "Sep",
508 "Oct",
509 "Nov",
510 "Dec",
511 ]);
512 expect(".o_date_item_cell.o_selected").toHaveText("Feb");
513
514 await click(getPickerCell("Jan"));
515 await animationFrame();
516 // The picker should be closed
517 expect(".o_date_item_cell").toHaveCount(0);
518 expect(".o_field_widget[name='date']").toHaveText("Jan 1, 2017");
519 });
520
521 test("date field with max_precision option", async () => {
522 await mountView({
523 type: "form",
524 resModel: "res.partner",
525 resId: 1,
526 // Do not let the date field get the focus in the first place
527 arch: `
528 <form>
529 <group>
530 <field name="date" options="{'max_precision': 'months'}" />
531 </group>
532 </form>`,
533 });
534
535 await click(".o_field_date button");
536 await animationFrame();
537 // Try to zoomOut twice to be in the year selector
538 await zoomOut();
539 // Currently in the month selector
540 expect(".o_datetime_picker_header").toHaveText("2017");
541 await zoomOut();
542 // Stay in the month selector according to the max precision value
543 expect(".o_datetime_picker_header").toHaveText("2017");
544 expect(".o_date_item_cell.o_selected").toHaveText("Feb");
545
546 await click(getPickerCell("Jan"));
547 await animationFrame();
548 await click(getPickerCell("12"));
549 await animationFrame();
550 expect(".o_field_widget[name='date']").toHaveText("Jan 12, 2017");
551 });
552
553 test("DateField with onchange forcing a specific date", async () => {
554 mockDate("2009-05-04 10:00:00", +1);
555
556 Partner._onChanges.date = (obj) => {
557 if (obj.char_field === "force today") {
558 obj.date = "2009-05-04";
559 }
560 };
561
562 await mountView({
563 type: "form",
564 resModel: "res.partner",
565 arch: /* xml */ `
566 <form>
567 <field name="char_field"/>
568 <field name="date"/>
569 </form>`,
570 });
571
572 expect(".o_field_date input").toHaveValue("");
573
574 // enable the onchange
575 await contains(".o_field_widget[name=char_field] input").edit("force today");
576
577 // open the picker and try to set a value different from today
578 await click(".o_field_date input");
579 await animationFrame();
580 expect(".o_datetime_picker").toHaveCount(1);
581 await contains(getPickerCell("22")).click(); // 22 May 2009
582 expect(".o_field_date").toHaveText("May 4"); // value forced by the onchange
583
584 // do it again (the technical flow is a bit different as now the current value is already today)
585 await click(".o_field_date button");
586 await animationFrame();
587 expect(".o_datetime_picker").toHaveCount(1);
588 await contains(getPickerCell("22")).click(); // 22 May 2009
589 expect(".o_field_date").toHaveText("May 4"); // value forced by the onchange
590 });