diff --git a/src/vue-wrapper.ts b/src/vue-wrapper.ts index 019ebae1d..aaf2cc3c9 100644 --- a/src/vue-wrapper.ts +++ b/src/vue-wrapper.ts @@ -68,10 +68,14 @@ export class VueWrapper { return true } - emitted(): Record { - // TODO Should we define this? - // @ts-ignore - return this.vm.__emitted + emitted(): Record + emitted(eventName?: string): T[] + emitted(eventName?: string): T[] | Record { + if (eventName) { + const emitted = (this.vm['__emitted'] as Record)[eventName] + return emitted + } + return this.vm['__emitted'] as Record } html() { diff --git a/test-dts/wrapper.d-test.ts b/test-dts/wrapper.d-test.ts index 0a2c12c84..fd56848ea 100644 --- a/test-dts/wrapper.d-test.ts +++ b/test-dts/wrapper.d-test.ts @@ -61,6 +61,15 @@ expectType(lineArray[0].element) byClassArray = domWrapper.findAll('.todo') expectType(byClassArray[0].element) +// emitted +// event name +let incrementEvent = wrapper.emitted<{ count: number }>('increment') +expectType<{ count: number }>(incrementEvent[0]) + +// without event name +let allEvents = wrapper.emitted() +expectType>(allEvents) + // get // HTML element selector let input = wrapper.get('input') diff --git a/tests/emit.spec.ts b/tests/emit.spec.ts index b2cd0d215..1db828249 100644 --- a/tests/emit.spec.ts +++ b/tests/emit.spec.ts @@ -96,4 +96,29 @@ describe('emitted', () => { wrapper.find('button').trigger('click') expect(wrapper.emitted().parent[1]).toEqual(['foo', 'bar']) }) + + it('should allow passing the name of an event', () => { + const Component = defineComponent({ + name: 'ContextEmit', + + setup(props, ctx) { + return () => + h('div', [ + h('button', { onClick: () => ctx.emit('hello', 'foo', 'bar') }) + ]) + } + }) + const wrapper = mount(Component) + + // test what happens if you pass a none existent event + expect(wrapper.emitted('hello')).toEqual(undefined) + + wrapper.find('button').trigger('click') + expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar']) + + wrapper.find('button').trigger('click') + expect(wrapper.emitted('hello')[1]).toEqual(['foo', 'bar']) + + expect(wrapper.emitted('hello')).toHaveLength(2) + }) })