Skip to content

Commit

Permalink
fix: stub child components (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyerburgh authored Jun 17, 2018
1 parent 7302e05 commit bc736fb
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 32 deletions.
22 changes: 14 additions & 8 deletions packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ export default function createInstance (
...stubComponents
}
}

_Vue.mixin({
created () {
Object.assign(
this.$options.components,
stubComponents
)
}
})
Object.keys(component.components || {}).forEach(c => {
if (
component.components[c].extendOptions &&
Expand All @@ -79,14 +86,13 @@ export default function createInstance (
}
})

Object.keys(stubComponents).forEach(c => {
_Vue.component(c, stubComponents[c])
})
if (component.options) {
component.options._base = _Vue
}

const Constructor =
vueVersion < 2.3 && typeof component === 'function'
? component.extend(instanceOptions)
: _Vue.extend(component).extend(instanceOptions)
const Constructor = vueVersion < 2.3 && typeof component === 'function'
? component.extend(instanceOptions)
: _Vue.extend(component).extend(instanceOptions)

Object.keys(instanceOptions.components || {}).forEach(key => {
Constructor.component(key, instanceOptions.components[key])
Expand Down
8 changes: 6 additions & 2 deletions packages/server-test-utils/src/renderToString.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export default function renderToString (
if (options.attachToDocument) {
throwError(`you cannot use attachToDocument with ` + `renderToString`)
}
const vueClass = options.localVue || testUtils.createLocalVue()
const vm = createInstance(component, mergeOptions(options, config), vueClass)
const vueConstructor = testUtils.createLocalVue(options.localVue)
const vm = createInstance(
component,
mergeOptions(options, config),
vueConstructor
)
let renderedString = ''

// $FlowIgnore
Expand Down
8 changes: 4 additions & 4 deletions packages/test-utils/src/create-local-vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import Vue from 'vue'
import cloneDeep from 'lodash/cloneDeep'
import errorHandler from './error-handler'

function createLocalVue (): Component {
const instance = Vue.extend()
function createLocalVue (_Vue: Component = Vue): Component {
const instance = _Vue.extend()

// clone global APIs
Object.keys(Vue).forEach(key => {
Object.keys(_Vue).forEach(key => {
if (!instance.hasOwnProperty(key)) {
const original = Vue[key]
const original = _Vue[key]
instance[key] =
typeof original === 'object' ? cloneDeep(original) : original
}
Expand Down
3 changes: 1 addition & 2 deletions packages/test-utils/src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export default function mount (

// Remove cached constructor
delete component._Ctor

const vueConstructor = options.localVue || createLocalVue()
const vueConstructor = createLocalVue(options.localVue)

const elm = options.attachToDocument ? createElement() : undefined

Expand Down
11 changes: 3 additions & 8 deletions test/specs/mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,15 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'mount', () => {
}
})

it('returns new VueWrapper with mounted Vue instance initialized with Vue.extend with props, if passed as propsData', () => {
const prop1 = { test: 'TEST' }
it('handles propsData for extended components', () => {
const prop1 = 'test'
const TestComponent = Vue.extend(ComponentWithProps)
const wrapper = mount(TestComponent, {
propsData: {
prop1
}
})
expect(wrapper.vm).to.be.an('object')
if (wrapper.vm.$props) {
expect(wrapper.vm.$props.prop1).to.equal(prop1)
} else {
expect(wrapper.vm.$options.propsData.prop1).to.equal(prop1)
}
expect(wrapper.text()).to.contain(prop1)
})

it('handles uncompiled extended Vue component', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/specs/mounting-options/localVue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ describeWithMountingMethods('options.localVue', mountingMethod => {
expect(HTML).to.contain('2')
}
})

it('does not add created mixin to localVue', () => {
const localVue = createLocalVue()
mountingMethod({ render: () => {} }, {
localVue
})
expect(localVue.options.created).to.equal(undefined)
})
})
46 changes: 46 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,52 @@ describeWithMountingMethods('options.stub', mountingMethod => {
expect(HTML).to.contain('<registered-component-stub>')
})

itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'stubs nested components', () => {
const GrandchildComponent = {
template: '<span />'
}
const ChildComponent = {
template: '<grandchild-component />',
components: { GrandchildComponent }
}
const TestComponent = {
template: '<child-component />',
components: { ChildComponent }
}
const wrapper = mountingMethod(TestComponent, {
stubs: ['grandchild-component']
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).not.to.contain('<span>')
})

itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'stubs nested components on extended components', () => {
const GrandchildComponent = {
template: '<span />'
}
const ChildComponent = {
template: '<grandchild-component />',
components: { GrandchildComponent }
}
const TestComponent = {
template: '<div><child-component /></div>',
components: { ChildComponent }
}
const wrapper = mountingMethod(Vue.extend(TestComponent), {
stubs: ['grandchild-component']
})
const HTML = mountingMethod.name === 'renderToString'
? wrapper
: wrapper.html()
expect(HTML).not.to.contain('<span>')
})

it('stubs components with dummy when passed a boolean', () => {
const ComponentWithGlobalComponent = {
render: h => h('div', [h('registered-component')])
Expand Down
15 changes: 7 additions & 8 deletions test/specs/shallow-mount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,13 @@ describeRunIf(process.env.TEST_ENV !== 'node', 'shallowMount', () => {
expect(wrapper.find('p').exists()).to.equal(false)
})

it('stubs Vue class component children', () => {
if (vueVersion < 2.3) {
return
}
const wrapper = shallowMount(ComponentAsAClassWithChild)
expect(wrapper.find(Component).exists()).to.equal(true)
expect(wrapper.findAll('div').length).to.equal(1)
})
itDoNotRunIf(
vueVersion < 2.3,
'stubs Vue class component children', () => {
const wrapper = shallowMount(ComponentAsAClassWithChild)
expect(wrapper.find(Component).exists()).to.equal(true)
expect(wrapper.findAll('div').length).to.equal(1)
})

it('works correctly with find, contains, findAll, and is on unnamed components', () => {
const TestComponent = {
Expand Down

0 comments on commit bc736fb

Please sign in to comment.