Skip to content

Commit

Permalink
fix(find): allow finding root without name
Browse files Browse the repository at this point in the history
  • Loading branch information
xanf committed Aug 27, 2021
1 parent df43338 commit 3849591
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/mount.ts
Expand Up @@ -35,7 +35,12 @@ import {
import { processSlot } from './utils/compileSlots'
import { createWrapper, VueWrapper } from './vueWrapper'
import { attachEmitListener } from './emit'
import { createStub, stubComponents, addToDoNotStubComponents } from './stubs'
import {
createStub,
stubComponents,
addToDoNotStubComponents,
registerStub
} from './stubs'
import {
isLegacyFunctionalComponent,
unwrapLegacyVueExtendComponent
Expand Down Expand Up @@ -244,6 +249,7 @@ export function mount(
}

addToDoNotStubComponents(component)
registerStub(originalComponent, component)
const el = document.createElement('div')

if (options?.attachTo) {
Expand Down
16 changes: 12 additions & 4 deletions src/stubs.ts
Expand Up @@ -26,7 +26,13 @@ interface StubOptions {
renderStubDefaultSlot?: boolean
}

const stubsMap: WeakMap<ConcreteComponent, VNodeTypes> = new WeakMap()
const stubsMap: WeakMap<ConcreteComponent, ConcreteComponent> = new WeakMap()
export const registerStub = (
source: ConcreteComponent,
stub: ConcreteComponent
) => {
stubsMap.set(stub, source)
}

export const getOriginalVNodeTypeFromStub = (
type: ConcreteComponent
Expand Down Expand Up @@ -119,7 +125,7 @@ const getComponentName = (type: VNodeTypes): string => {
}

function createStubOnceForType(
type: {} & VNodeTypes,
type: ConcreteComponent,
factoryFn: () => ConcreteComponent,
cache: WeakMap<{} & VNodeTypes, ConcreteComponent>
): ConcreteComponent {
Expand All @@ -129,7 +135,7 @@ function createStubOnceForType(
}

const stub = factoryFn()
stubsMap.set(stub, type)
registerStub(type, stub)
cache.set(type, stub)
return stub
}
Expand All @@ -143,7 +149,7 @@ export function stubComponents(
new WeakMap()

const createStubOnce = (
type: {} & VNodeTypes,
type: ConcreteComponent,
factoryFn: () => ConcreteComponent
) => createStubOnceForType(type, factoryFn, createdStubsMap)

Expand Down Expand Up @@ -216,6 +222,8 @@ export function stubComponents(
type,
() => specializedStubComponent
)
specializedStub.props = stub.props
registerStub(type, specializedStub)
// pass the props and children, for advanced stubbing
return [specializedStub, props, children, patchFlag, dynamicProps]
}
Expand Down
16 changes: 16 additions & 0 deletions tests/findComponent.spec.ts
Expand Up @@ -113,6 +113,22 @@ describe('findComponent', () => {
expect(wrapper.findComponent(Comp).props('depth')).toBe(0)
})

it('finds root component without name', async () => {
const Comp = defineComponent({
template: `
<input v-model="msg" />
{{ msg }}
`,
data() {
return { msg: 'foo' }
}
})
const wrapper = mount(Comp)
expect(wrapper.findComponent(Comp).exists()).toBe(true)
await wrapper.find('input').setValue('bar')
expect(wrapper.html()).toContain('bar')
})

it('finds component without a name by using its object definition', () => {
const Component = {
template: '<div><component-without-name/></div>',
Expand Down
2 changes: 1 addition & 1 deletion tests/getComponent.spec.ts
Expand Up @@ -48,7 +48,7 @@ describe('getComponent', () => {
})

it('should throw if not found with a component selector that has no name', () => {
const wrapper = mount(compA)
const wrapper = mount(compB)
expect(() => wrapper.getComponent(compA)).toThrowError(
'Unable to get specified component within: <div class="A"></div>'
)
Expand Down

0 comments on commit 3849591

Please sign in to comment.