Skip to content

Commit 91d529d

Browse files
fix: allow passing an event name to the emitted method
1 parent 418aff7 commit 91d529d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/vue-wrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export class VueWrapper<T extends ComponentPublicInstance> {
6262
return true
6363
}
6464

65-
emitted(): Record<string, unknown[]> {
65+
emitted(eventName?: string): Record<string, unknown[]> | undefined {
6666
// TODO Should we define this?
6767
// @ts-ignore
68-
return this.vm.__emitted
68+
return eventName ? this.vm.__emitted[eventName] : this.vm.__emitted
6969
}
7070

7171
html() {

tests/emit.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,29 @@ describe('emitted', () => {
9696
wrapper.find('button').trigger('click')
9797
expect(wrapper.emitted().parent[1]).toEqual(['foo', 'bar'])
9898
})
99+
100+
it('should allow passing the name of an event', () => {
101+
const Component = defineComponent({
102+
name: 'ContextEmit',
103+
104+
setup(props, ctx) {
105+
return () =>
106+
h('div', [
107+
h('button', { onClick: () => ctx.emit('hello', 'foo', 'bar') })
108+
])
109+
}
110+
})
111+
const wrapper = mount(Component)
112+
113+
// test what happens if you pass a none existent event
114+
expect(wrapper.emitted('hello')).toEqual(undefined)
115+
116+
wrapper.find('button').trigger('click')
117+
expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar'])
118+
119+
wrapper.find('button').trigger('click')
120+
expect(wrapper.emitted('hello')[1]).toEqual(['foo', 'bar'])
121+
122+
expect(wrapper.emitted('hello')).toHaveLength(2)
123+
})
99124
})

0 commit comments

Comments
 (0)