File tree Expand file tree Collapse file tree 5 files changed +74
-1
lines changed Expand file tree Collapse file tree 5 files changed +74
-1
lines changed Original file line number Diff line number Diff line change 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+
37Works just like [ find] ( ./find.md ) but will throw an error if nothing matching
48the given selector is found. You should use ` find ` when searching for an element
59that may not exist. You should use this method when getting an element that should
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change 11## get
22
3+ ::: warning 废弃警告
4+ 使用 ` get ` 搜索组件的方式已经被废弃并会被移除。请换用 [ ` getComponent ` ] ( ./getComponent.md ) 。
5+ :::
6+
37和 [ find] ( ./find.md ) 工作起来一样,但是如果未匹配到给定的选择器时会抛出错误。当搜索一个可能不存在的元素时你应该使用 ` find ` 。当获取一个应该存在的元素时你应该使用这个方法,并且如果没有找到的话它会提供一则友好的错误信息。
48
59``` js
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments