forked from vuejs/vue-test-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support of arbitrary mounting point via attachTo option
This allows for users to specify where in the document their component should attach, either through a CSS selector string or a provided HTMLElement. This option is passed through directly to the vm.$mount method that is called as part of mount.js. This enables testing of SSR code with Vue test utils as well as rendering of applications via vue-test-utils in contexts that aren't entirely Vue fixes vuejs#1492
- Loading branch information
Showing
10 changed files
with
151 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { describeWithShallowAndMount } from '~resources/utils' | ||
|
||
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 } | ||
|
||
describeWithShallowAndMount('options.attachTo', mountingMethod => { | ||
it('should not mount to document when null', () => { | ||
const wrapper = mountingMethod(TestComponent, {}) | ||
expect(wrapper.vm.$el.parentNode).to.be.null | ||
wrapper.destroy() | ||
}) | ||
it('attaches to a provided HTMLElement', () => { | ||
const div = document.createElement('div') | ||
div.id = 'root' | ||
document.body.appendChild(div) | ||
expect(document.getElementById('root')).to.not.be.null | ||
expect(document.getElementById('attach-to')).to.be.null | ||
const wrapper = mountingMethod(TestComponent, { | ||
attachTo: div | ||
}) | ||
|
||
const root = document.getElementById('root') | ||
const rendered = document.getElementById('attach-to') | ||
expect(wrapper.vm.$el.parentNode).to.not.be.null | ||
expect(root).to.be.null | ||
expect(rendered).to.not.be.null | ||
expect(rendered.outerHTML).to.equal(outerHTML) | ||
expect(wrapper.options.attachedToDocument).to.equal(true) | ||
wrapper.destroy() | ||
expect(document.getElementById('attach-to')).to.be.null | ||
}) | ||
it('attaches to a provided CSS selector string', () => { | ||
const div = document.createElement('div') | ||
div.id = 'root' | ||
document.body.appendChild(div) | ||
expect(document.getElementById('root')).to.not.be.null | ||
expect(document.getElementById('attach-to')).to.be.null | ||
const wrapper = mountingMethod(TestComponent, { | ||
attachTo: '#root' | ||
}) | ||
|
||
const root = document.getElementById('root') | ||
const rendered = document.getElementById('attach-to') | ||
expect(wrapper.vm.$el.parentNode).to.not.be.null | ||
expect(root).to.be.null | ||
expect(rendered).to.not.be.null | ||
expect(rendered.outerHTML).to.equal(outerHTML) | ||
expect(wrapper.options.attachedToDocument).to.equal(true) | ||
wrapper.destroy() | ||
expect(document.getElementById('attach-to')).to.be.null | ||
}) | ||
|
||
it('correctly hydrates markup', () => { | ||
expect(document.getElementById('attach-to')).to.be.null | ||
|
||
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).to.equal(ssrHTML) | ||
const wrapper = mountingMethod(TestComponent, { | ||
attachTo: '#attach-to' | ||
}) | ||
|
||
const rendered = document.getElementById('attach-to') | ||
expect(wrapper.vm.$el.parentNode).to.not.be.null | ||
expect(rendered).to.not.be.null | ||
expect(rendered.outerHTML).to.equal(outerHTML) | ||
expect(wrapper.options.attachedToDocument).to.equal(true) | ||
wrapper.destroy() | ||
expect(document.getElementById('attach-to')).to.be.null | ||
}) | ||
}) |