Skip to content

Commit a87d5e0

Browse files
committed
refactor: move stub logic to addStubs function
1 parent b1c68fc commit a87d5e0

File tree

5 files changed

+22
-64
lines changed

5 files changed

+22
-64
lines changed

packages/create-instance/add-mocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import $$Vue from 'vue'
33
import { warn } from 'shared/util'
44

55
export default function addMocks (
6-
mockedProperties: Object,
6+
mockedProperties: Object = {},
77
Vue: Component
88
): void {
99
Object.keys(mockedProperties).forEach(key => {

packages/create-instance/create-instance.js

+7-57
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { createSlotVNodes } from './create-slot-vnodes'
44
import addMocks from './add-mocks'
55
import { addEventLogger } from './log-events'
6-
import { createComponentStubs } from 'shared/stub-components'
6+
import { addStubs } from './add-stubs'
77
import { throwError, vueVersion } from 'shared/util'
88
import { compileTemplate } from 'shared/compile-template'
99
import { isRequiredComponent } from 'shared/validators'
@@ -34,18 +34,15 @@ export default function createInstance (
3434
// Remove cached constructor
3535
delete component._Ctor
3636

37-
// mounting options are vue-test-utils specific
38-
//
37+
3938
// instance options are options that are passed to the
4039
// root instance when it's instantiated
41-
//
42-
// component options are the root components options
40+
const instanceOptions = extractInstanceOptions(options)
4341

44-
const instanceOptions = extractInstanceOptions(options)
42+
addEventLogger(_Vue)
43+
addMocks(options.mocks, _Vue)
44+
addStubs(component, options.stubs, _Vue)
4545

46-
if (options.mocks) {
47-
addMocks(options.mocks, _Vue)
48-
}
4946
if (
5047
(component.options && component.options.functional) ||
5148
component.functional
@@ -61,7 +58,6 @@ export default function createInstance (
6158
compileTemplate(component)
6259
}
6360

64-
addEventLogger(_Vue)
6561

6662
// Replace globally registered components with components extended
6763
// from localVue. This makes sure the beforeMount mixins to add stubs
@@ -76,68 +72,22 @@ export default function createInstance (
7672
}
7773
}
7874

79-
const stubComponents = createComponentStubs(
80-
component.components,
81-
// $FlowIgnore
82-
options.stubs
83-
)
84-
8575
extendExtendedComponents(
8676
component,
8777
_Vue,
8878
options.logModifiedComponents,
8979
instanceOptions.components
9080
)
9181

92-
// stub components should override every component defined in a component
93-
if (options.stubs) {
94-
instanceOptions.components = {
95-
...instanceOptions.components,
96-
...stubComponents
97-
}
98-
}
99-
function addStubComponentsMixin () {
100-
Object.assign(
101-
this.$options.components,
102-
stubComponents
103-
)
104-
}
105-
_Vue.mixin({
106-
beforeMount: addStubComponentsMixin,
107-
// beforeCreate is for components created in node, which
108-
// never mount
109-
beforeCreate: addStubComponentsMixin
110-
})
111-
11282
if (component.options) {
11383
component.options._base = _Vue
11484
}
11585

116-
function getExtendedComponent (component, instanceOptions) {
117-
const extendedComponent = component.extend(instanceOptions)
118-
// to keep the possible overridden prototype and _Vue mixins,
119-
// we need change the proto chains manually
120-
// @see https://github.com/vuejs/vue-test-utils/pull/856
121-
// code below equals to
122-
// `extendedComponent.prototype.__proto__.__proto__ = _Vue.prototype`
123-
const extendedComponentProto =
124-
Object.getPrototypeOf(extendedComponent.prototype)
125-
Object.setPrototypeOf(extendedComponentProto, _Vue.prototype)
126-
127-
return extendedComponent
128-
}
129-
13086
// extend component from _Vue to add properties and mixins
131-
const Constructor = typeof component === 'function'
132-
? getExtendedComponent(component, instanceOptions)
133-
: _Vue.extend(component).extend(instanceOptions)
87+
const Constructor = _Vue.extend(component).extend(instanceOptions)
13488

13589
Constructor._vueTestUtilsRoot = component
13690

137-
Object.keys(instanceOptions.components || {}).forEach(c => {
138-
Constructor.component(c, instanceOptions.components[c])
139-
})
140-
14191
if (options.slots) {
14292
compileTemplateForSlots(options.slots)
14393
// $FlowIgnore

packages/shared/stub-components.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function createClassString (staticClass, dynamicClass) {
9090
return staticClass || dynamicClass
9191
}
9292

93-
function createBlankStub (
93+
export function createBlankStub (
9494
originalComponent: Component,
9595
name: string
9696
): Component {
@@ -242,8 +242,6 @@ export function createComponentStubsForAll (component: Component): Components {
242242
stubComponents(component.components, stubbedComponents)
243243
}
244244

245-
stubbedComponents[component.name] = createBlankStub(component, component.name)
246-
247245
let extended = component.extends
248246

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

packages/test-utils/src/shallow-mount.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import mount from './mount'
66
import type VueWrapper from './vue-wrapper'
77
import {
88
createComponentStubsForAll,
9-
createComponentStubsForGlobals
9+
createComponentStubsForGlobals,
10+
createBlankStub
1011
} from 'shared/stub-components'
1112
import { camelize, capitalize, hyphenate } from 'shared/util'
1213

@@ -23,6 +24,16 @@ export default function shallowMount (
2324
delete component.components[hyphenate(component.name)]
2425
}
2526

27+
if(!options.stubs) {
28+
options.stubs = {}
29+
}
30+
31+
// In Vue.extends, Vue adds a recursive component to the options
32+
// This stub will override the component added by Vue
33+
if (!options.stubs[component.name]) {
34+
options.stubs[component.name] = createBlankStub(component, component.name)
35+
}
36+
2637
return mount(component, {
2738
...options,
2839
components: {

test/specs/shallow-mount.spec.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
389389
AsyncComponent: () => import('~resources/components/component.vue')
390390
}
391391
}
392-
const wrapper = shallowMount(TestComponent)
393-
expect(wrapper.find({ name: 'AsyncComponent' }).exists()).to.equal(true)
392+
shallowMount(TestComponent)
394393
})
395394

396395
it('stubs components registered on localVue after multiple installs', () => {

0 commit comments

Comments
 (0)