Debian Codegraph @

addons/web/static/tests/views/calendar/calendar_test_helpers.js

raw
1 import { click, drag, edit, hover, queryFirst, queryRect } from "@odoo/hoot-dom";
2 import { advanceFrame, advanceTime, animationFrame } from "@odoo/hoot-mock";
3 import { EventBus } from "@odoo/owl";
4 import { contains, getMockEnv, swipeLeft, swipeRight } from "@web/../tests/web_test_helpers";
5
6 import { createElement } from "@web/core/utils/xml";
7 import { CalendarModel } from "@web/views/calendar/calendar_model";
8 import { Field } from "@web/views/fields/field";
9
10 export const DEFAULT_DATE = luxon.DateTime.local(2021, 7, 16, 8, 0, 0, 0);
11
12 export const FAKE_RECORDS = {
13 1: {
14 id: 1,
15 title: "1 day, all day in July",
16 start: DEFAULT_DATE,
17 isAllDay: true,
18 end: DEFAULT_DATE,
19 },
20 2: {
21 id: 2,
22 title: "3 days, all day in July",
23 start: DEFAULT_DATE.plus({ days: 2 }),
24 isAllDay: true,
25 end: DEFAULT_DATE.plus({ days: 4 }),
26 },
27 3: {
28 id: 3,
29 title: "1 day, all day in June",
30 start: DEFAULT_DATE.plus({ months: -1 }),
31 isAllDay: true,
32 end: DEFAULT_DATE.plus({ months: -1 }),
33 },
34 4: {
35 id: 4,
36 title: "3 days, all day in June",
37 start: DEFAULT_DATE.plus({ months: -1, days: 2 }),
38 isAllDay: true,
39 end: DEFAULT_DATE.plus({ months: -1, days: 4 }),
40 },
41 5: {
42 id: 5,
43 title: "Over June and July",
44 start: DEFAULT_DATE.startOf("month").plus({ days: -2 }),
45 isAllDay: true,
46 end: DEFAULT_DATE.startOf("month").plus({ days: 2 }),
47 },
48 };
49
50 export const FAKE_FILTER_SECTIONS = [
51 {
52 label: "Attendees",
53 fieldName: "partner_ids",
54 avatar: {
55 model: "res.partner",
56 field: "avatar_128",
57 },
58 hasAvatar: true,
59 write: {
60 model: "filter_partner",
61 field: "partner_id",
62 },
63 canAddFilter: true,
64 filters: [
65 {
66 type: "user",
67 label: "Mitchell Admin",
68 active: true,
69 value: 3,
70 colorIndex: 3,
71 recordId: null,
72 canRemove: false,
73 hasAvatar: true,
74 },
75 {
76 type: "record",
77 label: "Brandon Freeman",
78 active: true,
79 value: 4,
80 colorIndex: 4,
81 recordId: 1,
82 canRemove: true,
83 hasAvatar: true,
84 },
85 {
86 type: "record",
87 label: "Marc Demo",
88 active: false,
89 value: 6,
90 colorIndex: 6,
91 recordId: 2,
92 canRemove: true,
93 hasAvatar: true,
94 },
95 ],
96 },
97 {
98 label: "Users",
99 fieldName: "user_id",
100 avatar: {
101 model: null,
102 field: null,
103 },
104 hasAvatar: false,
105 write: {
106 model: null,
107 field: null,
108 },
109 canAddFilter: false,
110 filters: [
111 {
112 type: "record",
113 label: "Brandon Freeman",
114 active: false,
115 value: 1,
116 colorIndex: false,
117 recordId: null,
118 canRemove: true,
119 hasAvatar: true,
120 },
121 {
122 type: "record",
123 label: "Marc Demo",
124 active: false,
125 value: 2,
126 colorIndex: false,
127 recordId: null,
128 canRemove: true,
129 hasAvatar: true,
130 },
131 ],
132 },
133 ];
134
135 export const FAKE_FIELDS = {
136 id: { string: "Id", type: "integer" },
137 user_id: { string: "User", type: "many2one", relation: "user", default: -1 },
138 partner_id: {
139 string: "Partner",
140 type: "many2one",
141 relation: "partner",
142 related: "user_id.partner_id",
143 default: 1,
144 },
145 name: { string: "Name", type: "char" },
146 description: { string: "Description", type: "html" },
147 start_date: { string: "Start Date", type: "date" },
148 stop_date: { string: "Stop Date", type: "date" },
149 start: { string: "Start Datetime", type: "datetime" },
150 stop: { string: "Stop Datetime", type: "datetime" },
151 delay: { string: "Delay", type: "float" },
152 allday: { string: "Is All Day", type: "boolean" },
153 partner_ids: {
154 string: "Attendees",
155 type: "one2many",
156 relation: "partner",
157 default: [[6, 0, [1]]],
158 },
159 type: { string: "Type", type: "integer" },
160 event_type_id: { string: "Event Type", type: "many2one", relation: "event_type" },
161 color: { string: "Color", type: "integer", related: "event_type_id.color" },
162 };
163
164 export const FAKE_MODEL = {
165 bus: new EventBus(),
166 canCreate: true,
167 canDelete: true,
168 canEdit: true,
169 date: DEFAULT_DATE,
170 fieldMapping: {
171 date_start: "start_date",
172 date_stop: "stop_date",
173 date_delay: "delay",
174 all_day: "allday",
175 color: "color",
176 },
177 fieldNames: ["start_date", "stop_date", "color", "delay", "allday", "user_id"],
178 fields: FAKE_FIELDS,
179 filterSections: FAKE_FILTER_SECTIONS,
180 firstDayOfWeek: 0,
181 isDateHidden: false,
182 isTimeHidden: false,
183 hasAllDaySlot: true,
184 hasEditDialog: false,
185 quickCreate: false,
186 popoverFieldNodes: {
187 name: Field.parseFieldNode(
188 createElement("field", { name: "name" }),
189 { event: { fields: FAKE_FIELDS } },
190 "event",
191 "calendar"
192 ),
193 description: Field.parseFieldNode(
194 createElement("field", { name: "description" , class: "text-wrap"}),
195 { event: { fields: FAKE_FIELDS } },
196 "event",
197 "calendar"
198 ),
199 },
200 activeFields: {
201 name: {
202 context: "{}",
203 invisible: false,
204 readonly: false,
205 required: false,
206 onChange: false,
207 },
208 description: {
209 context: "{}",
210 invisible: false,
211 readonly: false,
212 required: false,
213 onChange: false,
214 },
215 },
216 rangeEnd: DEFAULT_DATE.endOf("month"),
217 rangeStart: DEFAULT_DATE.startOf("month"),
218 records: FAKE_RECORDS,
219 resModel: "event",
220 scale: "month",
221 scales: ["day", "week", "month", "year"],
222 unusualDays: [],
223 load() {},
224 createFilter() {},
225 createRecord() {},
226 unlinkFilter() {},
227 unlinkRecord() {},
228 updateFilter() {},
229 updateRecord() {},
230 };
231
232 // DOM Utils
233 //------------------------------------------------------------------------------
234
235 /**
236 * @param {HTMLElement} element
237 */
238 function instantScrollTo(element) {
239 element.scrollIntoView({ behavior: "instant", block: "center" });
240 }
241
242 /**
243 * @param {string} date
244 * @returns {HTMLElement}
245 */
246 export function findAllDaySlot(date) {
247 return queryFirst(`.fc-daygrid-body .fc-day[data-date="${date}"]`);
248 }
249
250 /**
251 * @param {string} date
252 * @returns {HTMLElement}
253 */
254 export function findDateCell(date) {
255 return queryFirst(`.fc-day[data-date="${date}"]`);
256 }
257
258 /**
259 * @param {number} eventId
260 * @returns {HTMLElement}
261 */
262 export function findEvent(eventId) {
263 return queryFirst(`.o_event[data-event-id="${eventId}"]`);
264 }
265
266 /**
267 * @param {string} date
268 * @returns {HTMLElement}
269 */
270 export function findDateColumn(date) {
271 return queryFirst(`.fc-col-header-cell.fc-day[data-date="${date}"]`);
272 }
273
274 /**
275 * @param {string} time
276 * @returns {HTMLElement}
277 */
278 export function findTimeRow(time) {
279 return queryFirst(`.fc-timegrid-slot[data-time="${time}"]:eq(1)`);
280 }
281
282 /**
283 * @param {string} sectionName
284 * @returns {HTMLElement}
285 */
286 export function findFilterPanelSection(sectionName) {
287 return queryFirst(`.o_calendar_filter[data-name="${sectionName}"]`);
288 }
289
290 /**
291 * @param {string} sectionName
292 * @param {string} filterValue
293 * @returns {HTMLElement}
294 */
295 export function findFilterPanelFilter(sectionName, filterValue) {
296 const root = findFilterPanelSection(sectionName);
297 return queryFirst(`.o_calendar_filter_item[data-value="${filterValue}"]`, { root });
298 }
299
300 /**
301 * @param {string} sectionName
302 * @returns {HTMLElement}
303 */
304 export function findFilterPanelSectionFilter(sectionName) {
305 const root = findFilterPanelSection(sectionName);
306 return queryFirst(`.o_calendar_filter_items_checkall`, { root });
307 }
308
309 /**
310 * @param {string} date
311 * @returns {Promise<void>}
312 */
313 export async function pickDate(date) {
314 const day = date.split("-")[2];
315 const iDay = parseInt(day, 10) - 1;
316 await click(`.o_datetime_picker .o_date_item_cell:not(.o_out_of_range):eq(${iDay})`);
317 await animationFrame();
318 }
319
320 /**
321 * @param {string} date
322 * @returns {Promise<void>}
323 */
324 export async function clickAllDaySlot(date) {
325 const slot = findAllDaySlot(date);
326
327 instantScrollTo(slot);
328
329 await click(slot);
330 await animationFrame();
331 }
332
333 /**
334 * @param {string} date
335 * @returns {Promise<void>}
336 */
337 export async function clickDate(date) {
338 const cell = findDateCell(date);
339
340 instantScrollTo(cell);
341
342 await click(cell);
343 await advanceTime(500);
344 }
345
346 /**
347 * @param {number} eventId
348 * @returns {Promise<void>}
349 */
350 export async function clickEvent(eventId) {
351 const eventEl = findEvent(eventId);
352
353 instantScrollTo(eventEl);
354
355 await click(eventEl);
356 await advanceTime(500); // wait for the popover to open (debounced)
357 }
358
359 export function expandCalendarView() {
360 // Expends Calendar view and FC too
361 let tmpElement = queryFirst(".fc");
362 do {
363 tmpElement = tmpElement.parentElement;
364 tmpElement.classList.add("h-100");
365 } while (!tmpElement.classList.contains("o_view_controller"));
366 }
367
368 /**
369 * @param {string} startDateTime
370 * @param {string} endDateTime
371 * @returns {Promise<void>}
372 */
373 export async function selectTimeRange(startDateTime, endDateTime) {
374 const [startDate, startTime] = startDateTime.split(" ");
375 const [endDate, endTime] = endDateTime.split(" ");
376
377 // Try to display both rows on the screen before drag'n'drop.
378 const startHour = Number(startTime.slice(0, 2));
379 const endHour = Number(endTime.slice(0, 2));
380 const midHour = Math.floor((startHour + endHour) / 2);
381 const midTime = `${String(midHour).padStart(2, "0")}:00:00`;
382
383 instantScrollTo(
384 queryFirst(`.fc-timegrid-slot[data-time="${midTime}"]:eq(1)`, { visible: false })
385 );
386
387 const startColumnRect = queryRect(`.fc-col-header-cell.fc-day[data-date="${startDate}"]`);
388 const startRow = queryFirst(`.fc-timegrid-slot[data-time="${startTime}"]:eq(1)`);
389 const endColumnRect = queryRect(`.fc-col-header-cell.fc-day[data-date="${endDate}"]`);
390 const endRow = queryFirst(`.fc-timegrid-slot[data-time="${endTime}"]:eq(1)`);
391 const optionStart = {
392 relative: true,
393 position: { y: 1, x: startColumnRect.left },
394 };
395
396 await hover(startRow, optionStart);
397 await animationFrame();
398 const { drop } = await drag(startRow, optionStart);
399 await animationFrame();
400 await drop(endRow, {
401 position: { y: -1, x: endColumnRect.left },
402 relative: true,
403 });
404
405 await animationFrame();
406 }
407
408 /**
409 * @param {string} startDate
410 * @param {string} endDate
411 * @returns {Promise<void>}
412 */
413 export async function selectDateRange(startDate, endDate) {
414 const startCell = findDateCell(startDate);
415 const endCell = findDateCell(endDate);
416
417 instantScrollTo(startCell);
418
419 await hover(startCell);
420 await animationFrame();
421
422 const { moveTo, drop } = await drag(startCell);
423 await animationFrame();
424
425 await moveTo(endCell);
426 await animationFrame();
427
428 await drop();
429 await animationFrame();
430 }
431
432 /**
433 * @param {string} startDate
434 * @param {string} endDate
435 * @returns {Promise<void>}
436 */
437 export async function selectAllDayRange(startDate, endDate) {
438 const start = findAllDaySlot(startDate);
439 const end = findAllDaySlot(endDate);
440
441 instantScrollTo(start);
442
443 await hover(start);
444 await animationFrame();
445
446 const { drop } = await drag(start);
447 await animationFrame();
448
449 await drop(end);
450 await animationFrame();
451 }
452 export async function closeCwPopOver() {
453 if (getMockEnv().isSmall) {
454 await contains(`.oi-arrow-left`).click();
455 } else {
456 await contains(`.o_cw_popover_close`).click();
457 }
458 }
459 /**
460 * @param {number} eventId
461 * @param {string} date
462 * @param {{ disableDrop: boolean }} [options]
463 * @returns {Promise<void>}
464 */
465 export async function moveEventToDate(eventId, date, options) {
466 const eventEl = findEvent(eventId);
467 const cell = findDateCell(date);
468
469 instantScrollTo(eventEl);
470
471 await hover(eventEl);
472 await animationFrame();
473
474 const { drop, moveTo } = await drag(eventEl);
475 await animationFrame();
476
477 await moveTo(cell);
478 await animationFrame();
479
480 if (!options?.disableDrop) {
481 await drop();
482 }
483
484 await animationFrame();
485 await animationFrame();
486 }
487
488 /**
489 * @param {number} eventId
490 * @param {string} dateTime
491 * @returns {Promise<void>}
492 */
493 export async function moveEventToTime(eventId, dateTime) {
494 const eventEl = findEvent(eventId);
495 const [date, time] = dateTime.split(" ");
496
497 instantScrollTo(eventEl);
498
499 const row = findTimeRow(time);
500 const rowRect = queryRect(row);
501
502 const column = findDateColumn(date);
503 const columnRect = queryRect(column);
504
505 const { drop, moveTo } = await drag(eventEl, {
506 position: { y: 1 },
507 relative: true,
508 });
509
510 if (getMockEnv().isSmall) {
511 await advanceTime(500);
512 }
513
514 await animationFrame();
515
516 await moveTo(row, {
517 position: {
518 y: rowRect.y + 1,
519 x: columnRect.x + columnRect.width / 2,
520 },
521 });
522 await animationFrame();
523
524 await drop();
525 await advanceFrame(5);
526 }
527
528 export async function selectHourOnPicker(selectedValue) {
529 await click(".o_time_picker_input:eq(0)");
530 await animationFrame();
531 await edit(selectedValue, { confirm: "enter" });
532 await animationFrame();
533 }
534
535 /**
536 * @param {number} eventId
537 * @param {string} date
538 * @returns {Promise<void>}
539 */
540 export async function moveEventToAllDaySlot(eventId, date) {
541 const eventEl = findEvent(eventId);
542 const slot = findAllDaySlot(date);
543
544 instantScrollTo(eventEl);
545
546 const columnRect = queryRect(eventEl);
547 const slotRect = queryRect(slot);
548
549 const { drop, moveTo } = await drag(eventEl, {
550 position: { y: 1 },
551 relative: true,
552 });
553
554 if (getMockEnv().isSmall) {
555 await advanceTime(500);
556 }
557
558 await animationFrame();
559
560 await moveTo(slot, {
561 position: {
562 x: columnRect.x + columnRect.width / 2,
563 y: slotRect.y,
564 },
565 });
566 await animationFrame();
567
568 await drop();
569 await advanceFrame(5);
570 }
571
572 /**
573 * @param {number} eventId
574 * @param {string} dateTime
575 * @returns {Promise<void>}
576 */
577 export async function resizeEventToTime(eventId, dateTime) {
578 const eventEl = findEvent(eventId);
579
580 instantScrollTo(eventEl);
581
582 await hover(`.fc-event-main:first`, { root: eventEl });
583 await animationFrame();
584
585 const resizer = queryFirst(`.fc-event-resizer-end`, { root: eventEl });
586 Object.assign(resizer.style, {
587 display: "block",
588 height: "1px",
589 bottom: "0",
590 });
591
592 const [date, time] = dateTime.split(" ");
593
594 const row = findTimeRow(time);
595
596 const column = findDateColumn(date);
597 const columnRect = queryRect(column);
598
599 await (
600 await drag(resizer)
601 ).drop(row, {
602 position: { x: columnRect.x, y: -1 },
603 relative: true,
604 });
605 await advanceTime(500);
606 }
607
608 /**
609 * @param {number} eventId
610 * @param {string} date
611 * @returns {Promise<void>}
612 */
613 export async function resizeEventToDate(eventId, date) {
614 const eventEl = findEvent(eventId);
615 const slot = findAllDaySlot(date);
616
617 instantScrollTo(eventEl);
618
619 await hover(".fc-event-main", { root: eventEl });
620 await animationFrame();
621
622 // Show the resizer
623 const resizer = queryFirst(".fc-event-resizer-end", { root: eventEl });
624 Object.assign(resizer.style, { display: "block", height: "1px", bottom: "0" });
625
626 instantScrollTo(slot);
627
628 const rowRect = queryRect(resizer);
629
630 // Find the date cell and calculate the positions for dragging
631 const dateCell = findDateCell(date);
632 const columnRect = queryRect(dateCell);
633
634 // Perform the drag-and-drop operation
635 await hover(resizer, {
636 position: { x: 0 },
637 relative: true,
638 });
639 await animationFrame();
640
641 const { drop } = await drag(resizer);
642 await animationFrame();
643
644 await drop(dateCell, {
645 position: { y: rowRect.y - columnRect.y },
646 relative: true,
647 });
648 await advanceTime(500);
649 }
650
651 /**
652 * @param {"day" | "week" | "month" | "year"} scale
653 * @returns {Promise<void>}
654 */
655 export async function changeScale(scale) {
656 await contains(`.o_view_scale_selector .scale_button_selection`).click();
657 await contains(`.o-dropdown--menu .o_scale_button_${scale}`).click();
658 }
659
660 export async function displayCalendarPanel() {
661 if (getMockEnv().isSmall) {
662 await contains(".o_calendar_container .o_other_calendar_panel").click();
663 }
664 }
665
666 export async function hideCalendarPanel() {
667 if (getMockEnv().isSmall) {
668 await contains(".o_calendar_container .o_other_calendar_panel").click();
669 }
670 }
671
672 /**
673 * @param {"prev" | "next"} direction
674 * @returns {Promise<void>}
675 */
676 export async function navigate(direction) {
677 if (getMockEnv().isSmall) {
678 if (direction === "next") {
679 await swipeLeft(".o_calendar_widget");
680 } else {
681 await swipeRight(".o_calendar_widget");
682 }
683 await advanceFrame(16);
684 } else {
685 await contains(`.o_calendar_navigation_buttons .o_calendar_button_${direction}`).click();
686 }
687 }
688
689 /**
690 * @param {string} sectionName
691 * @param {string} filterValue
692 * @returns {Promise<void>}
693 */
694 export async function toggleFilter(sectionName, filterValue) {
695 const otherCalendarPanel = queryFirst(".o_other_calendar_panel");
696 if (otherCalendarPanel) {
697 click(otherCalendarPanel);
698 await animationFrame();
699 }
700 const root = findFilterPanelFilter(sectionName, filterValue);
701 const input = queryFirst(`input`, { root });
702
703 instantScrollTo(input);
704
705 await click(input);
706 await animationFrame();
707
708 if (otherCalendarPanel) {
709 await click(otherCalendarPanel);
710 await animationFrame();
711 }
712 await advanceTime(CalendarModel.DEBOUNCED_LOAD_DELAY);
713 await animationFrame();
714 }
715
716 /**
717 * @param {string} sectionName
718 * @returns {Promise<void>}
719 */
720 export async function toggleSectionFilter(sectionName) {
721 const otherCalendarPanel = queryFirst(".o_other_calendar_panel");
722 if (otherCalendarPanel) {
723 await click(otherCalendarPanel);
724 await animationFrame();
725 }
726 const root = findFilterPanelSectionFilter(sectionName);
727 const input = queryFirst(`input`, { root });
728
729 instantScrollTo(input);
730
731 await click(input);
732 await animationFrame();
733
734 if (otherCalendarPanel) {
735 await click(otherCalendarPanel);
736 await animationFrame();
737 }
738 await advanceTime(CalendarModel.DEBOUNCED_LOAD_DELAY);
739 await animationFrame();
740 }
741
742 /**
743 * @param {string} sectionName
744 * @param {string} filterValue
745 * @returns {Promise<void>}
746 */
747 export async function removeFilter(sectionName, filterValue) {
748 const root = findFilterPanelFilter(sectionName, filterValue);
749 const button = queryFirst(`.o_remove`, { root });
750
751 instantScrollTo(button);
752
753 await click(button);
754 await advanceTime(CalendarModel.DEBOUNCED_LOAD_DELAY);
755 await animationFrame();
756 }