Skip to content

Commit

Permalink
fix: find component by its definition when using stub without name
Browse files Browse the repository at this point in the history
  • Loading branch information
xanf committed Jun 28, 2021
1 parent 181cd17 commit 9c9e8bf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
ComponentOptions,
defineComponent,
VNodeProps,
VNodeTypes
VNodeTypes,
ConcreteComponent
} from 'vue'
import { hyphenate } from './utils/vueShared'
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
Expand All @@ -23,7 +24,7 @@ interface StubOptions {
renderStubDefaultSlot?: boolean
}

const stubsMap: WeakMap<ComponentOptions, VNodeTypes> = new WeakMap()
const stubsMap: WeakMap<ConcreteComponent, VNodeTypes> = new WeakMap()

export const getOriginalVNodeTypeFromStub = (
type: ComponentOptions
Expand Down Expand Up @@ -184,6 +185,7 @@ export function stubComponents(

// case 2: custom implementation
if (stub && stub !== true) {
stubsMap.set(stubs[name], type)
// pass the props and children, for advanced stubbing
return [stubs[name], props, children, patchFlag, dynamicProps]
}
Expand Down
18 changes: 18 additions & 0 deletions tests/findComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ describe('findComponent', () => {
expect(wrapper.findComponent(Hello).exists()).toBe(true)
})

it('finds a component by its definition when using stub without name', () => {
const StubWithoutName = {
template: '<div>stub-without-name</div>'
}

const Component = {
template: '<div><other-name /></div>',
components: {
OtherName: Hello
}
}

const wrapper = mount(Component, {
global: { stubs: { Hello: StubWithoutName } }
})
expect(wrapper.findComponent(Hello).exists()).toBe(true)
})

it('finds a component without a name by its locally assigned name', () => {
const Component = {
template: '<div><component-without-name/></div>',
Expand Down

0 comments on commit 9c9e8bf

Please sign in to comment.