Skip to content

Commit 1bac7fb

Browse files
committed
refactor: refactoring emit some edge solution
1 parent 840736a commit 1bac7fb

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

src/emitMixin.ts renamed to src/emit.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import { setDevtoolsHook, ComponentPublicInstance, devtools } from 'vue'
1+
import { setDevtoolsHook, devtools } from 'vue'
22

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

7-
export const attachEmitListener = (vm: ComponentPublicInstance) => {
8-
const events: Record<string, unknown[]> = {}
9-
;(vm as any).__emitted = events
7+
let events: Record<string, unknown[]>
8+
9+
export function emitted<T = unknown>(
10+
eventName?: string
11+
): T[] | Record<string, T[]> {
12+
if (eventName) {
13+
const emitted = (events as Record<string, T[]>)[eventName]
14+
return emitted
15+
}
16+
17+
return events as Record<string, T[]>
18+
}
19+
20+
export const attachEmitListener = () => {
21+
events = {}
1022
// use devtools to capture this "emit"
1123
setDevtoolsHook(createDevTools(events))
1224
}
@@ -29,9 +41,6 @@ function createDevTools(events): any {
2941

3042
function recordEvent(events, event, args) {
3143
// Record the event message sent by the emit
32-
// Stored by a vm
33-
// emitted by a subsequent wrapper.emitted
34-
// An event object and a vm.__emitted is a reference
3544
events[event]
3645
? (events[event] = [...events[event], [...args]])
3746
: (events[event] = [[...args]])

src/mount.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
} from './utils'
3333
import { processSlot } from './utils/compileSlots'
3434
import { createWrapper, VueWrapper } from './vueWrapper'
35-
import { attachEmitListener } from './emitMixin'
35+
import { attachEmitListener } from './emit'
3636
import { createDataMixin } from './dataMixin'
3737
import { MOUNT_COMPONENT_REF, MOUNT_PARENT_NAME } from './constants'
3838
import { createStub, stubComponents } from './stubs'
@@ -346,6 +346,9 @@ export function mount(
346346
return vm.$nextTick()
347347
}
348348

349+
// add tracking for emitted events
350+
attachEmitListener()
351+
349352
// create the app
350353
const app = createApp(Parent)
351354

@@ -446,9 +449,6 @@ export function mount(
446449
const $vm = Reflect.ownKeys(appRef).length ? appRef : vm
447450
console.warn = warnSave
448451

449-
// add tracking for emitted events
450-
attachEmitListener($vm)
451-
452452
return createWrapper(app, $vm, setProps)
453453
}
454454

src/vueWrapper.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createWrapperError } from './errorWrapper'
88
import { TriggerOptions } from './createDomEvent'
99
import { find, matches } from './utils/find'
1010
import { mergeDeep } from './utils'
11+
import { emitted } from './emit'
1112

1213
export class VueWrapper<T extends ComponentPublicInstance> {
1314
private componentVM: T
@@ -72,12 +73,7 @@ export class VueWrapper<T extends ComponentPublicInstance> {
7273
emitted<T = unknown>(): Record<string, T[]>
7374
emitted<T = unknown>(eventName?: string): T[]
7475
emitted<T = unknown>(eventName?: string): T[] | Record<string, T[]> {
75-
if (eventName) {
76-
const emitted = (this.vm['__emitted'] as Record<string, T[]>)[eventName]
77-
return emitted
78-
}
79-
80-
return this.vm['__emitted'] as Record<string, T[]>
76+
return emitted(eventName)
8177
}
8278

8379
html() {

tests/emit.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,14 @@ describe('emitted', () => {
154154
expect(wrapper.emitted('hello')).toHaveLength(1)
155155
expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar'])
156156
})
157+
158+
it('captures an event emitted in setup', () => {
159+
const Comp = {
160+
setup(_, { emit }) {
161+
emit('foo')
162+
}
163+
}
164+
const wrapper = mount(Comp)
165+
expect(wrapper.emitted().foo).toBeTruthy()
166+
})
157167
})

0 commit comments

Comments
 (0)