import { before, beforeEach, describe, expect, test } from "@odoo/hoot"; import { animationFrame, click, dblclick, queryAll, queryFirst, queryOne, freezeTime, } from "@odoo/hoot-dom"; import { advanceTime, Deferred } from "@odoo/hoot-mock"; import { Component, onWillDestroy, markup, xml } from "@odoo/owl"; import { clearRegistry, patchWithCleanup } from "@web/../tests/web_test_helpers"; import { registry } from "@web/core/registry"; import { patch } from "@web/core/utils/patch"; import { Colibri } from "@web/public/colibri"; import { Interaction } from "@web/public/interaction"; import { patchDynamicContent } from "@web/public/utils"; import { startInteraction, startInteractions } from "./helpers"; describe.current.tags("interaction_dev"); const TemplateBase = `
coucou
`; const TemplateTest = `
coucou
`; const TemplateTestDoubleSpan = `
span1 span2
`; const TemplateTestDoubleButton = `
`; const getTemplateWithAttribute = function (attribute) { return `
coucou
`; }; function installProtect() { patchWithCleanup(Colibri.prototype, { updateContent() { expect.step("updateContent"); super.updateContent(); }, protectSyncAfterAsync(interaction, name, fn) { fn = super.protectSyncAfterAsync(interaction, name, fn); return (...args) => { expect.step("protect"); fn(...args); expect.step("unprotect"); }; }, }); } describe("adding listeners", () => { test("can add a listener on a single element", async () => { let clicked = 0; class Test extends Interaction { static selector = ".test"; dynamicContent = { span: { "t-on-click": () => clicked++ }, }; } await startInteraction(Test, TemplateTest); expect(clicked).toBe(0); await click("span"); expect(clicked).toBe(1); }); test("can add a listener on multiple elements", async () => { let clicked = 0; class Test extends Interaction { static selector = ".test"; dynamicContent = { span: { "t-on-click": () => clicked++ }, }; } await startInteraction(Test, TemplateTestDoubleSpan); expect(clicked).toBe(0); const spans = queryAll("span"); await click(spans[0]); await click(spans[1]); expect(clicked).toBe(2); }); test.tags("desktop"); test("can add multiple listeners on an element", async () => { let clicked = 0; class Test extends Interaction { static selector = ".test"; dynamicContent = { span: { "t-on-click": () => clicked++, "t-on-dblclick": () => clicked++, }, }; } await startInteraction(Test, TemplateTest); expect(clicked).toBe(0); await dblclick("span"); expect(clicked).toBe(3); // event dblclick = click + click + dblclick }); test("can use addListener on HTMLCollection", async () => { let clicked = 0; class Test extends Interaction { static selector = ".test"; start() { this.addListener(this.el.querySelectorAll("span"), "click", () => clicked++); } } await startInteraction(Test, TemplateTestDoubleSpan); expect(clicked).toBe(0); const spans = queryAll("span"); await click(spans[0]); await click(spans[1]); expect(clicked).toBe(2); }); test("listener is added between willstart and start", async () => { class Test extends Interaction { static selector = ".test"; dynamicContent = { span: { "t-on-click": () => expect.step("click") }, }; setup() { expect.step("setup"); } async willStart() { await click("span"); expect.step("willStart"); } start() { expect.step("start"); } } await startInteraction(Test, TemplateTest); await click("span"); expect.verifySteps(["setup", "willStart", "start", "click"]); }); test("listener is added on iframe single element", async () => { class Test extends Interaction { static selector = "iframe"; start() { const spanEl = this.el.contentDocument.createElement("span"); spanEl.textContent = "abc"; this.el.contentDocument.body.appendChild(spanEl); this.addListener(spanEl, "click", () => expect.step("click")); spanEl.click(); } } await startInteraction(Test, `