Skip to content

Commit 0033c4a

Browse files
committed
docs: documentation for emitted() and emittedByOrder()
1 parent bae2731 commit 0033c4a

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

docs/en/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- [clone](api/options.md#clone)
2323
* [Wrapper](api/wrapper/README.md)
2424
* [contains](api/wrapper/contains.md)
25+
* [emitted](api/wrapper/emitted.md)
26+
* [emittedByOrder](api/wrapper/emittedByOrder.md)
2527
* [find](api/wrapper/find.md)
2628
* [hasAttribute](api/wrapper/hasAttribute.md)
2729
* [hasClass](api/wrapper/hasClass.md)

docs/en/api/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- [clone](./options.md#clone)
1515
* [Wrapper](./wrapper/README.md)
1616
* [contains](./wrapper/contains.md)
17+
* [emitted](./wrapper/emitted.md)
18+
* [emittedByOrder](./wrapper/emittedByOrder.md)
1719
* [find](./wrapper/find.md)
1820
* [hasAttribute](./wrapper/hasAttribute.md)
1921
* [hasClass](./wrapper/hasClass.md)

docs/en/api/wrapper/emitted.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# emitted()
2+
3+
Return an object containing custom events emitted by the `Wrapper` `vm`.
4+
5+
- **Returns:** `{ [name: string]: Array<Array<any>> }`
6+
7+
- **Example:**
8+
9+
```js
10+
import { mount } from 'vue-test-utils'
11+
import { expect } from 'chai'
12+
13+
const wrapper = mount(Foo)
14+
15+
wrapper.vm.$emit('foo')
16+
wrapper.vm.$emit('foo', 123)
17+
18+
/*
19+
wrapper.emitted() returns the following object:
20+
{
21+
foo: [[], [123]]
22+
}
23+
*/
24+
25+
// assert event has been emitted
26+
expect(wrapper.emitted().foo).to.exist
27+
28+
// assert event count
29+
expect(wrapper.emitted().foo.length).to.equal(2)
30+
31+
// assert event payload
32+
expect(wrapper.emitted().foo[1]).to.eql([123])
33+
```

docs/en/api/wrapper/emittedByOrder.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# emittedByOrder()
2+
3+
Return an Array containing custom events emitted by the `Wrapper` `vm`.
4+
5+
- **Returns:** `Array<{ name: string, args: Array<any> }>`
6+
7+
- **Example:**
8+
9+
```js
10+
import { mount } from 'vue-test-utils'
11+
import { expect } from 'chai'
12+
13+
const wrapper = mount(Foo)
14+
15+
wrapper.vm.$emit('foo')
16+
wrapper.vm.$emit('bar', 123)
17+
18+
/*
19+
wrapper.emittedByOrder() returns the following Array:
20+
[
21+
{ name: 'foo', args: [] },
22+
{ name: 'bar', args: [123] }
23+
]
24+
*/
25+
26+
// assert event emit order
27+
expect(wrapper.emittedByOrder().map(e => e.name)).to.eql(['foo', 'bar'])
28+
```

docs/en/guides/choosing-a-test-runner.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ There are many popular JavaScript test runners, and `vue-test-utils` works with
66

77
There are a few things to consider when choosing a test runner though: feature set, performance, and support for single-file component (SFC) pre-compilation. After carefully comparing existing libraries, here are two test runners that we recommend:
88

9-
- [Jest](https://facebook.github.io/jest/docs/en/getting-started.html#content) is the most fully featured test runner. It requires the least configuration, sets up JSDOM by default, and has a great command line user experience. However, you will need a preprocessor to be able to import SFC components in your tests. We have created the `jest-vue` preprocessor which can handle most common SFC features, but it currently does not have 100% feature parity with `vue-loader`.
9+
- [Jest](https://facebook.github.io/jest/docs/en/getting-started.html#content) is the most fully featured test runner. It requires the least configuration, sets up JSDOM by default, provides built-in assertions, and has a great command line user experience. However, you will need a preprocessor to be able to import SFC components in your tests. We have created the `jest-vue` preprocessor which can handle most common SFC features, but it currently does not have 100% feature parity with `vue-loader`.
1010

1111
- [mocha-webpack](https://github.com/zinserjan/mocha-webpack) is a wrapper around webpack + Mocha, but with a more streamlined interface and watch mode. The benefits of this setup is that we can get complete SFC support via webpack + `vue-loader`, but it requires more configuration upfront.
1212

docs/en/guides/common-tips.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ const wrapper = shallow(Component) // returns a Wrapper containing a mounted Com
2727
wrapper.vm // the mounted Vue instance
2828
```
2929

30+
## Asserting Emitted Events
31+
32+
Each mounted wrapper automatically records all events emitted by the underlying Vue instance. You can retrieve the recorded events using the `wrapper.emitted()` method:
33+
34+
``` js
35+
const wrapper = mount(Foo)
36+
37+
wrapper.vm.$emit('foo')
38+
wrapper.vm.$emit('foo', 123)
39+
40+
/*
41+
wrapper.emitted() returns the following object:
42+
{
43+
foo: [[], [123]]
44+
}
45+
*/
46+
```
47+
48+
You can then make assertions based on these data:
49+
50+
``` js
51+
import { expect } from 'chai'
52+
53+
// assert event has been emitted
54+
expect(wrapper.emitted().foo).to.exist
55+
56+
// assert event count
57+
expect(wrapper.emitted().foo.length).to.equal(2)
58+
59+
// assert event payload
60+
expect(wrapper.emitted().foo[1]).to.eql([123])
61+
```
62+
63+
You can also get an Array of the events in their emit order by calling [wrapper.emittedByOrder()](../api/emittedByOrder.md).
64+
3065
## Manipulating Component State
3166

3267
You can directly manipulate the state of the component using the `setData` or `setProps` method on the wrapper:
@@ -51,7 +86,7 @@ mount(Component, {
5186
})
5287
```
5388

54-
You can also update the props of an already-mounted component with the `wrapper.setProps` method.
89+
You can also update the props of an already-mounted component with the `wrapper.setProps({})` method.
5590

5691
*For a full list of options, please see the [mount options section](./api/options.md) of the docs.*
5792

0 commit comments

Comments
 (0)