From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- .../static/tests/unit/helpers/test_env.js | 46 ++++++++++++++++++++++ .../static/tests/unit/helpers/test_main.js | 23 +++++++++++ 2 files changed, 69 insertions(+) create mode 100644 addons/point_of_sale/static/tests/unit/helpers/test_env.js create mode 100644 addons/point_of_sale/static/tests/unit/helpers/test_main.js (limited to 'addons/point_of_sale/static/tests/unit/helpers') diff --git a/addons/point_of_sale/static/tests/unit/helpers/test_env.js b/addons/point_of_sale/static/tests/unit/helpers/test_env.js new file mode 100644 index 00000000..c4b0b3ec --- /dev/null +++ b/addons/point_of_sale/static/tests/unit/helpers/test_env.js @@ -0,0 +1,46 @@ +odoo.define('point_of_sale.test_env', async function (require) { + 'use strict'; + + /** + * Many components in PoS are dependent on the PosModel instance (pos). + * Therefore, for unit tests that require pos in the Components' env, we + * prepared here a test env maker (makePosTestEnv) based on + * makeTestEnvironment of web. + */ + + const makeTestEnvironment = require('web.test_env'); + const env = require('web.env'); + const models = require('point_of_sale.models'); + const Registries = require('point_of_sale.Registries'); + + Registries.Component.add(owl.misc.Portal); + + await env.session.is_bound; + const pos = new models.PosModel({ + rpc: env.services.rpc, + session: env.session, + do_action: async () => {}, + setLoadingMessage: () => {}, + setLoadingProgress: () => {}, + showLoadingSkip: () => {}, + }); + await pos.ready; + + /** + * @param {Object} env default env + * @param {Function} providedRPC mock rpc + * @param {Function} providedDoAction mock do_action + */ + function makePosTestEnv(env = {}, providedRPC = null, providedDoAction = null) { + env = Object.assign(env, { pos }); + let posEnv = makeTestEnvironment(env, providedRPC); + // Replace rpc in the PosModel instance after loading + // data from the server so that every succeeding rpc calls + // made by pos are mocked by the providedRPC. + pos.rpc = posEnv.rpc; + pos.do_action = providedDoAction; + return posEnv; + } + + return makePosTestEnv; +}); diff --git a/addons/point_of_sale/static/tests/unit/helpers/test_main.js b/addons/point_of_sale/static/tests/unit/helpers/test_main.js new file mode 100644 index 00000000..f42e01cb --- /dev/null +++ b/addons/point_of_sale/static/tests/unit/helpers/test_main.js @@ -0,0 +1,23 @@ +odoo.define('web.web_client', function (require) { + // this module is required by the test + const { bus } = require('web.core'); + const WebClient = require('web.AbstractWebClient'); + + // listen to unhandled rejected promises, and when the rejection is not due + // to a crash, prevent the browser from displaying an 'unhandledrejection' + // error in the console, which would make tests crash on each Promise.reject() + // something similar is done by the CrashManagerService, but by default, it + // isn't deployed in tests + bus.on('crash_manager_unhandledrejection', this, function (ev) { + if (!ev.reason || !(ev.reason instanceof Error)) { + ev.stopPropagation(); + ev.stopImmediatePropagation(); + ev.preventDefault(); + } + }); + + owl.config.mode = "dev"; + + const webClient = new WebClient(); + return webClient; +}); -- cgit v1.2.3