Skip to content

Fix extended child components #757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { addEventLogger } from './log-events'
import { createComponentStubs } from 'shared/stub-components'
import { throwError, warn, vueVersion } from 'shared/util'
import { compileTemplate } from 'shared/compile-template'
import deleteMountingOptions from './delete-mounting-options'
import extractInstanceOptions from './extract-instance-options'
import createFunctionalComponent from './create-functional-component'
import { componentNeedsCompiling } from 'shared/validators'
import { validateSlots } from './validate-slots'
Expand All @@ -20,6 +20,18 @@ export default function createInstance (
// Remove cached constructor
delete component._Ctor

// mounting options are vue-test-utils specific
//
// instance options are options that are passed to the
// root instance when it's instantiated
//
// component options are the root components options
const componentOptions = typeof component === 'function'
? component.extendOptions
: component

const instanceOptions = extractInstanceOptions(options)

if (options.mocks) {
addMocks(options.mocks, _Vue)
}
Expand All @@ -40,12 +52,6 @@ export default function createInstance (

addEventLogger(_Vue)

const instanceOptions = {
...options
}

deleteMountingOptions(instanceOptions)

const stubComponents = createComponentStubs(
// $FlowIgnore
component.components,
Expand All @@ -67,9 +73,9 @@ export default function createInstance (
)
}
})
Object.keys(component.components || {}).forEach(c => {
Object.keys(componentOptions.components || {}).forEach(c => {
if (
component.components[c].extendOptions &&
componentOptions.components[c].extendOptions &&
!instanceOptions.components[c]
) {
if (options.logModifiedComponents) {
Expand All @@ -82,7 +88,9 @@ export default function createInstance (
`option.`
)
}
instanceOptions.components[c] = _Vue.extend(component.components[c])
instanceOptions.components[c] = _Vue.extend(
componentOptions.components[c]
)
}
})

Expand Down
12 changes: 0 additions & 12 deletions packages/create-instance/delete-mounting-options.js

This file was deleted.

20 changes: 20 additions & 0 deletions packages/create-instance/extract-instance-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const MOUNTING_OPTIONS = [
'attachToDocument',
'mocks',
'slots',
'localVue',
'stubs',
'context',
'clone',
'attrs',
'listeners',
'propsData'
]

export default function extractInstanceOptions (options) {
const instanceOptions = { ...options }
MOUNTING_OPTIONS.forEach(mountingOption => {
delete instanceOptions[mountingOption]
})
return instanceOptions
}
15 changes: 15 additions & 0 deletions test/specs/mounting-options/localVue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
}
})

it('is applies to child extended components', () => {
const ChildComponent = Vue.extend({
template: '<div>{{$route.params}}</div>'
})
const TestComponent = Vue.extend({
template: '<child-component />',
components: { ChildComponent }
})
const localVue = createLocalVue()
localVue.prototype.$route = {}
mountingMethod(TestComponent, {
localVue
})
})

it('does not add created mixin to localVue', () => {
const localVue = createLocalVue()
mountingMethod({ render: () => {} }, {
Expand Down