Skip to content

Commit 0d63f35

Browse files
authored
docs: Add type info about WrapperLike on findComponent (#1436)
1 parent 4551f19 commit 0d63f35

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

docs/api/index.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,12 @@ Finds a Vue Component instance and returns a `VueWrapper` if found. Returns `Err
10961096
**Signature:**
10971097
10981098
```ts
1099-
findComponent<T extends ComponentPublicInstance>(selector: new () => T): VueWrapper<T>
1100-
findComponent<T extends ComponentPublicInstance>(selector: FindComponentSelector): VueWrapper<T>
1099+
findComponent<T extends never>(selector: string): WrapperLike
1100+
findComponent<T extends DefinedComponent>(selector: T | Exclude<FindComponentSelector, FunctionalComponent>): VueWrapper<InstanceType<T>>
1101+
findComponent<T extends FunctionalComponent>(selector: T | string): DOMWrapper<Element>
1102+
findComponent<T extends never>(selector: NameSelector | RefSelector): VueWrapper
1103+
findComponent<T extends ComponentPublicInstance>(selector: T | FindComponentSelector): VueWrapper<T>
1104+
findComponent(selector: FindComponentSelector): WrapperLike
11011105
```
11021106
11031107
**Details:**
@@ -1194,12 +1198,29 @@ expect(childByCss.vm.$options.name).toBe('Root') // => still Root
11941198
The reason for such behavior is that `RootComponent` and `ChildComponent` are sharing same DOM node and only first matching component is included for each unique DOM node
11951199
:::
11961200
1201+
:::info WrapperLike type when using CSS selector
1202+
When using `wrapper.findComponent('.foo')` for example then VTU will return the `WrapperLike` type. This is because functional components
1203+
would need a `DOMWrapper` otherwise a `VueWrapper`. You can force to return a `VueWrapper` by providing the correct component type:
1204+
1205+
```typescript
1206+
wrapper.findComponent('.foo') // returns WrapperLike
1207+
wrapper.findComponent<typeof FooComponent>('.foo') // returns VueWrapper
1208+
wrapper.findComponent<DefineComponent>('.foo') // returns VueWrapper
1209+
```
1210+
:::
1211+
11971212
### findAllComponents
11981213
11991214
**Signature:**
12001215
12011216
```ts
1202-
findAllComponents(selector: { name: string } | string): VueWrapper<T>[]
1217+
findAllComponents<T extends never>(selector: string): WrapperLike[]
1218+
findAllComponents<T extends DefinedComponent>(selector: T | Exclude<FindAllComponentsSelector, FunctionalComponent>): VueWrapper<InstanceType<T>>[]
1219+
findAllComponents<T extends FunctionalComponent>(selector: string): DOMWrapper<Element>[]
1220+
findAllComponents<T extends FunctionalComponent>(selector: T): DOMWrapper<Node>[]
1221+
findAllComponents<T extends never>(selector: NameSelector): VueWrapper[]
1222+
findAllComponents<T extends ComponentPublicInstance>(selector: T | FindAllComponentsSelector): VueWrapper<T>[]
1223+
findAllComponents(selector: FindAllComponentsSelector): WrapperLike[]
12031224
```
12041225
12051226
**Details:**

docs/guide/advanced/component-instance.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ A more thorough way to test this would be asserting against the rendered content
7272
Note: if you are using a `<script setup>` component, `vm` will not be available. That's because `<script setup>` components are [closed by default](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md#exposing-components-public-interface). For these components, and in general, consider avoiding `vm` and asserting against the rendered markup.
7373
:::
7474

75+
:::warning WrapperLike type when using CSS selector
76+
When using `wrapper.findComponent('.foo')` for example then VTU will return the `WrapperLike` type. This is because functional components
77+
would need a `DOMWrapper` otherwise a `VueWrapper`. You can force to return a `VueWrapper` by providing the correct component type:
78+
79+
```typescript
80+
wrapper.findComponent('.foo') // returns WrapperLike
81+
wrapper.findComponent<typeof FooComponent>('.foo') // returns VueWrapper
82+
wrapper.findComponent<DefineComponent>('.foo') // returns VueWrapper
83+
```
84+
:::
85+
7586
## Conclusion
7687

7788
- use `vm` to access the internal Vue instance

0 commit comments

Comments
 (0)