Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: record native events against each wrapper they bubble through #394

Merged
merged 4 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ export const attachEmitListener = () => {
setDevtoolsHook(createDevTools(events))
}

// devtools hook only catches Vue component custom events
function createDevTools(events: Events): any {
return {
emit(eventType, ...payload) {
if (eventType !== DevtoolsHooks.COMPONENT_EMIT) return

const [rootVM, componentVM, event, eventArgs] = payload
recordEvent(events, componentVM, event, eventArgs)
recordEvent(componentVM, event, eventArgs)
}
} as Partial<typeof devtools>
}

function recordEvent(
events: Events,
export const recordEvent = (
vm: ComponentInternalInstance,
event: string,
args: unknown[]
): void {
): void => {
// Functional component wrapper creates a parent component
let wrapperVm = vm
while (typeof wrapperVm?.type === 'function') wrapperVm = wrapperVm.parent!
Expand Down
22 changes: 20 additions & 2 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createWrapperError } from './errorWrapper'
import { TriggerOptions } from './createDomEvent'
import { find, matches } from './utils/find'
import { mergeDeep, textContent } from './utils'
import { emitted } from './emit'
import { emitted, recordEvent } from './emit'

export class VueWrapper<T extends ComponentPublicInstance> {
private componentVM: T
Expand All @@ -30,6 +30,9 @@ export class VueWrapper<T extends ComponentPublicInstance> {
this.rootVM = vm?.$root
this.componentVM = vm as T
this.__setProps = setProps

this.attachNativeEventListener()

Comment on lines +39 to +41
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this method should be exported from emit.ts with this as an argument, the style looks very different from anything else so I tried to add a private method instead. There is definitely some bleeding of concerns.

config.plugins.VueWrapper.extend(this)
}

Expand All @@ -42,6 +45,21 @@ export class VueWrapper<T extends ComponentPublicInstance> {
return this.vm.$el.parentElement
}

private attachNativeEventListener(): void {
const vm = this.vm
if (!vm) return
Comment on lines +55 to +56
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this.element breaks in some circumstances that I had trouble debugging, for some reason findComponent creates Wrapper objects without a vm? Anyway, if there's no vm, I guess there's nothing to bind event listeners to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot imagine why this would happen 🤔 I will look at it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it makes sense, functional components can be found with findComponent but they do not have a vm

most likely someone is going to ask about capturing emitted events from functional components. let's deal with it if and when it comes up 🤷

Copy link
Contributor Author

@aethr aethr Feb 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test I added has a functional component for the grandparent, which works because it captures events that bubbled to the element of the outer wrapper, I suppose. It might be different for functional components without their own native DOM element but I suppose in that case DOM events wouldn't expect to be captured?


const element = this.element
for (let key in element) {
if (/^on/.test(key)) {
const eventName = key.slice(2)
element.addEventListener(eventName, (...args) => {
recordEvent(vm.$, eventName, args)
})
}
aethr marked this conversation as resolved.
Show resolved Hide resolved
}
}

get element(): Element {
// if the component has multiple root elements, we use the parent's element
return this.hasMultipleRoots ? this.parentElement : this.vm.$el
Expand Down Expand Up @@ -79,7 +97,7 @@ export class VueWrapper<T extends ComponentPublicInstance> {
}

emitted<T = unknown>(): Record<string, T[]>
emitted<T = unknown>(eventName?: string): undefined | T[]
emitted<T = unknown>(eventName: string): undefined | T[]
emitted<T = unknown>(
eventName?: string
): undefined | T[] | Record<string, T[]> {
Expand Down
76 changes: 65 additions & 11 deletions tests/emit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,73 @@ describe('emitted', () => {
expect(wrapper.emitted().hello[1]).toEqual(['foo', 'bar'])
})

it('should not propagate child events', () => {
it('should propagate child native events', async () => {
const Child = defineComponent({
name: 'Child',
setup(props, { emit }) {
name: 'Button',
setup(props) {
return () => h('div', [h('input')])
}
})

const Parent = defineComponent({
name: 'Parent',
setup() {
return () =>
h('div', [
h('button', { onClick: () => emit('hello', 'foo', 'bar') })
h(Child, {
onClick: (event: Event) => event.stopPropagation()
})
])
}
})

const GrandParent: FunctionalComponent<{ level: number }> = (
props,
ctx
) => {
return h(`h${props.level}`, [h(Parent)])
}

const wrapper = mount(GrandParent)
const parentWrapper = wrapper.findComponent(Parent)
const childWrapper = wrapper.findComponent(Child)
const input = wrapper.find('input')

expect(wrapper.emitted()).toEqual({})
expect(parentWrapper.emitted()).toEqual({})
expect(childWrapper.emitted()).toEqual({})

await input.trigger('click')

// Propagation should stop at Parent
expect(childWrapper.emitted().click).toHaveLength(1)
expect(parentWrapper.emitted().click).toEqual(undefined)
expect(wrapper.emitted().click).toEqual(undefined)

input.setValue('hey')

expect(childWrapper.emitted().input).toHaveLength(1)
expect(parentWrapper.emitted().input).toHaveLength(1)
expect(wrapper.emitted().input).toHaveLength(1)
})

it('should not propagate child custom events', () => {
const Child = defineComponent({
name: 'Child',
emits: ['hi'],
setup(props, { emit }) {
return () =>
h('div', [h('button', { onClick: () => emit('hi', 'foo', 'bar') })])
}
})

const Parent = defineComponent({
name: 'Parent',
emits: ['hello'],
aethr marked this conversation as resolved.
Show resolved Hide resolved
setup(props, { emit }) {
return () =>
h(Child, {
onHello: (...events: unknown[]) => emit('parent', ...events)
onHi: (...events: unknown[]) => emit('hello', ...events)
})
}
})
Expand All @@ -105,14 +155,18 @@ describe('emitted', () => {
expect(childWrapper.emitted()).toEqual({})

wrapper.find('button').trigger('click')
expect(wrapper.emitted().parent[0]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().hello).toEqual(undefined)
expect(childWrapper.emitted().hello[0]).toEqual(['foo', 'bar'])

// Parent should emit custom event 'hello' but not 'hi'
expect(wrapper.emitted().hello[0]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().hi).toEqual(undefined)
// Child should emit custom event 'hi'
expect(childWrapper.emitted().hi[0]).toEqual(['foo', 'bar'])

// Additional events should accumulate in the same format
wrapper.find('button').trigger('click')
expect(wrapper.emitted().parent[1]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().hello).toEqual(undefined)
expect(childWrapper.emitted().hello[1]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().hello[1]).toEqual(['foo', 'bar'])
expect(wrapper.emitted().hi).toEqual(undefined)
expect(childWrapper.emitted().hi[1]).toEqual(['foo', 'bar'])
})

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