Creating a local testing environment requires the temporary override of querySelector. Despite knowing that monkeypatching is generally frowned upon, in this specific instance, the code will only be utilized locally by developers. The following snippet has been crafted to achieve this (it overrides querySelector to retrieve all selectors containing another substring named addonID):
function maybeOverrideForTestStart(partialIDOfWidget, fullIDOfWidget) {
if(!isLocal) return;
const addonID = fullIDOfWidget.replace(partialIDOfWidget, "");
Element.prototype.querySelectorTemp = Element.prototype.querySelector.bind(Element);
Element.prototype.querySelector = function(selector) {
const element = this.querySelectorTemp(selector);
if (element) return element;
if (addonID) {
return this.querySelectorTemp(selector + addonID) || null;
}
};
}
function maybeOverrideForTestEnd() {
if(!isLocal) return;
Element.prototype.querySelector = Element.querySelectorTemp;
}
The maybeOverrideForTestStart
function is called at the start of testing, while maybeOverrideForTestEnd
is called at the end. However, the setup does not seem to be functioning properly and the issue remains unclear. Errors like
someElement.querySelector is not a function
or "Uncaught TypeError: Illegal invocation"
are being encountered.
An additional concern is whether this override impacts document.querySelector and document.body.querySelector as well, or is limited to someElement.querySelector.
Your assistance in resolving this matter would be highly appreciated. Thank you.