From 3756d97018be4ed1279a7b6b00fdad0a2b409b66 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 17:08:42 +0100 Subject: [PATCH 1/3] fix: add stubbed components to ignored elements --- packages/shared/stub-components.js | 20 ++++++------ test/specs/shallow-mount.spec.js | 50 +++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/packages/shared/stub-components.js b/packages/shared/stub-components.js index 697e0eaf2..9358fcee0 100644 --- a/packages/shared/stub-components.js +++ b/packages/shared/stub-components.js @@ -62,10 +62,17 @@ function createStubFromString ( } function createBlankStub (originalComponent: Component) { + const name = `${originalComponent.name}-stub` + + // ignoreElements does not exist in Vue 2.0.x + if (Vue.config.ignoredElements) { + Vue.config.ignoredElements.push(name) + } + return { ...getCoreProperties(originalComponent), render (h) { - return h(`${originalComponent.name}-stub`) + return h(name) } } } @@ -131,10 +138,6 @@ export function createComponentStubs ( } } } - // ignoreElements does not exist in Vue 2.0.x - if (Vue.config.ignoredElements) { - Vue.config.ignoredElements.push(`${stub}-stub`) - } }) } return components @@ -148,11 +151,6 @@ function stubComponents (components: Object, stubbedComponents: Object) { components[component].name = component } stubbedComponents[component] = createBlankStub(components[component]) - - // ignoreElements does not exist in Vue 2.0.x - if (Vue.config.ignoredElements) { - Vue.config.ignoredElements.push(`${components[component].name}-stub`) - } }) } @@ -163,6 +161,8 @@ export function createComponentStubsForAll (component: Component): Object { stubComponents(component.components, stubbedComponents) } + stubbedComponents[component.name] = createBlankStub(component) + let extended = component.extends // Loop through extended component chains to stub all child components diff --git a/test/specs/shallow-mount.spec.js b/test/specs/shallow-mount.spec.js index 441afe32c..5c090e96a 100644 --- a/test/specs/shallow-mount.spec.js +++ b/test/specs/shallow-mount.spec.js @@ -13,14 +13,14 @@ import { describeRunIf } from 'conditional-specs' describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { - let info - beforeEach(() => { - info = sinon.stub(console, 'info') + sinon.stub(console, 'info') + sinon.stub(console, 'error') }) afterEach(() => { - info.restore() + console.info.restore() + console.error.restore() }) it('returns new VueWrapper of Vue localVue if no options are passed', () => { @@ -61,7 +61,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', localVue.component('registered-component', ComponentWithLifecycleHooks) mount(TestComponent, { localVue }) - expect(info.callCount).to.equal(4) + expect(console.info.callCount).to.equal(4) }) it('stubs globally registered components', () => { @@ -72,12 +72,48 @@ describeRunIf(process.env.TEST_ENV !== 'node', shallowMount(Component) mount(Component) - expect(info.callCount).to.equal(4) + expect(console.info.callCount).to.equal(4) + }) + + it('adds stubbed components to ignored elements', () => { + const TestComponent = { + template: ` +
+ + + +
+ `, + components: { + 'router-link': { + template: '
' + }, + 'custom-component': { + template: '
' + } + } + } + shallowMount(TestComponent) + expect(console.error).not.called + }) + + it('handles recursive components', () => { + const TestComponent = { + template: ` +
+ +
+ `, + name: 'test-component' + } + const wrapper = shallowMount(TestComponent) + expect(wrapper.html()).to.contain('') + expect(console.error).not.called }) it('does not call stubbed children lifecycle hooks', () => { shallowMount(ComponentWithNestedChildren) - expect(info.called).to.equal(false) + expect(console.info.called).to.equal(false) }) it('stubs extended components', () => { From eeef708316b2177ccd9c2a946334495a3115b436 Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 17:23:56 +0100 Subject: [PATCH 2/3] test: do not run on < 2.1 --- test/specs/shallow-mount.spec.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/test/specs/shallow-mount.spec.js b/test/specs/shallow-mount.spec.js index 5c090e96a..883594c7a 100644 --- a/test/specs/shallow-mount.spec.js +++ b/test/specs/shallow-mount.spec.js @@ -9,7 +9,7 @@ import ComponentWithoutName from '~resources/components/component-without-name.v import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue' import RecursiveComponent from '~resources/components/recursive-component.vue' import { vueVersion } from '~resources/utils' -import { describeRunIf } from 'conditional-specs' +import { describeRunIf, itDoNotRunIf } from 'conditional-specs' describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => { @@ -75,27 +75,29 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(console.info.callCount).to.equal(4) }) - it('adds stubbed components to ignored elements', () => { - const TestComponent = { - template: ` + itDoNotRunIf( + vueVersion < 2.1, + 'adds stubbed components to ignored elements', () => { + const TestComponent = { + template: `
`, - components: { - 'router-link': { - template: '
' - }, - 'custom-component': { - template: '
' + components: { + 'router-link': { + template: '
' + }, + 'custom-component': { + template: '
' + } } } - } - shallowMount(TestComponent) - expect(console.error).not.called - }) + shallowMount(TestComponent) + expect(console.error).not.called + }) it('handles recursive components', () => { const TestComponent = { From 65e2421ecb1b44d7b6100c1273a441154acf7b9c Mon Sep 17 00:00:00 2001 From: eddyerburgh Date: Thu, 14 Jun 2018 17:52:29 +0100 Subject: [PATCH 3/3] test: skip if vueVersion < 2.1 --- test/specs/shallow-mount.spec.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/specs/shallow-mount.spec.js b/test/specs/shallow-mount.spec.js index 883594c7a..d6baa1c82 100644 --- a/test/specs/shallow-mount.spec.js +++ b/test/specs/shallow-mount.spec.js @@ -99,19 +99,21 @@ describeRunIf(process.env.TEST_ENV !== 'node', expect(console.error).not.called }) - it('handles recursive components', () => { - const TestComponent = { - template: ` + itDoNotRunIf( + vueVersion < 2.1, + 'handles recursive components', () => { + const TestComponent = { + template: `
`, - name: 'test-component' - } - const wrapper = shallowMount(TestComponent) - expect(wrapper.html()).to.contain('') - expect(console.error).not.called - }) + name: 'test-component' + } + const wrapper = shallowMount(TestComponent) + expect(wrapper.html()).to.contain('') + expect(console.error).not.called + }) it('does not call stubbed children lifecycle hooks', () => { shallowMount(ComponentWithNestedChildren)