Skip to content

Testing events when using $listeners #1314

@lee-chase

Description

@lee-chase

Version

1.0.0-beta.29

Reproduction link

https://github.com/lee-chase/vue-test-utils-listeners

Steps to reproduce

Given a simple button wrapper component

<template>
  <button v-bind="$attrs" v-on="$listeners">
    <div class="content">{{ buttonText }}</div>
  </button>
</template>

I have been unable to test the click event as follows:

  it("Raises click event when clicked", () => {
    const wrapper = shallowMount(MyButton2);
    wrapper.find("button").trigger("click");
    expect(wrapper.emitted().click).toBeTruthy();
  });

If I change the component to explicitly handle clicky it works, but this is not desirable for native component wrappers and negates the usefulness of $listeners

<template>
  <button v-bind="$attrs" v-on="$listeners" @click="$emit('click')>
    <div class="content">{{ buttonText }}</div>
  </button>
</template> 

What is expected?

It should be possible to test events surfaced through $listeners

What is actually happening?

No event is raised

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions