| 1 | /** @odoo-module alias=@web/../tests/legacy_tests/helpers/test_utils_mock default=false */ |
| 2 | |
| 3 | /** |
| 4 | * Mock Test Utils |
| 5 | * |
| 6 | * This module defines various utility functions to help mocking data. |
| 7 | * |
| 8 | * Note that all methods defined in this module are exported in the main |
| 9 | * testUtils file. |
| 10 | */ |
| 11 | |
| 12 | import { patchDate } from "@web/../tests/helpers/utils"; |
| 13 | |
| 14 | //------------------------------------------------------------------------------ |
| 15 | // Public functions |
| 16 | //------------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * intercepts an event bubbling up the widget hierarchy. The event intercepted |
| 20 | * must be a "custom event", i.e. an event generated by the method 'trigger_up'. |
| 21 | * |
| 22 | * Note that this method really intercepts the event if @propagate is not set. |
| 23 | * It will not be propagated further, and even the handlers on the target will |
| 24 | * not fire. |
| 25 | * |
| 26 | * @param {Widget} widget the target widget (any Odoo widget) |
| 27 | * @param {string} eventName description of the event |
| 28 | * @param {function} fn callback executed when the even is intercepted |
| 29 | * @param {boolean} [propagate=false] |
| 30 | */ |
| 31 | function intercept(widget, eventName, fn, propagate) { |
| 32 | var _trigger_up = widget._trigger_up.bind(widget); |
| 33 | widget._trigger_up = function (event) { |
| 34 | if (event.name === eventName) { |
| 35 | fn(event); |
| 36 | if (!propagate) { return; } |
| 37 | } |
| 38 | _trigger_up(event); |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Patch window.Date so that the time starts its flow from the provided Date. |
| 44 | * |
| 45 | * Usage: |
| 46 | * |
| 47 | * ``` |
| 48 | * testUtils.mock.patchDate(2018, 0, 10, 17, 59, 30) |
| 49 | * new window.Date(); // "Wed Jan 10 2018 17:59:30 GMT+0100 (Central European Standard Time)" |
| 50 | * ... // 5 hours delay |
| 51 | * new window.Date(); // "Wed Jan 10 2018 22:59:30 GMT+0100 (Central European Standard Time)" |
| 52 | * ``` |
| 53 | * |
| 54 | * The returned function is there to preserve the former API. Before it was |
| 55 | * necessary to call that function to unpatch the date. Now the unpatch is |
| 56 | * done automatically via a call to registerCleanup. |
| 57 | * |
| 58 | * @param {integer} year |
| 59 | * @param {integer} month index of the month, starting from zero. |
| 60 | * @param {integer} day the day of the month. |
| 61 | * @param {integer} hours the digits for hours (24h) |
| 62 | * @param {integer} minutes |
| 63 | * @param {integer} seconds |
| 64 | * @returns {Function} callback function is now useless |
| 65 | */ |
| 66 | function legacyPatchDate(year, month, day, hours, minutes, seconds) { |
| 67 | patchDate(year, month, day, hours, minutes, seconds); |
| 68 | return function () {}; // all calls to that function are now useless |
| 69 | } |
| 70 | |
| 71 | export default { |
| 72 | intercept: intercept, |
| 73 | patchDate: legacyPatchDate, |
| 74 | }; |