Skip to content

feat: add options.attachTo #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ component | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-u
directives | ✅ | (new!) nested in [`global`](https://vuejs.github.io/vue-test-utils-next-docs/api/#global)
stubs | ✅
attachToDocument | ❌| will rename to `attachTo`. See [here](https://github.com/vuejs/vue-test-utils/pull/1492)
attachTo | ✅
attrs | ❌ |
scopedSlots | ⚰️ | scopedSlots are merged with slots in Vue 3
context | ⚰️ | different from Vue 2, may not make sense anymore.
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function createEntry(options) {
'lodash/upperFirst',
'lodash/kebabCase',
'lodash/flow',
'lodash/isString',
'dom-event-types'
],
plugins: [resolve()],
Expand Down
18 changes: 15 additions & 3 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {

import { config } from './config'
import { GlobalMountOptions } from './types'
import { mergeGlobalProperties } from './utils'
import { mergeGlobalProperties, isString } from './utils'
import { createWrapper, VueWrapper } from './vue-wrapper'
import { attachEmitListener } from './emitMixin'
import { createDataMixin } from './dataMixin'
Expand All @@ -36,6 +36,7 @@ interface MountingOptions<Props> {
[key: string]: Slot
}
global?: GlobalMountOptions
attachTo?: HTMLElement | string
}

// Component declared with defineComponent
Expand Down Expand Up @@ -76,10 +77,21 @@ export function mount(
): VueWrapper<any> {
const component = { ...originalComponent }

// Reset the document.body
document.getElementsByTagName('html')[0].innerHTML = ''
const el = document.createElement('div')
el.id = MOUNT_ELEMENT_ID

if (options?.attachTo) {
const to = isString(options.attachTo)
? document.querySelector(options.attachTo)
: options.attachTo

el.appendChild(to)
}
if (el.children.length === 0) {
// Reset the document.body
document.getElementsByTagName('html')[0].innerHTML = ''
}

document.body.appendChild(el)

// handle any slots passed via mounting options
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import camelCase from 'lodash/camelCase'
import upperFirst from 'lodash/upperFirst'
import kebabCase from 'lodash/kebabCase'
import flow from 'lodash/flow'
import isString from 'lodash/isString'
import mergeWith from 'lodash/mergeWith'
import { GlobalMountOptions } from './types'

const pascalCase = flow(camelCase, upperFirst)

export { kebabCase, pascalCase }
export { kebabCase, pascalCase, isString }

export function mergeGlobalProperties(
configGlobal: GlobalMountOptions = {},
Expand Down
69 changes: 69 additions & 0 deletions tests/mountingOptions/attachTo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { mount } from '../../src'

const innerHTML = '<input><span>Hello world</span>'
const outerHTML = `<div id="attach-to">${innerHTML}</div>`
const ssrHTML = `<div id="attach-to" data-server-rendered="true">${innerHTML}</div>`
const template = '<div id="attach-to"><input /><span>Hello world</span></div>'
const TestComponent = { template }

describe('options.attachTo', () => {
it('attaches to a provided HTMLElement', () => {
const div = document.createElement('div')
div.id = 'root'
document.body.appendChild(div)
expect(document.getElementById('root')).not.toBeNull()
expect(document.getElementById('attach-to')).toBeNull()
const wrapper = mount(TestComponent, {
attachTo: div
})

const root = document.getElementById('root')
const rendered = document.getElementById('attach-to')
expect(wrapper.vm.$el.parentNode).not.toBeNull()
expect(root).toBeNull()
expect(rendered).not.toBeNull()
expect(rendered.outerHTML).toBe(outerHTML)
wrapper.unmount()
expect(document.getElementById('attach-to')).toBeNull()
})
it('attaches to a provided CSS selector string', () => {
const div = document.createElement('div')
div.id = 'root'
document.body.appendChild(div)
expect(document.getElementById('root')).not.toBeNull()
expect(document.getElementById('attach-to')).toBeNull()
const wrapper = mount(TestComponent, {
attachTo: '#root'
})

const root = document.getElementById('root')
const rendered = document.getElementById('attach-to')
expect(wrapper.vm.$el.parentNode).not.toBeNull()
expect(root).toBeNull()
expect(rendered).not.toBeNull()
expect(rendered.outerHTML).toBe(outerHTML)
wrapper.unmount()
expect(document.getElementById('attach-to')).toBeNull()
})

it('correctly hydrates markup', () => {
expect(document.getElementById('attach-to')).toBeNull()

const div = document.createElement('div')
div.id = 'attach-to'
div.setAttribute('data-server-rendered', 'true')
div.innerHTML = innerHTML
document.body.appendChild(div)
expect(div.outerHTML).toBe(ssrHTML)
const wrapper = mount(TestComponent, {
attachTo: '#attach-to'
})

const rendered = document.getElementById('attach-to')
expect(wrapper.vm.$el.parentNode).not.toBeNull()
expect(rendered).not.toBeNull()
expect(rendered.outerHTML).toBe(outerHTML)
wrapper.unmount()
expect(document.getElementById('attach-to')).toBeNull()
})
})