From 91a8b4a087a30fcd18e2317d79126c1ad49773c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 14 Nov 2024 11:00:46 +0100 Subject: [PATCH] add test, add waitForTimeout helper, small cleanup --- addons/website/static/src/core/colibri.js | 2 ++ addons/website/static/src/core/interaction.js | 9 +++++++++ .../static/tests/core/website_core.test.js | 20 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/addons/website/static/src/core/colibri.js b/addons/website/static/src/core/colibri.js index aee99b458249f..299d06ced6278 100644 --- a/addons/website/static/src/core/colibri.js +++ b/addons/website/static/src/core/colibri.js @@ -92,9 +92,11 @@ export class Colibri { for (let cleanup of this.cleanups.reverse()) { cleanup(); } + this.cleanups = []; for (let [el, ev, fn, options] of this.handlers) { el.removeEventListener(ev, fn, options); } + this.handlers = []; this.classMap.clear(); this.interaction.destroy(); this.interaction.isDestroyed = true; diff --git a/addons/website/static/src/core/interaction.js b/addons/website/static/src/core/interaction.js index f96c55c0136f7..03f8eed4cde3c 100644 --- a/addons/website/static/src/core/interaction.js +++ b/addons/website/static/src/core/interaction.js @@ -38,6 +38,15 @@ export class Interaction { }); } + waitForTimeout(fn, delay) { + setTimeout(() => { + if (!this.isDestroyed) { + fn(); + this.updateDOM(); + } + }, delay) + } + updateDOM() { this.__colibri__.scheduleUpdate(); } diff --git a/addons/website/static/tests/core/website_core.test.js b/addons/website/static/tests/core/website_core.test.js index 47f1f21d3bbc1..3bc3e6b78e8f9 100644 --- a/addons/website/static/tests/core/website_core.test.js +++ b/addons/website/static/tests/core/website_core.test.js @@ -29,3 +29,23 @@ test("wait for translation before starting interactions", async () => { await animationFrame(); expect(started).toBe(true); }); + + +test("starting interactions twice should only actually do it once", async () => { + let n = 0; + class Test extends Interaction { + static selector = ".test"; + + setup() { + n++; + } + } + + const { core } = await startInteraction(Test, `
`); + + expect(n).toBe(1); + core.startInteractions(); + await animationFrame(); + expect(n).toBe(1); +}); +