Skip to content

fix: support async components in stubs #1039

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 3 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
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'
? 'DymaicHello2-stub'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 😅

: 'dynamichello2-stub'
)
expect(HTML).to.contain('h1')
})
})