|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict'; |
|
|
|
const ROOT_SELECTOR = '[data-cy-root]'; |
|
|
|
|
|
|
|
|
|
|
|
const getContainerEl = () => { |
|
const el = document.querySelector(ROOT_SELECTOR); |
|
if (el) { |
|
return el; |
|
} |
|
throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please add a root element with data-cy-root attribute to your "component-index.html" file so that Cypress can attach your component to the DOM.`); |
|
}; |
|
function checkForRemovedStyleOptions(mountingOptions) { |
|
for (const key of ['cssFile', 'cssFiles', 'style', 'styles', 'stylesheet', 'stylesheets']) { |
|
if (mountingOptions[key]) { |
|
Cypress.utils.throwErrByPath('mount.removed_style_mounting_options', key); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
function setupHooks(optionalCallback) { |
|
|
|
|
|
|
|
if (Cypress.testingType !== 'component') { |
|
return; |
|
} |
|
|
|
|
|
|
|
Cypress.Commands.overwrite('visit', () => { |
|
throw new Error('cy.visit from a component spec is not allowed'); |
|
}); |
|
Cypress.Commands.overwrite('session', () => { |
|
throw new Error('cy.session from a component spec is not allowed'); |
|
}); |
|
Cypress.Commands.overwrite('origin', () => { |
|
throw new Error('cy.origin from a component spec is not allowed'); |
|
}); |
|
|
|
Cypress.on('test:before:after:run:async', () => { |
|
optionalCallback === null || optionalCallback === void 0 ? void 0 : optionalCallback(); |
|
}); |
|
} |
|
|
|
const DEFAULT_COMP_NAME = 'unknown';
|
|
let componentInstance;
|
|
const cleanup = () => {
|
|
componentInstance === null || componentInstance === void 0 ? void 0 : componentInstance.$destroy();
|
|
};
|
|
|
|
const getComponentDisplayName = (Component) => {
|
|
if (Component.name) {
|
|
const [, match] = /Proxy\<(\w+)\>/.exec(Component.name) || [];
|
|
return match || Component.name;
|
|
}
|
|
return DEFAULT_COMP_NAME;
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function mount(Component, options = {}) {
|
|
checkForRemovedStyleOptions(options);
|
|
return cy.then(() => {
|
|
|
|
cleanup();
|
|
const target = getContainerEl();
|
|
const ComponentConstructor = (Component.default || Component);
|
|
componentInstance = new ComponentConstructor(Object.assign({ target }, options));
|
|
|
|
|
|
return cy.wait(0, { log: false }).then(() => {
|
|
if (options.log !== false) {
|
|
const mountMessage = `<${getComponentDisplayName(Component)} ... />`;
|
|
Cypress.log({
|
|
name: 'mount',
|
|
message: [mountMessage],
|
|
});
|
|
}
|
|
})
|
|
.wrap({ component: componentInstance }, { log: false });
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setupHooks(cleanup); |
|
|
|
exports.mount = mount; |
|
|