Skip to content

Commit 24ea6f4

Browse files
Lindsay Gaineslmiller1990
authored andcommitted
fix: record emitted events against each wrapper
1 parent e8da2be commit 24ea6f4

File tree

3 files changed

+39
-21
lines changed

3 files changed

+39
-21
lines changed

src/emit.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1-
import { setDevtoolsHook, devtools } from 'vue'
1+
import { setDevtoolsHook, devtools, ComponentPublicInstance } from 'vue'
22

33
const enum DevtoolsHooks {
44
COMPONENT_EMIT = 'component:emit'
55
}
66

7-
let events: Record<string, unknown[]>
7+
let componentEvents: Record<string, unknown[]>
8+
let events: Record<string, typeof componentEvents>
89

910
export function emitted<T = unknown>(
11+
vm: ComponentPublicInstance,
1012
eventName?: string
1113
): T[] | Record<string, T[]> {
14+
const cid = vm.$.uid
15+
16+
const vmEvents = (events as Record<string, Record<string, T[]>>)[cid] || {}
1217
if (eventName) {
13-
const emitted = (events as Record<string, T[]>)[eventName]
18+
const emitted = vmEvents
19+
? (vmEvents as Record<string, T[]>)[eventName]
20+
: undefined
1421
return emitted
1522
}
1623

17-
return events as Record<string, T[]>
24+
return vmEvents as Record<string, T[]>
1825
}
1926

2027
export const attachEmitListener = () => {
@@ -28,20 +35,27 @@ function createDevTools(events): any {
2835
emit(eventType, ...payload) {
2936
if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return
3037

31-
// The first argument is root component
32-
// The second argument is vm
33-
// The third argument is event
34-
// The fourth argument is args of event
35-
recordEvent(events, payload[2], payload[3])
38+
const [rootVM, componentVM, event, eventArgs] = payload
39+
recordEvent(events, componentVM, event, eventArgs)
3640
}
3741
}
3842

3943
return devTools
4044
}
4145

42-
function recordEvent(events, event, args) {
46+
function recordEvent(events, vm, event, args): void {
47+
// Functional component wrapper creates a parent component
48+
let wrapperVm = vm
49+
while (typeof wrapperVm.type === 'function') wrapperVm = wrapperVm.parent
50+
51+
const cid = wrapperVm.uid
52+
if (!(cid in events)) {
53+
events[cid] = {}
54+
}
55+
if (!(event in events[cid])) {
56+
events[cid][event] = []
57+
}
58+
4359
// Record the event message sent by the emit
44-
events[event]
45-
? (events[event] = [...events[event], [...args]])
46-
: (events[event] = [[...args]])
60+
events[cid][event].push(args)
4761
}

src/vueWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class VueWrapper<T extends ComponentPublicInstance> {
7373
emitted<T = unknown>(): Record<string, T[]>
7474
emitted<T = unknown>(eventName?: string): T[]
7575
emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
76-
return emitted(eventName)
76+
return emitted(this.vm, eventName)
7777
}
7878

7979
html() {

tests/emit.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ describe('emitted', () => {
7777
expect(wrapper.emitted().hello[1]).toEqual(['foo', 'bar'])
7878
})
7979

80-
it('should propagate the original event', () => {
81-
const Component = defineComponent({
82-
name: 'ContextEmit',
83-
80+
it('should not propagate child events', () => {
81+
const Child = defineComponent({
82+
name: 'Child',
8483
setup(props, { emit }) {
8584
return () =>
8685
h('div', [
@@ -91,21 +90,26 @@ describe('emitted', () => {
9190

9291
const Parent = defineComponent({
9392
name: 'Parent',
94-
9593
setup(props, { emit }) {
9694
return () =>
97-
h(Component, { onHello: (...events) => emit('parent', ...events) })
95+
h(Child, { onHello: (...events) => emit('parent', ...events) })
9896
}
9997
})
10098
const wrapper = mount(Parent)
99+
const childWrapper = wrapper.findComponent(Child)
100+
101101
expect(wrapper.emitted()).toEqual({})
102-
expect(wrapper.emitted().hello).toEqual(undefined)
102+
expect(childWrapper.emitted()).toEqual({})
103103

104104
wrapper.find('button').trigger('click')
105105
expect(wrapper.emitted().parent[0]).toEqual(['foo', 'bar'])
106+
expect(wrapper.emitted().hello).toEqual(undefined)
107+
expect(childWrapper.emitted().hello[0]).toEqual(['foo', 'bar'])
106108

107109
wrapper.find('button').trigger('click')
108110
expect(wrapper.emitted().parent[1]).toEqual(['foo', 'bar'])
111+
expect(wrapper.emitted().hello).toEqual(undefined)
112+
expect(childWrapper.emitted().hello[1]).toEqual(['foo', 'bar'])
109113
})
110114

111115
it('should allow passing the name of an event', () => {

0 commit comments

Comments
 (0)