Skip to content

Emit api #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/en/api/wrapper/emitted.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ expect(wrapper.emitted().foo.length).toBe(2)
// assert event payload
expect(wrapper.emitted().foo[1]).toEqual([123])
```

You can also write the above as follows:

```js
// assert event has been emitted
expect(wrapper.emitted('foo')).toBeTruthy()

// assert event count
expect(wrapper.emitted('foo').length).toBe(2)

// assert event payload
expect(wrapper.emitted('foo')[1]).toEqual([123])
```
13 changes: 13 additions & 0 deletions docs/ja/api/wrapper/emitted.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ expect(wrapper.emitted().foo.length).toBe(2)
// イベントのペイロードを検証します
expect(wrapper.emitted().foo[1]).toEqual([123])
```

別の構文があります。

```js
// イベントが発行されたか検証します
expect(wrapper.emitted('foo')).toBeTruthy()

// イベントの数を検証します
expect(wrapper.emitted('foo').length).toBe(2)

// イベントのペイロードを検証します
expect(wrapper.emitted('foo')[1]).toEqual([123])
```
5 changes: 4 additions & 1 deletion src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ export default class Wrapper implements BaseWrapper {
/**
* Returns an object containing custom events emitted by the Wrapper vm
*/
emitted () {
emitted (event: ?string) {
if (!this._emitted && !this.vm) {
throwError('wrapper.emitted() can only be called on a Vue instance')
}
if (event) {
return this._emitted[event]
}
return this._emitted
}

Expand Down
22 changes: 22 additions & 0 deletions test/unit/specs/mount/Wrapper/emitted.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import mount from '~src/mount'

describe('emitted', () => {
it('captures emitted events with a different api', () => {
const wrapper = mount({
render: h => h('div')
})

wrapper.vm.$emit('foo')
expect(wrapper.emitted('foo')).to.exist
expect(wrapper.emitted('foo').length).to.equal(1)
expect(wrapper.emitted('foo')[0]).to.eql([])

expect(wrapper.emitted('bar')).not.to.exist
wrapper.vm.$emit('bar', 1, 2, 3)
expect(wrapper.emitted('bar')).to.exist
expect(wrapper.emitted('bar').length).to.equal(1)
expect(wrapper.emitted('bar')[0]).to.eql([1, 2, 3])

wrapper.vm.$emit('foo', 2, 3, 4)
expect(wrapper.emitted('foo')).to.exist
expect(wrapper.emitted('foo').length).to.equal(2)
expect(wrapper.emitted('foo')[1]).to.eql([2, 3, 4])
})

it('captures emitted events', () => {
const wrapper = mount({
render: h => h('div')
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface Wrapper<V extends Vue> extends BaseWrapper {
text (): string
name (): string

emitted (): { [name: string]: Array<Array<any>> }
emitted (string? event): { [name: string]: Array<Array<any>> }
emittedByOrder (): Array<{ name: string, args: Array<any> }>
}

Expand Down