Skip to content

Commit

Permalink
Allow selectors in template option when testing
Browse files Browse the repository at this point in the history
  • Loading branch information
earnubs committed Dec 16, 2019
1 parent 3115396 commit e1c1b95
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/shared/compile-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export function compileFromString(str: string) {

export function compileTemplate(component: Component): void {
if (component.template) {
if (component.template.charAt('#') === '#') {
var el = document.querySelector(component.template)
if (!el) {
throwError('Cannot find element' + component.template)

el = document.createElement('div')
}
component.template = el.innerHTML
}

Object.assign(component, compileToFunctions(component.template))
}

Expand Down
20 changes: 20 additions & 0 deletions test/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
expect(wrapper.html()).to.equal(`<div>foo</div>`)
})

it('compiles templates from querySelector', () => {
if (
!(navigator.userAgent.includes && navigator.userAgent.includes('node.js'))
) {
return
}
const template = window.createElement('div')
template.setAttribute('id', 'foo')
template.innerHTML = '<div>foo</div>'
window.document.body.appendChild(template)

const wrapper = mount({
template: '#foo'
})
expect(wrapper.vm).to.be.an('object')
expect(wrapper.html()).to.equal(`<div>foo</div>`)

window.body.removeChild(template)
})

itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => {
const stub = sandbox.stub()
const TestComponent = Vue.extend({
Expand Down

0 comments on commit e1c1b95

Please sign in to comment.