Skip to content
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

fix: extend unextended inline constructor #1015

Closed
Closed
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
9 changes: 6 additions & 3 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createSlotVNodes } from './create-slot-vnodes'
import addMocks from './add-mocks'
import { addEventLogger } from './log-events'
import { addStubs } from './add-stubs'
import { patchRender } from './patch-render'
import { throwError, vueVersion } from 'shared/util'
import {
compileTemplate,
Expand Down Expand Up @@ -60,6 +61,7 @@ export default function createInstance (
addEventLogger(_Vue)
addMocks(options.mocks, _Vue)
addStubs(component, options.stubs, _Vue, options.shouldProxy)
patchRender(_Vue)

if (
(component.options && component.options.functional) ||
Expand Down Expand Up @@ -104,11 +106,12 @@ export default function createInstance (
component.options._base = _Vue
}

const componentOptions = typeof component === 'function'
? component.options
: component
// extend component from _Vue to add properties and mixins
// extend does not work correctly for sub class components in Vue < 2.2
const Constructor = typeof component === 'function'
? _Vue.extend(component.options).extend(instanceOptions)
: _Vue.extend(component).extend(instanceOptions)
const Constructor = _Vue.extend(componentOptions).extend(instanceOptions)

// used to identify extended component using constructor
Constructor.options.$_vueTestUtils_original = component
Expand Down
20 changes: 20 additions & 0 deletions packages/create-instance/patch-render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { addHook } from './add-hook'

// This is used to extend component constructors
// used directly in a render function
// see https://github.com/vuejs/vue-test-utils/issues/994
export function patchRender (_Vue) {
addHook(_Vue.options, 'beforeCreate', function () {
const createElementSave = this.$createElement
this.$createElement = function (el, ...args) {
if (
typeof el === 'function' &&
el.super !== _Vue &&
!el.options.$_vueTestUtils_original
) {
el = _Vue.extend(el.options)
}
return createElementSave(el, ...args)
}
})
}
17 changes: 17 additions & 0 deletions test/specs/mounting-options/localVue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
})
})

it('is applied to inline constructor functions', () => {
const ChildComponent = Vue.extend({
render (h) {
h('p', this.$route.params)
}
})
const TestComponent = {
render: h => h(ChildComponent)
}
const localVue = createLocalVue()
localVue.prototype.$route = {}
const wrapper = mountingMethod(TestComponent, {
localVue
})
expect(wrapper.findAll(ChildComponent).length).to.equal(1)
})

itRunIf(
vueVersion < 2.3,
'throws an error if used with an extended component in Vue 2.3', () => {
Expand Down