Skip to content
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

feat: treat document.body in attachTo in special way #1699

Merged
merged 1 commit into from
Oct 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ HTMLElement, to which your component will be fully mounted in the document.
When attaching to the DOM, you should call `wrapper.destroy()` at the end of your test to
remove the rendered elements from the document and destroy the component instance.

::: tip
When using `attachTo: document.body` new `div` instead of replacing entire body new `<div>` will be appended. This is designed to mimic Vue3 behavior and simplify future migration. See [this comment](https://github.com/vuejs/vue-test-utils/issues/1578#issuecomment-674652747) for details
:::

```js
const div = document.createElement('div')
div.id = 'root'
Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export default function mount(component, options = {}) {
const parentVm = createInstance(component, mergedOptions, _Vue)

const el =
options.attachTo || (options.attachToDocument ? createElement() : undefined)
options.attachToDocument || options.attachTo instanceof HTMLBodyElement
? createElement()
: options.attachTo
const vm = parentVm.$mount(el)

component._Ctor = {}
Expand Down
13 changes: 13 additions & 0 deletions test/specs/mounting-options/attachTo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ describeWithShallowAndMount('options.attachTo', mountingMethod => {
wrapper.destroy()
expect(document.getElementById('attach-to')).toBeNull()
})
it('appends new node when attached to document.body', () => {
const unrelatedDiv = document.createElement('div')
unrelatedDiv.id = 'unrelated'
document.body.appendChild(unrelatedDiv)
const wrapper = mountingMethod(TestComponent, {
attachTo: document.body
})
expect(document.body.contains(unrelatedDiv)).toBe(true)
expect(wrapper.vm.$el.parentNode).toBe(document.body)
expect(wrapper.options.attachedToDocument).toEqual(true)
wrapper.destroy()
unrelatedDiv.remove()
})
it('attaches to a provided CSS selector string', () => {
const div = document.createElement('div')
div.id = 'root'
Expand Down