Skip to content

Commit e1c1b95

Browse files
committed
Allow selectors in template option when testing
1 parent 3115396 commit e1c1b95

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Diff for: packages/shared/compile-template.js

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export function compileFromString(str: string) {
1717

1818
export function compileTemplate(component: Component): void {
1919
if (component.template) {
20+
if (component.template.charAt('#') === '#') {
21+
var el = document.querySelector(component.template)
22+
if (!el) {
23+
throwError('Cannot find element' + component.template)
24+
25+
el = document.createElement('div')
26+
}
27+
component.template = el.innerHTML
28+
}
29+
2030
Object.assign(component, compileToFunctions(component.template))
2131
}
2232

Diff for: test/specs/mount.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
161161
expect(wrapper.html()).to.equal(`<div>foo</div>`)
162162
})
163163

164+
it('compiles templates from querySelector', () => {
165+
if (
166+
!(navigator.userAgent.includes && navigator.userAgent.includes('node.js'))
167+
) {
168+
return
169+
}
170+
const template = window.createElement('div')
171+
template.setAttribute('id', 'foo')
172+
template.innerHTML = '<div>foo</div>'
173+
window.document.body.appendChild(template)
174+
175+
const wrapper = mount({
176+
template: '#foo'
177+
})
178+
expect(wrapper.vm).to.be.an('object')
179+
expect(wrapper.html()).to.equal(`<div>foo</div>`)
180+
181+
window.body.removeChild(template)
182+
})
183+
164184
itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => {
165185
const stub = sandbox.stub()
166186
const TestComponent = Vue.extend({

0 commit comments

Comments
 (0)