diff options
| author | Vincent Schippefilt <vsc@odoo.com> | 2019-03-06 14:02:32 +0100 |
|---|---|---|
| committer | Aaron Bohy <aab@odoo.com> | 2019-03-06 20:07:17 +0100 |
| commit | ab56e637b7f97ddcdbcc9baea99ae097e342409c (patch) | |
| tree | 4cb47da9f7ef89b19d18429220a7507470ec12a9 /addons/web/static/tests/helpers/test_utils_create.js | |
| parent | eeed894fd50e477135e1ac9a82f6e98ce063ab90 (diff) | |
[REF] web: adapt code after jQuery update
Part of task 1896658
Co-authored-by: Aaron Bohy <aab@odoo.com>
Co-authored-by: Christophe Matthieu <chm@odoo.com>
Co-authored-by: Mathieu Duckerts-Antoine <dam@odoo.com>
Co-authored-by: David Monjoie <dmo@odoo.com>
Co-authored-by: Martin Geubelle <mge@odoo.com>
Co-authored-by: svs-odoo <svs@odoo.com>
Co-authored-by: Vincent Schippefilt <vsc@odoo.com>
Diffstat (limited to 'addons/web/static/tests/helpers/test_utils_create.js')
| -rw-r--r-- | addons/web/static/tests/helpers/test_utils_create.js | 94 |
1 files changed, 51 insertions, 43 deletions
diff --git a/addons/web/static/tests/helpers/test_utils_create.js b/addons/web/static/tests/helpers/test_utils_create.js index 5a64512260e..7e4de1060bc 100644 --- a/addons/web/static/tests/helpers/test_utils_create.js +++ b/addons/web/static/tests/helpers/test_utils_create.js @@ -13,6 +13,7 @@ odoo.define('web.test_utils_create', function (require) { var ActionManager = require('web.ActionManager'); var config = require('web.config'); var ControlPanelView = require('web.ControlPanelView'); +var concurrency = require('web.concurrency'); var DebugManager = require('web.DebugManager'); var dom = require('web.dom'); var testUtilsMock = require('web.test_utils_mock'); @@ -30,7 +31,7 @@ var Widget = require('web.Widget'); * @param {function} [params.mockRPC] * @returns {ActionManager} */ -function createActionManager (params) { +var createActionManager = async function (params) { params = params || {}; var $target = $('#qunit-fixture'); if (params.debug) { @@ -47,7 +48,7 @@ function createActionManager (params) { _.extend(params, { mockRPC: function (route, args) { if (args.model === 'ir.attachment') { - return $.when([]); + return Promise.resolve([]); } if (mockRPC) { return mockRPC.apply(this, arguments); @@ -56,7 +57,7 @@ function createActionManager (params) { }, }); testUtilsMock.addMockEnvironment(widget, _.defaults(params, { debounce: false })); - widget.prependTo($target); + await widget.prependTo($target); widget.$el.addClass('o_web_client'); if (config.device.isMobile) { widget.$el.addClass('o_touch_device'); @@ -71,14 +72,14 @@ function createActionManager (params) { widget.destroy(); }; var fragment = document.createDocumentFragment(); - actionManager.appendTo(fragment).then(function () { + return actionManager.appendTo(fragment).then(function () { dom.append(widget.$el, fragment, { callbacks: [{ widget: actionManager }], in_DOM: true, }); + return actionManager; }); - return actionManager; -} +}; /** * create a view from various parameters. Here, a view means a javascript @@ -89,10 +90,6 @@ function createActionManager (params) { * through a mock method using the data object as source, and already loaded/ * started. * - * Most views can be tested synchronously (@see createView), but some view have - * external dependencies (like lazy loaded libraries). In that case, it is - * necessary to use this method. - * * @param {Object} params * @param {string} params.arch the xml (arch) of the view to be instantiated * @param {any[]} [params.domain] the initial domain for the view @@ -109,9 +106,9 @@ function createActionManager (params) { * Note that this is particularly useful if you want to intercept events going * up in the init process of the view, because there are no other way to do it * after this method returns - * @returns {Deferred<AbstractView>} resolves with the instance of the view + * @returns {Promise<AbstractController>} resolves with the instance of the view */ -function createAsyncView(params) { +async function createView(params) { var $target = $('#qunit-fixture'); var widget = new Widget(); if (params.debug) { @@ -175,6 +172,7 @@ function createAsyncView(params) { // when it will be called the second time (by its parent) delete view.destroy; widget.destroy(); + $webClient.remove(); testUtilsMock.unpatch(View); }; @@ -187,14 +185,45 @@ function createAsyncView(params) { callbacks: [{ widget: view }], in_DOM: true, }); + view.$el.on('click', 'a', function (ev) { ev.preventDefault(); }); - return view; }); }); } + +/** + * Similar as createView, but specific for calendar views. Some calendar + * tests need to trigger positional clicks on the DOM produced by fullcalendar. + * Those tests must use this helper with option positionalClicks set to true. + * This will move the rendered calendar to the body (required to do positional + * clicks), and wait for a setTimeout(0) before returning, because fullcalendar + * makes the calendar scroll to 6:00 in a setTimeout(0), which might have an + * impact according to where we want to trigger positional clicks. + * + * @param {Object} params see @createView + * @param {Object} [options] + * @param {boolean} [options.positionalClicks=false] + * @returns {Promise<CalendarController>} + */ +async function createCalendarView(params, options) { + var calendar = await createView(params); + if (!options || !options.positionalClicks) { + return calendar; + } + var $view = $('#qunit-fixture').contents(); + $view.prependTo('body'); + var destroy = calendar.destroy; + calendar.destroy = function () { + $view.remove(); + destroy(); + }; + await concurrency.delay(0); + return calendar; +} + /** * create a controlPanel from various parameters. * @@ -227,7 +256,7 @@ function createAsyncView(params) { * @param {boolean} [params.withSearchBar=true] if set to false, no default * search bar will be rendered * - * @returns {Deferred<ControlPanel>} resolves with an instance of the ControlPanelController + * @returns {Promise<ControlPanel>} resolves with an instance of the ControlPanelController */ function createControlPanel(params) { params = params || {}; @@ -270,6 +299,7 @@ function createControlPanel(params) { // children when it will be called the second time (by its parent) delete controlPanel.destroy; widget.destroy(); + $webClient.remove(); }; // render the controlPanel in a fragment as it must be able to render @@ -290,16 +320,16 @@ function createControlPanel(params) { * * @param {Object} [params={}] */ -function createDebugManager (params) { +var createDebugManager = function (params) { params = params || {}; var mockRPC = params.mockRPC; _.extend(params, { mockRPC: function (route, args) { if (args.method === 'check_access_rights') { - return $.when(true); + return Promise.resolve(true); } if (args.method === 'xmlid_to_res_id') { - return $.when(true); + return Promise.resolve(true); } if (mockRPC) { return mockRPC.apply(this, arguments); @@ -309,7 +339,7 @@ function createDebugManager (params) { session: { user_has_group: function (group) { if (group === 'base.group_no_one') { - return $.when(true); + return Promise.resolve(true); } return this._super.apply(this, arguments); }, @@ -318,7 +348,7 @@ function createDebugManager (params) { var debugManager = new DebugManager(); testUtilsMock.addMockEnvironment(debugManager, params); return debugManager; -} +}; /** * create a model from given parameters. @@ -360,32 +390,10 @@ function createParent(params) { return widget; } -/** - * create a view synchronously. This method uses the createAsyncView method. - * Most views are synchronous, so the deferred can be resolved immediately and - * this method will work. - * - * Be careful, if for some reason a view is async, this method will crash. - * @see createAsyncView - * - * @param {Object} params will be given to createAsyncView - * @returns {AbstractView} - */ -function createView(params) { - var view; - createAsyncView(params).then(function (result) { - view = result; - }); - if (!view) { - throw "The view that you are trying to create is async. Please use createAsyncView instead"; - } - return view; -} - - return { createActionManager: createActionManager, - createAsyncView: createAsyncView, + createView: createView, + createCalendarView: createCalendarView, createControlPanel: createControlPanel, createDebugManager: createDebugManager, createModel: createModel, |
