Skip to content

Commit 5072274

Browse files
authored
fix: add stubbed components to ignored elements (#714)
1 parent dae0b1c commit 5072274

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

Diff for: packages/shared/stub-components.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ function createStubFromString (
6262
}
6363

6464
function createBlankStub (originalComponent: Component) {
65+
const name = `${originalComponent.name}-stub`
66+
67+
// ignoreElements does not exist in Vue 2.0.x
68+
if (Vue.config.ignoredElements) {
69+
Vue.config.ignoredElements.push(name)
70+
}
71+
6572
return {
6673
...getCoreProperties(originalComponent),
6774
render (h) {
68-
return h(`${originalComponent.name}-stub`)
75+
return h(name)
6976
}
7077
}
7178
}
@@ -131,10 +138,6 @@ export function createComponentStubs (
131138
}
132139
}
133140
}
134-
// ignoreElements does not exist in Vue 2.0.x
135-
if (Vue.config.ignoredElements) {
136-
Vue.config.ignoredElements.push(`${stub}-stub`)
137-
}
138141
})
139142
}
140143
return components
@@ -148,11 +151,6 @@ function stubComponents (components: Object, stubbedComponents: Object) {
148151
components[component].name = component
149152
}
150153
stubbedComponents[component] = createBlankStub(components[component])
151-
152-
// ignoreElements does not exist in Vue 2.0.x
153-
if (Vue.config.ignoredElements) {
154-
Vue.config.ignoredElements.push(`${components[component].name}-stub`)
155-
}
156154
})
157155
}
158156

@@ -163,6 +161,8 @@ export function createComponentStubsForAll (component: Component): Object {
163161
stubComponents(component.components, stubbedComponents)
164162
}
165163

164+
stubbedComponents[component.name] = createBlankStub(component)
165+
166166
let extended = component.extends
167167

168168
// Loop through extended component chains to stub all child components

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

+49-9
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import ComponentWithoutName from '~resources/components/component-without-name.v
99
import ComponentAsAClassWithChild from '~resources/components/component-as-a-class-with-child.vue'
1010
import RecursiveComponent from '~resources/components/recursive-component.vue'
1111
import { vueVersion } from '~resources/utils'
12-
import { describeRunIf } from 'conditional-specs'
12+
import { describeRunIf, itDoNotRunIf } from 'conditional-specs'
1313

1414
describeRunIf(process.env.TEST_ENV !== 'node',
1515
'shallowMount', () => {
16-
let info
17-
1816
beforeEach(() => {
19-
info = sinon.stub(console, 'info')
17+
sinon.stub(console, 'info')
18+
sinon.stub(console, 'error')
2019
})
2120

2221
afterEach(() => {
23-
info.restore()
22+
console.info.restore()
23+
console.error.restore()
2424
})
2525

2626
it('returns new VueWrapper of Vue localVue if no options are passed', () => {
@@ -61,7 +61,7 @@ describeRunIf(process.env.TEST_ENV !== 'node',
6161
localVue.component('registered-component', ComponentWithLifecycleHooks)
6262
mount(TestComponent, { localVue })
6363

64-
expect(info.callCount).to.equal(4)
64+
expect(console.info.callCount).to.equal(4)
6565
})
6666

6767
it('stubs globally registered components', () => {
@@ -72,12 +72,52 @@ describeRunIf(process.env.TEST_ENV !== 'node',
7272
shallowMount(Component)
7373
mount(Component)
7474

75-
expect(info.callCount).to.equal(4)
76-
})
75+
expect(console.info.callCount).to.equal(4)
76+
})
77+
78+
itDoNotRunIf(
79+
vueVersion < 2.1,
80+
'adds stubbed components to ignored elements', () => {
81+
const TestComponent = {
82+
template: `
83+
<div>
84+
<router-link>
85+
</router-link>
86+
<custom-component />
87+
</div>
88+
`,
89+
components: {
90+
'router-link': {
91+
template: '<div/>'
92+
},
93+
'custom-component': {
94+
template: '<div/>'
95+
}
96+
}
97+
}
98+
shallowMount(TestComponent)
99+
expect(console.error).not.called
100+
})
101+
102+
itDoNotRunIf(
103+
vueVersion < 2.1,
104+
'handles recursive components', () => {
105+
const TestComponent = {
106+
template: `
107+
<div>
108+
<test-component />
109+
</div>
110+
`,
111+
name: 'test-component'
112+
}
113+
const wrapper = shallowMount(TestComponent)
114+
expect(wrapper.html()).to.contain('<test-component-stub>')
115+
expect(console.error).not.called
116+
})
77117

78118
it('does not call stubbed children lifecycle hooks', () => {
79119
shallowMount(ComponentWithNestedChildren)
80-
expect(info.called).to.equal(false)
120+
expect(console.info.called).to.equal(false)
81121
})
82122

83123
it('stubs extended components', () => {

0 commit comments

Comments
 (0)