[IMP] web: improve test helpers to mock timeouts
Part-of: odoo/odoo#88994
This commit is contained in:
@@ -49,7 +49,7 @@ QUnit.module("Effect Service", (hooks) => {
|
||||
message: markup("<div>Congrats!</div>"),
|
||||
};
|
||||
|
||||
execRegisteredTimeouts = mockTimeout();
|
||||
execRegisteredTimeouts = mockTimeout().execRegisteredTimeouts;
|
||||
patchWithCleanup(session, { show_effect: true }); // enable effects
|
||||
|
||||
serviceRegistry.add("user", userService);
|
||||
|
||||
@@ -20,7 +20,7 @@ QUnit.test("useTransition hook", async (assert) => {
|
||||
}
|
||||
}
|
||||
Parent.template = xml`<div t-if="transition.shouldMount" t-att-class="transition.className"/>`;
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
|
||||
const target = getFixture();
|
||||
const parent = await mount(Parent, target);
|
||||
@@ -58,7 +58,7 @@ QUnit.test("Transition HOC", async (assert) => {
|
||||
</Transition>
|
||||
`;
|
||||
Parent.components = { Transition };
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
|
||||
const target = getFixture();
|
||||
const parent = await mount(Parent, target);
|
||||
|
||||
@@ -69,7 +69,7 @@ QUnit.module("utils", () => {
|
||||
});
|
||||
|
||||
QUnit.test("debounce with immediate", async function (assert) {
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
const myFunc = () => {
|
||||
assert.step("myFunc");
|
||||
return 42;
|
||||
@@ -100,7 +100,7 @@ QUnit.module("utils", () => {
|
||||
|
||||
QUnit.test("debounced call can be cancelled", async function (assert) {
|
||||
assert.expect(3);
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
const myFunc = () => {
|
||||
assert.step("myFunc");
|
||||
};
|
||||
|
||||
@@ -360,21 +360,38 @@ for (const propName of Object.keys(window.console)) {
|
||||
|
||||
export function mockTimeout() {
|
||||
const timeouts = new Map();
|
||||
let currentTime = 0;
|
||||
let id = 1;
|
||||
patchWithCleanup(browser, {
|
||||
setTimeout(fn) {
|
||||
timeouts.set(id, fn);
|
||||
setTimeout(fn, delay = 0) {
|
||||
timeouts.set(id, { fn, scheduledFor: delay + currentTime, id });
|
||||
return id++;
|
||||
},
|
||||
clearTimeout(id) {
|
||||
timeouts.delete(id);
|
||||
},
|
||||
});
|
||||
return function execRegisteredTimeouts() {
|
||||
for (const fn of timeouts.values()) {
|
||||
fn();
|
||||
}
|
||||
timeouts.clear();
|
||||
return {
|
||||
execRegisteredTimeouts() {
|
||||
for (const { fn } of timeouts.values()) {
|
||||
fn();
|
||||
}
|
||||
timeouts.clear();
|
||||
},
|
||||
async advanceTime(duration) {
|
||||
// wait here so all microtasktick scheduled in this frame can be
|
||||
// executed and possibly register their own timeout
|
||||
await nextTick();
|
||||
currentTime += duration;
|
||||
for (const { fn, scheduledFor, id } of timeouts.values()) {
|
||||
if (scheduledFor <= currentTime) {
|
||||
fn();
|
||||
timeouts.delete(id);
|
||||
}
|
||||
}
|
||||
// wait here to make sure owl can update the UI
|
||||
await nextTick();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +151,7 @@ QUnit.test("loading indicator blocks UI", async (assert) => {
|
||||
|
||||
QUnit.test("loading indicator doesn't unblock ui if it didn't block it", async (assert) => {
|
||||
const env = await makeTestEnv({ ...baseConfig });
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
const ui = env.services.ui;
|
||||
ui.bus.on("BLOCK", null, () => {
|
||||
assert.step("block");
|
||||
|
||||
@@ -427,7 +427,7 @@ QUnit.test("'more' menu sections properly updated on app change", async (assert)
|
||||
QUnit.test("Do not execute adapt when navbar is destroyed", async (assert) => {
|
||||
assert.expect(5);
|
||||
|
||||
const execRegisteredTimeouts = mockTimeout();
|
||||
const { execRegisteredTimeouts } = mockTimeout();
|
||||
class MyNavbar extends NavBar {
|
||||
async adapt() {
|
||||
assert.step("adapt NavBar");
|
||||
|
||||
Reference in New Issue
Block a user