-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <afontcu@gmail.com> * Update docs/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com> * Update docs/zh/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com> * Update docs/zh/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com> * Update docs/api/wrapper/getComponent.md Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com> Co-authored-by: yinquan <gogs@fake.local> Co-authored-by: Adrià Fontcuberta <afontcu@gmail.com>
- Loading branch information
1 parent
16790b9
commit c90f597
Showing
5 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## getComponent | ||
|
||
Works just like [findComponent](./findComponent.md) but will throw an error if nothing matching | ||
the given selector is found. You should use `findComponent` when searching for an element | ||
that may not exist. You should use this method when getting an element that should | ||
exist and it will provide a nice error message if that is not the case. | ||
|
||
```js | ||
import { mount } from '@vue/test-utils' | ||
import Foo from './Foo.vue' | ||
import Bar from './Bar.vue' | ||
|
||
const wrapper = mount(Foo) | ||
|
||
// similar to `wrapper.findComponent`. | ||
// `getComponent` will throw an error if an element is not found. `findComponent` will do nothing. | ||
expect(wrapper.getComponent(Bar)) // => gets Bar by component instance | ||
expect(wrapper.getComponent({ name: 'bar' })) // => gets Bar by `name` | ||
expect(wrapper.getComponent({ ref: 'bar' })) // => gets Bar by `ref` | ||
|
||
expect(() => wrapper.getComponent({ name: 'does-not-exist' })) | ||
.to.throw() | ||
.with.property( | ||
'message', | ||
"Unable to get a component named 'does-not-exist' within: <div>the actual DOM here...</div>" | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## getComponent | ||
|
||
和 [findComponent](./findComponent.md) 工作起来一样,但是如果未匹配到给定的选择器时会抛出错误。当搜索一个可能不存在的元素时你应该使用 `findComponent`。当获取一个应该存在的元素时你应该使用这个方法,并且如果没有找到的话它会提供一则友好的错误信息。 | ||
|
||
```js | ||
import { mount } from '@vue/test-utils' | ||
import Foo from './Foo.vue' | ||
import Bar from './Bar.vue' | ||
|
||
const wrapper = mount(Foo) | ||
|
||
// 和 `wrapper.findComponent` 相似。 | ||
// 如果 `getComponent` 没有找到任何元素将会抛出一个而错误。`findComponent` 则不会做任何事。 | ||
expect(wrapper.getComponent(Bar)) // => gets Bar by component instance | ||
expect(wrapper.getComponent({ name: 'bar' })) // => gets Bar by `name` | ||
expect(wrapper.getComponent({ ref: 'bar' })) // => gets Bar by `ref` | ||
|
||
expect(() => wrapper.getComponent({ name: 'does-not-exist' })) | ||
.to.throw() | ||
.with.property( | ||
'message', | ||
"Unable to get a component named 'does-not-exist' within: <div>the actual DOM here...</div>" | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters