10
10
:::
11
11
12
12
- [ ` context ` ] ( #context )
13
+ - [ ` data ` ] ( #data )
13
14
- [ ` slots ` ] ( #slots )
14
15
- [ ` scopedSlots ` ] ( #scopedslots )
15
16
- [ ` stubs ` ] ( #stubs )
16
17
- [ ` mocks ` ] ( #mocks )
17
18
- [ ` localVue ` ] ( #localvue )
19
+ - [ ` attachTo ` ] ( #attachto )
18
20
- [ ` attachToDocument ` ] ( #attachtodocument )
19
21
- [ ` propsData ` ] ( #propsdata )
20
22
- [ ` attrs ` ] ( #attrs )
@@ -44,6 +46,43 @@ const wrapper = mount(Component, {
44
46
expect (wrapper .is (Component)).toBe (true )
45
47
```
46
48
49
+ ## data
50
+
51
+ - 类型:` Function `
52
+
53
+ 向一个组件传入数据。这将会合并到现有的 ` data ` 函数中。
54
+
55
+ 示例:
56
+
57
+ ``` js
58
+ const Component = {
59
+ template: `
60
+ <div>
61
+ <span id="foo">{{ foo }}</span>
62
+ <span id="bar">{{ bar }}</span>
63
+ </div>
64
+ ` ,
65
+
66
+ data () {
67
+ return {
68
+ foo: ' foo' ,
69
+ bar: ' bar'
70
+ }
71
+ }
72
+ }
73
+
74
+ const wrapper = mount (Component, {
75
+ data () {
76
+ return {
77
+ bar: ' my-override'
78
+ }
79
+ }
80
+ })
81
+
82
+ wrapper .find (' #foo' ).text () // 'foo'
83
+ wrapper .find (' #bar' ).text () // 'my-override'
84
+ ```
85
+
47
86
## slots
48
87
49
88
- 类型:` { [name: string]: Array<Component>|Component|string } `
@@ -54,20 +93,43 @@ expect(wrapper.is(Component)).toBe(true)
54
93
55
94
``` js
56
95
import Foo from ' ./Foo.vue'
96
+ import MyComponent from ' ./MyComponent.vue'
57
97
58
98
const bazComponent = {
59
99
name: ' baz-component' ,
60
100
template: ' <p>baz</p>'
61
101
}
62
102
103
+ const yourComponent = {
104
+ props: {
105
+ foo: {
106
+ type: String ,
107
+ required: true
108
+ }
109
+ },
110
+ render (h ) {
111
+ return h (' p' , this .foo )
112
+ }
113
+ }
114
+
63
115
const wrapper = shallowMount (Component, {
64
116
slots: {
65
117
default: [Foo, ' <my-component />' , ' text' ],
66
118
fooBar: Foo, // 将会匹配 `<slot name="FooBar" />`.
67
119
foo: ' <div />' ,
68
120
bar: ' bar' ,
69
121
baz: bazComponent,
70
- qux: ' <my-component />'
122
+ qux: ' <my-component />' ,
123
+ quux: ' <your-component foo="lorem"/><your-component :foo="yourProperty"/>'
124
+ },
125
+ stubs: {
126
+ // 用来注册自定义组件
127
+ ' my-component' : MyComponent,
128
+ ' your-component' : yourComponent
129
+ },
130
+ mocks: {
131
+ // 用来向渲染上下文添加 property
132
+ yourProperty: ' ipsum'
71
133
}
72
134
})
73
135
@@ -124,12 +186,37 @@ shallowMount(Component, {
124
186
})
125
187
```
126
188
189
+ ::: warning 必备根元素
190
+ 由于该特性内部实现的原因,这里的插槽内容必须返回一个根元素,即使一个作用域插槽是允许返回一个元素数组的。
191
+
192
+ 如果你在测试中有这方面的需要,推荐的变通方式是把被测试的组件包裹在另一个组件里,然后挂载那个组件:
193
+ :::
194
+
195
+ ``` javascript
196
+ const WrapperComp = {
197
+ template: `
198
+ <ComponentUnderTest v-slot="props">
199
+ <p>Using the {{props.a}}</p>
200
+ <p>Using the {{props.a}}</p>
201
+ </ComponentUnderTest>
202
+ ` ,
203
+ components: {
204
+ ComponentUnderTest
205
+ }
206
+ }
207
+ const wrapper = mount (WrapperComp).find (ComponentUnderTest)
208
+ ```
209
+
127
210
## stubs
128
211
129
- - 类型:` { [name: string]: Component | boolean } | Array<string> `
212
+ - 类型:` { [name: string]: Component | string | boolean } | Array<string> `
130
213
131
214
将子组件存根。可以是一个要存根的组件名的数组或对象。如果 ` stubs ` 是一个数组,则每个存根都是一个 ` <${component name}-stub> ` 。
132
215
216
+ ** 废弃通知:**
217
+
218
+ 当对组件存根时,提供一个字符串的方式 (` ComponentToStub: '<div class="stubbed" /> ` ) 已经不再被支持。
219
+
133
220
示例:
134
221
135
222
``` js
@@ -169,6 +256,10 @@ const wrapper = shallowMount(Component, {
169
256
expect (wrapper .vm .$route .path ).toBe ($route .path )
170
257
```
171
258
259
+ ::: tip
260
+ 如果想要伪造 ` $root ` 请换用[ 这里] ( https://github.com/vuejs/vue-test-utils/issues/481#issuecomment-423716430 ) 描述的 ` parentComponent ` 选项。
261
+ :::
262
+
172
263
## localVue
173
264
174
265
- 类型:` Vue `
@@ -198,12 +289,42 @@ const wrapper = mount(Component, {
198
289
expect (wrapper .vm .$route ).toBeInstanceOf (Object )
199
290
```
200
291
292
+ ## attachTo
293
+
294
+ - 类型:` HTMLElement | string `
295
+ - 默认值:` null `
296
+
297
+ 指定一个 ` HTMLElement ` 或定位到一个 HTML 元素的 CSS 选择器字符串,组件将会被完全挂载到文档中的这个元素。
298
+
299
+ 当要挂载到 DOM 时,你需要在测试的结尾调用 ` wrapper.destroy() ` 以将该元素从文档中移除,并销毁该组件实例。
300
+
301
+ ``` js
302
+ const Component = {
303
+ template: ' <div>ABC</div>'
304
+ }
305
+ let wrapper = mount (Component, {
306
+ attachTo: ' #root'
307
+ })
308
+ expect (wrapper .vm .$el .parentNode ).to .not .be .null
309
+ wrapper .destroy ()
310
+
311
+ wrapper = mount (Component, {
312
+ attachTo: document .getElementById (' root' )
313
+ })
314
+ expect (wrapper .vm .$el .parentNode ).to .not .be .null
315
+ wrapper .destroy ()
316
+ ```
317
+
201
318
## attachToDocument
202
319
203
320
- 类型:` boolean `
204
321
- 默认值:` false `
205
322
206
- 当设为 ` true ` 时,组件在渲染时将会挂载到 DOM 上。
323
+ ::: warning 废弃警告
324
+ ` attachToDocument ` 已经被废弃并且将会在未来的发布中被移除。请换用 [ ` attachTo ` ] ( #attachto ) 。
325
+ :::
326
+
327
+ 像 [ ` attachTo ` ] ( #attachto ) 一样,不过会自动创建一个新的 ` div ` 元素并将其插入到 body 里。
207
328
208
329
如果添加到了 DOM 上,你应该在测试的最后调用 ` wrapper.destroy() ` 将元素从文档中移除并销毁组件实例。
209
330
@@ -244,6 +365,23 @@ expect(wrapper.text()).toBe('aBC')
244
365
245
366
设置组件实例的 ` $listeners ` 对象。
246
367
368
+ 示例:
369
+
370
+ ``` js
371
+ const Component = {
372
+ template: ' <button v-on:click="$emit(\' click\' )"></button>'
373
+ }
374
+ const onClick = jest .fn ()
375
+ const wrapper = mount (Component, {
376
+ listeners: {
377
+ click: onClick
378
+ }
379
+ })
380
+
381
+ wrapper .trigger (' click' )
382
+ expect (onClick).toHaveBeenCalled ()
383
+ ```
384
+
247
385
## parentComponent
248
386
249
387
- 类型:` Object `
@@ -292,26 +430,22 @@ expect(wrapper.text()).toBe('fooValue')
292
430
293
431
``` js
294
432
const Component = {
295
- template: ' <div>{{ foo() }}{{ bar() }}{{ baz() }}</div>' ,
296
- methods: {
297
- foo () {
298
- return ' a'
299
- },
300
- bar () {
301
- return ' b'
433
+ template: ' <div>{{ foo }}</div>' ,
434
+ data () {
435
+ return {
436
+ foo: ' fromComponent'
302
437
}
303
438
}
304
439
}
305
440
const options = {
306
- methods: {
307
- bar () {
308
- return ' B'
309
- },
310
- baz () {
311
- return ' C'
441
+ data () {
442
+ return {
443
+ foo: ' fromOptions'
312
444
}
313
445
}
314
446
}
447
+
315
448
const wrapper = mount (Component, options)
316
- expect (wrapper .text ()).toBe (' aBC' )
449
+
450
+ expect (wrapper .text ()).toBe (' fromOptions' )
317
451
```
0 commit comments