Files
odoo_source/addons/web/static/tests/helpers/test_env.js
T
Julien MougenotandMathieu Duckerts-Antoine 0a129284e4 [IMP] web,*: Enhance test helpers
1. Test utils helpers adapted to properly instantiate the new Owl control panel
2. New file 'test_utils_control_panel' to create a control panel and use helpers
3. New helper: createComponent (creates a generic component, see jsdoc)
4. Use of the new helpers in the current tests (more in sibling commits)
5. Helper 'addMockEnvironment' is now asynchronous
6. Owl config mode set to 'dev' in test suite (props validation)

Part of task 2196029

Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
2020-03-13 11:39:53 +00:00

83 lines
3.0 KiB
JavaScript

odoo.define('web.test_env', async function (require) {
"use strict";
const AbstractStorageService = require('web.AbstractStorageService');
const Bus = require("web.Bus");
const RamStorage = require('web.RamStorage');
const { buildQuery } = require("web.rpc");
const session = require('web.session');
/**
* Creates a test environment with the given environment object.
* Any access to a key that has not been explicitly defined in the given environment object
* will result in an error.
*
* @param {Object} [env={}]
* @param {Function} [providedRPC=null]
* @returns {Proxy}
*/
function makeTestEnvironment(env = {}, providedRPC = null) {
const RamStorageService = AbstractStorageService.extend({
storage: new RamStorage(),
});
const database = {
parameters: {
code: "en_US",
date_format: '%m/%d/%Y',
decimal_point: ".",
direction: 'ltr',
grouping: [],
thousands_sep: ",",
time_format: '%H:%M:%S',
},
};
let testEnv = {};
const defaultEnv = {
_t: env._t || Object.assign((s => s), { database }),
_lt: env._lt || Object.assign((s => s), { database }),
bus: new Bus(),
device: Object.assign({ isMobile: false }, env.device),
isDebug: env.isDebug || (() => false),
qweb: new owl.QWeb({ templates: session.owlTemplates }),
services: Object.assign({
ajax: { // for legacy subwidgets
rpc() {
const prom = testEnv.session.rpc(...arguments);
prom.abort = function () {
throw new Error("Can't abort this request");
};
return prom;
},
},
getCookie() { },
rpc(params, options) {
const query = buildQuery(params);
return testEnv.session.rpc(query.route, query.params, options);
},
local_storage: new RamStorageService(),
session_storage: new RamStorageService(),
notification: { notify() { } },
}, env.services),
session: Object.assign({
rpc(route, params, options) {
if (providedRPC) {
return providedRPC(route, params, options);
}
throw new Error(`No method to perform RPC`);
},
}, env.session),
};
testEnv = Object.assign(env, defaultEnv);
return testEnv;
}
/**
* Before each test, we want owl.Component.env to be a fresh test environment.
*/
QUnit.on('OdooBeforeTestHook', function () {
owl.Component.env = makeTestEnvironment();
});
return makeTestEnvironment;
});