Debian Codegraph @

addons/web/static/lib/hoot/tests/hoot-dom/events.test.js

raw
1 /** @odoo-module */
2
3 import {
4 advanceTime,
5 after,
6 animationFrame,
7 clear,
8 click,
9 dblclick,
10 describe,
11 drag,
12 edit,
13 expect,
14 fill,
15 getFixture,
16 hover,
17 keyDown,
18 keyUp,
19 leave,
20 middleClick,
21 mockFetch,
22 mockTouch,
23 mockUserAgent,
24 on,
25 pointerDown,
26 pointerUp,
27 press,
28 queryOne,
29 resize,
30 rightClick,
31 scroll,
32 select,
33 setInputFiles,
34 setInputRange,
35 test,
36 uncheck,
37 } from "@odoo/hoot";
38 import { Component, xml } from "@odoo/owl";
39 import { EventList } from "@web/../lib/hoot-dom/helpers/events";
40 import { mountForTest, parseUrl } from "../local_helpers";
41
42 /**
43 * @param {Event} ev
44 */
45 const formatEvent = (ev) => {
46 const { currentTarget, type } = ev;
47 const id = currentTarget.id ? `#${currentTarget.id}` : currentTarget.tagName.toLowerCase();
48 let formatted = "";
49
50 // Mouse
51 if (ev.button >= 0) {
52 formatted += `:${ev.button}`;
53 }
54 if (ev.buttons) {
55 formatted += `(${ev.buttons})`;
56 }
57
58 // Keyboard
59 if (ev.key) {
60 formatted += `:${ev.key}`;
61 }
62 if (ev.altKey) {
63 formatted += `.alt`;
64 }
65 if (ev.ctrlKey) {
66 formatted += `.ctrl`;
67 }
68 if (ev.metaKey) {
69 formatted += `.meta`;
70 }
71 if (ev.shiftKey) {
72 formatted += `.shift`;
73 }
74
75 // Input
76 if (ev.data) {
77 formatted += `:${ev.data}`;
78 }
79
80 return `${type}${formatted}@${id}`;
81 };
82
83 /**
84 * @param {import("../../helpers/dom").Target} target
85 * @param {(ev: Event) => string} [formatStep]
86 */
87 const monitorEvents = (target, formatStep) => {
88 const handleEvent = (element, type) =>
89 after(
90 on(element, type, (ev) => {
91 const formattedStep = formatStep(ev);
92 if (formattedStep) {
93 expect.step(formattedStep);
94 }
95 })
96 );
97
98 formatStep ||= formatEvent;
99
100 for (const element of document.querySelectorAll(target)) {
101 for (const prop in element) {
102 const type = prop.match(/^on(\w+)/)?.[1];
103 if (!type || BLACK_LISTED_EVENT_TYPES.includes(type)) {
104 continue;
105 }
106 handleEvent(element, type);
107 }
108 for (const type of ADDITIONAL_EVENT_TYPES) {
109 handleEvent(element, type);
110 }
111 }
112 };
113
114 const ADDITIONAL_EVENT_TYPES = ["focusin", "focusout"];
115 const BLACK_LISTED_EVENT_TYPES = ["selectionchange"];
116
117 describe(parseUrl(import.meta.url), () => {
118 test("clear", async () => {
119 await mountForTest(/* xml */ `<input type="text" value="Test" />`);
120
121 expect("input").toHaveValue("Test");
122 expect.verifySteps([]);
123
124 await click("input");
125
126 monitorEvents("input");
127
128 await clear({ delay: 0 });
129
130 expect("input").not.toHaveValue();
131 expect.verifySteps([
132 "keydown:a.ctrl@input",
133 "select@input",
134 "keyup:a.ctrl@input",
135 "keydown:Backspace@input",
136 "beforeinput@input",
137 "input@input",
138 "keyup:Backspace@input",
139 ]);
140 });
141
142 test("clear: email", async () => {
143 await mountForTest(/* xml */ `<input type="email" value="[email protected]" />`);
144
145 expect("input").toHaveValue("[email protected]");
146
147 await click("input");
148 await clear();
149
150 expect("input").toHaveValue("");
151 });
152
153 test("clear: number", async () => {
154 await mountForTest(/* xml */ `<input type="number" value="421" />`);
155
156 expect("input").toHaveValue(421);
157
158 await click("input");
159 await clear();
160
161 expect("input").not.toHaveValue();
162 });
163
164 test("clear: files", async () => {
165 await mountForTest(/* xml */ `<input type="file" />`);
166 const file = new File([""], "file.txt");
167
168 expect("input").not.toHaveValue();
169
170 await click("input");
171 await fill(file);
172
173 expect("input").toHaveValue([file]);
174
175 await clear();
176
177 expect("input").not.toHaveValue();
178 });
179
180 test("click", async () => {
181 mockTouch(false);
182
183 await mountForTest(/* xml */ `<button autofocus="" type="button">Click me</button>`);
184 monitorEvents("button");
185
186 const events = await click("button");
187 const clickEvent = events.get("click");
188
189 expect(clickEvent.pointerId).toBeGreaterThan(0);
190 expect(clickEvent.pointerType).toBe("mouse");
191
192 expect.verifySteps([
193 // Hover
194 "pointerover:0@button",
195 "mouseover:0@button",
196 "pointerenter:0@button",
197 "mouseenter:0@button",
198 "pointermove:0@button",
199 "mousemove:0@button",
200 // Click
201 "pointerdown:0(1)@button",
202 "mousedown:0(1)@button",
203 "focus@button",
204 "focusin@button",
205 "pointerup:0@button",
206 "mouseup:0@button",
207 "click:0@button",
208 ]);
209 });
210
211 test("dblclick", async () => {
212 await mountForTest(/* xml */ `<button autofocus="" type="button">Click me</button>`);
213 monitorEvents("button");
214
215 await dblclick("button");
216
217 expect.verifySteps([
218 // Hover
219 "pointerover:0@button",
220 "mouseover:0@button",
221 "pointerenter:0@button",
222 "mouseenter:0@button",
223 "pointermove:0@button",
224 "mousemove:0@button",
225 // Click 1
226 "pointerdown:0(1)@button",
227 "mousedown:0(1)@button",
228 "focus@button",
229 "focusin@button",
230 "pointerup:0@button",
231 "mouseup:0@button",
232 "click:0@button",
233 // Click 2
234 "pointerdown:0(1)@button",
235 "mousedown:0(1)@button",
236 "pointerup:0@button",
237 "mouseup:0@button",
238 "click:0@button",
239 // Double click event
240 "dblclick:0@button",
241 ]);
242 });
243
244 test("triple click", async () => {
245 await mountForTest(/* xml */ `<button autofocus="" type="button">Click me</button>`);
246
247 const allEvents = new EventList(
248 // trigger 3 clicks
249 await click("button"),
250 await click("button"),
251 await click("button")
252 );
253
254 const clickEvents = allEvents.getAll("click");
255 const mouseDownEvents = allEvents.getAll("mousedown");
256 const mouseUpEvents = allEvents.getAll("mouseup");
257 const pointerDownEvents = allEvents.getAll("pointerdown");
258 const pointerUpEvents = allEvents.getAll("pointerup");
259
260 expect(pointerDownEvents).toHaveLength(3);
261 expect(pointerDownEvents[0].detail).toBe(0);
262 expect(pointerDownEvents[1].detail).toBe(0);
263 expect(pointerDownEvents[2].detail).toBe(0);
264
265 expect(mouseDownEvents).toHaveLength(3);
266 expect(mouseDownEvents[0].detail).toBe(1);
267 expect(mouseDownEvents[1].detail).toBe(2);
268 expect(mouseDownEvents[2].detail).toBe(3);
269
270 expect(pointerUpEvents).toHaveLength(3);
271 expect(pointerUpEvents[0].detail).toBe(0);
272 expect(pointerUpEvents[1].detail).toBe(0);
273 expect(pointerUpEvents[2].detail).toBe(0);
274
275 expect(mouseUpEvents).toHaveLength(3);
276 expect(mouseUpEvents[0].detail).toBe(1);
277 expect(mouseUpEvents[1].detail).toBe(2);
278 expect(mouseUpEvents[2].detail).toBe(3);
279
280 expect(clickEvents).toHaveLength(3);
281 expect(clickEvents[0].detail).toBe(1);
282 expect(clickEvents[1].detail).toBe(2);
283 expect(clickEvents[2].detail).toBe(3);
284
285 expect(allEvents.getAll("dblclick")).toHaveLength(1);
286
287 await advanceTime(1_000);
288
289 const events = await click("button");
290
291 expect(events.get("click").detail).toBe(1);
292 });
293
294 test("auxclick", async () => {
295 mockTouch(false);
296
297 await mountForTest(/* xml */ `<button autofocus="" type="button">Click me</button>`);
298 await hover("button");
299
300 monitorEvents("button");
301
302 await middleClick("button");
303
304 expect.verifySteps([
305 "pointerdown:1(4)@button",
306 "mousedown:1(4)@button",
307 "focus@button",
308 "focusin@button",
309 "pointerup:1@button",
310 "mouseup:1@button",
311 "auxclick:1@button",
312 ]);
313
314 await rightClick("button");
315
316 expect.verifySteps([
317 "pointerdown:2(2)@button",
318 "mousedown:2(2)@button",
319 "contextmenu:2(2)@button",
320 "pointerup:2@button",
321 "mouseup:2@button",
322 "auxclick:2@button",
323 ]);
324 });
325
326 test("click on disabled element", async () => {
327 await mountForTest(/* xml */ `<button type="button" disabled="">Click me</button>`);
328
329 monitorEvents("button");
330
331 await click("button");
332
333 expect.verifySteps([
334 // Hover
335 "pointerover:0@button",
336 "mouseover:0@button",
337 "pointerenter:0@button",
338 "mouseenter:0@button",
339 "pointermove:0@button",
340 "mousemove:0@button",
341 // Click (mouse events disabled)
342 "pointerdown:0(1)@button",
343 "pointerup:0@button",
344 ]);
345 });
346
347 test("click on element allowing or disallowing pointer events", async () => {
348 await mountForTest(/* xml */ `
349 <div class="container">
350 <button class="first" style="pointer-events: none">
351 Not allowed
352 </button>
353 <div style="pointer-events: none">
354 <button class="second" style="pointer-events: auto">
355 Allowed
356 </button>
357 </div>
358 <button class="third" style="pointer-events: none">
359 Not allowed
360 </button>
361 </div>
362 `);
363
364 const container = queryOne(".container");
365 const interactiveButton = queryOne(".second");
366 let events;
367
368 // Elements affected by pointer-events: none -> doesn't work
369 events = await click(".first");
370 expect(events.get("click").target).toBe(container);
371 events = await click(".third");
372 expect(events.get("click").target).toBe(container);
373
374 // Allowed button -> does work
375 events = await click(interactiveButton);
376 expect(events.get("click").target).toBe(interactiveButton);
377 expect("button:interactive").toHaveCount(1);
378
379 container.style.pointerEvents = "none";
380 interactiveButton.style.pointerEvents = "none";
381
382 // Does not work anymore
383 events = await click(interactiveButton);
384 expect(events.get("click").target).toBe(container.parentElement);
385 expect("button:interactive").not.toHaveCount();
386 });
387
388 test("click on inert element", async () => {
389 await mountForTest(/* xml */ `
390 <div class="container">
391 <button class="btn">Button</button>
392 <iframe inert="" srcdoc="&lt;button&gt;iframe button&lt;/button&gt;" />
393 </div>
394 `);
395
396 let events = await click(".btn");
397 expect(events.get("click")).not.toBe(null);
398
399 queryOne`.btn`.setAttribute("inert", "");
400
401 events = await click(".btn");
402 expect(events.get("click").target).toBe(queryOne`.container`);
403
404 await expect(click(":iframe button")).rejects.toThrow();
405 });
406
407 test("click on common parent", async () => {
408 await mountForTest(/* xml */ `
409 <main class="parent">
410 <button class="first">A</button>
411 <div>
412 <input class="second" />
413 </div>
414 </main>
415 `);
416
417 monitorEvents(".parent");
418 monitorEvents(".first");
419 monitorEvents(".second");
420
421 await pointerDown(".first");
422 await pointerUp(".second");
423
424 expect.verifySteps([
425 // Move to first
426 "pointerover:0@button",
427 "pointerover:0@main",
428 "mouseover:0@button",
429 "mouseover:0@main",
430 "pointerenter:0@main",
431 "pointerenter:0@button",
432 "mouseenter:0@main",
433 "mouseenter:0@button",
434 "pointermove:0@button",
435 "pointermove:0@main",
436 "mousemove:0@button",
437 "mousemove:0@main",
438 // Pointer down on first
439 "pointerdown:0(1)@button",
440 "pointerdown:0(1)@main",
441 "mousedown:0(1)@button",
442 "mousedown:0(1)@main",
443 "focus@button",
444 "focusin@button",
445 "focusin@main",
446 // Move to second
447 "pointermove:0(1)@button",
448 "pointermove:0(1)@main",
449 "mousemove:0(1)@button",
450 "mousemove:0(1)@main",
451 "pointerout:0(1)@button",
452 "pointerout:0(1)@main",
453 "mouseout:0(1)@button",
454 "mouseout:0(1)@main",
455 "pointerleave:0(1)@button",
456 "mouseleave:0(1)@button",
457 "pointerover:0(1)@input",
458 "pointerover:0(1)@main",
459 "mouseover:0(1)@input",
460 "mouseover:0(1)@main",
461 "pointerenter:0(1)@input",
462 "mouseenter:0(1)@input",
463 "pointermove:0(1)@input",
464 "pointermove:0(1)@main",
465 "mousemove:0(1)@input",
466 "mousemove:0(1)@main",
467 // Pointer up on second
468 "pointerup:0@input",
469 "pointerup:0@main",
470 "mouseup:0@input",
471 "mouseup:0@main",
472 "click:0@main",
473 ]);
474 });
475
476 test("click can be dispatched with pointer events prevented", async () => {
477 await mountForTest(/* xml */ `<button type="button">Click me</button>`);
478
479 const prevent = (ev) => ev.preventDefault();
480
481 on("button", "pointerdown", prevent);
482 on("button", "mousedown", prevent);
483 on("button", "pointerup", prevent);
484 on("button", "mouseup", prevent);
485
486 await hover("button");
487 monitorEvents("button");
488
489 await click("button");
490
491 expect.verifySteps(["pointerdown:0(1)@button", "pointerup:0@button", "click:0@button"]);
492 });
493
494 test("click: iframe", async () => {
495 await mountForTest(/* xml */ `
496 <button>Click me</button>
497 <iframe srcdoc="&lt;button&gt;iframe button&lt;/button&gt;" />
498 `);
499
500 expect("button").toHaveCount(1);
501 expect(":iframe button").toHaveCount(1);
502
503 await click("button");
504
505 expect("button").toBeFocused();
506 expect(":iframe button").not.toBeFocused();
507
508 await click(":iframe button");
509
510 expect("button").not.toBeFocused();
511 expect(":iframe button").toBeFocused();
512 });
513
514 test("drag & drop: draggable items", async () => {
515 await mountForTest(/* xml */ `
516 <ul>
517 <li id="first-item" draggable="true">Item 1</li>
518 <li id="second-item" draggable="true">Item 2</li>
519 <li id="third-item" draggable="true">Item 3</li>
520 </ul>
521 `);
522
523 monitorEvents("body");
524 monitorEvents("li");
525
526 // Drag & cancel
527 await (await drag("#first-item")).cancel();
528
529 expect.verifySteps([
530 // Move to first
531 "pointerover:0@#first-item",
532 "pointerover:0@body",
533 "mouseover:0@#first-item",
534 "mouseover:0@body",
535 "pointerenter:0@body",
536 "pointerenter:0@#first-item",
537 "mouseenter:0@body",
538 "mouseenter:0@#first-item",
539 "pointermove:0@#first-item",
540 "pointermove:0@body",
541 "mousemove:0@#first-item",
542 "mousemove:0@body",
543 // Drag first
544 "pointerdown:0(1)@#first-item",
545 "pointerdown:0(1)@body",
546 "mousedown:0(1)@#first-item",
547 "mousedown:0(1)@body",
548 // Cancel
549 "keydown:Escape@body",
550 "keyup:Escape@body",
551 ]);
552
553 // Drag & drop
554 await (await drag("#first-item")).drop("#third-item");
555
556 expect.verifySteps([
557 // Drag first
558 "pointerdown:0(1)@#first-item",
559 "pointerdown:0(1)@body",
560 "mousedown:0(1)@#first-item",
561 "mousedown:0(1)@body",
562 // Leave first
563 "dragstart:0@#first-item",
564 "dragstart:0@body",
565 "drag:0@#first-item",
566 "drag:0@body",
567 "dragover:0@#first-item",
568 "dragover:0@body",
569 "dragleave:0@#first-item",
570 "dragleave:0@body",
571 // Move to third
572 "dragenter:0@#third-item",
573 "dragenter:0@body",
574 "drag:0@#third-item",
575 "drag:0@body",
576 "dragover:0@#third-item",
577 "dragover:0@body",
578 // Drop
579 "dragend:0@#third-item",
580 "dragend:0@body",
581 ]);
582
583 // Drag, move & cancel
584 await (await (await drag("#first-item")).moveTo("#third-item")).cancel();
585
586 expect.verifySteps([
587 // Leave third
588 "pointermove:0@#third-item",
589 "pointermove:0@body",
590 "mousemove:0@#third-item",
591 "mousemove:0@body",
592 "pointerout:0@#third-item",
593 "pointerout:0@body",
594 "mouseout:0@#third-item",
595 "mouseout:0@body",
596 "pointerleave:0@#third-item",
597 "mouseleave:0@#third-item",
598 // Move to first
599 "pointerover:0@#first-item",
600 "pointerover:0@body",
601 "mouseover:0@#first-item",
602 "mouseover:0@body",
603 "pointerenter:0@#first-item",
604 "mouseenter:0@#first-item",
605 "pointermove:0@#first-item",
606 "pointermove:0@body",
607 "mousemove:0@#first-item",
608 "mousemove:0@body",
609 // Drag first
610 "pointerdown:0(1)@#first-item",
611 "pointerdown:0(1)@body",
612 "mousedown:0(1)@#first-item",
613 "mousedown:0(1)@body",
614 // Leave first
615 "dragstart:0@#first-item",
616 "dragstart:0@body",
617 "drag:0@#first-item",
618 "drag:0@body",
619 "dragover:0@#first-item",
620 "dragover:0@body",
621 "dragleave:0@#first-item",
622 "dragleave:0@body",
623 // Move to third
624 "dragenter:0@#third-item",
625 "dragenter:0@body",
626 "drag:0@#third-item",
627 "drag:0@body",
628 "dragover:0@#third-item",
629 "dragover:0@body",
630 // Cancel
631 "keydown:Escape@body",
632 "keyup:Escape@body",
633 ]);
634
635 // Drag, move & drop
636 await (await (await drag("#first-item")).moveTo("#third-item")).drop();
637
638 expect.verifySteps([
639 // Leave third
640 "pointermove:0@#third-item",
641 "pointermove:0@body",
642 "mousemove:0@#third-item",
643 "mousemove:0@body",
644 "pointerout:0@#third-item",
645 "pointerout:0@body",
646 "mouseout:0@#third-item",
647 "mouseout:0@body",
648 "pointerleave:0@#third-item",
649 "mouseleave:0@#third-item",
650 // Move to first
651 "pointerover:0@#first-item",
652 "pointerover:0@body",
653 "mouseover:0@#first-item",
654 "mouseover:0@body",
655 "pointerenter:0@#first-item",
656 "mouseenter:0@#first-item",
657 "pointermove:0@#first-item",
658 "pointermove:0@body",
659 "mousemove:0@#first-item",
660 "mousemove:0@body",
661 // Drag first
662 "pointerdown:0(1)@#first-item",
663 "pointerdown:0(1)@body",
664 "mousedown:0(1)@#first-item",
665 "mousedown:0(1)@body",
666 // Leave first
667 "dragstart:0@#first-item",
668 "dragstart:0@body",
669 "drag:0@#first-item",
670 "drag:0@body",
671 "dragover:0@#first-item",
672 "dragover:0@body",
673 "dragleave:0@#first-item",
674 "dragleave:0@body",
675 // Move to third
676 "dragenter:0@#third-item",
677 "dragenter:0@body",
678 "drag:0@#third-item",
679 "drag:0@body",
680 "dragover:0@#third-item",
681 "dragover:0@body",
682 // Drop
683 "dragend:0@#third-item",
684 "dragend:0@body",
685 ]);
686
687 // Drag, move & drop (different target)
688 await (await (await drag("#first-item")).moveTo("#second-item")).drop("#third-item");
689
690 expect.verifySteps([
691 // Leave third
692 "pointermove:0@#third-item",
693 "pointermove:0@body",
694 "mousemove:0@#third-item",
695 "mousemove:0@body",
696 "pointerout:0@#third-item",
697 "pointerout:0@body",
698 "mouseout:0@#third-item",
699 "mouseout:0@body",
700 "pointerleave:0@#third-item",
701 "mouseleave:0@#third-item",
702 // Move to first
703 "pointerover:0@#first-item",
704 "pointerover:0@body",
705 "mouseover:0@#first-item",
706 "mouseover:0@body",
707 "pointerenter:0@#first-item",
708 "mouseenter:0@#first-item",
709 "pointermove:0@#first-item",
710 "pointermove:0@body",
711 "mousemove:0@#first-item",
712 "mousemove:0@body",
713 // Drag first
714 "pointerdown:0(1)@#first-item",
715 "pointerdown:0(1)@body",
716 "mousedown:0(1)@#first-item",
717 "mousedown:0(1)@body",
718 // Leave first
719 "dragstart:0@#first-item",
720 "dragstart:0@body",
721 "drag:0@#first-item",
722 "drag:0@body",
723 "dragover:0@#first-item",
724 "dragover:0@body",
725 "dragleave:0@#first-item",
726 "dragleave:0@body",
727 // Move to second
728 "dragenter:0@#second-item",
729 "dragenter:0@body",
730 "drag:0@#second-item",
731 "drag:0@body",
732 "dragover:0@#second-item",
733 "dragover:0@body",
734 // Leave second
735 "drag:0@#second-item",
736 "drag:0@body",
737 "dragover:0@#second-item",
738 "dragover:0@body",
739 "dragleave:0@#second-item",
740 "dragleave:0@body",
741 // Move to third
742 "dragenter:0@#third-item",
743 "dragenter:0@body",
744 "drag:0@#third-item",
745 "drag:0@body",
746 "dragover:0@#third-item",
747 "dragover:0@body",
748 // Drop
749 "dragend:0@#third-item",
750 "dragend:0@body",
751 ]);
752 });
753
754 test("drag & drop: draggable items with files", async () => {
755 await mountForTest(/* xml */ `
756 <ul>
757 <li id="first-item" draggable="true">Item 1</li>
758 <li id="second-item" draggable="true">Item 2</li>
759 <li id="third-item" draggable="true">Item 3</li>
760 </ul>
761 `);
762
763 const { drop, moveTo } = await drag("#first-item", {
764 dropEffect: "move",
765 files: [new File([""], "dragged-file.txt")],
766 });
767 await moveTo("#second-item");
768 const events = await drop("#third-item");
769
770 const dragEvents = events.getAll((ev) => ev.type.startsWith("drag"));
771 const { dataTransfer } = dragEvents[0];
772
773 expect(dataTransfer.dropEffect).toBe("move");
774 expect(dataTransfer.effectAllowed).toBe("all");
775 expect(dataTransfer.files).toHaveLength(1);
776 expect(dataTransfer.items).toHaveLength(1);
777 expect(dataTransfer.types).toEqual(["Files"]);
778
779 for (const event of dragEvents) {
780 expect(event.dataTransfer).toBe(dataTransfer, {
781 message: `drag event "${event.type}" should share the same dataTransfer object`,
782 });
783 }
784 });
785
786 test("drag & drop: draggable items with dataTransfer items", async () => {
787 await mountForTest(/* xml */ `
788 <ul>
789 <li id="first-item" draggable="true">Item 1</li>
790 <li id="second-item" draggable="true">Item 2</li>
791 <li id="third-item" draggable="true">Item 3</li>
792 </ul>
793 `);
794
795 const { drop, moveTo } = await drag("#first-item", {
796 items: [
797 ["abc", "text/plain"],
798 ["<html/>", "text/html"],
799 ],
800 });
801 await moveTo("#second-item");
802 const events = await drop("#third-item");
803
804 const dragEvents = events.getAll((ev) => ev.type.startsWith("drag"));
805 const { dataTransfer } = dragEvents[0];
806
807 expect(dataTransfer.dropEffect).toBe("none");
808 expect(dataTransfer.effectAllowed).toBe("all");
809 expect(dataTransfer.files).toHaveLength(0);
810 expect(dataTransfer.items).toHaveLength(2);
811 expect(dataTransfer.types).toEqual(["text/plain", "text/html"]);
812
813 dataTransfer.setData("custom-data", "yes");
814
815 expect(dataTransfer.items).toHaveLength(3);
816 expect(dataTransfer.types).toEqual(["text/plain", "text/html", "custom-data"]);
817
818 for (const event of dragEvents) {
819 expect(event.dataTransfer).toBe(dataTransfer, {
820 message: `drag event "${event.type}" should share the same dataTransfer object`,
821 });
822 }
823 });
824
825 test("drag & drop: non-draggable items", async () => {
826 await mountForTest(/* xml */ `
827 <ul>
828 <li id="first-item">Item 1</li>
829 <li id="second-item">Item 2</li>
830 <li id="third-item">Item 3</li>
831 </ul>
832 `);
833
834 monitorEvents("body");
835 monitorEvents("li");
836
837 // Drag & cancel
838 await (await drag("#first-item")).cancel();
839
840 expect.verifySteps([
841 // Move to first
842 "pointerover:0@#first-item",
843 "pointerover:0@body",
844 "mouseover:0@#first-item",
845 "mouseover:0@body",
846 "pointerenter:0@body",
847 "pointerenter:0@#first-item",
848 "mouseenter:0@body",
849 "mouseenter:0@#first-item",
850 "pointermove:0@#first-item",
851 "pointermove:0@body",
852 "mousemove:0@#first-item",
853 "mousemove:0@body",
854 // Drag first
855 "pointerdown:0(1)@#first-item",
856 "pointerdown:0(1)@body",
857 "mousedown:0(1)@#first-item",
858 "mousedown:0(1)@body",
859 // Cancel
860 "keydown:Escape@body",
861 "keyup:Escape@body",
862 ]);
863
864 // Drag & drop
865 await (await drag("#first-item")).drop("#third-item");
866
867 expect.verifySteps([
868 // Drag first
869 "pointerdown:0(1)@#first-item",
870 "pointerdown:0(1)@body",
871 "mousedown:0(1)@#first-item",
872 "mousedown:0(1)@body",
873 // Leave first
874 "pointermove:0(1)@#first-item",
875 "pointermove:0(1)@body",
876 "mousemove:0(1)@#first-item",
877 "mousemove:0(1)@body",
878 "pointerout:0(1)@#first-item",
879 "pointerout:0(1)@body",
880 "mouseout:0(1)@#first-item",
881 "mouseout:0(1)@body",
882 "pointerleave:0(1)@#first-item",
883 "mouseleave:0(1)@#first-item",
884 // Move to third
885 "pointerover:0(1)@#third-item",
886 "pointerover:0(1)@body",
887 "mouseover:0(1)@#third-item",
888 "mouseover:0(1)@body",
889 "pointerenter:0(1)@#third-item",
890 "mouseenter:0(1)@#third-item",
891 "pointermove:0(1)@#third-item",
892 "pointermove:0(1)@body",
893 "mousemove:0(1)@#third-item",
894 "mousemove:0(1)@body",
895 // Drop
896 "pointerup:0@#third-item",
897 "pointerup:0@body",
898 "mouseup:0@#third-item",
899 "mouseup:0@body",
900 "click:0@body",
901 ]);
902
903 // Drag, move & cancel
904 await (await (await drag("#first-item")).moveTo("#third-item")).cancel();
905
906 expect.verifySteps([
907 // Leave third
908 "pointermove:0@#third-item",
909 "pointermove:0@body",
910 "mousemove:0@#third-item",
911 "mousemove:0@body",
912 "pointerout:0@#third-item",
913 "pointerout:0@body",
914 "mouseout:0@#third-item",
915 "mouseout:0@body",
916 "pointerleave:0@#third-item",
917 "mouseleave:0@#third-item",
918 // Move to first
919 "pointerover:0@#first-item",
920 "pointerover:0@body",
921 "mouseover:0@#first-item",
922 "mouseover:0@body",
923 "pointerenter:0@#first-item",
924 "mouseenter:0@#first-item",
925 "pointermove:0@#first-item",
926 "pointermove:0@body",
927 "mousemove:0@#first-item",
928 "mousemove:0@body",
929 // Drag first
930 "pointerdown:0(1)@#first-item",
931 "pointerdown:0(1)@body",
932 "mousedown:0(1)@#first-item",
933 "mousedown:0(1)@body",
934 // Leave first
935 "pointermove:0(1)@#first-item",
936 "pointermove:0(1)@body",
937 "mousemove:0(1)@#first-item",
938 "mousemove:0(1)@body",
939 "pointerout:0(1)@#first-item",
940 "pointerout:0(1)@body",
941 "mouseout:0(1)@#first-item",
942 "mouseout:0(1)@body",
943 "pointerleave:0(1)@#first-item",
944 "mouseleave:0(1)@#first-item",
945 // Move to third
946 "pointerover:0(1)@#third-item",
947 "pointerover:0(1)@body",
948 "mouseover:0(1)@#third-item",
949 "mouseover:0(1)@body",
950 "pointerenter:0(1)@#third-item",
951 "mouseenter:0(1)@#third-item",
952 "pointermove:0(1)@#third-item",
953 "pointermove:0(1)@body",
954 "mousemove:0(1)@#third-item",
955 "mousemove:0(1)@body",
956 // Cancel
957 "keydown:Escape@body",
958 "keyup:Escape@body",
959 ]);
960
961 // Drag, move & drop
962 await (await (await drag("#first-item")).moveTo("#third-item")).drop();
963
964 expect.verifySteps([
965 // Leave third
966 "pointermove:0@#third-item",
967 "pointermove:0@body",
968 "mousemove:0@#third-item",
969 "mousemove:0@body",
970 "pointerout:0@#third-item",
971 "pointerout:0@body",
972 "mouseout:0@#third-item",
973 "mouseout:0@body",
974 "pointerleave:0@#third-item",
975 "mouseleave:0@#third-item",
976 // Move to first
977 "pointerover:0@#first-item",
978 "pointerover:0@body",
979 "mouseover:0@#first-item",
980 "mouseover:0@body",
981 "pointerenter:0@#first-item",
982 "mouseenter:0@#first-item",
983 "pointermove:0@#first-item",
984 "pointermove:0@body",
985 "mousemove:0@#first-item",
986 "mousemove:0@body",
987 // Drag first
988 "pointerdown:0(1)@#first-item",
989 "pointerdown:0(1)@body",
990 "mousedown:0(1)@#first-item",
991 "mousedown:0(1)@body",
992 // Leave first
993 "pointermove:0(1)@#first-item",
994 "pointermove:0(1)@body",
995 "mousemove:0(1)@#first-item",
996 "mousemove:0(1)@body",
997 "pointerout:0(1)@#first-item",
998 "pointerout:0(1)@body",
999 "mouseout:0(1)@#first-item",
1000 "mouseout:0(1)@body",
1001 "pointerleave:0(1)@#first-item",
1002 "mouseleave:0(1)@#first-item",
1003 // Move to third
1004 "pointerover:0(1)@#third-item",
1005 "pointerover:0(1)@body",
1006 "mouseover:0(1)@#third-item",
1007 "mouseover:0(1)@body",
1008 "pointerenter:0(1)@#third-item",
1009 "mouseenter:0(1)@#third-item",
1010 "pointermove:0(1)@#third-item",
1011 "pointermove:0(1)@body",
1012 "mousemove:0(1)@#third-item",
1013 "mousemove:0(1)@body",
1014 // Drop
1015 "pointerup:0@#third-item",
1016 "pointerup:0@body",
1017 "mouseup:0@#third-item",
1018 "mouseup:0@body",
1019 "click:0@body",
1020 "dblclick:0@body",
1021 ]);
1022
1023 // Drag, move & drop (different target)
1024 await (await (await drag("#first-item")).moveTo("#second-item")).drop("#third-item");
1025
1026 expect.verifySteps([
1027 // Leave third
1028 "pointermove:0@#third-item",
1029 "pointermove:0@body",
1030 "mousemove:0@#third-item",
1031 "mousemove:0@body",
1032 "pointerout:0@#third-item",
1033 "pointerout:0@body",
1034 "mouseout:0@#third-item",
1035 "mouseout:0@body",
1036 "pointerleave:0@#third-item",
1037 "mouseleave:0@#third-item",
1038 // Move to first
1039 "pointerover:0@#first-item",
1040 "pointerover:0@body",
1041 "mouseover:0@#first-item",
1042 "mouseover:0@body",
1043 "pointerenter:0@#first-item",
1044 "mouseenter:0@#first-item",
1045 "pointermove:0@#first-item",
1046 "pointermove:0@body",
1047 "mousemove:0@#first-item",
1048 "mousemove:0@body",
1049 // Drag first
1050 "pointerdown:0(1)@#first-item",
1051 "pointerdown:0(1)@body",
1052 "mousedown:0(1)@#first-item",
1053 "mousedown:0(1)@body",
1054 // Leave first
1055 "pointermove:0(1)@#first-item",
1056 "pointermove:0(1)@body",
1057 "mousemove:0(1)@#first-item",
1058 "mousemove:0(1)@body",
1059 "pointerout:0(1)@#first-item",
1060 "pointerout:0(1)@body",
1061 "mouseout:0(1)@#first-item",
1062 "mouseout:0(1)@body",
1063 "pointerleave:0(1)@#first-item",
1064 "mouseleave:0(1)@#first-item",
1065 // Move to second
1066 "pointerover:0(1)@#second-item",
1067 "pointerover:0(1)@body",
1068 "mouseover:0(1)@#second-item",
1069 "mouseover:0(1)@body",
1070 "pointerenter:0(1)@#second-item",
1071 "mouseenter:0(1)@#second-item",
1072 "pointermove:0(1)@#second-item",
1073 "pointermove:0(1)@body",
1074 "mousemove:0(1)@#second-item",
1075 "mousemove:0(1)@body",
1076 // Leave second
1077 "pointermove:0(1)@#second-item",
1078 "pointermove:0(1)@body",
1079 "mousemove:0(1)@#second-item",
1080 "mousemove:0(1)@body",
1081 "pointerout:0(1)@#second-item",
1082 "pointerout:0(1)@body",
1083 "mouseout:0(1)@#second-item",
1084 "mouseout:0(1)@body",
1085 "pointerleave:0(1)@#second-item",
1086 "mouseleave:0(1)@#second-item",
1087 // Move to third
1088 "pointerover:0(1)@#third-item",
1089 "pointerover:0(1)@body",
1090 "mouseover:0(1)@#third-item",
1091 "mouseover:0(1)@body",
1092 "pointerenter:0(1)@#third-item",
1093 "mouseenter:0(1)@#third-item",
1094 "pointermove:0(1)@#third-item",
1095 "pointermove:0(1)@body",
1096 "mousemove:0(1)@#third-item",
1097 "mousemove:0(1)@body",
1098 // Drop
1099 "pointerup:0@#third-item",
1100 "pointerup:0@body",
1101 "mouseup:0@#third-item",
1102 "mouseup:0@body",
1103 "click:0@body",
1104 ]);
1105 });
1106
1107 test("drag & drop: touch environment", async () => {
1108 mockTouch(true);
1109
1110 await mountForTest(/* xml */ `
1111 <ul>
1112 <li id="first-item">Item 1</li>
1113 <li id="second-item">Item 2</li>
1114 <li id="third-item">Item 3</li>
1115 </ul>
1116 `);
1117
1118 const firstItem = queryOne("#first-item");
1119 const events = await (await drag("#first-item")).drop("#third-item");
1120 const touchEvents = events.getAll((ev) => ev.type.startsWith("touch"));
1121
1122 expect(touchEvents.map((e) => e.target)).toEqual(
1123 [
1124 firstItem, // start
1125 firstItem, // move (on first)
1126 firstItem, // move (on last)
1127 firstItem, // end
1128 ],
1129 { message: "touch events should all target the same element" }
1130 );
1131
1132 const [touchStart, touchMove, , touchEnd] = touchEvents;
1133
1134 expect(touchStart).not.toInclude("clientX");
1135 expect(touchStart).not.toInclude("clientY");
1136 expect(touchStart.touches).toHaveLength(1);
1137
1138 expect(touchMove.touches).toHaveLength(1);
1139
1140 expect(touchEnd).not.toInclude("clientX");
1141 expect(touchEnd).not.toInclude("clientY");
1142 expect(touchEnd.touches).toHaveLength(0);
1143 });
1144
1145 test("fill: text", async () => {
1146 await mountForTest(/* xml */ `<input type="text" value="" />`);
1147
1148 expect("input").not.toHaveValue();
1149 expect.verifySteps([]);
1150
1151 await click("input");
1152
1153 monitorEvents("input");
1154
1155 await fill("Test value");
1156
1157 expect("input").toHaveValue("Test value");
1158 expect.verifySteps([
1159 ...[..."Test value"].flatMap((char) => {
1160 let key = char.toLowerCase();
1161 if (char !== char.toLowerCase()) {
1162 key += ".shift";
1163 }
1164 return [
1165 `keydown:${key}@input`,
1166 `beforeinput:${char}@input`,
1167 `input:${char}@input`,
1168 `keyup:${key}@input`,
1169 ];
1170 }),
1171 ]);
1172 });
1173
1174 test("fill: text with previous value", async () => {
1175 await mountForTest(/* xml */ `<input type="text" value="Test" />`);
1176
1177 expect("input").toHaveValue("Test");
1178
1179 await click("input");
1180 await fill(" value");
1181
1182 expect("input").toHaveValue("Test value");
1183 });
1184
1185 test("fill: number", async () => {
1186 await mountForTest(/* xml */ `<input type="number" />`);
1187
1188 expect("input").not.toHaveValue();
1189
1190 await click("input");
1191 await fill(42);
1192
1193 expect("input").toHaveValue(42);
1194 });
1195
1196 test("fill: email", async () => {
1197 await mountForTest(/* xml */ `<input type="email" />`);
1198
1199 expect("input").not.toHaveValue();
1200
1201 await click("input");
1202 await fill("[email protected]");
1203
1204 expect("input").toHaveValue("[email protected]");
1205 });
1206
1207 test("edit on empty value", async () => {
1208 await mountForTest(/* xml */ `<input type="text" />`);
1209
1210 await click("input");
1211
1212 monitorEvents("input");
1213
1214 expect("input").not.toHaveValue();
1215
1216 await edit("test value");
1217
1218 expect("input").toHaveValue("test value");
1219 expect.verifySteps([
1220 ...[..."test value"].flatMap((char) => [
1221 `keydown:${char}@input`,
1222 `beforeinput:${char}@input`,
1223 `input:${char}@input`,
1224 `keyup:${char}@input`,
1225 ]),
1226 ]);
1227
1228 await click(getFixture());
1229
1230 expect.verifySteps([
1231 // Pointer out
1232 "pointermove:0@input",
1233 "mousemove:0@input",
1234 "pointerout:0@input",
1235 "mouseout:0@input",
1236 "pointerleave:0@input",
1237 "mouseleave:0@input",
1238 // Change
1239 "blur@input",
1240 "change@input",
1241 "focusout@input",
1242 ]);
1243 });
1244
1245 test("edit on existing value", async () => {
1246 await mountForTest(/* xml */ `<input type="text" value="Test" />`);
1247
1248 await click("input");
1249 await animationFrame();
1250
1251 monitorEvents("input");
1252
1253 expect("input").toHaveValue("Test");
1254
1255 await edit(" value");
1256 await animationFrame();
1257
1258 expect("input").toHaveValue(" value");
1259 expect.verifySteps([
1260 // Clear
1261 "keydown:a.ctrl@input",
1262 "select@input",
1263 "keyup:a.ctrl@input",
1264 "keydown:Backspace@input",
1265 "beforeinput@input",
1266 "input@input",
1267 "keyup:Backspace@input",
1268 // Fill
1269 ...[..." value"].flatMap((char) => [
1270 `keydown:${char}@input`,
1271 `beforeinput:${char}@input`,
1272 `input:${char}@input`,
1273 `keyup:${char}@input`,
1274 ]),
1275 ]);
1276 });
1277
1278 test("edit with dirty value and blur", async () => {
1279 await mountForTest(/* xml */ `
1280 <input type="text" />
1281 <button>Diversion</button>
1282 `);
1283 await click("input");
1284 await edit("test value");
1285
1286 monitorEvents("input");
1287 monitorEvents("button");
1288
1289 await click("button");
1290
1291 expect.verifySteps([
1292 // Move to button
1293 "pointermove:0@input",
1294 "mousemove:0@input",
1295 "pointerout:0@input",
1296 "mouseout:0@input",
1297 "pointerleave:0@input",
1298 "mouseleave:0@input",
1299 "pointerover:0@button",
1300 "mouseover:0@button",
1301 "pointerenter:0@button",
1302 "mouseenter:0@button",
1303 "pointermove:0@button",
1304 "mousemove:0@button",
1305 // Click on button
1306 "pointerdown:0(1)@button",
1307 "mousedown:0(1)@button",
1308 "change@input",
1309 "blur@input",
1310 "focusout@input",
1311 "focus@button",
1312 "focusin@button",
1313 "pointerup:0@button",
1314 "mouseup:0@button",
1315 "click:0@button",
1316 ]);
1317 });
1318
1319 test("edit with dirty value and confirm with enter", async () => {
1320 await mountForTest(/* xml */ `
1321 <input type="text" />
1322 <button>Diversion</button>
1323 `);
1324 await click("input");
1325 await edit("test value");
1326
1327 monitorEvents("input");
1328
1329 await press("Enter");
1330
1331 expect.verifySteps(["keydown:Enter@input", "change@input", "keyup:Enter@input"]);
1332 });
1333
1334 test("edit: iframe", async () => {
1335 await mountForTest(/* xml */ `
1336 <input type="text" />
1337 <iframe srcdoc="&lt;input type='text' /&gt;" />
1338 `);
1339
1340 expect("input").toHaveCount(1);
1341 expect(":iframe input").toHaveCount(1);
1342
1343 on("input", "change", () => expect.step("top:change"));
1344 on(":iframe input", "change", () => expect.step("iframe:change"));
1345
1346 await click("input");
1347 await edit("abc");
1348
1349 expect.verifySteps([]);
1350 expect("input").toHaveValue("abc");
1351 expect(":iframe input").toHaveValue("");
1352
1353 await click(":iframe input");
1354 await edit("def");
1355
1356 expect.verifySteps(["top:change"]);
1357 expect("input").toHaveValue("abc");
1358 expect(":iframe input").toHaveValue("def");
1359
1360 await click(":iframe body");
1361 expect.verifySteps(["iframe:change"]);
1362 });
1363
1364 test("setInputFiles: single file", async () => {
1365 await mountForTest(/* xml */ `<input type="file" />`);
1366 const file1 = new File([""], "file1.txt");
1367 const file2 = new File([""], "file2.txt");
1368
1369 expect("input").not.toHaveValue();
1370
1371 await click("input");
1372 await setInputFiles(file1);
1373
1374 expect("input").toHaveValue(/file1\.txt/);
1375 expect("input").toHaveValue([file1]);
1376
1377 await click("input");
1378 await setInputFiles(file2);
1379
1380 expect("input").toHaveValue(/file2\.txt/);
1381 expect("input").toHaveValue([file2]);
1382 });
1383
1384 test("setInputFiles: multiple files", async () => {
1385 await mountForTest(/* xml */ `<input type="file" multiple="multiple" />`);
1386 const file1 = new File([""], "file1.txt");
1387 const file2 = new File([""], "file2.txt");
1388
1389 expect("input").not.toHaveValue();
1390
1391 await click("input");
1392 await setInputFiles(file1);
1393
1394 expect("input").toHaveValue(/file1\.txt/);
1395 expect("input").toHaveValue([file1]);
1396
1397 await click("input");
1398 await setInputFiles([file1, file2]);
1399
1400 expect("input").toHaveValue([file1, file2]);
1401 });
1402
1403 test("setInputFiles: hidden input with label", async () => {
1404 await mountForTest(/* xml */ `
1405 <label for="file-input">Label</label>
1406 <input id="file-input" style="display: none" type="file" />
1407 `);
1408
1409 expect("input").not.toBeVisible();
1410 expect("input").not.toHaveValue();
1411 expect("label").toBeVisible();
1412
1413 await click("label");
1414 await setInputFiles(new File([""], "file.txt"));
1415
1416 expect("input").toHaveValue(/file\.txt/);
1417 });
1418
1419 test("setInputFiles: hidden input with programmatic click", async () => {
1420 await mountForTest(/* xml */ `
1421 <button>upload</button>
1422 <input style="display: none" type="file" />
1423 `);
1424
1425 on("button", "click", () => queryOne("input").click());
1426
1427 expect("input").not.toBeVisible();
1428 expect("input").not.toHaveValue();
1429 expect("button").toBeVisible();
1430
1431 await click("button");
1432 await setInputFiles(new File([""], "file.txt"));
1433
1434 expect("input").toHaveValue(/file\.txt/);
1435 });
1436
1437 test("setInputFiles: shadow root", async () => {
1438 await mountForTest(/* xml */ `
1439 <div class="container" />
1440 `);
1441
1442 const shadow = queryOne(".container").attachShadow({
1443 mode: "open",
1444 });
1445 const input = document.createElement("input");
1446 input.type = "file";
1447 shadow.appendChild(input);
1448
1449 await click(".container:shadow input");
1450 await setInputFiles(new File([""], "file.txt"));
1451
1452 expect(".container:shadow input").toHaveValue(/file\.txt/);
1453 });
1454
1455 test("setInputRange: basic case and events", async () => {
1456 await mountForTest(/* xml */ `<input type="range" min="10" max="40" />`);
1457
1458 monitorEvents("input");
1459
1460 await setInputRange("input", 30);
1461
1462 expect("input").toHaveValue(30);
1463 expect.verifySteps([
1464 // Hover input
1465 "pointerover:0@input",
1466 "mouseover:0@input",
1467 "pointerenter:0@input",
1468 "mouseenter:0@input",
1469 "pointermove:0@input",
1470 "mousemove:0@input",
1471 // Pointer down
1472 "pointerdown:0(1)@input",
1473 "mousedown:0(1)@input",
1474 "focus@input",
1475 "focusin@input",
1476 // Set range
1477 "input@input",
1478 "change@input",
1479 // Pointer up
1480 "pointerup:0@input",
1481 "mouseup:0@input",
1482 "click:0@input",
1483 ]);
1484 });
1485
1486 test("setInputRange: out of min and max values", async () => {
1487 await mountForTest(/* xml */ `<input type="range" min="10" max="40" />`);
1488
1489 await setInputRange("input", 5);
1490
1491 expect("input").toHaveValue(10);
1492
1493 await setInputRange("input", 50);
1494
1495 expect("input").toHaveValue(40);
1496 });
1497
1498 test("hover", async () => {
1499 await mountForTest(/* xml */ `<button type="button">Click me</button>`);
1500 monitorEvents("button");
1501
1502 await hover("button");
1503
1504 expect.verifySteps([
1505 "pointerover:0@button",
1506 "mouseover:0@button",
1507 "pointerenter:0@button",
1508 "mouseenter:0@button",
1509 "pointermove:0@button",
1510 "mousemove:0@button",
1511 ]);
1512
1513 await hover("button");
1514
1515 expect.verifySteps(["pointermove:0@button", "mousemove:0@button"]);
1516 });
1517
1518 test("leave", async () => {
1519 await mountForTest(/* xml */ `<button type="button">Click me</button>`);
1520
1521 await hover("button");
1522
1523 monitorEvents("button");
1524
1525 await leave();
1526
1527 expect.verifySteps([
1528 "pointermove:0@button",
1529 "mousemove:0@button",
1530 "pointerout:0@button",
1531 "mouseout:0@button",
1532 "pointerleave:0@button",
1533 "mouseleave:0@button",
1534 ]);
1535 });
1536
1537 test("keyDown", async () => {
1538 await mountForTest(/* xml */ `<input type="text" />`);
1539
1540 await click("input");
1541
1542 monitorEvents("input");
1543
1544 await keyDown("a");
1545
1546 expect.verifySteps(["keydown:a@input", "beforeinput:a@input", "input:a@input"]);
1547
1548 await keyUp("a");
1549
1550 expect("input").toHaveValue("a");
1551 expect.verifySteps(["keyup:a@input"]);
1552 });
1553
1554 test("multiple keyDown should be flagged as repeated", async () => {
1555 let events;
1556
1557 await mountForTest(/* xml */ `<input type="text" />`);
1558
1559 await click("input");
1560
1561 monitorEvents("input");
1562
1563 events = await keyDown("Enter");
1564 expect(events.get("keydown").repeat).toBe(false);
1565
1566 events = await keyDown("Enter");
1567 expect(events.get("keydown").repeat).toBe(true);
1568
1569 events = await keyDown("Enter");
1570 expect(events.get("keydown").repeat).toBe(true);
1571
1572 events = await keyDown("Escape");
1573 expect(events.get("keydown").repeat).toBe(false);
1574
1575 events = await keyDown("Enter");
1576 expect(events.get("keydown").repeat).toBe(false);
1577
1578 events = await keyUp("Enter");
1579 expect(events.get("keydown")).toBe(null);
1580
1581 events = await keyDown("Enter");
1582 expect(events.get("keydown").repeat).toBe(false);
1583
1584 events = await keyDown("Enter");
1585 expect(events.get("keydown").repeat).toBe(true);
1586
1587 expect.verifySteps([
1588 "keydown:Enter@input",
1589 "keydown:Enter@input",
1590 "keydown:Enter@input",
1591 "keydown:Escape@input",
1592 "keydown:Enter@input",
1593 "keyup:Enter@input",
1594 "keydown:Enter@input",
1595 "keydown:Enter@input",
1596 ]);
1597 });
1598
1599 test("pointerDown", async () => {
1600 await mountForTest(/* xml */ `<button type="button">Click me</button>`);
1601 monitorEvents("button");
1602
1603 await pointerDown("button");
1604
1605 expect.verifySteps([
1606 // Pointer enter on button
1607 "pointerover:0@button",
1608 "mouseover:0@button",
1609 "pointerenter:0@button",
1610 "mouseenter:0@button",
1611 "pointermove:0@button",
1612 "mousemove:0@button",
1613 // Pointer down
1614 "pointerdown:0(1)@button",
1615 "mousedown:0(1)@button",
1616 "focus@button",
1617 "focusin@button",
1618 ]);
1619
1620 await pointerUp("button");
1621
1622 expect.verifySteps(["pointerup:0@button", "mouseup:0@button", "click:0@button"]);
1623 });
1624
1625 test("press key on text input", async () => {
1626 await mountForTest(/* xml */ `<input type="text" />`);
1627
1628 await click("input");
1629
1630 monitorEvents("input");
1631
1632 await press("a");
1633
1634 expect("input").toHaveValue("a");
1635 expect.verifySteps([
1636 "keydown:a@input",
1637 "beforeinput:a@input",
1638 "input:a@input",
1639 "keyup:a@input",
1640 ]);
1641 });
1642
1643 test("press key on number input", async () => {
1644 await mountForTest(/* xml */ `<input type="number" />`);
1645
1646 expect("input").not.toHaveValue();
1647
1648 await click("input");
1649 await press("4");
1650
1651 expect("input").toHaveValue(4);
1652
1653 await press("2");
1654
1655 expect("input").toHaveValue(42);
1656 });
1657
1658 test("press arrow keys on input", async () => {
1659 await mountForTest(/* xml */ `<input value="value" />`);
1660
1661 await click("input");
1662
1663 expect("input").toHaveProperty("selectionStart", 5);
1664 expect("input").toHaveProperty("selectionEnd", 5);
1665
1666 await press("left");
1667
1668 expect("input").toHaveProperty("selectionStart", 4);
1669 expect("input").toHaveProperty("selectionEnd", 4);
1670
1671 await press("left");
1672 await press("left");
1673 await press("right");
1674
1675 expect("input").toHaveProperty("selectionStart", 3);
1676 expect("input").toHaveProperty("selectionEnd", 3);
1677
1678 await press(["control", "a"]);
1679
1680 expect("input").toHaveProperty("selectionStart", 0);
1681 expect("input").toHaveProperty("selectionEnd", 5);
1682
1683 await press("right");
1684
1685 expect("input").toHaveProperty("selectionStart", 5);
1686 expect("input").toHaveProperty("selectionEnd", 5);
1687
1688 await press(["ctrl", "a"]);
1689 await press("down");
1690
1691 expect("input").toHaveProperty("selectionStart", 5);
1692 expect("input").toHaveProperty("selectionEnd", 5);
1693
1694 await press(["ctrl", "a"]);
1695 await press("left");
1696
1697 expect("input").toHaveProperty("selectionStart", 0);
1698 expect("input").toHaveProperty("selectionEnd", 0);
1699
1700 await press(["ctrl", "a"]);
1701 await press("up");
1702
1703 expect("input").toHaveProperty("selectionStart", 0);
1704 expect("input").toHaveProperty("selectionEnd", 0);
1705 });
1706
1707 test("insert character updates selection", async () => {
1708 await mountForTest(/* xml */ `<input value="abc" />`);
1709
1710 await click("input");
1711
1712 const input = queryOne("input");
1713 input.selectionStart = 0;
1714 input.selectionEnd = 3;
1715
1716 await press("d");
1717
1718 expect("input").toHaveValue("d");
1719 expect("input").toHaveProperty("selectionStart", 1);
1720 expect("input").toHaveProperty("selectionEnd", 1);
1721
1722 await press("f");
1723
1724 expect("input").toHaveValue("df");
1725 expect("input").toHaveProperty("selectionStart", 2);
1726 expect("input").toHaveProperty("selectionEnd", 2);
1727
1728 input.selectionStart = 1;
1729 input.selectionEnd = 1;
1730
1731 await press("e");
1732
1733 expect("input").toHaveValue("def");
1734 expect("input").toHaveProperty("selectionStart", 2);
1735 expect("input").toHaveProperty("selectionEnd", 2);
1736 });
1737
1738 test("press 'Enter' on form input", async () => {
1739 await mountForTest(/* xml */ `
1740 <form t-on-submit.prevent="">
1741 <input type="text" />
1742 </form>
1743 `);
1744 monitorEvents("form");
1745 monitorEvents("input");
1746
1747 expect("input").not.toBeFocused();
1748
1749 await press("Tab");
1750 await animationFrame();
1751
1752 expect("input").toBeFocused();
1753
1754 await press("Enter");
1755 await animationFrame();
1756
1757 expect.verifySteps([
1758 // Tab
1759 "focus@input",
1760 "focusin@input",
1761 "focusin@form",
1762 // Enter
1763 "keydown:Enter@input",
1764 "keydown:Enter@form",
1765 "submit@form",
1766 "keyup:Enter@input",
1767 "keyup:Enter@form",
1768 ]);
1769 });
1770
1771 test("press 'Enter' on form button", async () => {
1772 await mountForTest(/* xml */ `
1773 <form t-on-submit.prevent="">
1774 <button type="button" />
1775 </form>
1776 `);
1777 monitorEvents("form");
1778 monitorEvents("button");
1779
1780 expect("button").not.toBeFocused();
1781
1782 await press("Tab");
1783
1784 expect("button").toBeFocused();
1785
1786 await press("Enter");
1787
1788 expect.verifySteps([
1789 // Tab
1790 "focus@button",
1791 "focusin@button",
1792 "focusin@form",
1793 // Enter
1794 "keydown:Enter@button",
1795 "keydown:Enter@form",
1796 "click:0@button",
1797 "click:0@form",
1798 "keyup:Enter@button",
1799 "keyup:Enter@form",
1800 ]);
1801 });
1802
1803 test("press 'Enter' on form submit button", async () => {
1804 await mountForTest(/* xml */ `
1805 <form t-on-submit.prevent="">
1806 <button type="submit" />
1807 </form>
1808 `);
1809 monitorEvents("form");
1810 monitorEvents("button");
1811
1812 expect("button").not.toBeFocused();
1813
1814 await press("Tab");
1815
1816 expect("button").toBeFocused();
1817
1818 await press("Enter");
1819
1820 expect.verifySteps([
1821 // Tab
1822 "focus@button",
1823 "focusin@button",
1824 "focusin@form",
1825 // Enter
1826 "keydown:Enter@button",
1827 "keydown:Enter@form",
1828 "submit@form",
1829 "keyup:Enter@button",
1830 "keyup:Enter@form",
1831 ]);
1832 });
1833
1834 test("form submissions are redirected to mocked fetch", async () => {
1835 await mountForTest(/* xml */ `
1836 <form action="/submit/url" method="POST">
1837 <input type="hidden" name="csrf_token" value="CSRF_TOKEN_VALUE" />
1838 <input type="text" name="name" />
1839 <input type="number" name="experience" />
1840 <input type="file" name="picture" />
1841 </form>
1842 `);
1843
1844 mockFetch((url, { body, headers, method }) => {
1845 expect.step(new URL(url).pathname);
1846
1847 expect(method).toBe("POST");
1848 expect(headers).toEqual(new Headers([["Content-Type", "multipart/form-data"]]));
1849 expect(body).toBeInstanceOf(FormData);
1850 expect(body.get("csrf_token")).toBe("CSRF_TOKEN_VALUE");
1851 expect(body.get("name")).toBe("Pierre");
1852 expect(body.get("experience")).toBe("3");
1853 expect(body.get("picture").name).toBe("/picture_128.png");
1854 });
1855
1856 await click("[name=name]");
1857 await fill("Pierre");
1858
1859 await press("tab");
1860 await fill(3);
1861
1862 await press("tab");
1863 await setInputFiles(new File([], "/picture_128.png"));
1864
1865 expect.verifySteps([]);
1866
1867 monitorEvents("form");
1868
1869 // Trigger submit
1870 await press("enter");
1871
1872 expect.verifySteps([
1873 "keydown:Enter@form",
1874 "submit@form",
1875 "formdata@form",
1876 "/submit/url",
1877 "keyup:Enter@form",
1878 ]);
1879 });
1880
1881 test("press 'Space' on checkbox input", async () => {
1882 await mountForTest(/* xml */ `<input type="checkbox" checked="" />`);
1883
1884 expect("input").toHaveProperty("checked", true);
1885
1886 await uncheck("input"); // true -> false
1887
1888 expect("input").toHaveProperty("checked", false);
1889
1890 monitorEvents("input");
1891
1892 await press(" "); // false -> true
1893
1894 expect("input").toHaveProperty("checked", true);
1895 expect.verifySteps([
1896 // Key press
1897 "keydown: @input",
1898 "beforeinput: @input",
1899 "input: @input",
1900 "keyup: @input",
1901 // Click triggered by key press
1902 "click:0@input",
1903 "input@input",
1904 "change@input",
1905 ]);
1906 });
1907
1908 test("press 'Backspace' on number input", async () => {
1909 await mountForTest(/* xml */ `<input type="number" value="421" />`);
1910
1911 expect("input").toHaveValue(421);
1912
1913 await click("input");
1914 await press("Backspace");
1915
1916 expect("input").toHaveValue(42);
1917
1918 await press("Backspace");
1919
1920 expect("input").toHaveValue(4);
1921 });
1922
1923 test("press 'Enter' on textarea", async () => {
1924 await mountForTest(/* xml */ `<textarea t-att-value="'aaa'" />`);
1925
1926 expect("textarea").toHaveValue("aaa");
1927
1928 await click("textarea");
1929 await press("Enter");
1930
1931 expect("textarea").toHaveValue("aaa\n");
1932 });
1933
1934 test("special keys modifiers: Windows", async () => {
1935 mockUserAgent("windows");
1936
1937 await mountForTest(/* xml */ `<input />`);
1938
1939 await click("input");
1940
1941 monitorEvents("input");
1942
1943 await press("alt");
1944
1945 expect.verifySteps(["keydown:Alt.alt@input", "keyup:Alt.alt@input"]);
1946
1947 await press("ctrl");
1948
1949 expect.verifySteps(["keydown:Control.ctrl@input", "keyup:Control.ctrl@input"]);
1950
1951 await press("meta");
1952
1953 expect.verifySteps(["keydown:Meta.meta@input", "keyup:Meta.meta@input"]);
1954
1955 await press("shift");
1956
1957 expect.verifySteps(["keydown:Shift.shift@input", "keyup:Shift.shift@input"]);
1958 });
1959
1960 test("special keys modifiers: Mac", async () => {
1961 mockUserAgent("mac");
1962
1963 await mountForTest(/* xml */ `<input />`);
1964
1965 await click("input");
1966
1967 monitorEvents("input");
1968
1969 await press("alt");
1970
1971 expect.verifySteps(["keydown:Alt.alt@input", "keyup:Alt.alt@input"]);
1972
1973 await press("ctrl");
1974
1975 expect.verifySteps(["keydown:Control.ctrl@input", "keyup:Control.ctrl@input"]);
1976
1977 await press("meta");
1978
1979 expect.verifySteps(["keydown:Meta.meta@input", "keyup:Meta.meta@input"]);
1980
1981 await press("shift");
1982
1983 expect.verifySteps(["keydown:Shift.shift@input", "keyup:Shift.shift@input"]);
1984 });
1985
1986 test("compose shift, alt and control and a key", async () => {
1987 await mountForTest(/* xml */ `<input />`);
1988
1989 await click("input");
1990
1991 monitorEvents("input");
1992
1993 await press(["ctrl", "b"]);
1994
1995 expect.verifySteps([
1996 "keydown:Control.ctrl@input",
1997 "keydown:b.ctrl@input",
1998 "keyup:b.ctrl@input",
1999 "keyup:Control.ctrl@input",
2000 ]);
2001
2002 await press(["shift", "b"]);
2003
2004 expect.verifySteps([
2005 "keydown:Shift.shift@input",
2006 "keydown:b.shift@input",
2007 "beforeinput:B@input",
2008 "input:B@input",
2009 "keyup:b.shift@input",
2010 "keyup:Shift.shift@input",
2011 ]);
2012
2013 await press(["Alt", "Control", "b"]);
2014
2015 expect.verifySteps([
2016 "keydown:Alt.alt@input",
2017 "keydown:Control.alt.ctrl@input",
2018 "keydown:b.alt.ctrl@input",
2019 "keyup:b.alt.ctrl@input",
2020 "keyup:Control.alt.ctrl@input",
2021 "keyup:Alt.alt@input",
2022 ]);
2023 });
2024
2025 test("scroll", async () => {
2026 await mountForTest(/* xml */ `
2027 <div class="scrollable" style="height: 200px; width: 200px; overflow: auto;">
2028 <div style="height: 2000px; width: 2000px;"></div>
2029 </div>
2030 `);
2031
2032 monitorEvents(".scrollable");
2033
2034 await scroll(".scrollable", { top: 500 });
2035 await animationFrame();
2036
2037 expect(".scrollable").toHaveProperty("scrollTop", 500);
2038 expect(".scrollable").toHaveProperty("scrollLeft", 0);
2039 expect.verifySteps(["wheel:0@div", "scroll@div", "scrollend@div"]);
2040
2041 await scroll(".scrollable", { left: 1200 });
2042 await animationFrame();
2043
2044 expect(".scrollable").toHaveProperty("scrollTop", 500);
2045 expect(".scrollable").toHaveProperty("scrollLeft", 1200);
2046 expect.verifySteps(["wheel:0@div", "scroll@div", "scrollend@div"]);
2047 });
2048
2049 test("resize", async () => {
2050 await mountForTest(/* xml */ `
2051 <div class="resizable" style="height: 200px; width: 200px; overflow: auto;"/>
2052 `);
2053
2054 const { innerHeight } = window;
2055
2056 after(on(window, "resize", () => expect.step("resize@window")));
2057
2058 await resize({ width: 300 });
2059
2060 expect(window.innerWidth).toBe(300);
2061 expect(window.innerHeight).toBe(innerHeight);
2062
2063 expect.verifySteps(["resize@window"]);
2064
2065 await resize({ height: 264 });
2066
2067 expect(window.innerWidth).toBe(300);
2068 expect(window.innerHeight).toBe(264);
2069
2070 expect.verifySteps(["resize@window"]);
2071 });
2072
2073 test("select", async () => {
2074 await mountForTest(/* xml */ `
2075 <select>
2076 <option value="a">A</option>
2077 <option value="b">B</option>
2078 <option value="c">C</option>
2079 </select>
2080 `);
2081
2082 expect("select").toHaveValue("a"); // default to first option
2083 expect.verifySteps([]);
2084
2085 await click("select");
2086
2087 monitorEvents("select");
2088
2089 await select("b");
2090
2091 expect("select").toHaveValue("b");
2092 expect.verifySteps(["change@select"]);
2093 });
2094
2095 test("can trigger synthetic event handlers", async () => {
2096 await mountForTest(
2097 class extends Component {
2098 static props = {};
2099 static template = xml`
2100 <button t-on-click.synthetic="onClick">Click me</button>
2101 `;
2102
2103 onClick() {
2104 expect.step("click");
2105 }
2106 }
2107 );
2108
2109 await click("button");
2110
2111 expect.verifySteps(["click"]);
2112 });
2113
2114 test("synthetic event handlers are not cleaned up between tests", async () => {
2115 await mountForTest(
2116 class extends Component {
2117 static props = {};
2118 static template = xml`
2119 <button t-on-click.synthetic="onClick">Click me</button>
2120 `;
2121
2122 onClick() {
2123 expect.step("clack");
2124 }
2125 }
2126 );
2127
2128 await click("button");
2129
2130 expect.verifySteps(["clack"]);
2131 });
2132 });