Skip to content

Commit

Permalink
fix: support async components in stubs (#1039)
Browse files Browse the repository at this point in the history
Fixes #1026
  • Loading branch information
eddyerburgh authored Nov 25, 2018
1 parent e1fd705 commit 6a4e19d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
camelize,
capitalize,
hyphenate
} from './util'
} from '../shared/util'
import {
componentNeedsCompiling,
templateContainsComponent,
isVueComponent
} from './validators'
} from '../shared/validators'
import {
compileTemplate,
compileFromString
} from './compile-template'
} from '../shared/compile-template'

function isVueComponentStub (comp): boolean {
return comp && comp.template || isVueComponent(comp)
Expand Down Expand Up @@ -68,10 +68,12 @@ export function createStubFromComponent (
originalComponent: Component,
name: string
): Component {
const componentOptions = typeof originalComponent === 'function'
? originalComponent.extendOptions
: originalComponent
const tagName = `${name}-stub`
const componentOptions =
typeof originalComponent === 'function' && originalComponent.cid
? originalComponent.extendOptions
: originalComponent

const tagName = `${name || 'anonymous'}-stub`

// ignoreElements does not exist in Vue 2.0.x
if (Vue.config.ignoredElements) {
Expand Down Expand Up @@ -112,9 +114,10 @@ export function createStubFromString (
throwError('options.stub cannot contain a circular reference')
}

const componentOptions = typeof originalComponent === 'function'
? originalComponent.extendOptions
: originalComponent
const componentOptions =
typeof originalComponent === 'function' && originalComponent.cid
? originalComponent.extendOptions
: originalComponent

return {
...getCoreProperties(componentOptions),
Expand Down Expand Up @@ -152,9 +155,10 @@ export function createStubsFromStubsObject (
}

if (typeof stub === 'string') {
const component = resolveComponent(originalComponents, stubName)
acc[stubName] = createStubFromString(
stub,
originalComponents[stubName],
component,
stubName
)
return acc
Expand Down
2 changes: 1 addition & 1 deletion packages/create-instance/create-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import createFunctionalComponent from './create-functional-component'
import { componentNeedsCompiling, isPlainObject } from 'shared/validators'
import { validateSlots } from './validate-slots'
import createScopedSlots from './create-scoped-slots'
import { createStubsFromStubsObject } from 'shared/create-component-stubs'
import { createStubsFromStubsObject } from './create-component-stubs'
import { patchRender } from './patch-render'

function vueExtendUnsupportedOption (option: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-instance/patch-render.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStubFromComponent } from 'shared/create-component-stubs'
import { createStubFromComponent } from './create-component-stubs'
import { resolveComponent, semVerGreaterThan } from 'shared/util'
import { isReservedTag } from 'shared/validators'
import { addHook } from './add-hook'
Expand Down
35 changes: 35 additions & 0 deletions test/specs/mounting-options/stubs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,39 @@ describeWithMountingMethods('options.stub', mountingMethod => {
expect(fn).to.throw()
.with.property('message', message)
})

it('works with async components', () => {
const StubComponent = {
template: '<h1 />'
}
const TestComponent = {
template: `<div>
<dynamic-hello />
<dynamic-hello-2 />
<dynamic-hello-3 />
</div>`,
components: {
DynamicHello: () => import('~resources/components/component.vue'),
DynamicHello2: () => import('~resources/components/component.vue'),
DynamicHello3: () => import('~resources/components/component.vue')
}
}
const wrapper = mountingMethod(TestComponent, {
stubs: {
DynamicHello: '<span />',
DynamicHello2: true,
DynamicHello3: StubComponent
}
})
const HTML =
mountingMethod.name === 'renderToString' ? wrapper : wrapper.html()

expect(HTML).to.contain('span')
expect(HTML).to.contain(
mountingMethod.name === 'renderToString'
? 'DynamicHello2-stub'
: 'dynamichello2-stub'
)
expect(HTML).to.contain('h1')
})
})

0 comments on commit 6a4e19d

Please sign in to comment.