From e1c1b95136cd029530f8f4b90813167fb8294d33 Mon Sep 17 00:00:00 2001 From: Stephen Stewart Date: Mon, 14 Oct 2019 15:08:51 +0100 Subject: [PATCH 1/5] Allow selectors in template option when testing --- packages/shared/compile-template.js | 10 ++++++++++ test/specs/mount.spec.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/packages/shared/compile-template.js b/packages/shared/compile-template.js index f5057024f..7999894bc 100644 --- a/packages/shared/compile-template.js +++ b/packages/shared/compile-template.js @@ -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)) } diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 9bf2493d7..036476913 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -161,6 +161,26 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { expect(wrapper.html()).to.equal(`
foo
`) }) + 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 = '
foo
' + window.document.body.appendChild(template) + + const wrapper = mount({ + template: '#foo' + }) + expect(wrapper.vm).to.be.an('object') + expect(wrapper.html()).to.equal(`
foo
`) + + window.body.removeChild(template) + }) + itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => { const stub = sandbox.stub() const TestComponent = Vue.extend({ From 006f161d40c1ca596299d7f0cd731845fe66a799 Mon Sep 17 00:00:00 2001 From: Stephen Stewart Date: Mon, 16 Dec 2019 12:13:35 +0000 Subject: [PATCH 2/5] Make conditional spec easier to read --- test/specs/mount.spec.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 036476913..8fd0fa18c 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -161,12 +161,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { expect(wrapper.html()).to.equal(`
foo
`) }) - it('compiles templates from querySelector', () => { - if ( - !(navigator.userAgent.includes && navigator.userAgent.includes('node.js')) - ) { - return - } + itDoNotRunIf(process.env.TEST_ENV === 'node', 'compiles templates from querySelector', () => { const template = window.createElement('div') template.setAttribute('id', 'foo') template.innerHTML = '
foo
' From ff500bcf4096db9ac3daf28ba26275428030b267 Mon Sep 17 00:00:00 2001 From: Stephen Stewart Date: Mon, 16 Dec 2019 12:19:11 +0000 Subject: [PATCH 3/5] Run prettier --- test/specs/mount.spec.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 8fd0fa18c..73eeb883a 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -161,20 +161,24 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { expect(wrapper.html()).to.equal(`
foo
`) }) - itDoNotRunIf(process.env.TEST_ENV === 'node', 'compiles templates from querySelector', () => { - const template = window.createElement('div') - template.setAttribute('id', 'foo') - template.innerHTML = '
foo
' - window.document.body.appendChild(template) + itDoNotRunIf( + process.env.TEST_ENV === 'node', + 'compiles templates from querySelector', + () => { + const template = window.createElement('div') + template.setAttribute('id', 'foo') + template.innerHTML = '
foo
' + window.document.body.appendChild(template) - const wrapper = mount({ - template: '#foo' - }) - expect(wrapper.vm).to.be.an('object') - expect(wrapper.html()).to.equal(`
foo
`) + const wrapper = mount({ + template: '#foo' + }) + expect(wrapper.vm).to.be.an('object') + expect(wrapper.html()).to.equal(`
foo
`) - window.body.removeChild(template) - }) + window.body.removeChild(template) + } + ) itDoNotRunIf(vueVersion < 2.3, 'overrides methods', () => { const stub = sandbox.stub() From 001e5de42794b55b6abfcbd0e13825fc5b96b5a4 Mon Sep 17 00:00:00 2001 From: Stephen Stewart Date: Mon, 16 Dec 2019 15:36:12 +0000 Subject: [PATCH 4/5] Exclude querySelector test from envs with no window object --- test/specs/mount.spec.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 73eeb883a..2f24cd350 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -162,9 +162,8 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { }) itDoNotRunIf( - process.env.TEST_ENV === 'node', - 'compiles templates from querySelector', - () => { + !(navigator.userAgent.includes && navigator.userAgent.includes('node.js')), + 'compiles templates from querySelector', () => { const template = window.createElement('div') template.setAttribute('id', 'foo') template.innerHTML = '
foo
' From 7b25c5ab7c45482b7cfa20ab39eb4a92896b9026 Mon Sep 17 00:00:00 2001 From: Stephen Stewart Date: Mon, 16 Dec 2019 15:39:54 +0000 Subject: [PATCH 5/5] Run prettier --- test/specs/mount.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/specs/mount.spec.js b/test/specs/mount.spec.js index 2f24cd350..3a7a78fb3 100644 --- a/test/specs/mount.spec.js +++ b/test/specs/mount.spec.js @@ -163,7 +163,8 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => { itDoNotRunIf( !(navigator.userAgent.includes && navigator.userAgent.includes('node.js')), - 'compiles templates from querySelector', () => { + 'compiles templates from querySelector', + () => { const template = window.createElement('div') template.setAttribute('id', 'foo') template.innerHTML = '
foo
'