Skip to content

chore: add definitions for findComponent and findAllComponents #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/wrapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ A `Wrapper` is an object that contains a mounted component or vnode and methods
!!!include(docs/api/wrapper/exists.md)!!!
!!!include(docs/api/wrapper/find.md)!!!
!!!include(docs/api/wrapper/findAll.md)!!!
!!!include(docs/api/wrapper/findComponent.md)!!!
!!!include(docs/api/wrapper/findAllComponents.md)!!!
!!!include(docs/api/wrapper/html.md)!!!
!!!include(docs/api/wrapper/get.md)!!!
!!!include(docs/api/wrapper/is.md)!!!
Expand Down
27 changes: 6 additions & 21 deletions docs/api/wrapper/find.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Returns `Wrapper` of first DOM node or Vue component matching selector.

Use any valid [selector](../selectors.md).
Use any valid DOM selector (uses `querySelector` syntax).

- **Arguments:**

- `{string|Component} selector`
- `{string} selector`

- **Returns:** `{Wrapper}`

Expand All @@ -20,34 +20,19 @@ import Bar from './Bar.vue'
const wrapper = mount(Foo)

const div = wrapper.find('div')
expect(div.is('div')).toBe(true)
expect(div.exists()).toBe(true)

const bar = wrapper.find(Bar)
expect(bar.is(Bar)).toBe(true)

const barByName = wrapper.find({ name: 'bar' })
expect(barByName.is(Bar)).toBe(true)

const fooRef = wrapper.find({ ref: 'foo' })
expect(fooRef.is(Foo)).toBe(true)
const byId = wrapper.find('#bar')
expect(byId.element.id).toBe('bar')
```

- **Note:**

- When chaining `find` calls together, only DOM selectors can be used
- You may chain `find` calls together:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


```js
let button

// Will throw an error
button = wrapper.find({ ref: 'testButton' })
expect(button.find(Icon).exists()).toBe(true)

// Will throw an error
button = wrapper.find({ ref: 'testButton' })
expect(button.find({ name: 'icon' }).exists()).toBe(true)

// Will work as expected
button = wrapper.find({ ref: 'testButton' })
expect(button.find('.icon').exists()).toBe(true)
```
Expand Down
23 changes: 23 additions & 0 deletions docs/api/wrapper/findAllComponents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## findAllComponents

Returns a [`WrapperArray`](../wrapper-array/) of all matching Vue components.

- **Arguments:**

- `{Component|ref|name} selector`

- **Returns:** `{WrapperArray}`

- **Example:**

```js
import { mount } from '@vue/test-utils'
import Foo from './Foo.vue'
import Bar from './Bar.vue'

const wrapper = mount(Foo)
const bar = wrapper.findAllComponents(Bar).at(0)
expect(bar.exists()).toBeTruthy()
const bars = wrapper.findAllComponents(Bar)
expect(bar).toHaveLength(1)
```
26 changes: 26 additions & 0 deletions docs/api/wrapper/findComponent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## findComponent

Returns `Wrapper` of first matching Vue component.

- **Arguments:**

- `{Component|ref|name} selector`

- **Returns:** `{Wrapper}`

- **Example:**

```js
import { mount } from '@vue/test-utils'
import Foo from './Foo.vue'
import Bar from './Bar.vue'

const wrapper = mount(Foo)

const bar = wrapper.findComponent(Bar) //=> finds Bar by component instance
expect(bar.exists()).toBe(true)
const barByName = wrapper.findComponent({ name: 'bar' }) //=> finds Bar by `name`
expect(barByName.exists()).toBe(true)
const barRef = wrapper.findComponent({ ref: 'bar' }) //=> finds Bar by `ref`
expect(barRef.exists()).toBe(true)
```
14 changes: 14 additions & 0 deletions packages/test-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ interface BaseWrapper {
classes(className: string): boolean
props(): { [name: string]: any }
props(key: string): any | void
overview(): void

is (selector: Selector): boolean
isEmpty (): boolean
Expand Down Expand Up @@ -99,6 +100,18 @@ export interface Wrapper<V extends Vue | null> extends BaseWrapper {
findAll (selector: RefSelector): WrapperArray<Vue>
findAll (selector: NameSelector): WrapperArray<Vue>

findComponent<R extends Vue> (selector: VueClass<R>): Wrapper<R>
findComponent<R extends Vue> (selector: ComponentOptions<R>): Wrapper<R>
findComponent<Props = DefaultProps, PropDefs = PropsDefinition<Props>>(selector: FunctionalComponentOptions<Props, PropDefs>): Wrapper<Vue>
findComponent (selector: RefSelector): Wrapper<Vue>
findComponent (selector: NameSelector): Wrapper<Vue>

findAllComponents<R extends Vue> (selector: VueClass<R>): WrapperArray<R>
findAllComponents<R extends Vue> (selector: ComponentOptions<R>): WrapperArray<R>
findAllComponents<Props = DefaultProps, PropDefs = PropsDefinition<Props>>(selector: FunctionalComponentOptions<Props, PropDefs>): WrapperArray<Vue>
findAllComponents(selector: RefSelector): WrapperArray<Vue>
findAllComponents(selector: NameSelector): WrapperArray<Vue>

html (): string
text (): string
name (): string
Expand All @@ -123,6 +136,7 @@ export interface WrapperArray<V extends Vue> extends BaseWrapper {
}

interface WrapperOptions {
attachTo?: Element | string
attachedToDocument?: boolean
}

Expand Down
9 changes: 9 additions & 0 deletions packages/test-utils/types/test/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ wrapper.trigger('mousedown.enter', {
button: 0
})

wrapper.findAllComponents({ name: 'foo' })
wrapper.findComponent({ name: 'foo' })

wrapper.findAllComponents({ ref: 'foo' })
wrapper.findComponent({ ref: 'foo' })

wrapper.findAllComponents(ClassComponent)
wrapper.findComponent(ClassComponent)

/**
* Tests for Wrapper API
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/test-utils/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"lib": ["es2015", "es2017", "dom"],
"module": "es2015",
"module": "esnext",
"target": "ES2017",
"moduleResolution": "node",
"strict": true,
"noEmit": true,
Expand Down