Skip to content

Commit ab470ad

Browse files
authored
Merge pull request #1405 from samgre/dev
docs: add note about waiting for async trigger event to be handled
2 parents 2784cd3 + 479dabc commit ab470ad

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

docs/api/wrapper/emitted.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@ Return an object containing custom events emitted by the `Wrapper` `vm`.
99
```js
1010
import { mount } from '@vue/test-utils'
1111

12-
const wrapper = mount(Component)
12+
test('emit demo', async () => {
13+
const wrapper = mount(Component)
1314

14-
wrapper.vm.$emit('foo')
15-
wrapper.vm.$emit('foo', 123)
15+
wrapper.vm.$emit('foo')
16+
wrapper.vm.$emit('foo', 123)
1617

17-
/*
18-
wrapper.emitted() returns the following object:
19-
{
20-
foo: [[], [123]]
21-
}
22-
*/
18+
await wrapper.vm.$nextTick() // Wait until $emits have been handled
2319

24-
// assert event has been emitted
25-
expect(wrapper.emitted().foo).toBeTruthy()
20+
/*
21+
wrapper.emitted() returns the following object:
22+
{
23+
foo: [[], [123]]
24+
}
25+
*/
2626

27-
// assert event count
28-
expect(wrapper.emitted().foo.length).toBe(2)
27+
// assert event has been emitted
28+
expect(wrapper.emitted().foo).toBeTruthy()
2929

30-
// assert event payload
31-
expect(wrapper.emitted().foo[1]).toEqual([123])
30+
// assert event count
31+
expect(wrapper.emitted().foo.length).toBe(2)
32+
33+
// assert event payload
34+
expect(wrapper.emitted().foo[1]).toEqual([123])
35+
})
3236
```
3337

3438
You can also write the above as follows:

docs/api/wrapper/trigger.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## trigger
22

3-
Triggers an event on the `Wrapper` DOM node.
3+
Triggers an event asynchronously on the `Wrapper` DOM node.
44

55
`trigger` takes an optional `options` object. The properties in the `options` object are added to the Event.
66

@@ -16,22 +16,26 @@ import { mount } from '@vue/test-utils'
1616
import sinon from 'sinon'
1717
import Foo from './Foo'
1818

19-
const clickHandler = sinon.stub()
20-
const wrapper = mount(Foo, {
21-
propsData: { clickHandler }
22-
})
19+
test('trigger demo', async () => {
20+
const clickHandler = sinon.stub()
21+
const wrapper = mount(Foo, {
22+
propsData: { clickHandler }
23+
})
2324

24-
wrapper.trigger('click')
25+
wrapper.trigger('click')
2526

26-
wrapper.trigger('click', {
27-
button: 0
28-
})
27+
wrapper.trigger('click', {
28+
button: 0
29+
})
2930

30-
wrapper.trigger('click', {
31-
ctrlKey: true // For testing @click.ctrl handlers
32-
})
31+
wrapper.trigger('click', {
32+
ctrlKey: true // For testing @click.ctrl handlers
33+
})
3334

34-
expect(clickHandler.called).toBe(true)
35+
await wrapper.vm.$nextTick() // Wait until trigger events have been handled
36+
37+
expect(clickHandler.called).toBe(true)
38+
})
3539
```
3640

3741
- **Setting the event target:**

0 commit comments

Comments
 (0)