*: mail, mrp Previously, to prevent images and iframes from making GET requests in tests, we would listen for the DOMNodeInserted event. This event is deprecated and should not be used. Additionally, we weren't listening for attiribute changes for performance reasons, which would result in some GET requests still being made when an src attribute was changed. This commit changes the mechanism that is used to do this from mutation events to a MutationObserver which is not deprecated, and now also listens for attribute modifications as MutationObservers can be configured to only listen for changes to certain attributes, greatly alleviating the performance impact of listening for those. This mechanism has also been enabled globally by doing it once in the test setup code, instead of requiring one to call a specific helper, as we never want to make real requests during tests. Some tests have been adapted to this new behaviour. closes odoo/odoo#85216 Related: odoo/enterprise#24665 Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
167 lines
5.8 KiB
JavaScript
167 lines
5.8 KiB
JavaScript
/** @odoo-module **/
|
|
|
|
import { browser } from "@web/core/browser/browser";
|
|
import { ormService } from "@web/core/orm_service";
|
|
import { registry } from "@web/core/registry";
|
|
import { uiService } from "@web/core/ui/ui_service";
|
|
import { hotkeyService } from "@web/core/hotkeys/hotkey_service";
|
|
import { UserMenu } from "@web/webclient/user_menu/user_menu";
|
|
import { preferencesItem } from "@web/webclient/user_menu/user_menu_items";
|
|
import { userService } from "@web/core/user_service";
|
|
import { makeTestEnv } from "@web/../tests/helpers/mock_env";
|
|
import { makeFakeLocalizationService } from "../helpers/mock_services";
|
|
import { click, getFixture, mount, patchWithCleanup } from "@web/../tests/helpers/utils";
|
|
import { session } from "@web/session";
|
|
|
|
const serviceRegistry = registry.category("services");
|
|
const userMenuRegistry = registry.category("user_menuitems");
|
|
let target;
|
|
let env;
|
|
let userMenu;
|
|
|
|
QUnit.module("UserMenu", {
|
|
async beforeEach() {
|
|
patchWithCleanup(session, { name: "Sauron" });
|
|
patchWithCleanup(browser, {
|
|
location: {
|
|
origin: "http://lordofthering",
|
|
},
|
|
});
|
|
serviceRegistry.add("user", userService);
|
|
serviceRegistry.add("hotkey", hotkeyService);
|
|
serviceRegistry.add("ui", uiService);
|
|
target = getFixture();
|
|
},
|
|
});
|
|
|
|
QUnit.test("can be rendered", async (assert) => {
|
|
env = await makeTestEnv();
|
|
userMenuRegistry.add("bad_item", function () {
|
|
return {
|
|
type: "item",
|
|
id: "bad",
|
|
description: "Bad",
|
|
callback: () => {
|
|
assert.step("callback bad_item");
|
|
},
|
|
sequence: 10,
|
|
};
|
|
});
|
|
userMenuRegistry.add("ring_item", function () {
|
|
return {
|
|
type: "item",
|
|
id: "ring",
|
|
description: "Ring",
|
|
callback: () => {
|
|
assert.step("callback ring_item");
|
|
},
|
|
sequence: 5,
|
|
};
|
|
});
|
|
userMenuRegistry.add("separator", function () {
|
|
return {
|
|
type: "separator",
|
|
sequence: 15,
|
|
};
|
|
});
|
|
userMenuRegistry.add("invisible_item", function () {
|
|
return {
|
|
type: "item",
|
|
id: "hidden",
|
|
description: "Hidden Power",
|
|
callback: () => {},
|
|
sequence: 5,
|
|
hide: true,
|
|
};
|
|
});
|
|
userMenuRegistry.add("eye_item", function () {
|
|
return {
|
|
type: "item",
|
|
id: "eye",
|
|
description: "Eye",
|
|
callback: () => {
|
|
assert.step("callback eye_item");
|
|
},
|
|
};
|
|
});
|
|
userMenu = await mount(UserMenu, target, { env });
|
|
let userMenuEl = userMenu.el;
|
|
assert.containsOnce(userMenuEl, "img.o_user_avatar");
|
|
assert.strictEqual(
|
|
userMenuEl.querySelector("img.o_user_avatar").dataset.src,
|
|
"http://lordofthering/web/image?model=res.users&field=avatar_128&id=7"
|
|
);
|
|
assert.containsOnce(userMenuEl, "span.oe_topbar_name");
|
|
assert.strictEqual(userMenuEl.querySelector(".oe_topbar_name").textContent, "Sauron");
|
|
assert.containsNone(userMenuEl, ".dropdown-menu .dropdown-item");
|
|
await click(userMenu.el.querySelector("button.dropdown-toggle"));
|
|
userMenuEl = userMenu.el;
|
|
assert.containsN(userMenuEl, ".dropdown-menu .dropdown-item", 3);
|
|
assert.containsOnce(userMenuEl, "div.dropdown-divider");
|
|
const children = [...(userMenuEl.querySelector(".dropdown-menu").children || [])];
|
|
assert.deepEqual(
|
|
children.map((el) => el.tagName),
|
|
["SPAN", "SPAN", "DIV", "SPAN"]
|
|
);
|
|
const items = [...userMenuEl.querySelectorAll(".dropdown-menu .dropdown-item")] || [];
|
|
assert.deepEqual(
|
|
items.map((el) => el.dataset.menu),
|
|
["ring", "bad", "eye"]
|
|
);
|
|
assert.deepEqual(
|
|
items.map((el) => el.textContent),
|
|
["Ring", "Bad", "Eye"]
|
|
);
|
|
for (const item of items) {
|
|
click(item);
|
|
}
|
|
assert.verifySteps(["callback ring_item", "callback bad_item", "callback eye_item"]);
|
|
});
|
|
|
|
QUnit.test("display the correct name in debug mode", async (assert) => {
|
|
patchWithCleanup(odoo, { debug: "1" });
|
|
env = await makeTestEnv();
|
|
userMenu = await mount(UserMenu, target, { env });
|
|
let userMenuEl = userMenu.el;
|
|
assert.containsOnce(userMenuEl, "img.o_user_avatar");
|
|
assert.containsOnce(userMenuEl, "span.oe_topbar_name");
|
|
assert.strictEqual(userMenuEl.querySelector(".oe_topbar_name").textContent, "Sauron (test)");
|
|
});
|
|
|
|
QUnit.test("can execute the callback of settings", async (assert) => {
|
|
const mockRPC = (route) => {
|
|
if (route === "/web/dataset/call_kw/res.users/action_get") {
|
|
return Promise.resolve({
|
|
name: "Change My Preferences",
|
|
res_id: 0,
|
|
});
|
|
}
|
|
};
|
|
const testConfig = { mockRPC };
|
|
serviceRegistry.add("localization", makeFakeLocalizationService());
|
|
serviceRegistry.add("orm", ormService);
|
|
const fakeActionService = {
|
|
name: "action",
|
|
start() {
|
|
return {
|
|
doAction(actionId) {
|
|
assert.step("" + actionId.res_id);
|
|
assert.step(actionId.name);
|
|
return Promise.resolve(true);
|
|
},
|
|
};
|
|
},
|
|
};
|
|
serviceRegistry.add("action", fakeActionService, { force: true });
|
|
|
|
env = await makeTestEnv(testConfig);
|
|
userMenuRegistry.add("profile", preferencesItem);
|
|
userMenu = await mount(UserMenu, target, { env });
|
|
await click(userMenu.el.querySelector("button.dropdown-toggle"));
|
|
assert.containsOnce(userMenu.el, ".dropdown-menu .dropdown-item");
|
|
const item = userMenu.el.querySelector(".dropdown-menu .dropdown-item");
|
|
assert.strictEqual(item.textContent, "Preferences");
|
|
await click(item);
|
|
assert.verifySteps(["7", "Change My Preferences"]);
|
|
});
|