[REF] web: tests: makeView: let View do the "get_views"

Before this commit, the makeView test helper processed the given
arch and generated the fields to pass to the View component. This
led us to do a similar work as what is done in the view service,
i.e. duplicate code, or factorize/reuse code.

An alternative is to let the View component fetch whatever it needs
(view arch, fields, search view...). This is the purpose of this
commit. That way, the code of the view service is executed (and
thus tested as well) in each test doing a "makeView".

This commit makes that change, and adapts some tests accordingly
(mainly, tests doing assertions in mockRPC, since we do a get_view
in every test).

Part-of: odoo/odoo#87522
This commit is contained in:
Aaron Bohy
2022-04-29 09:57:45 +02:00
committed by Denis Ledoux
parent 89112f8ecf
commit 45119056ee
3 changed files with 401 additions and 377 deletions
+10 -21
View File
@@ -3,7 +3,6 @@
import { makeTestEnv } from "@web/../tests/helpers/mock_env";
import { getFixture, mount } from "@web/../tests/helpers/utils";
import { getDefaultConfig, View } from "@web/views/view";
import { _getView } from "../helpers/mock_server";
import { addLegacyMockEnvironment } from "../webclient/helpers";
/**
@@ -35,29 +34,19 @@ export const makeView = async (params) => {
delete props.legacyParams;
delete props.config;
const env = await makeTestEnv({ serverData, mockRPC, config });
if (props.arch) {
const defaultFields = serverData.models[props.resModel].fields;
if (!props.fields) {
props.fields = Object.assign({}, defaultFields);
// write the field name inside the field description (as done by fields_get)
for (const fieldName in props.fields) {
props.fields[fieldName].name = fieldName;
}
}
const view = _getView({
arch: props.arch,
modelName: props.resModel,
fields: props.fields,
context: props.context || {},
models: serverData.models,
});
props.arch = view.arch;
props.searchViewArch = props.searchViewArch || "<search/>";
props.searchViewFields = props.searchViewFields || Object.assign({}, props.fields);
serverData.views = serverData.views || {};
props.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, config });
/** Legacy Environment, for compatibility sakes
* Remove this as soon as we drop the legacy support
*/
@@ -211,11 +211,13 @@ QUnit.module("Views", (hooks) => {
<field name="foo" type="measure"/>
</pivot>`,
mockRPC(route, args) {
assert.strictEqual(
args.kwargs.lazy,
false,
"the read_group should be done with the lazy=false option"
);
if (args.method === "read_group") {
assert.strictEqual(
args.kwargs.lazy,
false,
"the read_group should be done with the lazy=false option"
);
}
},
});
@@ -404,6 +406,11 @@ QUnit.module("Views", (hooks) => {
QUnit.test("clicking on a cell triggers a doAction", async function (assert) {
assert.expect(2);
serverData.views = {
"partner,2,form": "<form/>",
"partner,false,list": "<list/>",
"partner,5,kanban": "<kanban/>",
};
patchWithCleanup(session, {
user_context: { userContextKey: true },
});
@@ -748,14 +755,16 @@ QUnit.module("Views", (hooks) => {
<field name="date" interval="month" type="col"/>
<field name="foo" type="measure"/>
</pivot>`,
mockRPC(route, params) {
const wrongFields = params.kwargs.fields.filter((field) => {
return !(field.split(":")[0] in serverData.models.partner.fields);
});
assert.ok(
!wrongFields.length,
"fields given to read_group should exist on the model"
);
mockRPC(route, args) {
if (args.method === "read_group") {
const wrongFields = args.kwargs.fields.filter((field) => {
return !(field.split(":")[0] in serverData.models.partner.fields);
});
assert.ok(
!wrongFields.length,
"fields given to read_group should exist on the model"
);
}
},
});
});
@@ -850,7 +859,6 @@ QUnit.module("Views", (hooks) => {
assert.expect(7);
let rpcCount = 0;
await makeView({
type: "pivot",
resModel: "partner",
@@ -860,8 +868,10 @@ QUnit.module("Views", (hooks) => {
<field name="product_id" type="row"/>
<field name="foo" type="measure"/>
</pivot>`,
mockRPC() {
rpcCount++;
mockRPC(route, args) {
if (args.method === "read_group") {
rpcCount++;
}
},
});
assert.containsN(
@@ -4571,23 +4581,25 @@ QUnit.module("Views", (hooks) => {
<field name="foo" type="measure"/>
</pivot>`,
mockRPC(route, args) {
if (args.method === "read_group" && isSecondReadGroup) {
return Promise.resolve([
{
customer: [2, "Second"],
foo: 18,
__count: 2,
__domain: [["customer", "=", 2]],
},
{
customer: [1, "First"],
foo: 14,
__count: 2,
__domain: [["customer", "=", 1]],
},
]);
if (args.method === "read_group") {
if (isSecondReadGroup) {
return Promise.resolve([
{
customer: [2, "Second"],
foo: 18,
__count: 2,
__domain: [["customer", "=", 2]],
},
{
customer: [1, "First"],
foo: 14,
__count: 2,
__domain: [["customer", "=", 1]],
},
]);
}
isSecondReadGroup = true;
}
isSecondReadGroup = true;
},
});
File diff suppressed because it is too large Load Diff