Skip to content

Commit b405de9

Browse files
committed
Improve consistency and fix renderStubDefaultSlot docs
1 parent 4be14aa commit b405de9

File tree

1 file changed

+86
-54
lines changed

1 file changed

+86
-54
lines changed

docs/api/index.md

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ const Component = {
308308
template: '<div><global-component/></div>'
309309
}
310310

311-
test('installs a component globally', () => {
311+
test('global.components', () => {
312312
const wrapper = mount(Component, {
313313
global: {
314314
components: {
@@ -352,7 +352,7 @@ const Component = {
352352
template: '<div v-bar>Foo</div>'
353353
}
354354

355-
test('installs a directive globally', () => {
355+
test('global.directives', () => {
356356
const wrapper = mount(Component, {
357357
global: {
358358
directives: {
@@ -378,7 +378,7 @@ mixins?: ComponentOptions[]
378378
`Component.spec.js`:
379379

380380
```js
381-
test('adds a mixin', () => {
381+
test('global.mixins', () => {
382382
const wrapper = mount(Component, {
383383
global: {
384384
mixins: [mixin]
@@ -424,7 +424,7 @@ export default {
424424
`Component.spec.js`:
425425

426426
```js
427-
test('mocks a vuex store', async () => {
427+
test('global.mocks', async () => {
428428
const $store = {
429429
dispatch: jest.fn()
430430
}
@@ -460,7 +460,7 @@ plugins?: (Plugin | [Plugin, ...any[]])[]
460460
```js
461461
import myPlugin from '@/plugins/myPlugin'
462462

463-
test('installs a plugin', () => {
463+
test('global.plugins', () => {
464464
mount(Component, {
465465
global: {
466466
plugins: [myPlugin]
@@ -474,7 +474,7 @@ To use plugin with options, an array of options can be passed.
474474
`Component.spec.js`:
475475

476476
```js
477-
test('installs plugins with and without options', () => {
477+
test('global.plugins with options', () => {
478478
mount(Component, {
479479
global: {
480480
plugins: [Plugin, [PluginWithOptions, 'argument 1', 'another argument']]
@@ -519,7 +519,7 @@ export default {
519519
`Component.spec.js`:
520520

521521
```js
522-
test('injects dark theme via provide mounting option', () => {
522+
test('global.provide', () => {
523523
const wrapper = mount(Component, {
524524
global: {
525525
provide: {
@@ -562,35 +562,52 @@ renderStubDefaultSlot?: boolean
562562

563563
Defaults to **false**.
564564

565-
Due to technical limitations, this behavior cannot be extended to slots other than the default one.
565+
`Component.vue`
566566

567-
```js
568-
import { config, mount } from '@vue/test-utils'
567+
```vue
568+
<template>
569+
<slot />
570+
<another-component />
571+
</template>
569572
570-
beforeAll(() => {
571-
config.renderStubDefaultSlot = true
572-
})
573+
<script>
574+
export default {
575+
components: {
576+
AnotherComponent
577+
}
578+
}
579+
</script>
580+
```
573581

574-
afterAll(() => {
575-
config.renderStubDefaultSlot = false
576-
})
582+
`AnotherComponent.vue`
577583

578-
test('shallow with stubs', () => {
579-
const Component = {
580-
template: `<div><slot /></div>`
581-
}
584+
```vue
585+
<template>
586+
<p>Another component content</p>
587+
</template>
588+
```
582589

583-
const wrapper = mount(Component, {
584-
shallow: true
590+
`Component.spec.js`
591+
592+
```js
593+
test('global.renderStubDefaultSlot', () => {
594+
const wrapper = mount(ComponentWithSlots, {
595+
slots: {
596+
default: '<div>My slot content</div>'
597+
},
598+
shallow: true,
599+
global: {
600+
renderStubDefaultSlot: true
601+
}
585602
})
586603

587-
expect(wrapper.html()).toContain('Content from the default slot')
604+
expect(wrapper.html()).toBe(
605+
'<div>My slot content</div><another-component-stub></another-component-stub>'
606+
)
588607
})
589608
```
590609

591-
::: tip
592-
This behavior is global, not on a mount by mount basis. Remember to enable/disable it before and after each test.
593-
:::
610+
Due to technical limitations, **this behavior cannot be extended to slots other than the default one**.
594611

595612
#### global.stubs
596613

@@ -625,7 +642,7 @@ export default {
625642
`Component.spec.js`:
626643

627644
```js
628-
test('stubs a component using an array', () => {
645+
test('global.stubs using array syntax', () => {
629646
const wrapper = mount(Component, {
630647
global: {
631648
stubs: ['Foo']
@@ -635,7 +652,7 @@ test('stubs a component using an array', () => {
635652
expect(wrapper.html()).toEqual('<div><foo-stub></div>')
636653
})
637654

638-
test('stubs a component using an Object boolean syntax', () => {
655+
test('global.stubs using object syntax', () => {
639656
const wrapper = mount(Component, {
640657
global: {
641658
stubs: { Foo: true }
@@ -645,18 +662,19 @@ test('stubs a component using an Object boolean syntax', () => {
645662
expect(wrapper.html()).toEqual('<div><foo-stub></div>')
646663
})
647664

648-
test('stubs a component using a custom component', () => {
649-
const FooMock = {
650-
name: 'Foo',
651-
template: '<p>FakeFoo</p>'
665+
test('global.stubs using a custom component', () => {
666+
const CustomStub = {
667+
name: 'CustomStub',
668+
template: '<p>custom stub content</p>'
652669
}
670+
653671
const wrapper = mount(Component, {
654672
global: {
655-
stubs: { Foo: FooMock }
673+
stubs: { Foo: CustomStub }
656674
}
657675
})
658676

659-
expect(wrapper.html()).toEqual('<div><p>FakeFoo</p></div>')
677+
expect(wrapper.html()).toEqual('<div><p>custom stub content</p></div>')
660678
})
661679
```
662680

@@ -674,19 +692,28 @@ shallow?: boolean
674692

675693
Defaults to **false**.
676694

677-
```js
678-
test('stubs all components automatically using { shallow: true }', () => {
679-
const Component = {
680-
template: `
681-
<a-component />
682-
<another-component />
683-
`,
684-
components: {
685-
AComponent,
686-
AnotherComponent
687-
}
695+
`Component.vue`
696+
697+
```vue
698+
<template>
699+
<a-component />
700+
<another-component />
701+
</template>
702+
703+
<script>
704+
export default {
705+
components: {
706+
AComponent,
707+
AnotherComponent
688708
}
709+
}
710+
</script>
711+
```
689712

713+
`Component.spec.js`
714+
715+
```js
716+
test('shallow', () => {
690717
const wrapper = mount(Component, { shallow: true })
691718

692719
expect(wrapper.html()).toEqual(
@@ -776,7 +803,9 @@ Returns an array of classes on an element.
776803
test('classes', () => {
777804
const wrapper = mount(Component)
778805

779-
expect(wrapper.find('.my-span').classes()).toContain('my-span')
806+
expect(wrapper.classes()).toContain('my-span')
807+
expect(wrapper.classes('my-span')).toBe(true)
808+
expect(wrapper.classes('not-existing')).toBe(false)
780809
})
781810
```
782811
@@ -815,12 +844,12 @@ export default {
815844
test('emitted', () => {
816845
const wrapper = mount(Component)
817846

847+
// wrapper.emitted() equals to { greet: [ ['hello'], ['goodbye'] ] }
848+
818849
expect(wrapper.emitted()).toHaveProperty('greet')
819-
expect(wrapper.emitted().greet).toHaveLength(2')
850+
expect(wrapper.emitted().greet).toHaveLength(2)
820851
expect(wrapper.emitted().greet[0]).toEqual(['hello'])
821852
expect(wrapper.emitted().greet[1]).toEqual(['goodbye'])
822-
823-
// { greet: [ ['hello'], ['goodbye'] ] }
824853
})
825854
```
826855
@@ -836,13 +865,13 @@ exists(): boolean
836865
837866
**Details:**
838867
868+
You can use the same syntax `querySelector` implements.
869+
839870
`Component.vue`:
840871
841872
```vue
842873
<template>
843-
<div>
844-
<span />
845-
</div>
874+
<span />
846875
</template>
847876
```
848877
@@ -1283,8 +1312,7 @@ setData(data: Record<string, any>): Promise<void>
12831312
12841313
**Details:**
12851314
1286-
Notice that `setData` does not allow setting new properties that are not
1287-
defined in the component.
1315+
`setData` does not allow setting new properties that are not defined in the component.
12881316
12891317
Also, notice that `setData` does not modify composition API `setup()` data.
12901318
@@ -1681,3 +1709,7 @@ it('uses global config', () => {
16811709
console.log(wrapper.html()) // <p>message</p><div></div>
16821710
})
16831711
```
1712+
1713+
::: tip
1714+
Remember that this behavior is global, not on a mount by mount basis. You might need to enable/disable it before and after each test.
1715+
:::

0 commit comments

Comments
 (0)