UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

127 lines 5.29 kB
"use strict"; /** * Note: This file is not part of the running script, it is injected in the browser while running e2e tests. */ (function () { var MAX_WAITING_TIME_FOR_FETCH = 45 * 1000; // 45 seconds var fetchManager = /** @class */ (function () { function FetchManager() { this.nbCurrentFetch = 0; } FetchManager.prototype.interceptor = function (ref, nativeFetch) { var args = []; for (var _i = 2; _i < arguments.length; _i++) { args[_i - 2] = arguments[_i]; } var promise = Promise.resolve(args); promise = promise.then(function () { ref.nbCurrentFetch++; return [args[0], args[1]]; }, function (error) { return Promise.reject(new Error(error)); }); promise = promise.then(function () { return nativeFetch.apply(void 0, args); }); promise = promise.then(function (response) { ref.nbCurrentFetch--; return response; }, function (error) { ref.nbCurrentFetch--; return Promise.reject(new Error(error)); }); return promise; }; /** * Register the fetch events to count the number of pending fetch. */ FetchManager.prototype.registerFetchInterceptor = function () { var _this = this; var nativeFetch = window.fetch; // eslint-disable-next-line @typescript-eslint/no-this-alias, unicorn/no-this-assignment -- needed for the context var that = this; Object.assign(window, { fetch: function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return _this.interceptor.apply(_this, __spreadArray([that, nativeFetch], args, false)); } }); }; FetchManager.prototype.unregisterFetchInterceptor = function () { Object.assign(window, { fetch: this.windowFetch }); }; /** * Returns the single instance of FetchManager */ FetchManager.getInstance = function () { return this._instance || (this._instance = new this()); }; /** * Initialize the fetch manager */ FetchManager.prototype.init = function () { this.nbCurrentFetch = 0; this.registerFetchInterceptor(); }; /** * Stop the interceptor */ FetchManager.prototype.stop = function () { this.nbCurrentFetch = 0; this.unregisterFetchInterceptor(); }; /** * Get the number of active fetchs on the page. */ FetchManager.prototype.getPendingFetchs = function () { return this.nbCurrentFetch; }; /** * This function waits for all fetchs calls to be resolved and the page to be stable to call the callback. * It permits to easily run synchronous tests with protractor. * This is very usefull in the case of Otter calls to backend because protractor synchronization manager do not care * about fetchs calls. As a consequence, the `waitForAngular` method will not work. * @param callback : Callback called when all the fetchs are finished and the page is stable. * @param timeoutInterval : Interval in milliseconds between two checks of the number of pending fetchs */ FetchManager.prototype.waitForFetchs = function (callback, timeoutInterval) { var _this = this; if (timeoutInterval === void 0) { timeoutInterval = 100; } var interval; var timeout; if (!this.getPendingFetchs()) { callback(); return; } var cancelPolling = function () { if (interval) { clearInterval(interval); interval = undefined; } if (timeout) { clearTimeout(timeout); timeout = undefined; } // Handle polling result if (_this.getPendingFetchs() > 0) { // Error case will just log in console // and allow to continue as o3r elements might be checking fetchManager // concurrently _this.nbCurrentFetch = 0; callback(); throw new Error('Fetch timeout. Please check network requests.'); } else { callback(); } }; var polling = function () { return _this.getPendingFetchs() > 0 || cancelPolling(); }; interval = setInterval(polling, timeoutInterval); timeout = setTimeout(cancelPolling, MAX_WAITING_TIME_FOR_FETCH); }; return FetchManager; }()); if (!('fetchManager' in window)) { Object.assign(window, { fetchManager: fetchManager }); } })(); //# sourceMappingURL=_fetch-manager.js.map