Files
odoo_source/addons/web/static/tests/views/helpers.js
T
Lucas Perais 6c155e1713 [FIX] web: relationalModel: basic keys supported in record context
Have an editable list view, displaying a many2many tags field. The m2m field
has a context, mentioning `active_id`. (`context="{'default_field': active_id}"`)

"basic context keys" is to be understood as the usual suspects: active_id, active_ids, current_company_id, active_model

Type something in the m2m input, and selet create and edit, in order to create a new record in a dialog form view.

Before this commit, there was a crash because active_id was not present in the evaluation context.

After this commit, the new record opens in its form view, with the right value for the field that must have a default.

closes odoo/odoo#96910

Signed-off-by: Lucas Perais (lpe) <lpe@odoo.com>
2022-08-04 18:35:32 +02:00

111 lines
4.0 KiB
JavaScript

/** @odoo-module **/
import { registry } from "@web/core/registry";
import { makeTestEnv } from "@web/../tests/helpers/mock_env";
import { getFixture, mount } from "@web/../tests/helpers/utils";
import { getDefaultConfig, View } from "@web/views/view";
import { MainComponentsContainer } from "@web/core/main_components_container";
import {
setupControlPanelFavoriteMenuRegistry,
setupControlPanelServiceRegistry,
} from "../search/helpers";
import { addLegacyMockEnvironment } from "../webclient/helpers";
import {
fakeCompanyService,
makeFakeLocalizationService,
makeFakeRouterService,
makeFakeUserService,
} from "../helpers/mock_services";
import { dialogService } from "@web/core/dialog/dialog_service";
import { popoverService } from "@web/core/popover/popover_service";
import { createDebugContext } from "@web/core/debug/debug_context";
const serviceRegistry = registry.category("services");
/**
* @typedef {{
* serverData: Object,
* mockRPC?: Function,
* type: string,
* resModel: string,
* [prop:string]: any
* }} MakeViewParams
*/
/**
* @param {MakeViewParams} params
* @returns {Component}
*/
export async function makeView(params) {
const props = { ...params };
const serverData = props.serverData;
const mockRPC = props.mockRPC;
const config = {
...getDefaultConfig(),
...props.config,
};
const legacyParams = props.legacyParams || {};
delete props.serverData;
delete props.mockRPC;
delete props.legacyParams;
delete props.config;
if (props.arch) {
serverData.views = serverData.views || {};
props.viewId = params.viewId || 100000001; // hopefully will not conflict with an id already in views
serverData.views[`${props.resModel},${props.viewId},${props.type}`] = props.arch;
delete props.arch;
props.searchViewId = 100000002; // hopefully will not conflict with an id already in views
const searchViewArch = props.searchViewArch || "<search/>";
serverData.views[`${props.resModel},${props.searchViewId},search`] = searchViewArch;
delete props.searchViewArch;
}
const env = await makeTestEnv({ serverData, mockRPC });
Object.assign(env, createDebugContext(env)); // This is needed if the views are in debug mode
/** Legacy Environment, for compatibility sakes
* Remove this as soon as we drop the legacy support
*/
const models = params.serverData.models;
if (legacyParams && legacyParams.withLegacyMockServer && models) {
legacyParams.models = Object.assign({}, 0);
// In lagacy, data may not be sole models, but can contain some other variables
// So we filter them out for our WOWL mockServer
Object.entries(legacyParams.models).forEach(([k, v]) => {
if (!(v instanceof Object) || !("fields" in v)) {
delete models[k];
}
});
}
await addLegacyMockEnvironment(env, legacyParams);
const target = getFixture();
const viewEnv = Object.assign(Object.create(env), { config });
const view = await mount(View, target, { env: viewEnv, props });
await mount(MainComponentsContainer, target, { env, props });
const viewNode = view.__owl__;
const withSearchNode = Object.values(viewNode.children)[0];
const concreteViewNode = Object.values(withSearchNode.children)[0];
const concreteView = concreteViewNode.component;
return concreteView;
}
export function setupViewRegistries() {
setupControlPanelFavoriteMenuRegistry();
setupControlPanelServiceRegistry();
serviceRegistry.add(
"user",
makeFakeUserService((group) => group === "base.group_allow_export"),
{ force: true }
);
serviceRegistry.add("router", makeFakeRouterService(), { force: true });
serviceRegistry.add("localization", makeFakeLocalizationService()), { force: true };
serviceRegistry.add("dialog", dialogService), { force: true };
serviceRegistry.add("popover", popoverService), { force: true };
serviceRegistry.add("company", fakeCompanyService);
}