*: hr, hr_holidays, im_livechat, mail, snailmail, test_mail, website_livechat, website_slides. This commit will remove the need to duplicate models definition for test purposes. Indeed, for now, we need to mock the model definitions on the client side. Those models definitions are often very different from the ones found on the server. This approach will allow us : - to ease model definitions during tests - to get closer from the real models task-2767820 closes odoo/odoo#84828 Related: odoo/enterprise#24486 Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
58 lines
1.9 KiB
JavaScript
58 lines
1.9 KiB
JavaScript
/** @odoo-module **/
|
|
|
|
import { registry } from '@web/core/registry';
|
|
|
|
const modelDefinitionsRegistry = registry.category('mail.model.definitions');
|
|
const customModelFieldsRegistry = modelDefinitionsRegistry.category('fieldsToInsert');
|
|
const recordsToInsertRegistry = modelDefinitionsRegistry.category('recordsToInsert');
|
|
const fakeModelsRegistry = modelDefinitionsRegistry.category('fakeModels');
|
|
/**
|
|
* Add models whose definitions need to be fetched on the server.
|
|
*
|
|
* @param {string[]} modelName
|
|
*/
|
|
export function addModelNamesToFetch(modelNames) {
|
|
if (!modelDefinitionsRegistry.contains('modelNamesToFetch')) {
|
|
modelDefinitionsRegistry.add('modelNamesToFetch', []);
|
|
}
|
|
modelDefinitionsRegistry.get('modelNamesToFetch').push(...modelNames);
|
|
}
|
|
|
|
/**
|
|
* Add models that will be added to the model definitions. We should
|
|
* avoid to rely on fake models and use real models instead.
|
|
*
|
|
* @param {string} modelName
|
|
* @param {Object} fields
|
|
*/
|
|
export function addFakeModel(modelName, fields) {
|
|
fakeModelsRegistry.add(modelName, fields);
|
|
}
|
|
|
|
/**
|
|
* Add model fields that are not present on the server side model's definitions
|
|
* but are required to ease testing or add default values for existing fields.
|
|
*
|
|
* @param {string} modelName
|
|
* @param {Object} fieldNamesToFields
|
|
*/
|
|
export function insertModelFields(modelName, fieldNamesToFields) {
|
|
const modelCustomFieldsRegistry = customModelFieldsRegistry.category(modelName);
|
|
for (const fname in fieldNamesToFields) {
|
|
modelCustomFieldsRegistry.add(fname, fieldNamesToFields[fname]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add records to the initial server data.
|
|
*
|
|
* @param {string} modelName
|
|
* @param {Object[]} records
|
|
*/
|
|
export function insertRecords(modelName, records) {
|
|
if (!recordsToInsertRegistry.contains(modelName)) {
|
|
recordsToInsertRegistry.add(modelName, []);
|
|
}
|
|
recordsToInsertRegistry.get(modelName).push(...records);
|
|
}
|