|
1 | | -'use strict' |
| 1 | +/* global Cypress, cy */ |
2 | 2 |
|
3 | | -module.exports = true |
| 3 | +// having weak reference to styles prevents garbage collection |
| 4 | +// and "losing" styles when the next test starts |
| 5 | +const stylesCache = new Map() |
| 6 | + |
| 7 | +function copyStyles (component) { |
| 8 | + let styles |
| 9 | + if (stylesCache.has(component)) { |
| 10 | + styles = stylesCache.get(component) |
| 11 | + } else { |
| 12 | + styles = document.querySelectorAll('head style') |
| 13 | + if (styles.length) { |
| 14 | + console.log('injected %d styles', styles.length) |
| 15 | + stylesCache.set(component, styles) |
| 16 | + } else { |
| 17 | + console.log('No styles injected for this component') |
| 18 | + styles = null |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + if (!styles) { |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + const parentDocument = window.parent.document |
| 27 | + const projectName = Cypress.config('projectName') |
| 28 | + const appIframeId = `Your App: '${projectName}'` |
| 29 | + const appIframe = parentDocument.getElementById(appIframeId) |
| 30 | + const head = appIframe.contentDocument.querySelector('head') |
| 31 | + styles.forEach(style => { |
| 32 | + head.appendChild(style) |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +const deleteConstructor = comp => delete comp._Ctor |
| 37 | + |
| 38 | +function deleteCachedConstructors (component) { |
| 39 | + if (!component.components) { |
| 40 | + return |
| 41 | + } |
| 42 | + Cypress._.values(component.components).forEach(deleteConstructor) |
| 43 | +} |
| 44 | + |
| 45 | +const mountVue = component => () => { |
| 46 | + cy.visit('index.html') |
| 47 | + cy |
| 48 | + .window() |
| 49 | + .its('Vue') |
| 50 | + .then(Vue => { |
| 51 | + deleteCachedConstructors(component) |
| 52 | + Cypress.vue = new Vue(component).$mount('#app') |
| 53 | + copyStyles(component) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +module.exports = mountVue |
| 58 | + |
| 59 | +// export const loadAndMountMyComponent = VueComponent => () => { |
| 60 | +// cy.visit('index.html') |
| 61 | +// cy |
| 62 | +// .window() |
| 63 | +// .its('Vue') |
| 64 | +// .then(Vue => { |
| 65 | +// deleteCachedConstructors(VueComponent) |
| 66 | +// // TODO go through ITS components and delete their constructors |
| 67 | +// // wonder if there is unified list |
| 68 | +// Cypress.vue = new Vue(VueComponent).$mount('#app') |
| 69 | +// copyStyles(VueComponent) |
| 70 | +// }) |
| 71 | +// } |
0 commit comments