Skip to content

Commit

Permalink
refactor "don't stub" handling
Browse files Browse the repository at this point in the history
  • Loading branch information
freakzlike committed Feb 18, 2023
1 parent f6c2277 commit 1392c4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
16 changes: 7 additions & 9 deletions src/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { attachEmitListener } from './emit'
import { createVNodeTransformer } from './vnodeTransformers/util'
import {
createStubComponentsTransformer,
addToDoNotStubComponents
CreateStubComponentsTransformerConfig
} from './vnodeTransformers/stubComponentsTransformer'
import { createStubDirectivesTransformer } from './vnodeTransformers/stubDirectivesTransformer'
import {
Expand Down Expand Up @@ -315,6 +315,9 @@ export function mount(
let component: ConcreteComponent
const instanceOptions = getInstanceOptions(options ?? {})

const rootComponents: CreateStubComponentsTransformerConfig['rootComponents'] =
{}

if (
isFunctionalComponent(originalComponent) ||
isLegacyFunctionalComponent(originalComponent)
Expand All @@ -335,15 +338,14 @@ export function mount(
h(originalComponent, { ...props, ...attrs }, slots),
...instanceOptions
})
addToDoNotStubComponents(originalComponent)
rootComponents.legacy = originalComponent
} else if (isObjectComponent(originalComponent)) {
component = { ...originalComponent, ...instanceOptions }
} else {
component = originalComponent
}

// Don't stub component only on root level, but still stub if used inside
addToDoNotStubComponents(component, true)
rootComponents.component = component
// We've just replaced our component with its copy
// Let's register it as a stub so user can find it
registerStub({ source: originalComponent, stub: component })
Expand Down Expand Up @@ -465,11 +467,6 @@ export function mount(

// create the app
const app = createApp(Parent)
// the Parent type must not be stubbed
// but we can't add it directly, as createApp creates a copy
// and store it in app._component (since v3.2.32)
// So we store this one instead
addToDoNotStubComponents(app._component)

// add tracking for emitted events
// this must be done after `createApp`: https://github.com/vuejs/test-utils/issues/436
Expand Down Expand Up @@ -577,6 +574,7 @@ export function mount(
createVNodeTransformer({
transformers: [
createStubComponentsTransformer({
rootComponents,
stubs: getComponentsFromStubs(global.stubs),
shallow: options?.shallow,
renderStubDefaultSlot: global.renderStubDefaultSlot
Expand Down
34 changes: 17 additions & 17 deletions src/vnodeTransformers/stubComponentsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ interface StubOptions {
renderStubDefaultSlot?: boolean
}

const doNotStubComponents = new WeakMap<ConcreteComponent, boolean>()
const shouldNotStub = (type: ConcreteComponent) => doNotStubComponents.has(type)
const shouldNotStubRoot = (type: ConcreteComponent) =>
!!doNotStubComponents.get(type)
export const addToDoNotStubComponents = (
type: ConcreteComponent,
onlyRoot?: boolean
) => doNotStubComponents.set(type, !!onlyRoot)

const normalizeStubProps = (props: ComponentPropsOptions) => {
// props are always normalized to object syntax
const $props = props as unknown as ComponentObjectPropsOptions
Expand Down Expand Up @@ -106,13 +97,18 @@ const resolveComponentStubByName = (
}
}

interface CreateStubComponentsTransformerConfig {
export interface CreateStubComponentsTransformerConfig {
rootComponents: {
legacy?: Component
component?: Component
}
stubs?: Record<string, Component | boolean>
shallow?: boolean
renderStubDefaultSlot: boolean
}

export function createStubComponentsTransformer({
rootComponents,
stubs = {},
shallow = false,
renderStubDefaultSlot = false
Expand Down Expand Up @@ -166,11 +162,15 @@ export function createStubComponentsTransformer({
})
}

if (shouldNotStub(type)) {
// Either don't stub everytime or only on root level
if (!instance?.parent || !shouldNotStubRoot(type)) {
return type
}
if (
// Don't stub VTU_ROOT component
!instance ||
// Don't stub mounted component on root level
(rootComponents.component === type && !instance?.parent) ||
// Don't stub component with compat wrapper
(rootComponents.legacy && rootComponents.legacy === type)
) {
return type
}

const registeredName = getComponentRegisteredName(instance, type)
Expand Down Expand Up @@ -225,7 +225,7 @@ export function createStubComponentsTransformer({
// Set name when using shallow without stub
const stubName = name || registeredName || componentName

const newStub =
return (
config.plugins.createStubs?.({
name: stubName,
component: type
Expand All @@ -235,7 +235,7 @@ export function createStubComponentsTransformer({
type,
renderStubDefaultSlot
})
return newStub
)
}

return type
Expand Down

0 comments on commit 1392c4a

Please sign in to comment.