Skip to content

Commit a5df3c4

Browse files
authored
fix: update unit test code examples with Vue Test Utils (#2468)
* fix: update unit test code examples with Vue Test Utils * fix: use vm. instead of Vue.nextTick to avoid importing Vue * fix: change name in paragraph
1 parent 1c62b65 commit a5df3c4

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/v2/guide/unit-testing.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,49 @@ A component's render output is primarily determined by the props it receives. If
8383
</script>
8484
```
8585

86-
You can assert its render output with different props using the `propsData` option:
86+
You can assert its render output with different props using [Vue Test Utils](https://vue-test-utils.vuejs.org/):
8787

8888
``` js
89-
import Vue from 'vue'
89+
import { shallowMount } from '@vue/test-utils'
9090
import MyComponent from './MyComponent.vue'
9191

92-
// helper function that mounts and returns the rendered text
93-
function getRenderedText (Component, propsData) {
94-
const Constructor = Vue.extend(Component)
95-
const vm = new Constructor({ propsData: propsData }).$mount()
96-
return vm.$el.textContent
92+
// helper function that mounts and returns the rendered component
93+
function getMountedComponent(Component, propsData) {
94+
return shallowMount(MyComponent, {
95+
propsData
96+
})
9797
}
9898

9999
describe('MyComponent', () => {
100100
it('renders correctly with different props', () => {
101-
expect(getRenderedText(MyComponent, {
102-
msg: 'Hello'
103-
})).toBe('Hello')
104-
105-
expect(getRenderedText(MyComponent, {
106-
msg: 'Bye'
107-
})).toBe('Bye')
101+
expect(
102+
getMountedComponent(MyComponent, {
103+
msg: 'Hello'
104+
}).text()
105+
).toBe('Hello')
106+
107+
expect(
108+
getMountedComponent(MyComponent, {
109+
msg: 'Bye'
110+
}).text()
111+
).toBe('Bye')
108112
})
109113
})
110114
```
111115

112116
## Asserting Asynchronous Updates
113117

114-
Since Vue [performs DOM updates asynchronously](reactivity.html#Async-Update-Queue), assertions on DOM updates resulting from state change will have to be made in a `Vue.nextTick` callback:
118+
Since Vue [performs DOM updates asynchronously](reactivity.html#Async-Update-Queue), assertions on DOM updates resulting from state change will have to be made after `vm.$nextTick()` has resolved:
115119

116120
``` js
117121
// Inspect the generated HTML after a state update
118-
it('updates the rendered message when vm.message updates', done => {
119-
const vm = new Vue(MyComponent).$mount()
120-
vm.message = 'foo'
121-
122-
// wait a "tick" after state change before asserting DOM updates
123-
Vue.nextTick(() => {
124-
expect(vm.$el.textContent).toBe('foo')
125-
done()
126-
})
122+
it('updates the rendered message when wrapper.message updates', async () => {
123+
const wrapper = shallowMount(MyComponent)
124+
wrapper.setData({ message: 'foo' })
125+
126+
// Wait a "tick" after state change before asserting DOM updates
127+
await wrapper.vm.$nextTick()
128+
expect(wrapper.text()).toBe('foo')
127129
})
128130
```
129131

0 commit comments

Comments
 (0)