Skip to content

Commit 6541c3d

Browse files
authored
fix(find): allow finding root without name (#836)
1 parent ad2adfe commit 6541c3d

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

src/mount.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ import {
3535
import { processSlot } from './utils/compileSlots'
3636
import { createWrapper, VueWrapper } from './vueWrapper'
3737
import { attachEmitListener } from './emit'
38-
import { createStub, stubComponents, addToDoNotStubComponents } from './stubs'
38+
import {
39+
createStub,
40+
stubComponents,
41+
addToDoNotStubComponents,
42+
registerStub
43+
} from './stubs'
3944
import {
4045
isLegacyFunctionalComponent,
4146
unwrapLegacyVueExtendComponent
@@ -244,6 +249,7 @@ export function mount(
244249
}
245250

246251
addToDoNotStubComponents(component)
252+
registerStub(originalComponent, component)
247253
const el = document.createElement('div')
248254

249255
if (options?.attachTo) {

src/stubs.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ interface StubOptions {
2626
renderStubDefaultSlot?: boolean
2727
}
2828

29-
const stubsMap: WeakMap<ConcreteComponent, VNodeTypes> = new WeakMap()
29+
const stubsMap: WeakMap<ConcreteComponent, ConcreteComponent> = new WeakMap()
30+
export const registerStub = (
31+
source: ConcreteComponent,
32+
stub: ConcreteComponent
33+
) => {
34+
stubsMap.set(stub, source)
35+
}
3036

3137
export const getOriginalVNodeTypeFromStub = (
3238
type: ConcreteComponent
@@ -119,7 +125,7 @@ const getComponentName = (type: VNodeTypes): string => {
119125
}
120126

121127
function createStubOnceForType(
122-
type: {} & VNodeTypes,
128+
type: ConcreteComponent,
123129
factoryFn: () => ConcreteComponent,
124130
cache: WeakMap<{} & VNodeTypes, ConcreteComponent>
125131
): ConcreteComponent {
@@ -129,7 +135,7 @@ function createStubOnceForType(
129135
}
130136

131137
const stub = factoryFn()
132-
stubsMap.set(stub, type)
138+
registerStub(type, stub)
133139
cache.set(type, stub)
134140
return stub
135141
}
@@ -143,7 +149,7 @@ export function stubComponents(
143149
new WeakMap()
144150

145151
const createStubOnce = (
146-
type: {} & VNodeTypes,
152+
type: ConcreteComponent,
147153
factoryFn: () => ConcreteComponent
148154
) => createStubOnceForType(type, factoryFn, createdStubsMap)
149155

@@ -216,6 +222,8 @@ export function stubComponents(
216222
type,
217223
() => specializedStubComponent
218224
)
225+
specializedStub.props = stub.props
226+
registerStub(type, specializedStub)
219227
// pass the props and children, for advanced stubbing
220228
return [specializedStub, props, children, patchFlag, dynamicProps]
221229
}

tests/findComponent.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ describe('findComponent', () => {
9797
expect(wrapper.findComponent(Comp).props('depth')).toBe(0)
9898
})
9999

100+
it('finds root component without name', async () => {
101+
const Comp = defineComponent({
102+
template: `
103+
<input v-model="msg" />
104+
{{ msg }}
105+
`,
106+
data() {
107+
return { msg: 'foo' }
108+
}
109+
})
110+
const wrapper = mount(Comp)
111+
expect(wrapper.findComponent(Comp).exists()).toBe(true)
112+
await wrapper.find('input').setValue('bar')
113+
expect(wrapper.html()).toContain('bar')
114+
})
115+
100116
it('finds component without a name by using its object definition', () => {
101117
const Component = {
102118
template: '<div><component-without-name/></div>',

tests/getComponent.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('getComponent', () => {
4949
})
5050

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

0 commit comments

Comments
 (0)