Skip to content

Commit 1f03d80

Browse files
authored
add note on findComponent (#187)
1 parent abcc27b commit 1f03d80

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import { shallowMount, mount } from '@vue/test-utils'
2-
import ParentWithAPICallChild from '../../src/components/ParentWithAPICallChild.vue'
3-
import ComponentWithAsyncCall from '../../src/components/ComponentWithAsyncCall.vue'
1+
import { shallowMount, mount } from '@vue/test-utils';
2+
import ParentWithAPICallChild from '../../src/components/ParentWithAPICallChild.vue';
3+
import ComponentWithAsyncCall from '../../src/components/ComponentWithAsyncCall.vue';
44

55
describe('ParentWithAPICallChild.vue', () => {
66
it('renders with mount and does initialize API call', () => {
77
const wrapper = mount(ParentWithAPICallChild, {
8-
stubs: {
9-
ComponentWithAsyncCall: true
10-
}
11-
})
8+
global: {
9+
stubs: {
10+
ComponentWithAsyncCall: true,
11+
},
12+
},
13+
});
1214

13-
expect(wrapper.findComponent(ComponentWithAsyncCall).exists()).toBe(true)
14-
})
15+
expect(wrapper.findComponent(ComponentWithAsyncCall).exists()).toBe(true);
16+
});
1517

1618
it('renders with shallowMount and does not initialize API call', () => {
17-
const wrapper = shallowMount(ParentWithAPICallChild)
19+
const wrapper = shallowMount(ParentWithAPICallChild);
1820

19-
expect(wrapper.findComponent(ComponentWithAsyncCall).exists()).toBe(true)
20-
})
21-
})
21+
expect(wrapper.findComponent(ComponentWithAsyncCall).exists()).toBe(true);
22+
});
23+
});

src/v3/stubbing-components.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default {
8383

8484
## Write a test using `mount`
8585

86-
Let's start off by writing a test to verify that `<ComponentWithAsyncCall>` is rendered:
86+
Let's start off by writing a test to verify that `<ComponentWithAsyncCall>` is rendered. Note that `findComponent` is used. `find` is used for querying DOM elements, and uses the `querySelector` syntax. `findComponent` is used when looking for a specific component, a takes a component as the argument.
8787

8888
```js
8989
import { shallowMount, mount } from '@vue/test-utils'
@@ -94,7 +94,7 @@ describe('ParentWithAPICallChild.vue', () => {
9494
it('renders with mount and does initialize API call', () => {
9595
const wrapper = mount(ParentWithAPICallChild)
9696

97-
expect(wrapper.find(ComponentWithAsyncCall).exists()).toBe(true)
97+
expect(wrapper.findComponent(ComponentWithAsyncCall).exists()).toBe(true)
9898
})
9999
})
100100
```
@@ -108,7 +108,7 @@ console.log src/components/ComponentWithAsyncCall.vue:17
108108
Making api call
109109
```
110110

111-
The test is passing - great! However, we can do better. Notice the `console.log` in the test output - this comes from the `makeApiCall` method. Ideally we don't want to make calls to external services in our unit tests, especially when it's from a component that is not the main focus of the current test. We can use the `stubs` mounting option, described in the `vue-test-utils` docs [here](https://vue-test-utils.vuejs.org/api/options.html#stubs).
111+
The test is passing - great! However, we can do better. Notice the `console.log` in the test output - this comes from the `makeApiCall` method. Ideally we don't want to make calls to external services in our unit tests, especially when it's from a component that is not the main focus of the current test. We can use the `stubs` mounting option, described in the `vue-test-utils` docs [here](https://next.vue-test-utils.vuejs.org/migration/#mocks-and-stubs-are-now-in-global).
112112

113113
## Using `stubs` to stub `<ComponentWithAsyncCall>`
114114

@@ -117,9 +117,11 @@ Let's update the test, this time stubbing `<ComponentWithAsyncCall>`:
117117
```js
118118
it('renders with mount and does initialize API call', () => {
119119
const wrapper = mount(ParentWithAPICallChild, {
120-
stubs: {
121-
ComponentWithAsyncCall: true
122-
}
120+
global: {
121+
stubs: {
122+
ComponentWithAsyncCall: true
123+
},
124+
},
123125
})
124126

125127
expect(wrapper.find(ComponentWithAsyncCall).exists()).toBe(true)

0 commit comments

Comments
 (0)