cypress-controls-ext
Version:
Extension to embed controls to controls panel in cypress app
133 lines (132 loc) • 5.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupControlsExtension = exports.setupControlsExtensionWithEvent = exports.removeControls = exports.injectControl = void 0;
const common_1 = require("./common");
const style_handler_1 = require("./style-handler");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const appId = 'cypress-controls-ext';
const message = (msg) => `${appId}: ${msg}`;
const cyStopClick = () => {
(0, common_1.cypressAppSelect)('.stop').trigger('click');
};
const cyRestartClick = () => {
(0, common_1.cypressAppSelect)('.restart').trigger('click');
};
const wrapperIdFromSettings = (id) => `controlWrapper-${id}`;
const isMode = (openRun, setting) => {
if (!setting && openRun === 'run') {
// no setting and run mode - not inject
return false;
}
if (!setting && openRun === 'open') {
// no setting and open mode - inject
return true;
}
if (!setting) {
return false;
}
if (openRun === 'open' && setting.open === undefined) {
return false;
}
if (openRun === 'run' && setting.run === undefined) {
return false;
}
return setting[openRun];
};
const addControlWhenNotExist = (atPosition, wrapperId, toInject, html) => {
// When already exist on page
if (toInject.find(`#${wrapperId}`).length > 0 ||
(0, common_1.cypressAppSelect)('body').find(`#${wrapperId}`).length > 0) {
return;
}
console.log(message('Injecting: ' + wrapperId));
if (atPosition === 'start') {
toInject.prepend(html);
}
else if (atPosition === 'insertAfter') {
Cypress.$(html).insertAfter(toInject);
}
else if (atPosition === 'insertBefore') {
Cypress.$(html).insertBefore(toInject);
}
else {
toInject.append(html);
}
};
/**
* Injects controls to page and returns parent id of created element
* @param settings
*/
const injectControl = (settings) => {
var _a, _b;
const cypressInteractive = Cypress.config('isInteractive');
const isOpen = isMode('open', settings.mode);
const isRun = isMode('run', settings.mode);
if ((cypressInteractive && !isOpen) || (!cypressInteractive && !isRun)) {
// do not add according to config modes
console.info(message('Will not inject control: ' + JSON.stringify(settings.mode)));
return undefined;
}
const wrapperId = wrapperIdFromSettings(settings.id);
const control = `<span id="${wrapperId}" class="control-wrapper">${settings.control()}</span>`;
const controls = (0, common_1.cypressAppSelect)((_a = settings === null || settings === void 0 ? void 0 : settings.selectorToInject) !== null && _a !== void 0 ? _a : '.reporter header');
addControlWhenNotExist((_b = settings.inject) !== null && _b !== void 0 ? _b : 'end', wrapperId, controls, control);
if (settings.style) {
(0, style_handler_1.setStyle)(wrapperId, settings.style(wrapperId));
}
settings.addEventListener(wrapperId, (selector, event, handler) => {
const element = (0, common_1.cypressAppSelect)(selector);
if ((element === null || element === void 0 ? void 0 : element.length) === 0) {
console.warn(message(`could not set event listener: no matching elements for "${selector}"`));
return;
}
element.get().forEach((item) => {
item.addEventListener(event, (target) => {
handler(target);
if (settings.style) {
(0, style_handler_1.setStyle)(wrapperId, settings.style(wrapperId));
}
});
});
}, cyStopClick, cyRestartClick);
return wrapperId;
};
exports.injectControl = injectControl;
const injectControls = (controlSettings) => {
const ids = controlSettings.map((s) => s.id);
if (controlSettings.some((s) => ids.filter((c) => s.id === c).length > 1)) {
throw new Error(message(`Controls should have uniq ids: ${JSON.stringify(ids)}`));
}
controlSettings.forEach((setting) => {
(0, exports.injectControl)(setting);
});
};
/**
* Remove controls
* @param controlSettings
*/
const removeControls = (...controlSettings) => {
controlSettings.forEach((setting) => {
(0, common_1.cypressAppSelect)('#' + wrapperIdFromSettings(setting.id)).remove();
});
};
exports.removeControls = removeControls;
/**
* Injects controls and adds event listeners on action
* Cypress.on('test:before:run:async'
* @param controlSettings array of injected controls
*/
const setupControlsExtensionWithEvent = (...controlSettings) => {
Cypress.on('test:before:run:async', () => {
injectControls(controlSettings);
});
};
exports.setupControlsExtensionWithEvent = setupControlsExtensionWithEvent;
/**
* Injects controls and adds event listeners
* @param controlSettings array of injected controls
*/
const setupControlsExtension = (...controlSettings) => {
injectControls(controlSettings);
};
exports.setupControlsExtension = setupControlsExtension;