@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
157 lines • 6.07 kB
JavaScript
;
/**
* Note: This file is not part of the running script, it is injected in the browser while running e2e tests.
*/
Object.defineProperty(exports, "__esModule", { value: true });
// TODO: Move to PollyJS when the winter comes
(function () {
/**
* PostMessageInterceptor permits to intercept calls to postMessage.
*/
var postMessageInterceptor = /** @class */ (function () {
function PostMessageInterceptor() {
this.lastCalls = [];
this.listening = false;
}
/**
* Logs a message in the console
* @param message
* @param {...any} args
*/
PostMessageInterceptor.prototype.log = function (message) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
// eslint-disable-next-line no-console -- this is the purpose of this method
console.log.apply(console, __spreadArray(["#postMessageInterceptor: ".concat(message)], args, false));
};
/**
* Intercepts the native postMessage call
* @param ref Singleton instance of the interceptor
* @param nativeMethod native postMessage method
* @param args all the args passed to the postMessage
*/
PostMessageInterceptor.prototype.interceptor = function (ref, nativeMethod) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
this.log('Intercepted', args);
var postCall = {
data: args[0],
targetOrigin: args[1],
timestamp: new Date()
};
if (ref.listening) {
if (ref.conditionFn && !ref.conditionFn(postCall)) {
this.log('Intercepted message does not pass the condition');
return;
}
ref.lastCalls.push(postCall);
this.log('Last call count', ref.lastCalls.length);
}
else {
this.log('Not listening');
}
nativeMethod.apply(void 0, args);
};
/**
* Register the interceptor in the window object
*/
PostMessageInterceptor.prototype.registerFetchInterceptor = function () {
var _this = this;
var nativeMethod = window.postMessage;
this.nativeMethod = nativeMethod;
Object.assign(window, { postMessage: function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _this.interceptor.apply(_this, __spreadArray([_this, nativeMethod], args, false));
} });
};
/**
* Unregister the interceptor from the window object
*/
PostMessageInterceptor.prototype.unregisterFetchInterceptor = function () {
Object.assign(window, { postMessage: this.nativeMethod });
};
/**
* Returns the singleton instance of the interceptor
*/
PostMessageInterceptor.getInstance = function () {
return this._instance || (this._instance = new this());
};
/**
* Initialize the interceptor
*/
PostMessageInterceptor.prototype.init = function () {
this.reset();
this.log('Init');
this.registerFetchInterceptor();
};
/**
* Stops the interceptor
*/
PostMessageInterceptor.prototype.stop = function () {
this.reset();
this.log('Stop');
this.unregisterFetchInterceptor();
};
/**
* Resets the stack of lastCalls
*/
PostMessageInterceptor.prototype.reset = function () {
this.lastCalls = [];
};
/**
* Starts listening and saving postMessages
* @param conditionFnString The function string to be used as condition checker
*/
PostMessageInterceptor.prototype.listen = function (conditionFnString) {
this.listening = false;
this.reset();
if (conditionFnString) {
// eslint-disable-next-line no-eval -- done on purpose
this.conditionFn = eval(conditionFnString);
}
this.listening = true;
};
/**
* Stops listening
* NOTE: It resets the interceptor and clears the conditionFn (if any)
*/
PostMessageInterceptor.prototype.stopListening = function () {
this.listening = false;
this.reset();
this.conditionFn = undefined;
this.listening = true;
};
/**
* Get the messages stack
* @param timeoutInterval the interval, in ms, between each check
* @param retries number of tentatives if fail
* @param callback
*/
PostMessageInterceptor.prototype.getMessages = function (timeoutInterval, retries, callback) {
var _this = this;
var activeMessageWatch = function (remainingRetries) {
if (remainingRetries === 0 || _this.lastCalls.length > 0) {
var copyCalls = _this.lastCalls.slice();
_this.reset();
callback(copyCalls);
return;
}
else if (remainingRetries > 0) {
remainingRetries--;
}
setTimeout(function () { return activeMessageWatch(remainingRetries); }, timeoutInterval);
};
activeMessageWatch(retries);
};
return PostMessageInterceptor;
}());
Object.assign(window, { postMessageInterceptor: postMessageInterceptor });
})();
//# sourceMappingURL=_post-message-interceptor.js.map