Skip to content

Commit c48f0f6

Browse files
feat: add findComponent and findAllComponents
1 parent f321669 commit c48f0f6

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/api/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,85 @@ test('findAll', () => {
520520
})
521521
```
522522

523+
### `findComponent`
524+
525+
Finds a Vue Component instance and returns a `VueWrapper` if one is found, otherwise returns `ErrorWrapper`.
526+
527+
**Supported syntax:**
528+
529+
* **querySelector** - `findComponent('.component')` - Matches standard query selector.
530+
* **Name** - `findComponent({ name: 'myComponent' })` - matches PascalCase, snake-case, camelCase
531+
* **ref** - `findComponent({ ref: 'dropdown' })` - Can be used only on direct ref children of mounted component
532+
* **SFC** - `findComponent(ImportedComponent)` - Pass an imported component directly.
533+
534+
```vue
535+
<template>
536+
<div class="foo">
537+
Foo
538+
</div>
539+
</template>
540+
<script>
541+
export default { name: 'Foo' }
542+
</script>
543+
```
544+
545+
```vue
546+
<template>
547+
<div>
548+
<span>Span</span>
549+
<Foo data-test="foo" ref="foo"/>
550+
</div>
551+
</template>
552+
```
553+
554+
```js
555+
test('find', () => {
556+
const wrapper = mount(Component)
557+
558+
wrapper.find('.foo') //=> found; returns VueWrapper
559+
wrapper.find('[data-test="foo"]') //=> found; returns VueWrapper
560+
wrapper.find({ name: 'Foo' }) //=> found; returns VueWrapper
561+
wrapper.find({ name: 'foo' }) //=> found; returns VueWrapper
562+
wrapper.find({ ref: 'foo' }) //=> found; returns VueWrapper
563+
wrapper.find(Foo) //=> found; returns VueWrapper
564+
})
565+
```
566+
567+
### `findAllComponents`
568+
569+
Similar to `findComponent` but finds all Vue Component instances that match the query and returns an array of `VueWrapper`.
570+
571+
**Supported syntax:**
572+
573+
* **querySelector** - `findAllComponents('.component')`
574+
* **Name** - `findAllComponents({ name: 'myComponent' })`
575+
* **SFC** - `findAllComponents(ImportedComponent)`
576+
577+
**Note** - `Ref` is not supported here.
578+
579+
580+
```vue
581+
<template>
582+
<div>
583+
<FooComponent
584+
v-for="number in [1, 2, 3]"
585+
:key="number"
586+
data-test="number"
587+
>
588+
{{ number }}
589+
</FooComponent>
590+
</div>
591+
</template>
592+
```
593+
594+
```js
595+
test('findAllComponents', () => {
596+
const wrapper = mount(Component)
597+
598+
wrapper.findAllComponents('[data-test="number"]') //=> found; returns array of VueWrapper
599+
})
600+
```
601+
523602
### `trigger`
524603

525604
Triggers an event, for example `click`, `submit` or `keyup`. Since events often cause a re-render, `trigger` returns `Vue.nextTick`. If you expect the event to trigger a re-render, you should use `await` when you call `trigger` to ensure that Vue updates the DOM before you make an assertion.

0 commit comments

Comments
 (0)