Skip to content

Commit bc5aba3

Browse files
authored
fix: extend extended child components (#757)
1 parent 2068208 commit bc5aba3

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

Diff for: packages/create-instance/create-instance.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { addEventLogger } from './log-events'
66
import { createComponentStubs } from 'shared/stub-components'
77
import { throwError, warn, vueVersion } from 'shared/util'
88
import { compileTemplate } from 'shared/compile-template'
9-
import deleteMountingOptions from './delete-mounting-options'
9+
import extractInstanceOptions from './extract-instance-options'
1010
import createFunctionalComponent from './create-functional-component'
1111
import { componentNeedsCompiling } from 'shared/validators'
1212
import { validateSlots } from './validate-slots'
@@ -20,6 +20,18 @@ export default function createInstance (
2020
// Remove cached constructor
2121
delete component._Ctor
2222

23+
// mounting options are vue-test-utils specific
24+
//
25+
// instance options are options that are passed to the
26+
// root instance when it's instantiated
27+
//
28+
// component options are the root components options
29+
const componentOptions = typeof component === 'function'
30+
? component.extendOptions
31+
: component
32+
33+
const instanceOptions = extractInstanceOptions(options)
34+
2335
if (options.mocks) {
2436
addMocks(options.mocks, _Vue)
2537
}
@@ -40,12 +52,6 @@ export default function createInstance (
4052

4153
addEventLogger(_Vue)
4254

43-
const instanceOptions = {
44-
...options
45-
}
46-
47-
deleteMountingOptions(instanceOptions)
48-
4955
const stubComponents = createComponentStubs(
5056
// $FlowIgnore
5157
component.components,
@@ -67,9 +73,9 @@ export default function createInstance (
6773
)
6874
}
6975
})
70-
Object.keys(component.components || {}).forEach(c => {
76+
Object.keys(componentOptions.components || {}).forEach(c => {
7177
if (
72-
component.components[c].extendOptions &&
78+
componentOptions.components[c].extendOptions &&
7379
!instanceOptions.components[c]
7480
) {
7581
if (options.logModifiedComponents) {
@@ -82,7 +88,9 @@ export default function createInstance (
8288
`option.`
8389
)
8490
}
85-
instanceOptions.components[c] = _Vue.extend(component.components[c])
91+
instanceOptions.components[c] = _Vue.extend(
92+
componentOptions.components[c]
93+
)
8694
}
8795
})
8896

Diff for: packages/create-instance/delete-mounting-options.js

-12
This file was deleted.

Diff for: packages/create-instance/extract-instance-options.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const MOUNTING_OPTIONS = [
2+
'attachToDocument',
3+
'mocks',
4+
'slots',
5+
'localVue',
6+
'stubs',
7+
'context',
8+
'clone',
9+
'attrs',
10+
'listeners',
11+
'propsData'
12+
]
13+
14+
export default function extractInstanceOptions (options) {
15+
const instanceOptions = { ...options }
16+
MOUNTING_OPTIONS.forEach(mountingOption => {
17+
delete instanceOptions[mountingOption]
18+
})
19+
return instanceOptions
20+
}

Diff for: test/specs/mounting-options/localVue.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
7070
}
7171
})
7272

73+
it('is applies to child extended components', () => {
74+
const ChildComponent = Vue.extend({
75+
template: '<div>{{$route.params}}</div>'
76+
})
77+
const TestComponent = Vue.extend({
78+
template: '<child-component />',
79+
components: { ChildComponent }
80+
})
81+
const localVue = createLocalVue()
82+
localVue.prototype.$route = {}
83+
mountingMethod(TestComponent, {
84+
localVue
85+
})
86+
})
87+
7388
it('does not add created mixin to localVue', () => {
7489
const localVue = createLocalVue()
7590
mountingMethod({ render: () => {} }, {

0 commit comments

Comments
 (0)