Skip to content

Commit e29f288

Browse files
feat: return ComponentPublicInstance from findComponent
1 parent da83990 commit e29f288

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/utils/find.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { VNode } from 'vue/dist/vue'
1+
import { VNode, ComponentPublicInstance } from 'vue'
22

33
function matches(node: VNode, selector): boolean {
44
if (typeof selector === 'string') {
@@ -21,7 +21,7 @@ function aggregateChildren(nodes, children) {
2121
}
2222
}
2323

24-
function findAllVNodes(vnode: VNode, selector: any) {
24+
function findAllVNodes(vnode: VNode, selector: any): VNode[] {
2525
const matchingNodes = []
2626
const nodes = [vnode]
2727
while (nodes.length) {
@@ -36,6 +36,8 @@ function findAllVNodes(vnode: VNode, selector: any) {
3636
return matchingNodes
3737
}
3838

39-
export function find(root: VNode, selector: any) {
40-
return findAllVNodes(root, selector)
39+
export function find(root: VNode, selector: any): ComponentPublicInstance[] {
40+
return findAllVNodes(root, selector).map(
41+
(vnode: VNode) => vnode.component.proxy
42+
)
4143
}

src/vue-wrapper.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class VueWrapper implements WrapperAPI {
7676
}
7777

7878
html() {
79-
return this.appRootNode.innerHTML
79+
return this.parentElement.innerHTML
8080
}
8181

8282
text() {
@@ -102,20 +102,22 @@ export class VueWrapper implements WrapperAPI {
102102
return result
103103
}
104104

105-
findComponent(selector: FindComponentSelector): any {
105+
findComponent(selector: FindComponentSelector): ComponentPublicInstance {
106106
if (typeof selector === 'object' && 'ref' in selector) {
107-
return this.componentVM.$refs[selector.ref]
107+
return this.componentVM.$refs[selector.ref] as ComponentPublicInstance
108108
}
109109
const result = find(this.componentVM.$.subTree, selector)
110-
return result.length ? result[0] : result
110+
return result.length ? result[0] : undefined
111111
}
112112

113-
findAllComponents(selector: FindAllComponentsSelector): any[] {
113+
findAllComponents(
114+
selector: FindAllComponentsSelector
115+
): ComponentPublicInstance[] {
114116
return find(this.componentVM.$.subTree, selector)
115117
}
116118

117119
findAll<T extends Element>(selector: string): DOMWrapper<T>[] {
118-
const results = this.appRootNode.querySelectorAll<T>(selector)
120+
const results = this.parentElement.querySelectorAll<T>(selector)
119121
return Array.from(results).map((x) => new DOMWrapper(x))
120122
}
121123

tests/findComponent.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ describe('findComponent', () => {
1919
// find by ref
2020
expect(wrapper.findComponent({ ref: 'b' })).toBeTruthy()
2121
// find by DOM selector
22-
expect(wrapper.findComponent('.C').type.name).toEqual('ComponentC')
23-
expect(wrapper.findComponent({ name: 'Hello' }).el.textContent).toBe(
22+
expect(wrapper.findComponent('.C').$options.name).toEqual('ComponentC')
23+
expect(wrapper.findComponent({ name: 'Hello' }).$el.textContent).toBe(
2424
'Hello world'
2525
)
26-
expect(wrapper.findComponent(Hello).el.textContent).toBe('Hello world')
26+
expect(wrapper.findComponent(Hello).$el.textContent).toBe('Hello world')
2727
})
2828
})

0 commit comments

Comments
 (0)