Skip to content

Commit c90f597

Browse files
TinyWispyinquanafontcu
authored
add the wrapper.getComponent method and corresponding documents. (#1714)
* feat: add the wrapper.getComponent method docs: create the /docs/api/wrapper/getComponent.md, and add the deprecation message to the /docs/api/wrapper/get.md docs(zh): create the /docs/zh/api/wrapper/getComponent.md, and add the deprecation message to the /docs/zh/api/wrapper/get.md * Update packages/test-utils/src/wrapper.js Co-authored-by: Adrià Fontcuberta <[email protected]> * Update docs/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <[email protected]> * Update docs/zh/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <[email protected]> * Update docs/zh/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <[email protected]> * Update docs/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <[email protected]> Co-authored-by: yinquan <[email protected]> Co-authored-by: Adrià Fontcuberta <[email protected]>
1 parent 16790b9 commit c90f597

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

docs/api/wrapper/get.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## get
22

3+
::: warning Deprecation warning
4+
Using `get` to search for a Component is deprecated and will be removed. Use [`getComponent`](./getComponent.md) instead.
5+
:::
6+
37
Works just like [find](./find.md) but will throw an error if nothing matching
48
the given selector is found. You should use `find` when searching for an element
59
that may not exist. You should use this method when getting an element that should

docs/api/wrapper/getComponent.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## getComponent
2+
3+
Works just like [findComponent](./findComponent.md) but will throw an error if nothing matching
4+
the given selector is found. You should use `findComponent` when searching for an element
5+
that may not exist. You should use this method when getting an element that should
6+
exist and it will provide a nice error message if that is not the case.
7+
8+
```js
9+
import { mount } from '@vue/test-utils'
10+
import Foo from './Foo.vue'
11+
import Bar from './Bar.vue'
12+
13+
const wrapper = mount(Foo)
14+
15+
// similar to `wrapper.findComponent`.
16+
// `getComponent` will throw an error if an element is not found. `findComponent` will do nothing.
17+
expect(wrapper.getComponent(Bar)) // => gets Bar by component instance
18+
expect(wrapper.getComponent({ name: 'bar' })) // => gets Bar by `name`
19+
expect(wrapper.getComponent({ ref: 'bar' })) // => gets Bar by `ref`
20+
21+
expect(() => wrapper.getComponent({ name: 'does-not-exist' }))
22+
.to.throw()
23+
.with.property(
24+
'message',
25+
"Unable to get a component named 'does-not-exist' within: <div>the actual DOM here...</div>"
26+
)
27+
```

docs/zh/api/wrapper/get.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## get
22

3+
::: warning 废弃警告
4+
使用 `get` 搜索组件的方式已经被废弃并会被移除。请换用 [`getComponent`](./getComponent.md)
5+
:::
6+
37
[find](./find.md) 工作起来一样,但是如果未匹配到给定的选择器时会抛出错误。当搜索一个可能不存在的元素时你应该使用 `find`。当获取一个应该存在的元素时你应该使用这个方法,并且如果没有找到的话它会提供一则友好的错误信息。
48

59
```js

docs/zh/api/wrapper/getComponent.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## getComponent
2+
3+
[findComponent](./findComponent.md) 工作起来一样,但是如果未匹配到给定的选择器时会抛出错误。当搜索一个可能不存在的元素时你应该使用 `findComponent`。当获取一个应该存在的元素时你应该使用这个方法,并且如果没有找到的话它会提供一则友好的错误信息。
4+
5+
```js
6+
import { mount } from '@vue/test-utils'
7+
import Foo from './Foo.vue'
8+
import Bar from './Bar.vue'
9+
10+
const wrapper = mount(Foo)
11+
12+
// 和 `wrapper.findComponent` 相似。
13+
// 如果 `getComponent` 没有找到任何元素将会抛出一个而错误。`findComponent` 则不会做任何事。
14+
expect(wrapper.getComponent(Bar)) // => gets Bar by component instance
15+
expect(wrapper.getComponent({ name: 'bar' })) // => gets Bar by `name`
16+
expect(wrapper.getComponent({ ref: 'bar' })) // => gets Bar by `ref`
17+
18+
expect(() => wrapper.getComponent({ name: 'does-not-exist' }))
19+
.to.throw()
20+
.with.property(
21+
'message',
22+
"Unable to get a component named 'does-not-exist' within: <div>the actual DOM here...</div>"
23+
)
24+
```

packages/test-utils/src/wrapper.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@ export default class Wrapper implements BaseWrapper {
237237
return found
238238
}
239239

240+
/**
241+
* Gets first node in tree of the current wrapper that
242+
* matches the provided selector.
243+
*/
244+
getComponent(rawSelector: Selector): Wrapper {
245+
this.__warnIfDestroyed()
246+
247+
const found = this.findComponent(rawSelector)
248+
if (found instanceof ErrorWrapper) {
249+
throw new Error(`Unable to get ${rawSelector} within: ${this.html()}`)
250+
}
251+
return found
252+
}
253+
240254
/**
241255
* Finds first DOM node in tree of the current wrapper that
242256
* matches the provided selector.
@@ -248,7 +262,7 @@ export default class Wrapper implements BaseWrapper {
248262
if (selector.type !== DOM_SELECTOR) {
249263
warnDeprecated(
250264
'finding components with `find` or `get`',
251-
'Use `findComponent` instead'
265+
'Use `findComponent` and `getComponent` instead'
252266
)
253267
}
254268

0 commit comments

Comments
 (0)