Skip to content

Commit

Permalink
fix: extend extended child components (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh authored Jun 25, 2018
1 parent 2068208 commit bc5aba3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
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

0 comments on commit bc5aba3

Please sign in to comment.