|
| 1 | +import {createLocalVue, mount} from '@vue/test-utils' |
| 2 | + |
| 3 | +import {getQueriesForElement, prettyDOM} from '@testing-library/dom' |
| 4 | + |
| 5 | +const mountedWrappers = new Set() |
| 6 | + |
| 7 | +function render( |
| 8 | + Component, |
| 9 | + { |
| 10 | + store = null, |
| 11 | + routes = null, |
| 12 | + container: customContainer, |
| 13 | + baseElement: customBaseElement, |
| 14 | + ...mountOptions |
| 15 | + } = {}, |
| 16 | + configurationCb, |
| 17 | +) { |
| 18 | + const div = document.createElement('div') |
| 19 | + const baseElement = customBaseElement || customContainer || document.body |
| 20 | + const container = customContainer || baseElement.appendChild(div) |
| 21 | + |
| 22 | + const attachTo = document.createElement('div') |
| 23 | + container.appendChild(attachTo) |
| 24 | + |
| 25 | + const localVue = createLocalVue() |
| 26 | + let vuexStore = null |
| 27 | + let router = null |
| 28 | + let callbackOptions = {} |
| 29 | + |
| 30 | + if (store) { |
| 31 | + const Vuex = require('vuex') |
| 32 | + localVue.use(Vuex) |
| 33 | + |
| 34 | + vuexStore = new Vuex.Store(store) |
| 35 | + } |
| 36 | + |
| 37 | + if (routes) { |
| 38 | + const requiredRouter = require('vue-router') |
| 39 | + const VueRouter = requiredRouter.default || requiredRouter |
| 40 | + localVue.use(VueRouter) |
| 41 | + |
| 42 | + router = new VueRouter({routes}) |
| 43 | + } |
| 44 | + |
| 45 | + if (configurationCb && typeof configurationCb === 'function') { |
| 46 | + callbackOptions = configurationCb(localVue, vuexStore, router) |
| 47 | + } |
| 48 | + |
| 49 | + if (!mountOptions.propsData && !!mountOptions.props) { |
| 50 | + mountOptions.propsData = mountOptions.props |
| 51 | + delete mountOptions.props |
| 52 | + } |
| 53 | + |
| 54 | + const wrapper = mount(Component, { |
| 55 | + attachTo, |
| 56 | + localVue, |
| 57 | + router, |
| 58 | + store: vuexStore, |
| 59 | + ...mountOptions, |
| 60 | + ...callbackOptions, |
| 61 | + }) |
| 62 | + |
| 63 | + mountedWrappers.add(wrapper) |
| 64 | + container.appendChild(wrapper.element) |
| 65 | + |
| 66 | + return { |
| 67 | + container, |
| 68 | + baseElement, |
| 69 | + debug: (el = baseElement, ...args) => |
| 70 | + Array.isArray(el) |
| 71 | + ? el.forEach(e => console.log(prettyDOM(e, ...args))) |
| 72 | + : console.log(prettyDOM(el, ...args)), |
| 73 | + unmount: () => wrapper.destroy(), |
| 74 | + isUnmounted: () => wrapper.vm._isDestroyed, |
| 75 | + html: () => wrapper.html(), |
| 76 | + emitted: () => wrapper.emitted(), |
| 77 | + updateProps: _ => wrapper.setProps(_), |
| 78 | + ...getQueriesForElement(baseElement), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function cleanup() { |
| 83 | + mountedWrappers.forEach(cleanupAtWrapper) |
| 84 | +} |
| 85 | + |
| 86 | +function cleanupAtWrapper(wrapper) { |
| 87 | + if ( |
| 88 | + wrapper.element.parentNode && |
| 89 | + wrapper.element.parentNode.parentNode === document.body |
| 90 | + ) { |
| 91 | + document.body.removeChild(wrapper.element.parentNode) |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + wrapper.destroy() |
| 96 | + } finally { |
| 97 | + mountedWrappers.delete(wrapper) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export {cleanup, render} |
0 commit comments