Skip to content

Commit d4e14c2

Browse files
committed
feat: implement wrapper.emitted() and wrapper.emittedByOrder()
1 parent b4dcac5 commit d4e14c2

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/lib/log-events.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function logEvents (vm, emitted, emittedByOrder) {
2+
const emit = vm.$emit
3+
vm.$emit = (name, ...args) => {
4+
(emitted[name] || (emitted[name] = [])).push(args)
5+
emittedByOrder.push({ name, args })
6+
return emit.call(vm, name, ...args)
7+
}
8+
}

src/wrappers/vue-wrapper.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// @flow
22

33
import Wrapper from './wrapper'
4+
import { logEvents } from '../lib/log-events'
45

56
function update () {
67
this._update(this._render())
78
}
89

910
export default class VueWrapper extends Wrapper implements BaseWrapper {
11+
_emitted: { [name: string]: Array<Array<any>> };
12+
_emittedByOrder: Array<{ name: string; args: Array<any> }>;
13+
1014
constructor (vm: Component, options: WrapperOptions) {
1115
super(vm._vnode, update.bind(vm), options)
1216

@@ -18,5 +22,17 @@ export default class VueWrapper extends Wrapper implements BaseWrapper {
1822

1923
this.vm = vm
2024
this.isVueComponent = true
25+
this._emitted = Object.create(null)
26+
this._emittedByOrder = []
27+
28+
logEvents(vm, this._emitted, this._emittedByOrder)
29+
}
30+
31+
emitted () {
32+
return this._emitted
33+
}
34+
35+
emittedByOrder () {
36+
return this._emittedByOrder
2137
}
2238
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import mount from '~src/mount'
2+
3+
describe('emitted', () => {
4+
it('captures emitted events', () => {
5+
const wrapper = mount({
6+
render: h => h('div')
7+
})
8+
9+
wrapper.vm.$emit('foo')
10+
expect(wrapper.emitted().foo).to.exist
11+
expect(wrapper.emitted().foo.length).to.equal(1)
12+
expect(wrapper.emitted().foo[0]).to.eql([])
13+
14+
expect(wrapper.emitted().bar).not.to.exist
15+
wrapper.vm.$emit('bar', 1, 2, 3)
16+
expect(wrapper.emitted().bar).to.exist
17+
expect(wrapper.emitted().bar.length).to.equal(1)
18+
expect(wrapper.emitted().bar[0]).to.eql([1, 2, 3])
19+
20+
wrapper.vm.$emit('foo', 2, 3, 4)
21+
expect(wrapper.emitted().foo).to.exist
22+
expect(wrapper.emitted().foo.length).to.equal(2)
23+
expect(wrapper.emitted().foo[1]).to.eql([2, 3, 4])
24+
25+
expect(wrapper.emittedByOrder()).to.eql([
26+
{ name: 'foo', args: [] },
27+
{ name: 'bar', args: [1, 2, 3] },
28+
{ name: 'foo', args: [2, 3, 4] }
29+
])
30+
})
31+
})

0 commit comments

Comments
 (0)